createbatch image tool + send to telefram enrichment

This commit is contained in:
David Brazda
2024-02-11 12:37:19 +07:00
parent 2ba492ead2
commit 04cf2e2ba2
6 changed files with 58 additions and 15 deletions

18
testy/createbatchimage.py Normal file
View File

@ -0,0 +1,18 @@
import argparse
import v2realbot.reporting.metricstoolsimage as mt
# Parse the command-line arguments
# parser = argparse.ArgumentParser(description="Generate trading report image with batch ID")
# parser.add_argument("batch_id", type=str, help="The batch ID for the report")
# args = parser.parse_args()
# batch_id = args.batch_id
# Generate the report image
res, val = mt.generate_trading_report_image(batch_id="4d7dc163")
# Print the result
if res == 0:
print("BATCH REPORT CREATED")
else:
print(f"BATCH REPORT ERROR - {val}")

View File

@ -24,7 +24,6 @@ from tinydb import TinyDB, Query, where
from tinydb.operations import set from tinydb.operations import set
import orjson import orjson
import numpy as np import numpy as np
from numpy import ndarray
from rich import print from rich import print
import pandas as pd import pandas as pd
from traceback import format_exc from traceback import format_exc
@ -37,10 +36,10 @@ from v2realbot.strategyblocks.inits.init_indicators import initialize_dynamic_in
from v2realbot.strategyblocks.indicators.indicators_hub import populate_dynamic_indicators from v2realbot.strategyblocks.indicators.indicators_hub import populate_dynamic_indicators
from v2realbot.interfaces.backtest_interface import BacktestInterface from v2realbot.interfaces.backtest_interface import BacktestInterface
import os import os
from v2realbot.reporting.metricstoolsimage import generate_trading_report_image import v2realbot.reporting.metricstoolsimage as mt
import msgpack
import gzip import gzip
import os import os
import msgpack
#import gc #import gc
#from pyinstrument import Profiler #from pyinstrument import Profiler
#adding lock to ensure thread safety of TinyDB (in future will be migrated to proper db) #adding lock to ensure thread safety of TinyDB (in future will be migrated to proper db)
@ -349,13 +348,15 @@ def capsule(target: object, db: object, inter_batch_params: dict = None):
db.runners.remove(i) db.runners.remove(i)
#vytvoreni report image pro RUNNER #vytvoreni report image pro RUNNER
try: try:
res, val = generate_trading_report_image(runner_ids=[str(i.id)]) res, val = mt.generate_trading_report_image(runner_ids=[str(i.id)])
if res == 0: if res == 0:
print("DAILY REPORT IMAGE CREATED") print("DAILY REPORT IMAGE CREATED")
else: else:
print(f"Daily report ERROR - {val}") print(f"Daily report ERROR - {val}")
except Exception as e: except Exception as e:
print("Nepodarilo se vytvorit report image", str(e)+format_exc()) err_msg = "Nepodarilo se vytvorit daily report image" + str(e)+format_exc()
send_to_telegram(err_msg)
print(err_msg)
target.release() target.release()
print("Runner STOPPED") print("Runner STOPPED")
@ -567,14 +568,16 @@ def batch_run_manager(id: UUID, runReq: RunRequest, rundays: list[RunDay]):
runReq = None runReq = None
#vytvoreni report image pro batch #vytvoreni report image pro batch
try: try:
res, val = generate_trading_report_image(batch_id=batch_id) res, val = mt.generate_trading_report_image(batch_id=batch_id)
if res == 0: if res == 0:
print("BATCH REPORT CREATED") print("BATCH REPORT CREATED")
else: else:
print(f"BATCH REPORT ERROR - {val}") print(f"BATCH REPORT ERROR - {val}")
except Exception as e: except Exception as e:
print("Nepodarilo se vytvorit report image", str(e)+format_exc()) err_msg = "Nepodarilo se vytvorit batch report image" + str(e)+format_exc()
send_to_telegram(err_msg)
print(err_msg)
#gc.collect() #gc.collect()
@ -915,7 +918,7 @@ def archive_runner(runner: Runner, strat: StrategyInstance, inter_batch_params:
#pole indicatoru, kazdy ma svoji casovou osu time #pole indicatoru, kazdy ma svoji casovou osu time
flattened_indicators_list = [] flattened_indicators_list = []
for key, value in strat.state.indicators.items(): for key, value in strat.state.indicators.items():
if isinstance(value, ndarray): if isinstance(value, np.ndarray):
#print("is numpy", key,value) #print("is numpy", key,value)
flattened_indicators[key]= value.tolist() flattened_indicators[key]= value.tolist()
#print("changed numpy:",value.tolist()) #print("changed numpy:",value.tolist())
@ -925,7 +928,7 @@ def archive_runner(runner: Runner, strat: StrategyInstance, inter_batch_params:
flattened_indicators_list.append(flattened_indicators) flattened_indicators_list.append(flattened_indicators)
flattened_indicators = {} flattened_indicators = {}
for key, value in strat.state.cbar_indicators.items(): for key, value in strat.state.cbar_indicators.items():
if isinstance(value, ndarray): if isinstance(value, np.ndarray):
#print("is numpy", key,value) #print("is numpy", key,value)
flattened_indicators[key]= value.tolist() flattened_indicators[key]= value.tolist()
#print("changed numpy:",value.tolist()) #print("changed numpy:",value.tolist())

View File

@ -90,8 +90,8 @@ class Trade_Offline_Streamer(Thread):
time.sleep(backoff_factor * (2 ** attempt)) time.sleep(backoff_factor * (2 ** attempt))
print("All attempts to fetch data failed.") print("All attempts to fetch data failed.")
send_to_telegram(f"Failed to fetch stock trades after {max_retries} retries. Last exception: {last_exception}") send_to_telegram(f"Failed to fetch stock trades after {max_retries} retries. Last exception: {str(last_exception)} and {format_exc()}")
raise ConnectionError(f"Failed to fetch stock trades after {max_retries} retries. Last exception: {last_exception}") raise ConnectionError(f"Failed to fetch stock trades after {max_retries} retries. Last exception: {str(last_exception)} and {format_exc()}")
# Override the run() function of Thread class # Override the run() function of Thread class
#odebrano async #odebrano async

View File

@ -0,0 +1,20 @@
import argparse
import v2realbot.reporting.metricstoolsimage as mt
##Generates BATCH REPORT again for the given batch_id
##USAGE: python createbatchimage.py <batch_id>
#Parse the command-line arguments
parser = argparse.ArgumentParser(description="Generate trading report image with batch ID")
parser.add_argument("batch_id", type=str, help="The batch ID for the report")
args = parser.parse_args()
batch_id = args.batch_id
# Generate the report image
res, val = mt.generate_trading_report_image(batch_id=batch_id)
# Print the result
if res == 0:
print("BATCH REPORT CREATED")
else:
print(f"BATCH REPORT ERROR - {val}")

View File

@ -13,6 +13,7 @@ from collections import defaultdict
from pandas import to_datetime from pandas import to_datetime
from msgpack.ext import Timestamp from msgpack.ext import Timestamp
import time import time
from traceback import format_exc
def convert_historical_bars(daily_bars): def convert_historical_bars(daily_bars):
"""Converts a list of daily bars into a dictionary with the specified keys. """Converts a list of daily bars into a dictionary with the specified keys.
@ -124,5 +125,5 @@ def get_historical_bars(symbol: str, time_from: datetime, time_to: datetime, tim
time.sleep(backoff_factor * (2 ** attempt)) time.sleep(backoff_factor * (2 ** attempt))
print("All attempts to fetch historical bar data failed.") print("All attempts to fetch historical bar data failed.")
send_to_telegram(f"Failed to fetch historical bar data after {max_retries} retries. Last exception: {last_exception}") send_to_telegram(f"Failed to fetch historical bar data after {max_retries} retries. Last exception: {str(last_exception)} and {format_exc()}")
raise Exception(f"Failed to fetch historical bar data after {max_retries} retries. Last exception: {last_exception}") raise Exception(f"Failed to fetch historical bar data after {max_retries} retries. Last exception: {str(last_exception)} and {format_exc()}")

View File

@ -29,6 +29,7 @@ import numpy as np
from alpaca.trading.requests import GetCalendarRequest from alpaca.trading.requests import GetCalendarRequest
from alpaca.trading.client import TradingClient from alpaca.trading.client import TradingClient
import time as timepkg import time as timepkg
from traceback import format_exc
#Alpaca Calendar wrapper with retry #Alpaca Calendar wrapper with retry
def fetch_calendar_data(start, end, max_retries=5, backoff_factor=1): def fetch_calendar_data(start, end, max_retries=5, backoff_factor=1):
@ -60,8 +61,8 @@ def fetch_calendar_data(start, end, max_retries=5, backoff_factor=1):
timepkg.sleep(backoff_factor * (2 ** attempt)) timepkg.sleep(backoff_factor * (2 ** attempt))
richprint("****All attempts to fetch calendar data failed.****") richprint("****All attempts to fetch calendar data failed.****")
send_to_telegram(f"FETCH_CALENDER_DATA_FAILED. {last_exception} BACKEST STOPPED" ) send_to_telegram(f"FETCH_CALENDER_DATA_FAILED. {str(last_exception)} and {format_exc()} BACKEST STOPPED" )
raise ConnectionError(f"Failed to fetch calendar data after {max_retries} retries. Last exception: {last_exception}") raise ConnectionError(f"Failed to fetch calendar data after {max_retries} retries. Last exception: {str(last_exception)} and {format_exc()}")
def concatenate_weekdays(weekday_filter): def concatenate_weekdays(weekday_filter):
# Mapping of weekdays where 0 is Monday and 6 is Sunday # Mapping of weekdays where 0 is Monday and 6 is Sunday