Skip to content

Commit

Permalink
robots entry has been moved in a new thread
Browse files Browse the repository at this point in the history
  • Loading branch information
evgrmn committed Jan 31, 2024
1 parent 4ba3eff commit 6fdf7c3
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
10 changes: 5 additions & 5 deletions bots/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,19 @@ def load_frames(
# The 'frames' array is filled with timeframe data.

for num, row in enumerate(res):
utc = datetime.strptime(
tm = datetime.strptime(
row["timestamp"][0:19], "%Y-%m-%dT%H:%M:%S"
) - timedelta(minutes=robot["TIMEFR"])
frames[timeframe].append(
{
"date": (utc.year - 2000) * 10000 + utc.month * 100 + utc.day,
"time": utc.hour * 10000 + utc.minute * 100,
"date": (tm.year - 2000) * 10000 + tm.month * 100 + tm.day,
"time": tm.hour * 10000 + tm.minute * 100,
"bid": float(row["open"]),
"ask": float(row["open"]),
"hi": float(row["high"]),
"lo": float(row["low"]),
"funding": 0,
"datetime": utc,
"datetime": tm,
}
)
if num < len(res[:-1]) - 1:
Expand All @@ -220,7 +220,7 @@ def load_frames(
timeframe=timeframe,
frame=frames[timeframe][-1],
)
framing[timeframe]["time"] = utc
framing[timeframe]["time"] = tm

message = "Downloaded missing data from the exchange for " + timeframe
var.logger.info(message)
Expand Down
1 change: 1 addition & 0 deletions common/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ class Variables:
order_book_depth = "orderBook10"
else:
order_book_depth = "quote"
thread_is_active = ""
16 changes: 15 additions & 1 deletion connect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import time
from collections import OrderedDict
from datetime import datetime
from time import sleep
import threading

from dotenv import load_dotenv

Expand Down Expand Up @@ -29,6 +31,7 @@ def connection():
pass
var.ws = None
while not var.ws:
var.thread_is_active = ""
var.ws = Connect_websocket(
endpoint=os.getenv("EXCHANGE_API_URL"),
symbol=var.symbol_list,
Expand Down Expand Up @@ -58,6 +61,9 @@ def connection():
bot.robots[emi]["STATUS"] = value
for emi in bot.robots:
bot.robot_pos[emi] = 0
var.thread_is_active = "yes"
thread = threading.Thread(target=robots_thread)
thread.start()
else:
print("Error during loading timeframes.")
var.ws.exit()
Expand Down Expand Up @@ -97,7 +103,6 @@ def refresh() -> None:
var.ticker = var.ws.get_ticker(var.ticker)
var.instruments = var.ws.get_instrument(instruments=var.instruments)
function.ticker_hi_lo_minute_price(utc=utc)
function.robots_timeframes(utc=utc)
while var.ws.get_exec():
function.transaction(list(var.ws.get_exec().values())[0])
var.ws.get_exec().popitem(last=False)
Expand All @@ -124,3 +129,12 @@ def clear_params() -> None:
bot.robots = OrderedDict()
bot.frames = {}
bot.framing = {}


def robots_thread() -> None:
while var.thread_is_active:
utcnow = datetime.utcnow()
if bot.frames:
function.robots_entry(utc=utcnow)
rest = 1-time.time()%1
time.sleep(rest)
1 change: 1 addition & 0 deletions display/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


def terminal_reload(event) -> None:
var.thread_is_active = ""
function.info_display("Restarting...")
disp.root.update()
var.ws = connect.connection()
Expand Down
1 change: 1 addition & 0 deletions display/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def handler_robots(y_pos):


def on_closing(root, refresh_var):
var.thread_is_active = ""
root.after_cancel(refresh_var)
root.destroy()

Expand Down
12 changes: 1 addition & 11 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,16 +1169,6 @@ def display_order_book_values(
disp.labels["robots"][5][num + 1]["fg"] = "#212121"
bot.robot_pos[emi] = bot.robots[emi]["POS"]

'''if bot.robot_status[emi] != bot.robots[emi]["STATUS"]:
if bot.robots[emi]["STATUS"] == "OFF"
if bot.robots[emi]["STATUS"] in ["NOT IN LIST", "OFF", "NOT DEFINED"] or (
bot.robots[emi]["STATUS"] == "RESERVED" and bot.robots[emi]["POS"] != 0
):
disp.labels["robots"][5][num + 1]["fg"] = "red"
else:
disp.labels["robots"][5][num + 1]["fg"] = "#212121"'''

# Refresh Account table

for symbol, position in var.positions.items():
Expand Down Expand Up @@ -1571,7 +1561,7 @@ def save_timeframes_data(emi: str, timeframe: str, frame: dict) -> None:
f.write(data + "\n")


def robots_timeframes(utc: datetime) -> None:
def robots_entry(utc: datetime) -> None:
"""
Processing timeframes and entry point into robot algorithms
"""
Expand Down
2 changes: 1 addition & 1 deletion history.ini
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-01-29 12:00:00
2024-01-31 12:00:00

0 comments on commit 6fdf7c3

Please sign in to comment.