Skip to content
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

Make StrategyState available after Backtesting #219

Closed
1 of 2 tasks
josvanos opened this issue Jan 2, 2023 · 1 comment
Closed
1 of 2 tasks

Make StrategyState available after Backtesting #219

josvanos opened this issue Jan 2, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@josvanos
Copy link

josvanos commented Jan 2, 2023

Description of Feature Request

This feature is closely related to the open feature request of in-house plotting (#207). Currently I use the teardown function to create custom plots with plotly. However, this makes it difficult to show detailed information as trades and metrics as the backtest result is not yet available. The default figures of blankly are limited in showing what's going on with the data, such as visualizing indicators.

The real problem is that I have never the state and the backtest result together at one location, making it difficult to plot everything I want in a complete overview.

How would you implement this?

# Example of how my teardown function looks like:
# using the teardown function for ploting feels like misusing the purpose of this function
def teardown(symbol, state: blankly.StrategyState):
    # in the teardown I do something like this to show the candles and indicators. 
    plotly.show(state)

strategy.add_bar_event(bar_event, symbol='MSFT', resolution='4h', teardown=teardown)
res = strategy.backtest(to='1y', initial_values={'USD': 10000})
 
# Option 1: 

res.state[symbol][resolution]  # -> blankly.StrategyState
res.get_state(*args) # -> blankly.StrategyState


# Option 2: 
def teardown(symbol, state: blankly.StrategyState, res: BacktestResult ):
    # in the teardown I do something like this to show the candles and indicators. 
    plotly.show(state, res)

Would you like to Contribute?

  • Yes, I would love to contribute to this feature request 🎉 (I do not understand the internals of blankly that well)
  • No, but please keep me posted 🎉
@EmersonDove EmersonDove added the enhancement New feature or request label Jan 2, 2023
@EmersonDove
Copy link
Member

EmersonDove commented Jan 2, 2023

A super super simple workaround to this is to not use the teardown function and instead do something like this:

import blankly


global_state = None

def price_event(price, symbol, state: blankly.FuturesStrategyState):
    pass


def teardown(symbol, state: blankly.StrategyState):
    # In the teardown just copy the state to the global state and store that for later
    import copy
    global global_state
    global_state = copy.copy(state)


if __name__ == "__main__":
    # Authenticate Binance Futures exchange
    exchange = blankly.Binance()

    # Use our strategy helper on binance futures
    strategy = blankly.Strategy(exchange)

    strategy.add_price_event(price_event, symbol='BTC-USDT', resolution='1d', teardown=teardown)

    results = strategy.backtest(to='1M', initial_values={'USDT': 1000000})

    print("Now I have both the state and the results!")
    print(global_state)
    print(results)

^^^ Just keep the reference around for the state variable in global_state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants