Skip to content

Commit 836cba1

Browse files
authored
Merge pull request #1 from evgrmn/dev
Preparing the next release
2 parents b795740 + 3ac94f2 commit 836cba1

File tree

15 files changed

+494
-378
lines changed

15 files changed

+494
-378
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ pip3 install python-dotenv
7474
pip3 install websocket-client
7575
pip3 install requests
7676
```
77+
78+
*or use the command:*
79+
80+
```
81+
pip3 install -r requirements.txt
82+
```
83+
7784
4. Install the MySQL database on your local computer by following the instructions according to your operating system.
7885
5. Install your favorite visual tool like MySQL Workbench or something else if you need to.
7986
6. Create a database, for example "my_db".
@@ -276,7 +283,7 @@ def algo(robot: dict, frame: dict, ticker: dict, instrument: dict) -> None:
276283
quantaty = (
277284
robot["lotSize"]
278285
* robot["CAPITAL"]
279-
* instrument["underlyingToPositionMultiplier"]
286+
* instrument["myMultiplier"]
280287
)
281288
emi = robot["EMI"]
282289
symbol = robot["SYMBOL"]
@@ -356,7 +363,7 @@ def order_search(emi: int, side: str) -> str:
356363

357364

358365
def delete_orders(emi: int, side: str) -> None:
359-
for clOrdID, order in var.orders.items():
366+
for clOrdID, order in var.orders.copy().items():
360367
if order["emi"] == emi and order["side"] == side:
361368
function.del_order(clOrdID)
362369
```

algo/init.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#Robot strategy initialization file
1+
# Robot strategy initialization file
22

33

44
def init_algo():
5-
pass
5+
pass

bots/init.py

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import functions as function
55
from bots.variables import Variables as bot
66
from common.variables import Variables as var
7+
from ws.init import Variables as ws
78

89

910
def load_robots(db: str) -> dict:
@@ -90,8 +91,8 @@ def load_robots(db: str) -> dict:
9091
"POS": pos,
9192
"EMI": symbol,
9293
"STATUS": "RESERVED",
93-
"TIMEFR": None,
94-
"CAPITAL": None,
94+
"TIMEFR": "None",
95+
"CAPITAL": "None",
9596
}
9697

9798
# Loading all transactions and calculating financial results for each robot
@@ -123,9 +124,6 @@ def load_robots(db: str) -> dict:
123124
str(bot.robots[emi][col]), "%Y-%m-%d %H:%M:%S"
124125
)
125126
bot.robots[emi]["PNL"] = 0
126-
bot.robots[emi]["FRAME"] = bot.robots[emi]["SYMBOL"] + str(
127-
bot.robots[emi]["TIMEFR"]
128-
)
129127
bot.robots[emi]["lotSize"] = (
130128
var.instruments[bot.robots[emi]["SYMBOL"]]["lotSize"]
131129
/ var.instruments[bot.robots[emi]["SYMBOL"]]["myMultiplier"]
@@ -141,7 +139,7 @@ def download_data(
141139
) -> Tuple[Union[list, None], Union[datetime, None]]:
142140
res = list()
143141
while target > time:
144-
data = var.ws.trade_bucketed(symbol=symbol, time=time, timeframe=timeframe)
142+
data = ws.bitmex.trade_bucketed(symbol=symbol, time=time, timeframe=timeframe)
145143
if data:
146144
last = time
147145
time = datetime.strptime(
@@ -163,20 +161,28 @@ def download_data(
163161
)
164162
var.logger.error(message)
165163
return None, None
166-
var.ws.logNumFatal = 0
164+
ws.bitmex.logNumFatal = 0
167165

168166
return res, time
169167

170168

171169
def load_frames(
172-
robot: dict, frames: dict, framing: dict, timeframe: str
170+
robot: dict,
171+
frames: dict,
173172
) -> Union[dict, None]:
174173
"""
175174
Loading usual candlestick data from the exchange server. Data is recorded
176175
in files for each algorithm. Every time you reboot the files are
177176
overwritten.
178177
"""
179-
filename = "data/" + timeframe + "_EMI" + robot["EMI"] + ".txt"
178+
filename = (
179+
"data/"
180+
+ robot["SYMBOL"]
181+
+ str(robot["TIMEFR"])
182+
+ "_EMI"
183+
+ robot["EMI"]
184+
+ ".txt"
185+
)
180186
with open(filename, "w"):
181187
pass
182188
target = datetime.utcnow()
@@ -190,7 +196,7 @@ def load_frames(
190196
res, time = download_data(
191197
time=time,
192198
target=target,
193-
symbol=framing[timeframe]["symbol"],
199+
symbol=robot["SYMBOL"],
194200
timeframe=var.timefrs[robot["TIMEFR"]],
195201
)
196202
if not res:
@@ -199,30 +205,35 @@ def load_frames(
199205
# The 'frames' array is filled with timeframe data.
200206

201207
for num, row in enumerate(res):
202-
utc = datetime.strptime(
203-
row["timestamp"][0:19], "%Y-%m-%dT%H:%M:%S"
204-
) - timedelta(minutes=robot["TIMEFR"])
205-
frames[timeframe].append(
208+
tm = datetime.strptime(row["timestamp"][0:19], "%Y-%m-%dT%H:%M:%S") - timedelta(
209+
minutes=robot["TIMEFR"]
210+
)
211+
frames[robot["SYMBOL"]][robot["TIMEFR"]]["data"].append(
206212
{
207-
"date": (utc.year - 2000) * 10000 + utc.month * 100 + utc.day,
208-
"time": utc.hour * 10000 + utc.minute * 100,
213+
"date": (tm.year - 2000) * 10000 + tm.month * 100 + tm.day,
214+
"time": tm.hour * 10000 + tm.minute * 100,
209215
"bid": float(row["open"]),
210216
"ask": float(row["open"]),
211217
"hi": float(row["high"]),
212218
"lo": float(row["low"]),
213-
"funding": 0,
214-
"datetime": utc,
219+
"datetime": tm,
215220
}
216221
)
217222
if num < len(res[:-1]) - 1:
218223
function.save_timeframes_data(
219224
emi=robot["EMI"],
220-
timeframe=timeframe,
221-
frame=frames[timeframe][-1],
225+
symbol=robot["SYMBOL"],
226+
timefr=str(robot["TIMEFR"]),
227+
frame=frames[robot["SYMBOL"]][robot["TIMEFR"]]["data"][-1],
222228
)
223-
framing[timeframe]["time"] = utc
229+
frames[robot["SYMBOL"]][robot["TIMEFR"]]["time"] = tm
224230

225-
message = "Downloaded missing data from the exchange for " + timeframe
231+
message = (
232+
"Downloaded missing data from the exchange for symbol="
233+
+ robot["SYMBOL"]
234+
+ " TIMEFR="
235+
+ str(robot["TIMEFR"])
236+
)
226237
var.logger.info(message)
227238
function.info_display(message)
228239

@@ -231,40 +242,36 @@ def load_frames(
231242

232243
def init_timeframes() -> Union[dict, None]:
233244
for emi in bot.robots:
234-
# Initialize candlestick timeframe data using 'TIMEFR' fields
245+
# Initialize candlestick timeframe data using 'TIMEFR' fields
235246
# expressed in minutes.
236-
if bot.robots[emi]["TIMEFR"]:
247+
if bot.robots[emi]["TIMEFR"] != "None":
237248
time = datetime.utcnow()
249+
symbol = bot.robots[emi]["SYMBOL"]
250+
timefr = bot.robots[emi]["TIMEFR"]
238251
try:
239-
bot.frames[bot.robots[emi]["FRAME"]]
240-
bot.framing[bot.robots[emi]["FRAME"]]["robots"].append(emi)
252+
bot.frames[symbol]
241253
except KeyError:
242-
bot.frames[bot.robots[emi]["FRAME"]] = []
243-
bot.framing[bot.robots[emi]["FRAME"]] = {
244-
"symbol": bot.robots[emi]["SYMBOL"],
245-
"timefr": bot.robots[emi]["TIMEFR"],
246-
"time": time,
247-
"robots": [],
248-
"open": 0,
249-
"trigger": 0,
250-
}
251-
bot.framing[bot.robots[emi]["FRAME"]]["robots"].append(emi)
252-
for num, symbol in enumerate(var.symbol_list):
253-
if symbol == bot.framing[bot.robots[emi]["FRAME"]]["symbol"]:
254-
bot.framing[bot.robots[emi]["FRAME"]]["SYMBOL"] = num
255-
break
256-
res = load_frames(
257-
robot=bot.robots[emi],
258-
frames=bot.frames,
259-
framing=bot.framing,
260-
timeframe=bot.robots[emi]["FRAME"],
261-
)
262-
if not res:
263-
message = (
264-
"The emi " + emi + " candle timeframe data was not loaded!"
254+
bot.frames[symbol] = dict()
255+
try:
256+
bot.frames[symbol][timefr]
257+
except KeyError:
258+
bot.frames[symbol][timefr] = {
259+
"time": time,
260+
"robots": [],
261+
"open": 0,
262+
"data": [],
263+
}
264+
bot.frames[symbol][timefr]["robots"].append(emi)
265+
res = load_frames(
266+
robot=bot.robots[emi],
267+
frames=bot.frames,
265268
)
266-
var.logger.error(message)
267-
return None
269+
if not res:
270+
message = (
271+
"The emi " + emi + " candle timeframe data was not loaded!"
272+
)
273+
var.logger.error(message)
274+
return None
268275

269276
return bot.frames
270277

bots/variables.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class Variables:
77
full_symbol_list = var.symbol_list.copy()
88
robots = OrderedDict()
99
frames = dict()
10-
framing = dict()
1110
emi_list = list()
1211
robo = dict()
13-
robots_status = dict()
12+
robot_status = dict()
1413
missing_days_number = 2

common/init.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from bots.variables import Variables as bot
1212
from common.variables import Variables as var
1313
from display.variables import Variables as disp
14+
from ws.init import Variables as ws
1415

1516
load_dotenv()
1617
db = os.getenv("MYSQL_DATABASE")
@@ -67,7 +68,7 @@ def load_trading_history() -> None:
6768
"history.ini error. The time in the history.ini file is greater than the current time."
6869
)
6970
exit(1)
70-
history = var.ws.trading_history(histCount=500, time=last_history_time)
71+
history = ws.bitmex.trading_history(histCount=500, time=last_history_time)
7172
if history == "error":
7273
var.logger.error("history.ini error")
7374
exit(1)
@@ -80,11 +81,11 @@ def load_trading_history() -> None:
8081
last_history_time = datetime.strptime(
8182
history[-1]["transactTime"][0:19], "%Y-%m-%dT%H:%M:%S"
8283
)
83-
history = var.ws.trading_history(histCount=500, time=last_history_time)
84+
history = ws.bitmex.trading_history(histCount=500, time=last_history_time)
8485
if last_history_time == tmp:
8586
break
8687
tmp = last_history_time
87-
if var.ws.logNumFatal == 0:
88+
if ws.bitmex.logNumFatal == 0:
8889
with open("history.ini", "w") as f:
8990
f.write(str(last_history_time))
9091

@@ -93,7 +94,7 @@ def load_orders() -> None:
9394
"""
9495
Load Orders (if any)
9596
"""
96-
myOrders = var.ws.open_orders()
97+
myOrders = ws.bitmex.open_orders()
9798
var.orders = OrderedDict()
9899
disp.text_orders.delete("1.0", "end")
99100
disp.text_orders.insert("1.0", " - Orders -\n")
@@ -235,10 +236,8 @@ def initial_display(account: int) -> None:
235236
)
236237

237238

238-
def initial_hi_lo_ticker_values() -> None:
239+
def initial_ticker_values() -> None:
239240
for symbol in var.symbol_list:
240241
var.ticker[symbol]["open_ask"] = var.ticker[symbol]["ask"]
241242
var.ticker[symbol]["open_bid"] = var.ticker[symbol]["bid"]
242-
var.ticker[symbol]["hi"] = var.ticker[symbol]["ask"]
243-
var.ticker[symbol]["lo"] = var.ticker[symbol]["bid"]
244243
var.ticker[symbol]["fundingRate"] = var.instruments[symbol]["fundingRate"]

common/variables.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
from dotenv import load_dotenv
77

8-
from ws.api import Connect_websocket
9-
108
load_dotenv()
119

1210
if not os.path.isfile(".env"):
@@ -86,7 +84,6 @@ class Variables:
8684
for cur in currencies:
8785
accounts[cur] = {y: 0 for y in name_acc}
8886
accounts[cur]["SUMREAL"] = 0
89-
ws = Connect_websocket
9087
connect_count = 0
9188
logger = None
9289
connect_mysql = None
@@ -100,8 +97,7 @@ class Variables:
10097
last_order = int((time.time() - 1591000000) * 10)
10198
last_database_time = datetime(1900, 1, 1, 1, 1)
10299
message_time = datetime.utcnow()
103-
refresh = message_time
104-
refresh_rate = max(min(10, int(os.getenv("REFRESH_RATE"))), 1)
100+
refresh_rate = min(max(100, int(1000 / int(os.getenv("REFRESH_RATE")))), 1000)
105101
refresh_minute = message_time.minute
106102
refresh_hour = message_time.hour
107103
message_point = 0
@@ -111,3 +107,4 @@ class Variables:
111107
order_book_depth = "orderBook10"
112108
else:
113109
order_book_depth = "quote"
110+
thread_is_active = ""

0 commit comments

Comments
 (0)