jesse: semi-endless loop around add_candle call
Describe the bug Until this week I’d been running 0.44 w/o any problems, but 0.48 added some welcome changes, so I switched my backtesting to 0.48 and encountered a very long loop around https://github.com/jesse-ai/jesse/blob/master/jesse/store/state_candles.py#L102 call, which sometimes causes a backtest to hang for 10-15 minutes. This happens sporadically, maybe one out of 100-200 backtests run, but it effectively it blocks the whole pipeline until problematic test is finished with this loop.
This happens on all symbols and tfs.
When I say ‘around’ it means impossible to track down exact line that hangs, but what I could gather via py-spy is:
%Own %Total OwnTime TotalTime Function (filename)
48.00% 100.00% 627.62s 652.15s add_candle (jesse/store/state_candles.py)
52.00% 52.00% 624.50s 624.50s __getitem__ (jesse/libs/dynamic_numpy_array/__init__.py)
0.00% 0.00% 0.310s 0.320s _var (numpy/core/_methods.py)
0.00% 0.00% 0.070s 0.180s mean (numpy/core/fromnumeric.py)
0.00% 0.00% 0.070s 0.110s _mean (numpy/core/_methods.py)
0.00% 0.00% 0.070s 0.390s _std (numpy/core/_methods.py)
0.00% 0.00% 0.060s 0.060s timeframe_to_one_minutes (jesse/helpers.py)
0.00% 0.00% 0.050s 0.050s _count_reduce_items (numpy/core/_methods.py)
0.00% 100.00% 0.040s 53.00s _step_simulator (jesse/modes/backtest_mode.py)
0.00% 0.00% 0.030s 0.630s peaks (jesse-bot/custom_indicators/peaks.py)
0.00% 52.00% 0.020s 25.74s _simulate_price_change_effect (jesse/modes/backtest_mode.py)
0.00% 0.00% 0.020s 0.020s stochrsi (tulipy/__init__.py)
0.00% 0.00% 0.020s 0.020s is_collecting_data (jesse/helpers.py)
0.00% 0.00% 0.020s 0.020s wrapper (talib/__init__.py)
0.00% 0.00% 0.010s 0.190s mean (<__array_function__ internals>)
0.00% 0.00% 0.010s 0.010s _sum (numpy/core/_methods.py)
0.00% 0.00% 0.010s 0.010s is_live (jesse/helpers.py)
0.00% 0.00% 0.010s 0.400s std (numpy/core/fromnumeric.py)
0.00% 0.00% 0.010s 0.010s get_candles (jesse/store/state_candles.py)
0.00% 0.00% 0.010s 0.010s get_candle_source (jesse/helpers.py)
0.00% 0.00% 0.010s 0.410s std (<__array_function__ internals>)
0.00% 0.00% 0.010s 0.010s is_debuggable (jesse/helpers.py)
0.00% 0.00% 0.010s 0.010s _get_fixed_jumped_candle (jesse/modes/backtest_mode.py)
0.00% 0.00% 0.010s 0.700s _check (jesse/strategies/Strategy.py)
0.00% 100.00% 0.000s 53.00s backtest_rungs (jesse-bot/backtest_new.py)
0.00% 100.00% 0.000s 53.00s _resume_span (ray/util/tracing/tracing_helper.py)
0.00% 0.00% 0.000s 0.690s decorated (jesse/services/cache.py)
I’ve added some logging:
def get_storage(self, exchange: str, symbol: str, timeframe: str) -> DynamicNumpyArray:
key = jh.key(exchange, symbol, timeframe)
print(key) <----
[...]
def add_candle(
[...]
print('getting arr') <---
arr: DynamicNumpyArray = self.get_storage(exchange, symbol, timeframe)
print('got arr', len(arr)) <---
And from these prints we can see this method is called millions of times by a single backtest process:
Eventually number of calls falls:
Which makes me think there’s loop inside loop somewhere.
I could not instantly pinpoint recent changes responsible for this. Will roll back to 0.44 for now and slowly work my way to 0.48, maybe I can find where regression first appeared.
Any help from those familiar with recent changes is appreciated @yakir4123
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 27 (26 by maintainers)
Okay, I think I found the culprit, it was a mistake in my code (thanks to Python’s archaic scoping rules) when migrating to updated backtest function, but finding this mistake was so hard due to difference between
backtest()function handling ofwarmup_candlesargument whenfast_modeset toTrueorFalse.Consider the following generic backtest:
As you can see, here I deliberately offer
wrong_warmup_candlestobacktest()call, and withfast_mode=Falsethe test will SILENTLY run, although very slowly, but in many cases it will finish the run successfully (!).With
fast_mode=Truehowever, backtest fails with:This error message is expected with
wrong_warmup_candlesbut is somewhat misleading. A better error would beCould not find the candle [...] First candle's timestamp [...]. Make sure your warmup_candles are imported properly!I believe, this difference in handling of
warmup_candlescan lead us to some interesting discoveries of howfast_modeworks. It is really fast, I can confirm, but I will run further tests to see whether results match “slow mode” with complex exit strategies.@movy I updated the branch 1 hour ago, if you fetch the last update before that please give another try. I do something very similar to you and now it workings for me really fast
0.45 is the final version working w/o hanging, 0.46 introduced this bug
@movy Cool I didnt know that v0.46 didnt has this problem
please try this PR I did some fixes on these cases exactly!
https://github.com/jesse-ai/jesse/pull/450