Skip to content

Is it possible to track custom stats in results? Is it possible to use tick data? #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Dwyte opened this issue Aug 22, 2020 · 4 comments
Labels
question Not a bug, but a FAQ entry

Comments

@Dwyte
Copy link

Dwyte commented Aug 22, 2020

I need a way, per trade to track the maximum profit it can have before it gets stopped out or before the market closes/week ends. Then at the end results I need to tally this property as well like what's the maximum "maximum profit" from all the trades. Is this possible within the library? Can I append custom data on each trade? Thanks, great repo btw!

Edit: Follow up question, is it possible to use tick data instead, then resample it to whatever timeframe I need? thanks!

@Dwyte Dwyte changed the title Is it possible to track custom stats in results? Is it possible to track custom stats in results? And use tick bid/ask data instead of OHLC? Aug 22, 2020
@kernc
Copy link
Owner

kernc commented Aug 23, 2020

Thanks!

Can I append custom data on each trade?

Sure. I think something like the following might work. You can access the ran Strategy instance through stats._strategy:

class MyStrategy(Strategy):
    ...
    def next(self):
        ...
        for trade in self.trades:
            trade.max_pl = max(getattr(trade, 'max_pl', -np.inf), trade.pl)

...

stats = bt.run()

mean_max_pl = np.mean([trade.max_pl for trade in stats._strategy.closed_trades])

@kernc kernc changed the title Is it possible to track custom stats in results? And use tick bid/ask data instead of OHLC? Is it possible to track custom stats in results? And use tick data instead of OHLC? Aug 23, 2020
@kernc kernc changed the title Is it possible to track custom stats in results? And use tick data instead of OHLC? Is it possible to track custom stats in results? Is it possible to use tick data? Aug 23, 2020
@kernc
Copy link
Owner

kernc commented Aug 23, 2020

is it possible to use tick data

No, OHLC is hardcoded, but there's no lower bound: you can have 5-second OHLC bars or even 1-tick OHLC bars (O=H=L=C), limited only by your space/time resources.

@kernc kernc added the question Not a bug, but a FAQ entry label Aug 23, 2020
@kernc kernc closed this as completed Aug 23, 2020
@s-kust
Copy link

s-kust commented Dec 3, 2020

I try to pass some custom data with trades like you say:
trade.custom_data = something
but then I can't see it in stats._trades.tail(20) or stats._strategy.closed_trades[-1]

It shows only standard information.

How to see my custom data (aka custom column) in trades after the backtest has run?

@kernc
Copy link
Owner

kernc commented Dec 3, 2020

It won't, for the time being, automatically show in trades DataFrame nor in Trade.__repr__. You need to access it exactly the way it was set up:

for trade in stats._strategy.closed_trades:
    this = trade.custom_data
    ...

E.g. to add it to the data frame:

stats._trades.assign(custom_data=[t.custom_data for t in stats._strategy.closed_trades])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a bug, but a FAQ entry
Projects
None yet
Development

No branches or pull requests

3 participants