Skip to content

Commit

Permalink
Refactor backtest reports
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Apr 11, 2024
1 parent c227a2b commit f98c403
Show file tree
Hide file tree
Showing 12 changed files with 632 additions and 195 deletions.
7 changes: 5 additions & 2 deletions examples/backtest/algorithm/strategy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tulipy as ti

from investing_algorithm_framework import TimeUnit, TradingStrategy, \
Algorithm, OrderSide
Algorithm, OrderSide, BACKTESTING_INDEX_DATETIME

"""
This strategy is based on the golden cross strategy. It will buy when the
Expand Down Expand Up @@ -76,6 +76,8 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
if not algorithm.has_position(target_symbol) \
and is_crossover(fast, slow) \
and is_above_trend(fast, trend):
print(f"opening trade on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price}")

algorithm.create_limit_order(
target_symbol=target_symbol,
order_side=OrderSide.BUY,
Expand All @@ -86,7 +88,7 @@ def apply_strategy(self, algorithm: Algorithm, market_data):

if algorithm.has_position(target_symbol) \
and is_below_trend(fast, slow):

print(f"closing trade on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price}")
open_trades = algorithm.get_open_trades(
target_symbol=target_symbol
)
Expand All @@ -103,4 +105,5 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
current_price=market_data[f"{symbol}-ticker"]["bid"],
stop_loss_percentage=self.stop_loss_percentage
):
print(f"stop los triggered on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price} ")
algorithm.close_trade(open_trade)
146 changes: 146 additions & 0 deletions examples/backtest_experiment/data_sources.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,149 @@
[
{
"name": "9-50-100",
"description": "9-50-100",
"fast": 9,
"slow": 50,
"trend": 100,
"stop_loss_percentage": 7
},
{
"name": "10-50-100",
"description": "10-50-100",
"fast": 10,
"slow": 50,
"trend": 100,
"stop_loss_percentage": 7
},
{
"name": "11-50-100",
"description": "11-50-100",
"fast": 11,
"slow": 50,
"trend": 100,
"stop_loss_percentage": 7
},
{
"name": "9-75-150",
"description": "9-75-150",
"fast": 9,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "10-75-150",
"description": "10-75-150",
"fast": 10,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "11-75-150",
"description": "11-75-150",
"fast": 11,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "20-75-150",
"description": "20-75-150",
"fast": 20,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "21-75-150",
"description": "21-75-150",
"fast": 21,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "22-75-150",
"description": "22-75-150",
"fast": 22,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "23-75-150",
"description": "23-75-150",
"fast": 23,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "24-75-150",
"description": "24-75-150",
"fast": 24,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "25-75-150",
"description": "25-75-150",
"fast": 25,
"slow": 75,
"trend": 150,
"stop_loss_percentage": 7
},
{
"name": "20-75-200",
"description": "20-75-200",
"fast": 20,
"slow": 75,
"trend": 200,
"stop_loss_percentage": 7
},
{
"name": "21-75-200",
"description": "24-75-200",
"fast": 24,
"slow": 75,
"trend": 200,
"stop_loss_percentage": 7
},
{
"name": "22-75-200",
"description": "24-75-200",
"fast": 24,
"slow": 75,
"trend": 200,
"stop_loss_percentage": 7
},
{
"name": "23-75-200",
"description": "24-75-200",
"fast": 24,
"slow": 75,
"trend": 200,
"stop_loss_percentage": 7
},
{
"name": "24-75-200",
"description": "24-75-200",
"fast": 24,
"slow": 75,
"trend": 200,
"stop_loss_percentage": 7
},
{
"name": "25-75-150",
"description": "25-75-200",
"fast": 25,
"slow": 75,
"trend": 200,
"stop_loss_percentage": 7
}
]
from investing_algorithm_framework import CCXTOHLCVMarketDataSource, \
CCXTTickerMarketDataSource

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@
)
reports = app.run_backtests(
algorithms=algorithms,
start_date=start_date,
end_date=end_date,
date_ranges=[
(datetime(2023, 7, 2),
datetime(2023, 7, 2) + timedelta(days=200)),
(datetime(2022, 7, 2),
datetime(2022, 7, 2) + timedelta(days=200)),
(datetime(2024, 1, 1),
datetime(2024, 1, 1) + timedelta(days=100)),
],
pending_order_check_interval="2h",
)
evaluation = BacktestReportsEvaluation(reports)
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions examples/existing_backtest_reports_evaluation/evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from investing_algorithm_framework import BacktestReportsEvaluation, \
pretty_print_backtest_reports_evaluation, load_backtest_reports

if __name__ == "__main__":
reports = load_backtest_reports("backtest_reports")
evaluation = BacktestReportsEvaluation(reports)
pretty_print_backtest_reports_evaluation(evaluation)
Loading

0 comments on commit f98c403

Please sign in to comment.