@@ -79,8 +79,9 @@ The following example connects to Binance and buys BTC every 2 hours.
7979import logging.config
8080from dotenv import load_dotenv
8181
82- from investing_algorithm_framework import create_app, TimeUnit, Context, \
83- CCXTOHLCVMarketDataSource, CCXTTickerMarketDataSource, DEFAULT_LOGGING_CONFIG
82+ from investing_algorithm_framework import create_app, TimeUnit, Context, BacktestDateRange, \
83+ CCXTOHLCVMarketDataSource, CCXTTickerMarketDataSource, DEFAULT_LOGGING_CONFIG , \
84+ TradingStrategy, SnapshotInterval
8485
8586load_dotenv()
8687logging.config.dictConfig(DEFAULT_LOGGING_CONFIG )
@@ -104,24 +105,27 @@ app = create_app()
104105# Registered bitvavo market, credentials are read from .env file by default
105106app.add_market(market = " BITVAVO" , trading_symbol = " EUR" , initial_balance = 100 )
106107
107- # Define a strategy for the algorithm that will run every 10 seconds
108- @app.strategy (
109- time_unit = TimeUnit.SECOND ,
110- interval = 10 ,
111- market_data_sources = [bitvavo_btc_eur_ticker, bitvavo_btc_eur_ohlcv_2h]
108+ class MyStrategy (TradingStrategy ):
109+ interval = 2
110+ time_unit = TimeUnit.HOUR
111+ data_sources = [bitvavo_btc_eur_ohlcv_2h, bitvavo_btc_eur_ticker]
112+
113+ def run_strategy (self , context : Context, market_data ):
114+ # Access the data sources with the indentifier
115+ polars_df = market_data[" BTC-ohlcv" ]
116+ ticker_data = market_data[" BTC-ticker" ]
117+ unallocated_balance = context.get_unallocated()
118+ positions = context.get_positions()
119+ trades = context.get_trades()
120+ open_trades = context.get_open_trades()
121+ closed_trades = context.get_closed_trades()
122+
123+ date_range = BacktestDateRange(
124+ start_date = " 2023-08-24 00:00:00" ,
125+ end_date = " 2023-12-02 00:00:00"
112126)
113- def perform_strategy (context : Context, market_data : dict ):
114- # Access the data sources with the indentifier
115- polars_df = market_data[" BTC-ohlcv" ]
116- ticker_data = market_data[" BTC-ticker" ]
117- unallocated_balance = context.get_unallocated()
118- positions = context.get_positions()
119- trades = context.get_trades()
120- open_trades = context.get_open_trades()
121- closed_trades = context.get_closed_trades()
122-
123- if __name__ == " __main__" :
124- app.run()
127+ backtest_report = app.run_backtest(backtest_date_range = date_range, initial_amount = 100 , snapshot_interval = SnapshotInterval.STRATEGY_ITERATION )
128+ backtest_report.show()
125129```
126130
127131> You can find more examples [ here] ( ./examples ) folder.
0 commit comments