forked from ChipaDevTeam/BinaryOptionsTools-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy_example.py
More file actions
64 lines (46 loc) · 1.75 KB
/
strategy_example.py
File metadata and controls
64 lines (46 loc) · 1.75 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
import asyncio
import json
import os
from dotenv import load_dotenv
from BinaryOptionsToolsV2 import PyBot, PyStrategy, RawPocketOption, start_tracing
load_dotenv()
class MyRSIStrategy(PyStrategy):
def __init__(self, rsi_period=14):
super().__init__()
self.rsi_period = rsi_period
self.prices = {}
self._tasks = set()
def on_start(self, _ctx):
print("Strategy started!")
def on_candle(self, ctx, asset, candle_json):
candle = json.loads(candle_json)
print(f"New candle for {asset}: {candle['close']}")
# Simple logic: if price ends in .5, buy (just for demo)
if str(candle["close"]).endswith("5"):
print(f"Signal detected on {asset}! Buying...")
# ctx.buy returns a future
task = asyncio.create_task(self.execute_trade(ctx, asset))
self._tasks.add(task)
task.add_done_callback(self._tasks.discard)
async def execute_trade(self, ctx, asset):
try:
result = await ctx.buy(asset, 1.0, 60)
print(f"Trade executed: {result}")
except Exception as e:
print(f"Trade failed: {e}")
async def main():
start_tracing("info")
ssid = os.getenv("POCKET_OPTION_SSID")
if not ssid:
print("Please set POCKET_OPTION_SSID in .env file")
return
print("Connecting to PocketOption...")
client = await RawPocketOption.create(ssid)
strategy = MyRSIStrategy()
bot = PyBot(client, strategy)
# Add assets to monitor (60s candles)
bot.add_asset("EURUSD_otc", 60)
print("Running bot... Press Ctrl+C to stop.")
await bot.run()
if __name__ == "__main__":
asyncio.run(main())