-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy_sceleton.py
299 lines (232 loc) · 9.94 KB
/
strategy_sceleton.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
debug = False
if debug == False:
from StrategyFramework import *
else:
import threading
from enum import Enum
#from statemachine import StateMachine
import json
import sys
import time
#def main():
# while True:
# print("while loop!")
# time.sleep(1)
#if __name__ == '__main__':
# main()
#================= FSM =====================#
class StateMachine:
def __init__(self):
self.handlers = {}
self.startState = None
self.endStates = []
self.state = "FSM_ST_START"
self.old_state = "FSM_ST_START";
def add_state(self, name, handler, end_state=0):
name = name.upper()
self.handlers[name] = handler
if end_state:
self.endStates.append(name)
def set_start(self, name):
self.startState = name.upper()
def set_state(self, st):
if self.state in self.endStates:
print("reached ", self.state)
else:
self.old_state = self.state
self.state = st
print("## set_state run - {}/{}".format(self.state, st))
def get_state(self):
return self.state
def run(self, arg):
# if self.state == self.old_state:
# return;
if self.state in self.endStates:
print("reached ", self.state)
else:
print("## state run - {}".format(self.state))
self.handlers[self.state](arg)
# def run(self, cargo):
# try:
# handler = self.handlers[self.startState]
# except:
# raise InitializationError("must call .set_start() before .run()")
# if not self.endStates:
# raise InitializationError("at least one state must be an end_state")
# while True:
# (newState, cargo) = handler(cargo)
# if newState.upper() in self.endStates:
# print("reached ", newState)
# break
# else:
# handler = self.handlers[newState.upper()]
#================= DEBUG FUNCTIONS AND STRUCTS =====================#
if debug == True:
class Type(Enum):
MARKET = 1
LIMIT = 2
class Side(Enum):
BUY = 1
SELL = 2
class TimeInForce(Enum):
GTC = 1
IOC = 2
FOK = 3
class UserCommand(Enum):
YES = 1
NO = 2
EXEC = 3
CANCEL = 4
class Order():
def __init__(self):
self.symbol = "BTCUSDT"
self.newClientOrderId = ""
class Account():
def __init__(self):
self.maker_commission = 0.00
self.taker_commission = 0.00
self.free_usdt = 0.00
self.locked_usdt = 0.00
self.free_btc = 0.00
self.locked_btc = 0.00
class StrategyInstance():
def __init__(self, server):
self.server = server
self.timer = threading.Timer(0, self.stop_timer)
def exec_order(self, order):
print("DEBUG func: {}".format(sys._getframe().f_code.co_name))
def correct_order(self, order, autocancel):
print("DEBUG func: {}".format(sys._getframe().f_code.co_name))
def delete_order(self, oname):
print("DEBUG func: {}".format(sys._getframe().f_code.co_name))
def delete_all_orders(self):
print("DEBUG func: {}".format(sys._getframe().f_code.co_name))
def send_message(self, msg):
print("DEBUG func: {}".format(sys._getframe().f_code.co_name))
print("TLG MSG: %s", msg)
def request_value(self, msg):
print("DEBUG func: {}".format(sys._getframe().f_code.co_name))
print("TLG USER VAL: %s", msg)
def create_timer(self, interval, func, recurring = False):
print("DEBUG func: {}".format(sys._getframe().f_code.co_name))
self.timer = threading.Timer(interval, func)
self.timer.start()
def stop_timer(self):
print("DEBUG func: {}".format(sys._getframe().f_code.co_name))
self.timer.cancel()
#================= STRATEGY CLASS =====================#
class Strategy(StrategyInstance):
def __init__(self, server):
StrategyInstance.__init__(self, server)
self.fsm = StateMachine()
self.fsm.add_state("FSM_ST_READY", self.fsm_ready)
self.fsm.add_state("FSM_ST_START", self.fsm_start)
self.fsm.add_state("FSM_ST_RESUME", self.fsm_resume)
self.fsm.add_state("FSM_ST_STOP", None, end_state = 1)
self.fsm.set_start("FSM_ST_START")
self.alert_dict = {}
self.kline_low_list = [float(0.00),float(0.00)]
self.line = float(0.00)
self.kline_open = float(0.00)
self.kline_close = float(0.00)
self.fsm.set_state("FSM_ST_READY")
def reset_all(self):
self.stop_timer()
self.reset_correct_mode()
self.kline_low_list = [float(0.00),float(0.00)]
self.line = float(0.00)
self.kline_open = float(0.00)
self.kline_close = float(0.00)
# сигнал от TreadingView
def on_alert(self, name, body):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
tmp_dict = dict(x.split("=") for x in body.split("\n"))
# if tmp_dict["label"] == "MACD" :
# tmp_dict["Итог: "] = "Покупать" if float(tmp_dict["MACD"]) > 0.00 else "Продавать"
self.alert_dict[tmp_dict["label"]] = tmp_dict
def dict_to_string(self, dict):
str = ""
for key, value in dict.items():
if "label" == key:
continue
if "title" == key:
key = "Индикатор:"
str += key + " " + value + "\n"
return str
#================= CALL FROM BOT FUNCTIONS =====================#
# тестовая функция
def eval(self):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
#self.fsm.run(None)
# запуск скрипта
def on_start(self):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
if "FSM_ST_READY" != self.fsm.get_state() and "FSM_ST_STOP" != self.fsm.get_state():
self.send_message("FSM: Невозможно выполнить: скрипт работает")
else:
self.fsm.set_state("FSM_ST_START")
self.fsm.run(None)
# остановка скрипта
def on_stop(self):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.send_message("FSM: Скрипт остановлен пользователем")
self.stop_timer()
self.delete_all_orders()
self.fsm.set_state("FSM_ST_RESUME")
self.fsm.run(None)
# вывод пользователю данных индикаторов от TreadingView
def on_indicators(self):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
for key, value in self.alert_dict.items():
msg = self.dict_to_string(value)
self.send_message(msg)
# пришли данные аккаунта
def on_account(self, acc):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.account = acc
# пришли данные свечи
def on_kline(self, kl_open, kl_close):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
if len(self.kline_low_list) < 2:
self.kline_low_list.append(min(kl_open, kl_close))
else:
self.kline_low_list.pop()
self.kline_low_list.insert(0, min(kl_open, kl_close))
# команда от пользователя
def on_command(self, answ):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.to_log("USER COMMAND: {}".format(answ))
def on_user_value(self, val):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.to_log("USER VALUE: {}".format(answ))
# ордер после редактирования
def on_order_update(self, order):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.order = order
self.send_message("FSM: Ордер {} обновлен".format(self.order.newClientOrderId))
# отчет о статусе ордера от Binance
def on_order_exc(self, rep): #pep - json string
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.report = json.loads(rep)
self.to_log("ORDER EXEC REPORT: {}".format(rep))
#================= SERVICE FUNCTIONS =====================#
def is_cash_in btc():
return (self.account.free_usdt / self.kline_close) < self.account.free_btc
def is_line_in_kline():
return self.line in range(self.kline_close, kline_open)
def is_market_fall(self):
return self.kline_low_list[0] < self.kline_low_list[1]
#================= FSM FUNCTIONS =====================#
def fsm_ready(self, arg):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.fsm.run(None)
def fsm_start(self, arg):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.send_message("FSM: Скрипт ЗАПУЩЕН");
def fsm_resume(self, arg):
self.to_log("FSM state: {} func: {}".format(self.fsm.get_state(), sys._getframe().f_code.co_name))
self.send_message("FSM: Скрипт ОСТАНОВЛЕН");
self.reset_all()
self.fsm.set_state("FSM_ST_READY")
self.fsm.run(None)
self