You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you add both a bar_event and price_event, the bar_event's resolution impacts the price_event's price. For example, if I have a bar_event that is triggered at a resolution of 15m, and a price_event with a resolution of 1m, then the price_event will be triggered every 1m, but the price will only change at 15m thresholds.
from blankly import *
from datetime import datetime
import pytz
est = pytz.timezone('US/Eastern')
def price_event(price, symbol, state: StrategyState):
time = datetime.fromisoformat(utils.iso8601_from_epoch(state.time)).astimezone(est)
print(f'[PRICE_EVENT][{time}] price:{price}')
def bar_event(bar, symbol, state):
time = time = datetime.fromisoformat(utils.iso8601_from_epoch(bar['time'])).astimezone(est)
price = bar['close']
print(f'[BAR_EVENT] ({time}) price:{price}')
exchange = Oanda()
s = Strategy(exchange)
currency_pair = "AUD-JPY"
currency_base = currency_pair.split('-')[0]
currency_quoted = currency_pair.split('-')[1]
s.add_price_event(price_event, currency_pair, resolution="1m")
s.add_bar_event(bar_event, currency_pair, resolution="15m")
results = s.backtest(quote_account_value_in=currency_quoted, initial_values={currency_base: 1000000}, start_date="10/29/2023", end_date="10/31/2023", GUI_output=False)
It looks like if I swap the order add_price_event() and add_bar_event(), things work better.
Digging into the code a bit, it initially looks like the final_prices are getting squashed here:
(BacktestController.py, line 418)
if len(relevant_data) > 0:
final_prices[symbol] = pd.concat(relevant_data)
My initial testing shows that the following code preserves all prices @ all resolutions:
if len(relevant_data) > 0:
if symbol not in final_prices:
final_prices[symbol] = pd.concat(relevant_data)
else:
new_data = pd.concat(relevant_data)
final_prices[symbol] = pd.concat([final_prices[symbol], new_data])
I'm not super familiar with enough of the code to understand what impact this could cause, but if there are tests I can run against it just let me know. Going to keep testing to see if it breaks anything else that I can see.
Description
If you add both a bar_event and price_event, the bar_event's resolution impacts the price_event's price. For example, if I have a bar_event that is triggered at a resolution of 15m, and a price_event with a resolution of 1m, then the price_event will be triggered every 1m, but the price will only change at 15m thresholds.
Output:
If I comment the add_bar_event() out, or simply change the resolution to 1m (matches price_event) then it works fine:
So is this an issue, or is my implementation/assumption incorrect?
The text was updated successfully, but these errors were encountered: