-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
126 lines (104 loc) · 5 KB
/
example.py
File metadata and controls
126 lines (104 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
"""
Example script demonstrating Kraken CLI usage
This script shows how to use the Kraken trading application programmatically
"""
import os
import sys
from pathlib import Path
# Add the current directory to the path
sys.path.append(str(Path(__file__).parent))
from config import Config
from api.kraken_client import KrakenAPIClient
from trading.trader import Trader
from portfolio.portfolio_manager import PortfolioManager
from utils.helpers import format_currency, format_percentage
def main():
"""Demonstrate Kraken CLI functionality"""
print("🚀 Kraken Pro Trading CLI - Example Usage")
print("=" * 50)
# Load configuration
config = Config()
# Check if credentials are configured
if not config.has_credentials():
print("❌ API credentials not configured!")
print("Please set up your .env file with Kraken API credentials.")
print("See README.md for setup instructions.")
return
print(f"🔧 Using {'Sandbox' if config.is_sandbox() else 'Live'} environment")
try:
# Initialize API client
print("🔌 Connecting to Kraken API...")
api_client = KrakenAPIClient(
api_key=config.api_key,
api_secret=config.api_secret,
sandbox=config.sandbox
)
# Initialize components
trader = Trader(api_client)
portfolio = PortfolioManager(api_client)
# Test connection
print("📡 Testing API connection...")
time_info = api_client.get_server_time()
print(f"✅ Connected successfully! Server time: {time_info['result']['unixtime']}")
# Get account balances
print("\n💰 Getting account balances...")
balances = portfolio.get_balances()
if balances:
print("Account balances:")
for asset, amount in balances.items():
if float(amount) > 0:
print(f" {asset}: {format_currency(amount)}")
else:
print("No balances found or insufficient permissions")
# Get ticker data
print("\n📊 Getting ticker data for XBTUSD...")
ticker = api_client.get_ticker("XBTUSD")
if ticker and 'result' in ticker:
xbt_data = ticker['result'].get('XXBTZUSD', {})
if xbt_data:
print(f"Bitcoin (XBT/USD):")
print(f" Last Price: {format_currency(xbt_data['c'][0])}")
print(f" 24h Change: {format_percentage(xbt_data['p'][0])}")
print(f" 24h High: {format_currency(xbt_data['h'][0])}")
print(f" 24h Low: {format_currency(xbt_data['l'][0])}")
print(f" Volume: {xbt_data['v'][0]}")
# Get portfolio summary
print("\n📈 Portfolio Summary...")
summary = portfolio.get_portfolio_summary()
if summary['total_usd_value']:
print(f"Total Portfolio Value: {format_currency(str(summary['total_usd_value']))}")
print(f"Total Assets: {summary['total_assets']}")
print(f"Open Orders: {summary['open_orders_count']}")
print(f"Open Positions: {summary['open_positions_count']}")
# Performance metrics
print("\n📊 Performance Metrics...")
metrics = portfolio.get_performance_metrics()
print(f"Total Trades: {metrics['total_trades']}")
print(f"Profitable Trades: {metrics['profitable_trades']}")
print(f"Win Rate: {format_percentage(metrics['win_rate'])}")
print(f"Total Volume: {metrics['total_volume']}")
# Example order validation
print("\n🔍 Example Order Validation...")
pair = "XBTUSD"
volume = 0.001
has_balance = trader.validate_sufficient_balance(pair, "buy", volume)
print(f"Can place buy order for {volume} {pair}? {'✅ Yes' if has_balance else '❌ No'}")
if has_balance:
# Estimate fees
fees = trader.estimate_fees(pair, volume, "market")
print(f"Estimated trade value: {format_currency(str(fees['trade_value']))}")
print(f"Estimated fee: {format_currency(str(fees['estimated_fee']))}")
print("\n🎯 Example Commands to Try:")
print(" python kraken_cli.py status # Check account status")
print(" python kraken_cli.py ticker --pair XBTUSD # View Bitcoin price")
print(" python kraken_cli.py orders # View open orders")
print(" python kraken_cli.py portfolio # View portfolio")
print(" python kraken_cli.py order --pair XBTUSD --side buy --order-type market --volume 0.001 # Place order")
print("\n⚠️ REMEMBER: Only place orders you can afford to lose!")
except Exception as e:
print(f"❌ Error: {str(e)}")
print("Please check your API credentials and internet connection.")
print("See README.md for troubleshooting tips.")
if __name__ == "__main__":
main()