This commit is contained in:
David Brazda
2023-11-23 12:16:45 +01:00
parent 159302d168
commit 9571a7e0e6
4 changed files with 37 additions and 22 deletions

View File

@ -381,7 +381,7 @@ def run_batch_stratin(id: UUID, runReq: RunRequest):
def get_market_days_in_interval(datefrom, dateto, note = None, id = None):
#getting dates from calendat
clientTrading = TradingClient(ACCOUNT1_PAPER_API_KEY, ACCOUNT1_PAPER_SECRET_KEY, raw_data=False)
clientTrading = TradingClient(ACCOUNT1_PAPER_API_KEY, ACCOUNT1_PAPER_SECRET_KEY, raw_data=False, paper=True)
calendar_request = GetCalendarRequest(start=datefrom,end=dateto)
cal_dates = clientTrading.get_calendar(calendar_request)
#list(Calendar)
@ -469,6 +469,11 @@ def batch_run_manager(id: UUID, runReq: RunRequest, rundays: list[RunDay]):
print("Entering BATCH RUN MANAGER")
print("generated batch_ID", batch_id)
#sort rundays according to start atttibute
print("RUNDAYS before sort:", rundays)
rundays.sort(key=lambda x: x.start)
print("RUNDAYS after sort:", rundays)
cnt_max = len(rundays)
cnt = 0
#promenna pro sdileni mezi runy jednotlivych batchů (např. daily profit)
@ -476,14 +481,15 @@ def batch_run_manager(id: UUID, runReq: RunRequest, rundays: list[RunDay]):
note_from_run_request = runReq.note
first = None
last = None
first_frm = runReq.bt_from.strftime("%d.%m.")
last_frm = runReq.bt_to.strftime("%d.%m.")
# Find the minimum start date and maximum end date to add to Note
first = min(runday.start for runday in rundays)
last = max(runday.end for runday in rundays)
first_frm = first.strftime("%d.%m.")
last_frm = last.strftime("%d.%m.")
for day in rundays:
cnt += 1
if cnt == 1:
first = day.start
elif cnt == cnt_max:
last = day.end
print("Datum od", day.start)
print("Datum do", day.end)
runReq.bt_from = day.start
@ -509,7 +515,7 @@ def batch_run_manager(id: UUID, runReq: RunRequest, rundays: list[RunDay]):
for i in db.stratins:
if str(i.id) == str(id):
i.history += "\nBatch: "+str(batch_id)+" "+str(first)+" "+str(last)+" P:"+str(int(batch_abs_profit))+ "R:"+str(round(batch_rel_profit,4))
i.history += "\nBatch: "+str(batch_id)+" "+str(first_frm)+" "+str(last_frm)+" P:"+str(int(batch_abs_profit))+ "R:"+str(round(batch_rel_profit,4))
#i.history += str(runner.__dict__)+"<BR>"
db.save()
@ -791,7 +797,7 @@ def populate_metrics_output_directory(strat: StrategyInstance, inter_batch_param
#archives runner and details
def archive_runner(runner: Runner, strat: StrategyInstance, inter_batch_params: dict = None):
results_metrics = dict()
print("inside archive_runner")
print("inside store archive_runner")
try:
if strat.bt is not None:
bp_from = strat.bt.bp_from

View File

@ -50,7 +50,10 @@ class Trade_Offline_Streamer(Thread):
pass
def run(self):
self.main()
try:
self.main()
except Exception as e:
print("ERROR IN TRADE OFFLINE STREAMER"+str(e)+format_exc())
# #create new asyncio loop in the thread
# loop = asyncio.new_event_loop()
# asyncio.set_event_loop(loop)

View File

@ -37,13 +37,9 @@ def generate_trading_report_image(runner_ids: list = None, batch_id: str = None,
return -1, f"no batch {batch_id} found"
trades = []
cnt_max = len(runner_ids)
cnt = 0
#zatim zjistujeme start a end z min a max dni - jelikoz muze byt i seznam runner_ids a nejenom batch
end_date = None
start_date = None
sada_list = []
for id in runner_ids:
cnt += 1
#get runner
res, sada =cs.get_archived_runner_header_byID(id)
if res != 0:
@ -51,12 +47,9 @@ def generate_trading_report_image(runner_ids: list = None, batch_id: str = None,
return -1, f"no runner {id} found"
print("archrunner")
print(sada)
#print(sada)
sada_list.append(sada)
if cnt == 1:
start_date = sada.bt_from if sada.mode in [Mode.BT,Mode.PREP] else sada.started
if cnt == cnt_max:
end_date = sada.bt_to if sada.mode in [Mode.BT or Mode.PREP] else sada.stopped
# Parse trades
trades_dicts = sada.metrics["prescr_trades"]
@ -70,6 +63,17 @@ def generate_trading_report_image(runner_ids: list = None, batch_id: str = None,
print(trades)
symbol = sada.symbol
mode = sada.mode
#get from to dates
#calculate start a end z min a max dni - jelikoz muze byt i seznam runner_ids a nejenom batch, pripadne testovaci sady
if mode in [Mode.BT,Mode.PREP]:
start_date = min(runner.bt_from for runner in sada_list)
end_date = max(runner.bt_to for runner in sada_list)
else:
start_date = min(runner.started for runner in sada_list)
end_date = max(runner.stopped for runner in sada_list)
#hour bars for backtested period
print(start_date,end_date)
bars= get_historical_bars(symbol, start_date, end_date, TimeFrame.Hour)
@ -542,5 +546,5 @@ def generate_trading_report_image(runner_ids: list = None, batch_id: str = None,
if __name__ == '__main__':
# id_list = ["e8938b2e-8462-441a-8a82-d823c6a025cb"]
# generate_trading_report_image(runner_ids=id_list)
batch_id = "90973e57"
batch_id = "05cb35e3"
generate_trading_report_image(batch_id=batch_id)

View File

@ -408,6 +408,8 @@ class Strategy:
#data loader thread
self.dataloader.start()
print("jsme za spustenim loaderu")
if self.mode == Mode.LIVE or self.mode == Mode.PAPER:
#live notification thread
self.order_notifs.start()