Skip to content

Commit

Permalink
Add support for dataframe stop loss check
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Apr 2, 2024
1 parent 1b393d8 commit 58d11b2
Show file tree
Hide file tree
Showing 26 changed files with 8,045 additions and 326 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,42 +147,42 @@ you will get the following backtesting report:
====================Portfolio overview============================
* Number of orders: 10
* Initial balance: 400.0000 EUR
* Final balance: 426.7818 EUR
* Total net gain: 23.4238 EUR
* Total net gain percentage: 5.8560%
* Growth rate: 6.6955%
* Growth 26.7818 EUR
* Final balance: 431.8837 EUR
* Total net gain: 28.4171 EUR
* Total net gain percentage: 7.1043%
* Growth rate: 7.9709%
* Growth 31.8837 EUR
====================Positions overview========================
╭────────────┬──────────┬──────────────────────┬───────────────────────┬──────────────┬───────────────┬───────────────────────────┬────────────────┬───────────────╮
│ Position │ Amount │ Pending buy amount │ Pending sell amount │ Cost (EUR) │ Value (EUR) │ Percentage of portfolio │ Growth (EUR) │ Growth_rate │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ EUR │ 213.928 │ 0 │ 0 │ 213.928213.92850.1259% │ 0 │ 0.0000% │
│ EUR │ 214.219 │ 0 │ 0 │ 214.219214.21949.6010% │ 0 │ 0.0000% │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ BTC │ 0.003 │ 0 │ 0 │ 103.64 106.84 │ 25.0338% │ 3.1999 │ 3.0875% │
│ BTC │ 0.0031 │ 0 │ 0 │ 107.095 110.401 │ 25.5627% │ 3.3066 │ 3.0875% │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ DOT │ 21.0805 │ 0 │ 0 │ 105.856106.014 │ 24.8403% │ 0.1581 │ 0.1494% │
│ DOT │ 21.3291 │ 0 │ 0 │ 107.104107.264 │ 24.8363% │ 0.16 │ 0.1494% │
╰────────────┴──────────┴──────────────────────┴───────────────────────┴──────────────┴───────────────┴───────────────────────────┴────────────────┴───────────────╯
====================Trades overview===========================
* Number of trades closed: 4
* Number of trades open: 2
* Percentage of positive trades: 60.0%
* Percentage of negative trades: 20.0%
* Average trade size: 98.8728 EUR
* Average trade duration: 191.0 hours
* Average trade duration: 183.5 hours
╭─────────┬─────────────────────┬─────────────────────┬────────────────────┬──────────────┬──────────────────┬───────────────────────┬────────────────────┬─────────────────────╮
│ Pair │ Open date │ Close date │ Duration (hours) │ Size (EUR) │ Net gain (EUR) │ Net gain percentage │ Open price (EUR) │ Close price (EUR) │
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ DOT-EUR │ 2023-11-30 20:00:00 │ │ 2802.99105.856 │ 0 │ 0.0000% │ 5.0215 │ │
│ DOT-EUR │ 2023-11-30 20:00:00 │ │ 2976.65107.104 │ 0 │ 0.0000% │ 5.0215 │ │
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ BTC-EUR │ 2023-11-29 14:00:00 │ │ 2832.99103.64 │ 0 │ 0.0000% │ 34546.6 │ │
│ BTC-EUR │ 2023-11-29 14:00:00 │ │ 3006.65107.095 │ 0 │ 0.0000% │ 34546.6 │ │
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ BTC-EUR │ 2023-11-08 00:00:00 │ 2023-11-14 16:00:00 │ 160 │ 99.2265 │ 1.3352 │ 1.3456% │ 33075.5 │ 33520.6 │
│ BTC-EUR │ 2023-11-08 00:00:00 │ 2023-11-14 16:00:00 │ 160 │ 99.2265 │ 1.3352 │ 1.3456% │ 33075.5 │ 33520.6
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ BTC-EUR │ 2023-11-06 16:00:00 │ 2023-11-06 20:00:00 │ 4 │ 97.8607 │ -0.0026 │ -0.0026% │ 32620.2 │ 32619.4 │
│ BTC-EUR │ 2023-11-06 16:00:00 │ 2023-11-06 20:00:00 │ 4 │ 97.8607 │ -0.0026 │ -0.0026% │ 32620.2 │ 32619.4
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ DOT-EUR │ 2023-10-30 06:00:00 │ 2023-11-15 06:00:00 │ 384 │ 100.551 │ 19.886119.7771% │ 4.0375 │ 4.836
│ DOT-EUR │ 2023-10-30 06:00:00 │ 2023-11-14 00:00:00 │ 354 │ 100.551 │ 24.879424.7430% │ 4.0375 │ 5.0365
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ BTC-EUR │ 2023-09-13 16:00:00 │ 2023-09-22 16:00:00 │ 216 │ 97.8529 │ 2.2051 │ 2.2534% │ 24463.2 │ 25014.5 │
│ BTC-EUR │ 2023-09-13 16:00:00 │ 2023-09-22 16:00:00 │ 216 │ 97.8529 │ 2.2051 │ 2.2534% │ 24463.2 │ 25014.5
╰─────────┴─────────────────────┴─────────────────────┴────────────────────┴──────────────┴──────────────────┴───────────────────────┴────────────────────┴─────────────────────╯
==================================================================
```
Expand Down
14 changes: 13 additions & 1 deletion examples/backtest/algorithm/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class CrossOverStrategy(TradingStrategy):
fast = 21
slow = 75
trend = 150
stop_loss_percentage = 7

def apply_strategy(self, algorithm: Algorithm, market_data):

Expand Down Expand Up @@ -84,11 +85,22 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
)

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

open_trades = algorithm.get_open_trades(
target_symbol=target_symbol
)

for trade in open_trades:
algorithm.close_trade(trade)

# Checking manual stop losses
open_trades = algorithm.get_open_trades(target_symbol)

for open_trade in open_trades:
if open_trade.is_manual_stop_loss_trigger(
ohlcv_df=df,
current_price=market_data[f"{symbol}-ticker"]["bid"],
stop_loss_percentage=self.stop_loss_percentage
):
algorithm.close_trade(open_trade)
18 changes: 6 additions & 12 deletions examples/backtest_experiment/algorithms/algorithm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from investing_algorithm_framework import Algorithm, TradingStrategy, \
TimeUnit, OrderSide, DATETIME_FORMAT
import tulipy as ti
import polars as pl

from investing_algorithm_framework import Algorithm, TradingStrategy, \
TimeUnit, OrderSide


def is_below_trend(fast_series, slow_series):
Expand Down Expand Up @@ -88,16 +88,10 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
open_trades = algorithm.get_open_trades(target_symbol)

for open_trade in open_trades:
filtered_df = df.filter(
pl.col('Datetime') >= open_trade.opened_at.strftime(DATETIME_FORMAT)
)
close_prices = filtered_df['Close'].to_numpy()
current_price = market_data[f"{symbol}-ticker"]

if open_trade.is_manual_stop_loss_trigger(
prices=close_prices,
current_price=current_price["bid"],
stop_loss_percentage=self.stop_loss_percentage
ohlcv_df=df,
current_price=market_data[f"{symbol}-ticker"]["bid"],
stop_loss_percentage=self.stop_loss_percentage
):
algorithm.close_trade(open_trade)

Expand Down
157 changes: 9 additions & 148 deletions examples/backtest_experiment/backtest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from datetime import datetime, timedelta

from algorithms import create_algorithm
Expand All @@ -8,6 +9,13 @@
if __name__ == "__main__":
end_date = datetime(2023, 12, 2)
start_date = end_date - timedelta(days=100)
json = json.load(open("configuration.json"))
algorithms = []

# Create the algorithms with the configuration json
for configuration in json:
algorithms.append(create_algorithm(**configuration))

# Add a portfolio configuration of 400 euro initial balance
app.add_portfolio_configuration(
PortfolioConfiguration(
Expand All @@ -16,155 +24,8 @@
initial_balance=400,
)
)

# Run the backtest for each algorithm
reports = app.run_backtests(
algorithms=[
create_algorithm(
name="9-50-100",
description="9-50-100",
fast=9,
slow=50,
trend=100,
stop_loss_percentage=7
),
create_algorithm(
name="10-50-100",
description="10-50-100",
fast=10,
slow=50,
trend=100,
stop_loss_percentage=7
),
create_algorithm(
name="11-50-100",
description="11-50-100",
fast=11,
slow=50,
trend=100,
stop_loss_percentage=7
),
create_algorithm(
name="9-75-150",
description="9-75-150",
fast=9,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="10-75-150",
description="10-75-150",
fast=10,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="11-75-150",
description="11-75-150",
fast=11,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="20-75-150",
description="20-75-150",
fast=20,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="21-75-150",
description="21-75-150",
fast=21,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="22-75-150",
description="22-75-150",
fast=22,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="23-75-150",
description="23-75-150",
fast=23,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="24-75-150",
description="24-75-150",
fast=24,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="25-75-150",
description="25-75-150",
fast=25,
slow=75,
trend=150,
stop_loss_percentage=7
),
create_algorithm(
name="20-75-200",
description="20-75-200",
fast=20,
slow=75,
trend=200,
stop_loss_percentage=7
),
create_algorithm(
name="21-75-200",
description="24-75-200",
fast=24,
slow=75,
trend=200,
stop_loss_percentage=7
),
create_algorithm(
name="22-75-200",
description="24-75-200",
fast=24,
slow=75,
trend=200,
stop_loss_percentage=7
),
create_algorithm(
name="23-75-200",
description="24-75-200",
fast=24,
slow=75,
trend=200,
stop_loss_percentage=7
),
create_algorithm(
name="24-75-200",
description="24-75-200",
fast=24,
slow=75,
trend=200,
stop_loss_percentage=7
),
create_algorithm(
name="25-75-150",
description="25-75-200",
fast=25,
slow=75,
trend=200,
stop_loss_percentage=7
),
],
algorithms=algorithms,
start_date=start_date,
end_date=end_date,
pending_order_check_interval="2h",
Expand Down
Loading

0 comments on commit 58d11b2

Please sign in to comment.