-
Notifications
You must be signed in to change notification settings - Fork 6
/
services.py
838 lines (727 loc) · 26.6 KB
/
services.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
import os
import platform
import time
import tkinter as tk
import traceback
from collections import OrderedDict
from datetime import datetime, timezone
from typing import Union
from dotenv import dotenv_values, set_key
from common.data import BotData, Bots, Instrument
from common.variables import Variables as var
from display.messages import ErrorMessage, Message
from indicators import BreakDown
if platform.system() == "Windows":
import ctypes
import ctypes.wintypes
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_READ = 0x0010
pid = os.getpid() # Current process PID
class PROCESS_MEMORY_COUNTERS(ctypes.Structure):
_fields_ = [
("cb", ctypes.wintypes.DWORD),
("PageFaultCount", ctypes.wintypes.DWORD),
("PeakWorkingSetSize", ctypes.wintypes.ctypes.c_size_t),
("WorkingSetSize", ctypes.wintypes.ctypes.c_size_t),
("QuotaPeakPagedPoolUsage", ctypes.wintypes.ctypes.c_size_t),
("QuotaPagedPoolUsage", ctypes.wintypes.ctypes.c_size_t),
("QuotaPeakNonPagedPoolUsage", ctypes.wintypes.ctypes.c_size_t),
("QuotaNonPagedPoolUsage", ctypes.wintypes.ctypes.c_size_t),
("PagefileUsage", ctypes.wintypes.ctypes.c_size_t),
("PeakPagefileUsage", ctypes.wintypes.ctypes.c_size_t),
]
counters = PROCESS_MEMORY_COUNTERS()
process_handle = ctypes.windll.kernel32.OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, pid
)
def get_memory_usage():
"""Get the memory usage of the current process (Windows)"""
ctypes.windll.psapi.GetProcessMemoryInfo(
process_handle, ctypes.byref(counters), ctypes.sizeof(counters)
)
# Return memory usage in bytes (WorkingSetSize = physical memory
# currently used by the process)
return counters.WorkingSetSize / (1024**2) # Convert bytes to MB
else:
# Unix-like OS (Darwin is macOS)
import resource
def get_memory_usage():
"""Get the memory usage of the current process (Unix)"""
usage = resource.getrusage(resource.RUSAGE_SELF)
if platform.system() == "Darwin":
return usage.ru_maxrss / 1048576 # Convert B to MB
else:
return usage.ru_maxrss / 1024 # Convert KB to MB
class Variables:
cpu_count = 1 # os.cpu_count()
start_time = 0
cpu_start = 0
cpu_usage = 0
memory_usage = 0
usage_count = 0
def get_usage():
per_second = int(1000 / var.refresh_rate)
if Variables.usage_count >= per_second:
# Comes here 1 time every second
current_time = time.time()
current_cpu = os.times()
if Variables.start_time != 0:
# Calculate elapsed real and user times
elapsed_real_time = current_time - Variables.start_time
elapsed_user_time = current_cpu.user - Variables.cpu_start.user
elapsed_sys_time = current_cpu.system - Variables.cpu_start.system
# Calculate CPU usage percentage
Variables.cpu_usage = int(
round(
((elapsed_user_time + elapsed_sys_time) / elapsed_real_time)
* 100
/ Variables.cpu_count,
0,
)
)
# Calculate memory usage in MB
Variables.memory_usage = int(round(get_memory_usage(), 0))
# print(f"s={current_time}, f={time.time()}, e={time.time() - current_time}")
Variables.start_time = current_time
Variables.cpu_start = current_cpu
Variables.usage_count = 0
Variables.usage_count += 1
def ticksize_rounding(price: float, ticksize: float) -> float:
"""
Rounds the price depending on the tickSize value.
"""
arg = 1 / ticksize
res = round(price * arg, 0) / arg
return res
def number_rounding(number: float, precision: int) -> str:
"""
Rounds a number to the specified precision.
"""
if not isinstance(number, float):
return number
number = "{:.{precision}f}".format(number, precision=precision)
for num, char in enumerate(reversed(number)):
if char != "0":
break
number = number[: len(number) - num]
if number[-1] == ".":
number = number[:-1]
return number
def time_converter(
time: Union[int, float, str, datetime], usec=False
) -> Union[datetime, int]:
"""
The datetime always corresponds to utc time, the timestamp always
corresponds to local time.
int, float -> datetime (utc)
datetime utc -> Unix timestamp (local time)
str utc -> datetime (utc)
"""
if isinstance(time, int) or isinstance(time, float):
return datetime.fromtimestamp(time, tz=timezone.utc)
elif isinstance(time, datetime):
return int(time.timestamp() * 1000)
elif isinstance(time, str):
time = time.replace("T", " ")
time = time.replace("Z", "")
f = time.find("+")
if f > 0:
time = time[:f]
if usec:
try:
dt = datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f")
except Exception:
dt = datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
else:
dt = datetime.strptime(time[:19], "%Y-%m-%d %H:%M:%S")
dt = dt.replace(tzinfo=timezone.utc)
return dt
else:
raise TypeError(type(time))
def precision(number: float) -> int:
r = str(number)
if "e" in r:
r = r.replace("e", "")
r = r.replace(".", "")
r = r.split("-")
precision = len(r[0]) - 1 + int(r[1])
elif "." in r:
r = r.split(".")
if int(r[1]) == 0:
precision = 0
else:
precision = len(r[1])
else:
precision = 0
return precision
def add_space(line: list) -> str:
n = max(map(lambda x: len(x), line))
lst = list()
for l in line:
lst.append((n - len(l)) * " " + l)
return "\n".join(lst)
def close(markets):
for bot_name in var.bot_thread_active:
var.bot_thread_active[bot_name] = False
for name in var.market_list:
ws = markets[name]
ws.exit()
def display_exception(exception, display=True) -> str:
formated = "".join(
traceback.format_exception(type(exception), exception, exception.__traceback__)
)
if display == True:
print(formated)
return formated
def select_database(query: str) -> list:
err_locked = 0
while True:
try:
var.sql_lock.acquire(True)
var.cursor_sqlite.execute(query)
orig = var.cursor_sqlite.fetchall()
var.sql_lock.release()
data = []
if orig:
data = list(map(lambda x: dict(zip(orig[0].keys(), x)), orig))
return data
except Exception as e: # var.error_sqlite
if "database is locked" not in str(e):
print("_____query:", query)
var.logger.error("Sqlite Error: " + str(e) + ")")
var.sql_lock.release()
break
else:
err_locked += 1
var.logger.error(
"Sqlite Error: Database is locked (attempt: "
+ str(err_locked)
+ ")"
)
var.sql_lock.release()
def insert_database(values: list, table: str) -> None:
err_locked = 0
while True:
try:
var.sql_lock.acquire(True)
if table == "coins":
var.cursor_sqlite.execute(
"insert into coins (EXECID,EMI,REFER,CURRENCY,SYMBOL,"
+ "TICKER,CATEGORY,MARKET,SIDE,QTY,QTY_REST,PRICE,"
+ "THEOR_PRICE,TRADE_PRICE,SUMREAL,COMMISS,CLORDID,TTIME,"
+ "ACCOUNT) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
values,
)
elif table == "robots":
var.cursor_sqlite.execute(
"insert into robots (EMI,STATE,TIMEFR) VALUES (?,?,?)",
values,
)
elif table in ["obsolete", "backtest"]:
qwr = (
"""insert into %s (SYMBOL,MARKET,CATEGORY,CURRENCY,
TICKER,MYMULTIPLIER,MULTIPLIER,TICKSIZE,
PRICE_PRECISION,MINORDERQTY,QTYSTEP,PRECISION,EXPIRE,
BASECOIN,QUOTECOIN,VALUEOFONECONTRACT,TAKERFEE,MAKERFEE)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"""
% table
)
var.cursor_sqlite.execute(qwr, values)
else:
return "Sqlite Error: Unknown database table."
var.connect_sqlite.commit()
var.sql_lock.release()
return None
except Exception as ex: # var.error_sqlite
if "database is locked" not in str(ex):
err_str = f"Sqlite Error: {str(ex)} for: {values[0]}"
var.logger.error(err_str)
var.sql_lock.release()
return err_str
else:
err_locked += 1
var.logger.error(
"Sqlite Error: Database is locked (attempt: "
+ str(err_locked)
+ ")"
)
var.connect_sqlite.rollback()
var.sql_lock.release()
def update_database(query: list) -> Union[str, None]:
err_locked = 0
while True:
try:
var.sql_lock.acquire(True)
var.cursor_sqlite.execute(query)
var.connect_sqlite.commit()
var.sql_lock.release()
return None
except Exception as e: # var.error_sqlite
if "database is locked" not in str(e):
err_str = f"Sqlite Error: {str(e)}"
var.logger.error(err_str)
var.sql_lock.release()
return err_str
else:
err_locked += 1
var.logger.error(
"Sqlite Error: Database is locked (attempt: "
+ str(err_locked)
+ ")"
)
var.connect_sqlite.rollback()
var.sql_lock.release()
def set_clOrdID(emi: str) -> str:
var.last_order += 1
clOrdID = f"{var.last_order}.{emi}"
return clOrdID
def fill_order(emi: str, clOrdID: str, category: str, value: dict) -> None:
if emi not in var.orders:
var.orders[emi] = OrderedDict()
if clOrdID not in var.orders[emi]:
var.orders[emi][clOrdID] = dict()
var.orders[emi][clOrdID]["emi"] = emi
var.orders[emi][clOrdID]["leavesQty"] = value["leavesQty"]
var.orders[emi][clOrdID]["transactTime"] = value["transactTime"]
var.orders[emi][clOrdID]["price"] = value["price"]
var.orders[emi][clOrdID]["symbol"] = value["symbol"]
var.orders[emi][clOrdID]["category"] = category
var.orders[emi][clOrdID]["market"] = value["symbol"][1]
var.orders[emi][clOrdID]["side"] = value["side"]
var.orders[emi][clOrdID]["orderID"] = value["orderID"]
var.orders[emi][clOrdID]["clOrdID"] = clOrdID
var.orders[emi][clOrdID]["orderQty"] = value["orderQty"]
def fill_bot_position(
bot_name: str, symbol: tuple, instrument: Instrument, user_id: int
) -> None:
bot = Bots[bot_name]
bot.bot_positions[symbol] = {
"emi": bot_name,
"symbol": instrument.symbol,
"category": instrument.category,
"market": instrument.market,
"ticker": instrument.ticker,
"position": 0,
"volume": 0,
"sumreal": 0,
"commiss": 0,
"ltime": None,
"pnl": 0,
"lotSize": instrument.minOrderQty,
"currency": instrument.settlCurrency[0],
"limits": instrument.minOrderQty,
"max_position": 0,
}
# Checks if this bot has any records in the database on this instrument.
if not var.backtest:
qwr = (
"select MARKET, SYMBOL, sum(abs(QTY)) as SUM_QTY, "
+ "sum(SUMREAL) as SUM_SUMREAL, sum(COMMISS) as "
+ "SUM_COMMISS, TTIME from (select * from coins where EMI = '"
+ bot_name
+ "' and SYMBOL = '"
+ instrument.symbol
+ "' and MARKET = '"
+ instrument.market
+ "' and ACCOUNT = "
+ str(user_id)
+ " and SIDE <> 'Fund' order by ID desc) T;"
)
data = select_database(qwr)[0]
if data and data["SUM_QTY"]:
bot.bot_positions[symbol]["volume"] = float(data["SUM_QTY"])
bot.bot_positions[symbol]["sumreal"] = float(data["SUM_SUMREAL"])
bot.bot_positions[symbol]["commiss"] = float(data["SUM_COMMISS"])
def timeframe_seconds(timefr: str) -> int:
"""
Converts a time interval in a string to seconds.
"""
timefr_minutes = var.timeframe_human_format[timefr]
return timefr_minutes * 60
def bot_error(bot: BotData) -> str:
if not bot.error_message:
error = "-"
else:
error = bot.error_message["error_type"]
return error
def kline_hi_lo_values(ws, symbol: tuple, instrument: Instrument) -> None:
"""
Updates the high and low values of kline data when websocket updates the
order book.
Parameters
----------
ws: Markets
Bitmex, Bybit, Deribit
symbol: tuple
Instrument symbol in (symbol, market name) format, e.g.
("BTCUSD", "Bybit").
instrument: Instrument
The Instrument instance for this symbol.
"""
if symbol in ws.klines:
ask = instrument.asks[0][0]
bid = instrument.bids[0][0]
for timefr, values in ws.klines[symbol].items():
if values["data"]:
if ask > values["data"][-1]["hi"]:
values["data"][-1]["hi"] = ask
if bid < values["data"][-1]["lo"]:
values["data"][-1]["lo"] = bid
if symbol in BreakDown.symbols:
if timefr in BreakDown.symbols[symbol]:
for parameters in BreakDown.symbols[symbol][timefr].values():
#print(Bots[parameters["bot_name"]].multitrade)
direct = parameters["first"] * (parameters["number"] % 2 * 2 - 1)
if direct >= 0 and ask > parameters["up"]:
parameters["number"] += 1
if parameters["first"] == 0:
parameters["first"] = -1
if direct <= 0 and bid < parameters["dn"]:
parameters["number"] += 1
if parameters["first"] == 0:
parameters["first"] = 1
def count_orders():
"""Temporarily created function for debugging"""
count = 0
for values in var.orders.values():
for _ in values.keys():
count += 1
# print("___________________orders", count)
def noll(val: str, length: int) -> str:
r = ""
for _ in range(length - len(val)):
r = r + "0"
return r + val
def format_message(market: str, message: str, tm=None) -> str:
"""
Formats the message in the required format.
"""
if market:
market += ": "
if not tm:
tm = datetime.now(tz=timezone.utc)
text = (
noll(str(tm.hour), 2)
+ ":"
+ noll(str(tm.minute), 2)
+ ":"
+ noll(str(tm.second), 2)
+ "."
+ noll(str(int(tm.microsecond / 1000)), 3)
+ " "
+ market
+ message
+ "\n"
)
if isinstance(text, tuple):
text = text[0]
return text
def wrap(frame: tk.Frame, padx):
for child in frame.winfo_children():
if type(child) is tk.Label:
child.config(wraplength=frame.winfo_width() - padx * 2)
def cancel_market(market: str) -> None:
"""
Removes an exchange from the boot process if an error like "Host name is
invalid" occurs.
"""
if market in var.market_list:
var.market_list.remove(market)
def check_symbol_list(symbols: list, market: str, symbol_list: list) -> list:
"""
Checks if the symbols in the symbol_list of a given market are valid for
further websocket subscription. Removes misspelled or expired symbols
from the symbol_list. If symbol_list is empty after removal, adds a
default symbol.
Parameters
----------
symbols: list
List of symbols of all available active instruments, which is
received from the exchange.
market: str
Exchange name.
symbol_list: list
Symbols for subscription.
Returns
-------
list
Corrected symbol_list.
"""
def put_default(symbol_list: list):
if not symbol_list:
default = var.default_symbol[market]
symbol_list = var.default_symbol[market]
message = Message.DEFAULT_SYMBOL_ADDED.format(
SYMBOL=default[0], MARKET=market
)
var.queue_info.put(
{
"market": market,
"message": message,
"time": datetime.now(tz=timezone.utc),
"warning": "warning",
}
)
var.logger.info(message)
return symbol_list
if symbols:
for symbol in symbol_list.copy():
if symbol not in symbols:
message = ErrorMessage.UNKNOWN_SYMBOL.format(
SYMBOL=symbol[0], MARKET=market
)
var.queue_info.put(
{
"market": market,
"message": message,
"time": datetime.now(tz=timezone.utc),
"warning": "error",
}
)
var.logger.error(message)
symbol_list.remove(symbol)
symbol_list = put_default(symbol_list=symbol_list)
else:
symbol_list = put_default(symbol_list=symbol_list)
return symbol_list
def unexpected_error(ws) -> str:
"""
if the http request produces an error, or if it is correct but the data
received does not match the expected data, and this error is assumed to
be FATAL.
"""
if not ws.logNumFatal:
ws.logNumFatal = "FATAL"
return ws.logNumFatal
def fill_instrument_index(index: OrderedDict, instrument: Instrument, ws) -> dict:
"""
Adds an instrument to the instrument_index dictionary.
Parameters
----------
index: dict
The instrument_index is used for the Instrument Menu. It ranks
instruments by category, currency.
instrument: Instrument
An Instrument instance, which belongs to a particular exchange.
ws: Markets
Bitmex, Bybit or Deribit object.
Returns
-------
dict
The instrument_index dictionary.
"""
category = instrument.category
if "spot" in category:
currency = instrument.baseCoin
else:
currency = instrument.settlCurrency[0]
if category not in index:
index[category] = OrderedDict()
if currency not in index[category]:
index[category][currency] = OrderedDict()
symb = instrument.symbol
if "option" in category and "combo" not in category:
parts = symb.split("-")
option_series = "-".join(parts[:2]) + var._series
if option_series not in index[category][currency]:
index[category][currency][option_series] = OrderedDict()
index[category][currency][option_series]["CALLS"] = list()
index[category][currency][option_series]["PUTS"] = list()
index[category][currency][option_series]["sort"] = option_series
if parts[-1] == "C":
index[category][currency][option_series]["CALLS"].append(symb)
else:
index[category][currency][option_series]["PUTS"].append(symb)
# Add a series of options.
symbol = (option_series, instrument.market)
series: Instrument = ws.Instrument.add(symbol)
series.market = instrument.market
series.symbol = option_series
series.ticker = "option!"
series.category = instrument.category
series.settlCurrency = instrument.settlCurrency
series.quoteCoin = instrument.quoteCoin
series.currentQty = "-"
series.avgEntryPrice = "-"
series.unrealisedPnl = "-"
series.marginCallPrice = "-"
series.state = instrument.state
series.volume24h = "-"
series.expire = instrument.expire
series.fundingRate = "-"
series.baseCoin = instrument.baseCoin
series.precision = instrument.precision
series.asks = [["-", "-"]]
series.bids = [["-", "-"]]
series.price_precision = 1
series.isInverse = instrument.isInverse
else:
if symb not in index[category][currency]:
index[category][currency][symb] = {"sort": symb}
return index
def define_symbol_key(market: str):
return f"{market}_SYMBOLS"
def set_dotenv(dotenv_path: str, key: str, value: str):
"""
Updates dotenv file.
"""
set_key(
dotenv_path=dotenv_path,
key_to_set=key,
value_to_set=value,
)
def sort_instrument_index(index: OrderedDict) -> OrderedDict:
"""
Categories and currencies are sorted by name, instruments by the `sort`
parameter, options are additionally sorted by ascending strike price.
"""
res = OrderedDict(sorted(index.items(), key=lambda x: x[0]))
for category, values_category in index.items():
res[category] = OrderedDict(sorted(values_category.items(), key=lambda x: x[0]))
for currency, values_currency in res[category].items():
res[category][currency] = OrderedDict(
sorted(values_currency.items(), key=lambda x: x[1]["sort"])
)
for series, values in res[category][currency].items():
for key, value in values.items():
if key in ["CALLS", "PUTS"]:
try:
value.sort(
key=lambda x: float(x.split("-")[-2].replace("d", "."))
)
except Exception:
try:
value.sort(
key=lambda x: float(x.split("-")[-1].split("_")[0])
)
except Exception:
value.sort()
res[category][currency][series][key] = value
return res
def select_option_strikes(index: dict, instrument: Instrument) -> list:
"""
Extracts all strikes from a series of options.
"""
series = index[instrument.category][instrument.settlCurrency[0]][instrument.symbol]
return series["CALLS"] + series["PUTS"]
def load_preferences(root, width, height):
"""
Load the last remembered params to be used for the terminal appearance
"""
if not os.path.isfile(var.preferences):
set_dotenv(
dotenv_path=var.preferences,
key="ROOT_WIDTH",
value=str(width),
)
set_dotenv(
dotenv_path=var.preferences,
key="ROOT_HEIGHT",
value=str(height),
)
set_dotenv(
dotenv_path=var.preferences,
key="ROOT_X_POS",
value=str(root.winfo_x()),
)
set_dotenv(
dotenv_path=var.preferences,
key="ROOT_Y_POS",
value=str(root.winfo_y()),
)
return dotenv_values(var.preferences)
def order_form_title():
if len(var.symbol[0]) > 22:
return var.symbol[0][:22] + "\n" + var.symbol[0][22:]
else:
return var.symbol[0]
def set_emi(symbol: tuple) -> str:
return ".".join(symbol)
def add_symbol_database(instrument: Instrument, table: str) -> None:
values = [
instrument.symbol,
instrument.market,
instrument.category,
instrument.settlCurrency[0],
instrument.ticker,
instrument.myMultiplier,
instrument.multiplier,
instrument.tickSize,
instrument.price_precision,
instrument.minOrderQty,
instrument.qtyStep,
instrument.precision,
instrument.expire,
instrument.baseCoin,
instrument.quoteCoin,
instrument.valueOfOneContract,
instrument.takerFee,
instrument.makerFee,
]
res = insert_database(values=values, table=table)
print("____", res)
def set_symbol(instrument: Instrument, data: dict) -> None:
instrument.symbol = data["SYMBOL"]
instrument.market = data["MARKET"]
instrument.category = data["CATEGORY"]
instrument.settlCurrency = (data["CURRENCY"], data["MARKET"])
instrument.ticker = data["TICKER"]
instrument.myMultiplier = data["MYMULTIPLIER"]
instrument.multiplier = data["MULTIPLIER"]
instrument.tickSize = data["TICKSIZE"]
instrument.price_precision = data["PRICE_PRECISION"]
instrument.minOrderQty = data["MINORDERQTY"]
instrument.qtyStep = data["QTYSTEP"]
instrument.precision = data["PRECISION"]
if data["EXPIRE"] == "Perpetual":
instrument.expire = data["EXPIRE"]
else:
instrument.expire = time_converter(time=data["EXPIRE"])
instrument.baseCoin = data["BASECOIN"]
instrument.quoteCoin = data["QUOTECOIN"]
instrument.valueOfOneContract = data["VALUEOFONECONTRACT"]
instrument.takerFee = data["TAKERFEE"]
instrument.makerFee = data["MAKERFEE"]
instrument.state = "Expired"
def display_backtest_parameters(bot: BotData):
symbols = ""
for symbol in var.backtest_symbols:
symbols += "\n " + str(symbol)
text = (
"Backtesting\n\nBot parameters:\n- name: "
+ bot.name
+ "\n- timeframe: "
+ bot.timefr
+ "\n- used instruments: "
+ symbols
)
print(text)
def process_position(
bot: BotData,
symbol: tuple,
instrument: Instrument,
user_id: int,
qty: float,
calc: dict,
ttime: Union[datetime, str],
):
if symbol not in bot.bot_positions:
fill_bot_position(
bot_name=bot.name,
symbol=symbol,
instrument=instrument,
user_id=user_id,
)
position = bot.bot_positions[symbol]
if "spot" not in instrument.category:
position["position"] += qty
position["position"] = round(
position["position"],
instrument.precision,
)
position["volume"] += abs(qty)
position["commiss"] += calc["commiss"]
position["sumreal"] += calc["sumreal"]
position["ltime"] = ttime
if abs(position["position"]) > position["max_position"]:
position["max_position"] = abs(position["position"])