Skip to content

Commit b67ce56

Browse files
authored
275 support for trade take profit and take loss operations (#295)
* Add initial stop loss and take profit support * Update example in readme * Update examples * Support for trade take profit and take loss operations * Fix flake8 warnings * Fix image height
1 parent cd17db7 commit b67ce56

File tree

96 files changed

+6408
-2532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+6408
-2532
lines changed

Diff for: README.md

+130-116
Large diffs are not rendered by default.

Diff for: examples/backtest_example/run_backtest.py

+30-23
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55

66
from investing_algorithm_framework import CCXTOHLCVMarketDataSource, \
7-
CCXTTickerMarketDataSource, Algorithm, PortfolioConfiguration, \
7+
CCXTTickerMarketDataSource, PortfolioConfiguration, \
88
create_app, pretty_print_backtest, BacktestDateRange, TimeUnit, \
9-
TradingStrategy, OrderSide, DEFAULT_LOGGING_CONFIG
9+
TradingStrategy, OrderSide, DEFAULT_LOGGING_CONFIG, Context
1010

1111
import tulipy as ti
1212

@@ -96,12 +96,12 @@ class CrossOverStrategy(TradingStrategy):
9696
trend = 150
9797
stop_loss_percentage = 7
9898

99-
def apply_strategy(self, algorithm: Algorithm, market_data):
99+
def apply_strategy(self, context: Context, market_data):
100100

101101
for symbol in self.symbols:
102102
target_symbol = symbol.split('/')[0]
103103

104-
if algorithm.has_open_orders(target_symbol):
104+
if context.has_open_orders(target_symbol):
105105
continue
106106

107107
df = market_data[f"{symbol}-ohlcv"]
@@ -111,36 +111,47 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
111111
trend = ti.sma(df['Close'].to_numpy(), self.trend)
112112
price = ticker_data["bid"]
113113

114-
if not algorithm.has_position(target_symbol) \
114+
if not context.has_position(target_symbol) \
115115
and is_crossover(fast, slow) \
116116
and is_above_trend(fast, trend):
117-
order = algorithm.create_limit_order(
117+
order = context.create_limit_order(
118118
target_symbol=target_symbol,
119119
order_side=OrderSide.BUY,
120120
price=price,
121121
percentage_of_portfolio=25,
122122
precision=4,
123123
)
124-
trade = algorithm.get_trade(order_id=order.id)
125-
algorithm.add_trailing_stop_loss(
126-
trade=trade, percentage=5
124+
trade = context.get_trade(order_id=order.id)
125+
context.add_stop_loss(
126+
trade=trade,
127+
percentage=5,
128+
sell_percentage=50
129+
)
130+
context.add_take_profit(
131+
trade=trade,
132+
percentage=5,
133+
trade_risk_type="trailing",
134+
sell_percentage=50
135+
)
136+
context.add_take_profit(
137+
trade=trade,
138+
percentage=10,
139+
trade_risk_type="trailing",
140+
sell_percentage=20
127141
)
128142

129-
130-
if algorithm.has_position(target_symbol) \
143+
if context.has_position(target_symbol) \
131144
and is_below_trend(fast, slow):
132-
open_trades = algorithm.get_open_trades(
145+
open_trades = context.get_open_trades(
133146
target_symbol=target_symbol
134147
)
135148

136149
for trade in open_trades:
137-
algorithm.close_trade(trade)
150+
context.close_trade(trade)
138151

139152

140-
app = create_app()
141-
algorithm = Algorithm("GoldenCrossStrategy")
142-
algorithm.add_strategy(CrossOverStrategy)
143-
app.add_algorithm(algorithm)
153+
app = create_app(name="GoldenCrossStrategy")
154+
app.add_strategy(CrossOverStrategy)
144155
app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h)
145156
app.add_market_data_source(bitvavo_dot_eur_ohlcv_2h)
146157
app.add_market_data_source(bitvavo_btc_eur_ticker)
@@ -157,17 +168,13 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
157168

158169
if __name__ == "__main__":
159170
end_date = datetime(2023, 12, 2)
160-
start_date = end_date - timedelta(days=400)
171+
start_date = end_date - timedelta(days=100)
161172
date_range = BacktestDateRange(
162173
start_date=start_date,
163174
end_date=end_date
164175
)
165176
start_time = time.time()
166-
167-
backtest_report = app.run_backtest(
168-
algorithm=algorithm,
169-
backtest_date_range=date_range,
170-
)
177+
backtest_report = app.run_backtest(backtest_date_range=date_range)
171178
pretty_print_backtest(backtest_report)
172179
end_time = time.time()
173180
print(f"Execution Time: {end_time - start_time:.6f} seconds")

0 commit comments

Comments
 (0)