-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapi_usage_example.py
More file actions
94 lines (71 loc) · 2.63 KB
/
api_usage_example.py
File metadata and controls
94 lines (71 loc) · 2.63 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
"""
Example: API Usage
Demonstrates how to use the REST API and WebSocket API.
"""
from forexsmartbot.cloud.api_client import APIClient, WebSocketClient
import asyncio
def rest_api_example():
"""Example of using REST API."""
# Initialize API client
api_key = "YOUR_API_KEY"
base_url = "http://localhost:5000"
client = APIClient(api_key=api_key, base_url=base_url)
try:
# Get account balance
balance = client.get_balance()
print(f"Account Balance: ${balance:.2f}")
# Get open positions
positions = client.get_positions()
print(f"Open Positions: {len(positions)}")
for pos in positions:
print(f" {pos['symbol']}: {pos['side']} {pos['quantity']} @ {pos['entry_price']}")
# Get current price
price = client.get_price("EURUSD")
print(f"EURUSD Price: {price:.4f}")
# Create an order
order = client.create_order(
symbol="EURUSD",
side=1, # Buy
quantity=1000,
stop_loss=1.0980,
take_profit=1.1100
)
print(f"Order created: {order['order_id']}")
# Close positions for a symbol
result = client.close_positions("EURUSD")
print(f"Closed positions: {result['message']}")
except Exception as e:
print(f"Error: {e}")
async def websocket_example():
"""Example of using WebSocket API."""
# Initialize WebSocket client
client = WebSocketClient(url="ws://localhost:8765")
try:
# Connect
await client.connect()
print("Connected to WebSocket server")
# Subscribe to price updates
await client.subscribe("EURUSD")
print("Subscribed to EURUSD price updates")
# Get balance
balance = await client.get_balance()
print(f"Balance: ${balance:.2f}")
# Get positions
positions = await client.get_positions()
print(f"Positions: {len(positions)}")
# Listen for real-time updates
def handle_update(data):
print(f"Update received: {data}")
print("Listening for updates...")
await client.listen(handle_update)
except Exception as e:
print(f"Error: {e}")
finally:
await client.disconnect()
def main():
print("=== REST API Example ===")
rest_api_example()
print("\n=== WebSocket API Example ===")
asyncio.run(websocket_example())
if __name__ == '__main__':
main()