From 20aaa2ac23518c4bf9c72ac1bb4049f1954b4fb3 Mon Sep 17 00:00:00 2001 From: David Brazda Date: Tue, 27 Feb 2024 12:03:57 +0700 Subject: [PATCH] #135 -> BT same period button --- v2realbot/controller/services.py | 18 ++++++---- v2realbot/static/index.html | 9 ++--- .../js/tables/archivetable/functions.js | 35 +++++++++++++------ .../static/js/tables/archivetable/handlers.js | 5 +++ .../static/js/tables/archivetable/init.js | 2 +- v2realbot/static/js/utils/utils.js | 4 +-- 6 files changed, 50 insertions(+), 23 deletions(-) diff --git a/v2realbot/controller/services.py b/v2realbot/controller/services.py index b43d0fe..5022917 100644 --- a/v2realbot/controller/services.py +++ b/v2realbot/controller/services.py @@ -1859,13 +1859,13 @@ def get_alpaca_history_bars(symbol: str, datetime_object_from: datetime, datetim """Returns Bar object """ try: + result = [] client = StockHistoricalDataClient(ACCOUNT1_LIVE_API_KEY, ACCOUNT1_LIVE_SECRET_KEY, raw_data=False) #datetime_object_from = datetime(2023, 2, 27, 18, 51, 38, tzinfo=datetime.timezone.utc) #datetime_object_to = datetime(2023, 2, 27, 21, 51, 39, tzinfo=datetime.timezone.utc) bar_request = StockBarsRequest(symbol_or_symbols=symbol,timeframe=timeframe, start=datetime_object_from, end=datetime_object_to, feed=ACCOUNT1_LIVE_FEED) #print("before df") bars = client.get_stock_bars(bar_request) - result = [] ##pridavame pro jistotu minutu z obou stran kvuli frontendu business_hours = { # monday = 0, tuesday = 1, ... same pattern as date.weekday() @@ -1896,11 +1896,17 @@ def get_alpaca_history_bars(symbol: str, datetime_object_from: datetime, datetim #bars.data[symbol] return 0, result except Exception as e: - print(str(e) + format_exc()) - if OFFLINE_MODE: - print("OFFLINE MODE ENABLED") - return 0, [] - return -2, str(e) + # Workaround of error when no data foun d AttributeError and has the specific message + if isinstance(e, AttributeError) and str(e) == "'NoneType' object has no attribute 'items'": + print("Caught the specific AttributeError: 'NoneType' object has no attribute 'items' means NO DATA FOUND") + #print(str(e) + format_exc()) + return 0, result + else: + print(str(e) + format_exc()) + if OFFLINE_MODE: + print("OFFLINE MODE ENABLED") + return 0, [] + return -2, str(e) # change_archived_runner # delete_archived_runner_details diff --git a/v2realbot/static/index.html b/v2realbot/static/index.html index 0c147ae..0cf8e13 100644 --- a/v2realbot/static/index.html +++ b/v2realbot/static/index.html @@ -561,6 +561,7 @@ + @@ -1150,7 +1151,7 @@ - + @@ -1160,10 +1161,10 @@ - - + + - + diff --git a/v2realbot/static/js/tables/archivetable/functions.js b/v2realbot/static/js/tables/archivetable/functions.js index b3d2921..dea9baa 100644 --- a/v2realbot/static/js/tables/archivetable/functions.js +++ b/v2realbot/static/js/tables/archivetable/functions.js @@ -78,10 +78,11 @@ function get_detail_and_chart(row) { }) } -//rerun stratin -function run_day_again() { +//rerun stratin (use to rerun strategy and also to rerun live/paper as bt on same period) +function run_day_again(turnintobt=false) { row = archiveRecords.row('.selected').data(); - $('#button_runagain_arch').attr('disabled',true); + var button_name = turnintobt ? '#button_runbt_arch' : '#button_runagain_arch' + $(button_name).attr('disabled',true) var record1 = new Object() //console.log(JSON.stringify(rows)) @@ -142,7 +143,7 @@ function run_day_again() { //console.log("Result from second request:", result2); //console.log("calling compare") - rerun_strategy(result1, result2) + rerun_strategy(result1, result2, turnintobt) // Perform your action with the results from both requests // Example: @@ -154,13 +155,22 @@ function run_day_again() { }); - function rerun_strategy(archRunner, stratData) { + function rerun_strategy(archRunner, stratData, turnintobt) { record1 = archRunner //console.log(record1) + var note_prefix = "RERUN " + if ((turnintobt) && ((record1.mode == 'live') || (record1.mode == 'paper'))) { + record1.mode = 'backtest' + record1.bt_from = record1.started + record1.bt_to = record1.stopped + note_prefix = "BT SAME PERIOD " + } + + record1.note = note_prefix + record1.note + //nebudeme muset odstanovat pri kazdem pridani noveho atributu v budoucnu //smazeneme nepotrebne a pridame potrebne //do budoucna predelat na vytvoreni noveho objektu - //nebudeme muset odstanovat pri kazdem pridani noveho atributu v budoucnu delete record1["end_positions"]; delete record1["end_positions_avgp"]; delete record1["profit"]; @@ -172,8 +182,6 @@ function run_day_again() { delete record1["settings"]; delete record1["stratvars"]; - record1.note = "RERUN " + record1.note - if (record1.bt_from == "") {delete record1["bt_from"];} if (record1.bt_to == "") {delete record1["bt_to"];} @@ -212,7 +220,7 @@ function run_day_again() { contentType: "application/json", data: jsonString, success:function(data){ - $('#button_runagain_arch').attr('disabled',false); + $(button_name).attr('disabled',false); setTimeout(function () { runnerRecords.ajax.reload(); stratinRecords.ajax.reload(); @@ -222,7 +230,7 @@ function run_day_again() { var err = eval("(" + xhr.responseText + ")"); window.alert(JSON.stringify(xhr)); //console.log(JSON.stringify(xhr)); - $('#button_runagain_arch').attr('disabled',false); + $(button_name).attr('disabled',false); } }) } @@ -541,6 +549,7 @@ function generateStorageKey(batchId) { function disable_arch_buttons() { //disable buttons (enable on row selection) $('#button_runagain_arch').attr('disabled','disabled'); + $('#button_runbt_arch').attr('disabled','disabled'); $('#button_show_arch').attr('disabled','disabled'); $('#button_delete_arch').attr('disabled','disabled'); $('#button_delete_batch').attr('disabled','disabled'); @@ -563,4 +572,10 @@ function enable_arch_buttons() { $('#button_report').attr('disabled',false); $('#button_export_xml').attr('disabled',false); $('#button_export_csv').attr('disabled',false); + + //Backtest same period button is displayed only when row with mode paper/live is selected + row = archiveRecords.row('.selected').data(); + if ((row.mode == 'paper') || (row.mode == 'live')) { + $('#button_runbt_arch').attr('disabled',false); + } } \ No newline at end of file diff --git a/v2realbot/static/js/tables/archivetable/handlers.js b/v2realbot/static/js/tables/archivetable/handlers.js index 00f4e60..36c39d0 100644 --- a/v2realbot/static/js/tables/archivetable/handlers.js +++ b/v2realbot/static/js/tables/archivetable/handlers.js @@ -462,6 +462,11 @@ $(document).ready(function () { //run again button $('#button_runagain_arch').click(run_day_again) + //run in bt mode + $('#button_runbt_arch').click(function() { + run_day_again(true); + }); + //workaround pro spatne oznacovani selectu i pro group-headery // $('#archiveTable tbody').on('click', 'tr.group-header', function(event) { // var $row = $(this); diff --git a/v2realbot/static/js/tables/archivetable/init.js b/v2realbot/static/js/tables/archivetable/init.js index b5c5e17..0bb2ef0 100644 --- a/v2realbot/static/js/tables/archivetable/init.js +++ b/v2realbot/static/js/tables/archivetable/init.js @@ -82,7 +82,7 @@ function initialize_archiveRecords() { }) if (isToday(date)) { - console.log("volame isToday s", date) + //console.log("volame isToday s", date) //return local time only return '
'+ 'dnes ' + format_date(data,true,true)+'
' } diff --git a/v2realbot/static/js/utils/utils.js b/v2realbot/static/js/utils/utils.js index 5a85c50..689e0ce 100644 --- a/v2realbot/static/js/utils/utils.js +++ b/v2realbot/static/js/utils/utils.js @@ -994,8 +994,8 @@ JSON.safeStringify = (obj, indent = 2) => { function isToday(someDate) { // Convert input date to Eastern Time var dateInEastern = new Date(someDate.toLocaleString('en-US', { timeZone: 'America/New_York' })); - console.log("vstupuje ",someDate) - console.log("americky ",dateInEastern) + //console.log("vstupuje ",someDate) + //console.log("americky ",dateInEastern) // Get today's date in Eastern Time var todayInEastern = new Date(new Date().toLocaleString('en-US', { timeZone: 'America/New_York' }));