Everyone's opinion needed :-) Are these statistics better looking/more helpful?
I created a simple Analyzer that calculates 'bread and butter' popular statistics, all in one place.
TradeAnalyzer holds alot of information, just when I print output, it scrolls through several pages and therefore difficult to read.
I also wanted to add some other basic stats like Win:Loss ratio and Reward:Risk (also called 'Risk/reward')
I have completed the 'BasicTradeStats' Analyzer code.
I'm now experimenting with subclassing the print() method to make the output easier to read.
Instead of this..
It's going to take me half a day to complete the improved print output and my time is short.
Will this improved output be more helpful to you guys? don't be shy, let me know :)
(more for Backtrader) Are you comfortable I'm overriding the print method? The output is different than the standard output for Analyzers.
Are there any other essential basic statistics that I have missed that you feel belong here? [Drawdown and Sharpe are useful, but already an Analyzer for them, and the output is fine.. so unnecessary to add here.]
Please let me know asap, I may be able to complete this pull request with my free time tomorrow.
ab_trader last edited by
@Richard-O'Regan thank you for your work. Would be great to introduce MAE and MFE per trade.
Hey Ab_trader, thanks for that.
Funny, a guy wrote a post a month ago asking for MFE&MAE and I wrote down a rough algorithm for calculating.
Two approaches are
Simple & rough: We can calculate it roughly, with trade data. e.g. for each trade open, for each period, look at the pnl value. Store highest and lowest pnl before trade closed.
But we are limited to bar period. If daily, the close may be very different than high/low values, so MFE&MAE are at best an approximation (of course approximation gest better as granuality of bar decreases, i.e. from daily -> hourly -> minute data etc)
The better way to do it: would be to read the underlying data, and find high and low for each period, and assuming no stop or target was hit, we can use the high and low for MFE&MAE etc. This is much more accurate than just the close.
I think it would be a great feature to add, tho my main reason to not add here, is that to use properly, it would need a chart not just a table with couple of averages. e.g.
I personally feel this is more advanced and should be a separate Analyzer dedicated to MFE&MAE with chart features.
These stats above are more for simple basic obvious stats.
This is just my opinion mate , if you disagree let me know, everything is doable.
Thanks again for replying - if anyone else thinks a major basic popular statistic that fits in here, e.g. like Risk/Reward let me know. Thanks
@Richard-O'Regan said in Everyone's opinion needed :-) Are these statistics better looking/more helpful?:
- (more for Backtrader) Are you comfortable I'm overriding the print method? The output is different than the standard output for Analyzers.
In Python you can do monkey-patching and override the default behavior of the
Noted, just I thought it would be helpful for everyone, hence why I'm considering making public in the code of my Analyzer pull-request (rather than just a monkey-patch on my local computer). Just it is breaking the default convention for BT and wanted to check would be ok by you:)
The monkey-patching statement is not only addressed at your local code. Your open source code can monkey-patch the
Example usage (there are for sure many patterns)
import backtrader as bt import bt_analyzer_mega_print # just work with your code as usual ...
import bt_analyzer_mega_printyour code patches the
You may try something more advance like
import bt_analyzer_mega_print as bt
Here your module is imported, which in turn imports
backtraderinternally. Before returning your replace the entry in
sys.modulesfor your module with the imported
Note: unsure if
as btwill be a direct reference to the module in
sys.modulesor a variable referencing your original module. But it's worth a try.
Richard O'Regan last edited by
Hey backtrader, I read and clicked links, didn't really understand, buts its late here and I'm tired, at computer all day. I will take a look with fresher eyes tomorrow - always something new I can learn :) cheers mate
The key is here, from Van Rossum
# module foo.py import sys class Foo: def funct1(self, <args>): <code> def funct2(self, <args>): <code> sys.modules[__name__] = Foo()
This works because the import machinery is actively enabling this
hack, and as its final step pulls the actual module out of
sys.modules, after loading it. (This is no accident. The hack was
proposed long ago and we decided we liked enough to support it in the
You can then (according to the description) try:
# module foo.py import backtrader do_the_monkey_patching() sys.modules[__name__] = backtrader
And if everything works as expected, importing your module has effectively imported
backtraderfor the external user after monkey patching.
Richard O'Regan last edited by
Thank-you for extra explanation, just finishing off some code for BT then I will investigate your links thoroughly. Kind Regards
Guest last edited by
@richard-o-regan Is this code available somewhere?
Q.E.D. last edited by
@richard-o-regan Good afternoon. I just joined BackTrader. Long-time futures trader; technical. Years ago with aid of programmers created own linux-based backtesting and then real time trading. Now looking for back testing platform. Found QuantConnect on IB site, but others recommended Backtrader. I will be working with programmer, but based on number of years experience backtesting tens of thousands of iterations, what we did was to rather than merely take output, and rearrange, place all output into a large spd sheet. That included say all 500 iterations for a test, along with all the indiv vars, and columns such as you suggest, % pft tdes, etc. However, with spd sheet, could easily sort on columns, as well as sub-sorts. Regards,