-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
167 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pathlib | ||
from datetime import datetime, timedelta | ||
|
||
from investing_algorithm_framework import create_app, PortfolioConfiguration, \ | ||
TimeUnit, TradingTimeFrame, TradingDataType, TradingStrategy, \ | ||
RESOURCE_DIRECTORY | ||
|
||
|
||
class MyTradingStrategy(TradingStrategy): | ||
time_unit = TimeUnit.SECOND | ||
interval = 3 | ||
trading_data_type = TradingDataType.OHLCV | ||
trading_time_frame_start_date = datetime.utcnow() - timedelta(days=1) | ||
trading_time_frame = TradingTimeFrame.ONE_MINUTE | ||
market = "BITVAVO" | ||
symbols = ["BTC/EUR"] | ||
|
||
def apply_strategy( | ||
self, | ||
algorithm, | ||
market_data, | ||
): | ||
print(len(algorithm.get_orders())) | ||
print(market_data) | ||
|
||
|
||
# No resource directory specified, so an in-memory database will be used | ||
app = create_app({RESOURCE_DIRECTORY: pathlib.Path(__file__).parent.resolve()}) | ||
app.add_portfolio_configuration( | ||
PortfolioConfiguration( | ||
market="bitvavo", | ||
api_key="<your_api_key>", | ||
secret_key="<your_secret_key>", | ||
trading_symbol="EUR" | ||
) | ||
) | ||
app.add_strategy(MyTradingStrategy) | ||
|
||
if __name__ == "__main__": | ||
report = app.backtest( | ||
start_datetime=datetime.utcnow() - timedelta(days=1), | ||
end_datetime=datetime.utcnow(), | ||
source="ccxt" | ||
) | ||
# pretty_print_report(report) | ||
# print(report.get_orders()) | ||
# print(report.get_trades()) | ||
# print(report.get_positions()) | ||
# | ||
# report = app.backtest( | ||
# start_datetime=datetime.utcnow() - timedelta(days=1), | ||
# end_datetime=datetime.utcnow(), | ||
# source="ccxt", | ||
# unallocated=10000, | ||
# commission=0.001, | ||
# commission_currency="EUR" | ||
# ) | ||
# pretty_print_report(report) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
investing_algorithm_framework/services/backtest_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import os | ||
from investing_algorithm_framework.domain import RESOURCE_DIRECTORY, \ | ||
OperationalException | ||
|
||
|
||
class BackTestService: | ||
|
||
def __init__(self, market_data_service): | ||
self._market_data_service = market_data_service | ||
|
||
def backtest(self, start_date, end_date, strategies, config): | ||
self._create_test_data_csv( | ||
start_date, | ||
end_date, | ||
[strategy.trade_profile for strategy in strategies], | ||
config | ||
) | ||
self._create_test_data( | ||
start_date, | ||
end_date, | ||
[strategy.trade_profile for strategy in strategies] | ||
) | ||
|
||
def _create_test_data(self, start_date, end_date, trade_profiles): | ||
|
||
for trade_profile in trade_profiles: | ||
data = self._market_data_service.get_market_data( | ||
trade_profile.market, | ||
trade_profile.trading_symbol, | ||
start_date, | ||
end_date | ||
) | ||
|
||
def _create_test_data_csv(self, start_date, end_date, trade_profiles, config): | ||
print(config) | ||
|
||
if RESOURCE_DIRECTORY not in config \ | ||
or config.get(RESOURCE_DIRECTORY) is None: | ||
raise OperationalException( | ||
"The resource directory is not configured. Please configure " | ||
"the resource directory before backtesting." | ||
) | ||
trading_test_data_path = os.path.join( | ||
config[RESOURCE_DIRECTORY], | ||
"ohclv_trading_test_data.csv" | ||
) | ||
|
||
if not os.path.exists(trading_test_data_path): | ||
with open(trading_test_data_path, 'w') as file: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters