Creating Analyzers - calculations on the fly via notify_trade() VS at the end via stop()
-
Hey backtrader,
Ok so since I've been working with Analyzers, a question has been on my mind.
Should I use notify_trade() to do calculations for system statistics - called multiple times during a strategy.
Or put all calculations in stop() - which is ran just once at the end of the strategy.Usually I have done the equivalent of the latter when coding my own systems in the past. i.e. I run a system, generate trades, then after all trades completed, then I run statistics.
In most cases, it doesn't matter as results are the same.
Sometimes using notify_trade() is more convenient than stop() coz data is at your fingertips and can quickly be accumulated into statistics trade by trade.
In many cases stop() is more efficient than notify_trade() because calculations are not repeated unnecessarily.
For example in TradeAnalyser code, your calculations are all in notify_trade().. although summing the pnl per trade, or at the end, is the same number of calculations regardless what method it is in; for the averages you calculate, they are re-calculated over and over every new trade. Since we only care about the average at the end of all trades, there are now n-1 redundant calculations. (n = number of closed trades)
e.g. in notify_trade();
trpnl.gross.average = trades.pnl.gross.total / trades.total.closedIt would be more efficient to move these average calculations from notify_trade() to stop(), so they are calculated just once at the end.
But I figure there is probably a reason why you put code here. I'm guessing because a) it's only a few averages and won't make much difference to speed and more convenient to put all the code together
or b) updating each trade, allows DYNAMIC values to be created on the fly, so in the middle of system running, stats can be seen.-
Is this correct? If so, is there a reason why you'd want to see statistics in the middle of a system running? (Perhaps if system is trading live, stop() would never get called?) (Or perhaps wish to plot how an Analyzer changes over each trade?)
-
Right now, I'm following your lead and putting my code in notify_trade(). Can you clarify if that is the best thing to do and whether it makes any difference? Since I usually would view my stats AFTER backtest complete, would there be any potential compatibility issues with other parts of BT by putting all my code in stop() instead?
I'm enjoying following your code and making changes. Thank-you.
-
-
@Richard-O'Regan said in Creating Analyzers - calculations on the fly via notify_trade() VS at the end via stop():
- Is this correct? If so, is there a reason why you'd want to see statistics in the middle of a system running? (Perhaps if system is trading live, stop() would never get called?) (Or perhaps wish to plot how an Analyzer changes over each trade?)
Your analyzer may calculate something which may have an impact on the next trade (increase/reduce the stake for example). This applies to any situation
@Richard-O'Regan said in Creating Analyzers - calculations on the fly via notify_trade() VS at the end via stop():
- Right now, I'm following your lead and putting my code in notify_trade(). Can you clarify if that is the best thing to do and whether it makes any difference? Since I usually would view my stats AFTER backtest complete, would there be any potential compatibility issues with other parts of BT by putting all my code in stop() instead?
There is no right or wrong per se. Some calculations seem to more naturally fit a recurring pattern. But this is probably just a matter of taste.
-
I read this several times and considered and meant to reply but forgot.
Thank-you, your reply was helpful!!