-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathohlc_usage_example.py
More file actions
61 lines (48 loc) · 2 KB
/
ohlc_usage_example.py
File metadata and controls
61 lines (48 loc) · 2 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
#!/usr/bin/env python3
"""
Usage example for OHLC candle aggregation with PocketOption API.
"""
import asyncio
import time
from BinaryOptionsToolsAsync.platforms.pocketoption.stable_api import PocketOption
async def main():
# Your SSID from browser (replace with real value)
ssid = "your_ssid_here"
# Create API instance
api = PocketOption(ssid, demo=True)
# Connect to WebSocket
await api.connect()
# Callback for completed candles
def on_candle_complete(asset, candle):
print(f"🕯️ New {asset} candle: O:{candle.open} H:{candle.high} L:{candle.low} C:{candle.close} at {candle.timestamp}")
# Subscribe with OHLC aggregation
api.subscribe_candles(
active="EURUSD_otc",
create_ohlc=True, # Enable OHLC aggregation
timeframe_seconds=60, # 1-minute candles
max_candles=1000, # Keep last 1000 candles
on_candle_complete=on_candle_complete
)
print("Collecting OHLC data... (press Ctrl+C to stop)")
try:
while True:
await asyncio.sleep(10)
# Get completed OHLC candles
candles = api.get_ohlc_candles("EURUSD_otc", timeframe_seconds=60, count=5)
print(f"📈 Latest 5 completed candles: {len(candles)}")
# Get current incomplete candle
current = api.get_current_ohlc_candle("EURUSD_otc", timeframe_seconds=60)
if current:
print(f"📊 Current candle: O:{current['open']} H:{current['high']} L:{current['low']} C:{current['close']} | Ticks:{current['tick_count']}")
# Show aggregation stats
stats = api.get_ohlc_stats()
if stats:
print(f"📋 Stats: {stats}")
except KeyboardInterrupt:
print("\nStopping...")
finally:
# Unsubscribe
api.unsubscribe_candles("EURUSD_otc")
await api.disconnect()
if __name__ == "__main__":
asyncio.run(main())