-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmain_example.py
272 lines (210 loc) · 9.62 KB
/
main_example.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import json, config
from flask import Flask, request
from api.binance_spot import BinanceSpotHttpClient
from api.binance_future import BinanceFutureHttpClient, OrderSide, OrderType
from event import EventEngine, Event, EVENT_TIMER, EVENT_SIGNAL
from decimal import Decimal
app = Flask(__name__)
@app.route('/', methods=['GET'])
def welcome():
return "Hello Flask, This is for testing. If you receive this message, it means your configuration is correct."
@app.route('/webhook', methods=['POST'])
def webhook():
try:
data = json.loads(request.data)
print(data)
if data.get('passphrase', None) != config.WEBHOOK_PASSPHRASE:
return "failure: passphrase is incorrect."
event = Event(EVENT_SIGNAL, data=data)
event_engine.put(event)
return "success"
except Exception as error:
print(f"error: {error}")
return "failure"
def future_trade(data: dict):
symbol = data.get('symbol', None)
action = data.get('action', '').upper()
strategy_name = data.get('strategy_name', None)
if not strategy_name:
return
strategy_config = config.strategies.get(strategy_name, None)
if not strategy_config:
return
current_pos = strategy_config.get('pos', 0)
trading_volume = strategy_config.get('trading_volume', 0)
# print('strategy name', strategy_name, 'action: ', action, "current_pos: ", current_pos, "trading_volume: ", trading_volume)
# print('order id :', future_strategy_order_dict.get(strategy_name))
price = str(data.get('price', '0'))
if action == 'EXIT':
if current_pos > 0:
vol1 = str(current_pos)
order_id = binance_future_client.get_client_order_id()
# the order support: LIMIT, MARKET, MAKER order
# if you want to place a maker order, set the order_type=OrderType.MAKER
# 支持限价单,市价单,做市单,如果你想下做市单,设置参数 order_type=OrderType.MAKER
status, order = binance_future_client.place_order(
symbol=symbol,
order_side=OrderSide.SELL,
order_type=OrderType.MAKER,
quantity=Decimal(vol1),
price=Decimal(price),
client_order_id=order_id
)
print("exit long: ", status, order)
if status == 200:
future_strategy_order_dict[strategy_name] = order_id
elif current_pos < 0:
vol1 = str(abs(current_pos))
order_id = binance_future_client.get_client_order_id()
status, order = binance_future_client.place_order(
symbol=symbol,
order_side=OrderSide.BUY,
order_type=OrderType.MAKER,
quantity=Decimal(vol1),
price=Decimal(price),
client_order_id=order_id
)
print("exit short: ", status, order)
if status == 200:
future_strategy_order_dict[strategy_name] = order_id
elif action == 'LONG':
if current_pos < 0:
vol1 = str(abs(current_pos) + trading_volume)
order_id = binance_future_client.get_client_order_id()
status, order = binance_future_client.place_order(
symbol=symbol,
order_side=OrderSide.BUY,
order_type=OrderType.MAKER,
quantity=Decimal(vol1),
price=Decimal(price),
client_order_id=order_id
)
print("exit short & long: ", status, order)
if status == 200:
future_strategy_order_dict[strategy_name] = order_id
if current_pos == 0:
# config your trading volume in config.py
vol1 = str(trading_volume)
order_id = binance_future_client.get_client_order_id()
status, order = binance_future_client.place_order(
symbol=symbol,
order_side=OrderSide.BUY,
order_type=OrderType.MAKER,
quantity=Decimal(vol1),
price=Decimal(price),
client_order_id=order_id
)
print("long: ", status, order)
if status == 200:
future_strategy_order_dict[strategy_name] = order_id
elif action == 'SHORT':
if current_pos > 0:
vol1 = str(abs(current_pos) + trading_volume)
order_id = binance_future_client.get_client_order_id()
status, order = binance_future_client.place_order(
symbol=symbol,
order_side=OrderSide.SELL,
order_type=OrderType.MAKER,
quantity=Decimal(vol1),
price=Decimal(price),
client_order_id=order_id
)
print("exit long & short: ", status, order)
if status == 200:
future_strategy_order_dict[strategy_name] = order_id
if current_pos == 0:
vol1 = str(trading_volume)
order_id = binance_future_client.get_client_order_id()
status, order = binance_future_client.place_order(
symbol=symbol,
order_side=OrderSide.SELL,
order_type=OrderType.MAKER,
quantity=Decimal(str(vol1)),
price=Decimal(price),
client_order_id=order_id
)
print("short: ", status, order)
if status == 200:
future_strategy_order_dict[strategy_name] = order_id
def timer_event(event: Event):
global cancel_orders_timer
global query_orders_timer
cancel_orders_timer += 1
query_orders_timer += 1
if cancel_orders_timer > config.CANCEL_ORDERS_IN_SECONDS: # for cancel order.
cancel_orders_timer = 0 # reset the timer
# will cancel the order repeatedly. the default value is CANCEL_ORDER_IN_SECONDS = 60
for strategy_name in future_strategy_order_dict.keys():
order_id = future_strategy_order_dict[strategy_name]
if not order_id:
continue
symbol = config.strategies.get(strategy_name, {}).get('symbol', "")
binance_future_client.cancel_order(symbol, client_order_id=order_id)
if query_orders_timer > config.QUERY_ORDERS_STATUS_IN_SECONDS: # for updating order.
query_orders_timer = 0 # reset the query order timer.
for strategy_name in future_strategy_order_dict.keys():
order_id = future_strategy_order_dict[strategy_name]
if not order_id:
continue
symbol = config.strategies.get(strategy_name, {}).get('symbol', "")
status_code, order = binance_future_client.get_order(symbol, client_order_id=order_id)
if status_code == 200 and order:
if order.get('status') == 'CANCELED' or order.get('status') == 'FILLED':
side = order.get('side')
strategy_config = config.strategies.get(strategy_name, {})
executed_qty = Decimal(order.get('executedQty', "0"))
if side == "BUY": # BUY
strategy_config['pos'] = strategy_config['pos'] + executed_qty
elif side == "SELL": # SELL
strategy_config['pos'] = strategy_config['pos'] - executed_qty
# print(strategy_config)
config.strategies[strategy_name] = strategy_config # update the data.
future_strategy_order_dict[strategy_name] = None
elif status_code == 400 and order.get('code') == -2013: # Order does not exist.
future_strategy_order_dict[strategy_name] = None
for strategy_name in future_signal_dict.keys():
orderid = future_strategy_order_dict.get(strategy_name, None)
if not orderid:
data = future_signal_dict.get(strategy_name, None)
if data:
future_trade(data)
for key in spot_signal_dict.keys():
"""
check your spot signal here, whether your buy/sell order filled.
please refer to the future signal and code your logic below.
"""
pass
def signal_event(event: Event):
"""
:param event: the event that contains the signal data
the signal data like below.
{'action': 'long',
'symbol': 'ETHUSDT', 'exchange': 'binance_future',
'price': '3054.66', 'close': '3054.66',
'passphrase': 'your password'}
:return: None
"""
data = event.data
strategy_name = data.get('strategy_name', None)
if not strategy_name:
print("config from tradingview does not have strategy_name key.")
return None
if data.get('exchange', None) == 'binance_future':
future_signal_dict[strategy_name] = data # strategy_name -> data
future_trade(data)
elif data.get('exchange', None) == 'binance_spot':
future_signal_dict[strategy_name] = data # strategy_name -> data
# write your logic code here.
if __name__ == '__main__':
future_signal_dict = {}
spot_signal_dict = {}
future_strategy_order_dict = {}
cancel_orders_timer = 0 # 撤单的timer.
query_orders_timer = 0 # 查询订单的tismer.
binance_spot_client = BinanceSpotHttpClient(api_key=config.API_KEY, secret=config.API_SECRET)
binance_future_client = BinanceFutureHttpClient(api_key=config.API_KEY, secret=config.API_SECRET)
event_engine = EventEngine(interval=1) # you can update the loop interval.
event_engine.start()
event_engine.register(EVENT_TIMER, timer_event)
event_engine.register(EVENT_SIGNAL, signal_event)
app.run(host='127.0.0.1', port=8888, debug=False)