4
4
5
5
6
6
from investing_algorithm_framework import CCXTOHLCVMarketDataSource , \
7
- CCXTTickerMarketDataSource , Algorithm , PortfolioConfiguration , \
7
+ CCXTTickerMarketDataSource , PortfolioConfiguration , \
8
8
create_app , pretty_print_backtest , BacktestDateRange , TimeUnit , \
9
- TradingStrategy , OrderSide , DEFAULT_LOGGING_CONFIG
9
+ TradingStrategy , OrderSide , DEFAULT_LOGGING_CONFIG , Context
10
10
11
11
import tulipy as ti
12
12
@@ -96,12 +96,12 @@ class CrossOverStrategy(TradingStrategy):
96
96
trend = 150
97
97
stop_loss_percentage = 7
98
98
99
- def apply_strategy (self , algorithm : Algorithm , market_data ):
99
+ def apply_strategy (self , context : Context , market_data ):
100
100
101
101
for symbol in self .symbols :
102
102
target_symbol = symbol .split ('/' )[0 ]
103
103
104
- if algorithm .has_open_orders (target_symbol ):
104
+ if context .has_open_orders (target_symbol ):
105
105
continue
106
106
107
107
df = market_data [f"{ symbol } -ohlcv" ]
@@ -111,36 +111,47 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
111
111
trend = ti .sma (df ['Close' ].to_numpy (), self .trend )
112
112
price = ticker_data ["bid" ]
113
113
114
- if not algorithm .has_position (target_symbol ) \
114
+ if not context .has_position (target_symbol ) \
115
115
and is_crossover (fast , slow ) \
116
116
and is_above_trend (fast , trend ):
117
- order = algorithm .create_limit_order (
117
+ order = context .create_limit_order (
118
118
target_symbol = target_symbol ,
119
119
order_side = OrderSide .BUY ,
120
120
price = price ,
121
121
percentage_of_portfolio = 25 ,
122
122
precision = 4 ,
123
123
)
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
127
141
)
128
142
129
-
130
- if algorithm .has_position (target_symbol ) \
143
+ if context .has_position (target_symbol ) \
131
144
and is_below_trend (fast , slow ):
132
- open_trades = algorithm .get_open_trades (
145
+ open_trades = context .get_open_trades (
133
146
target_symbol = target_symbol
134
147
)
135
148
136
149
for trade in open_trades :
137
- algorithm .close_trade (trade )
150
+ context .close_trade (trade )
138
151
139
152
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 )
144
155
app .add_market_data_source (bitvavo_btc_eur_ohlcv_2h )
145
156
app .add_market_data_source (bitvavo_dot_eur_ohlcv_2h )
146
157
app .add_market_data_source (bitvavo_btc_eur_ticker )
@@ -157,17 +168,13 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
157
168
158
169
if __name__ == "__main__" :
159
170
end_date = datetime (2023 , 12 , 2 )
160
- start_date = end_date - timedelta (days = 400 )
171
+ start_date = end_date - timedelta (days = 100 )
161
172
date_range = BacktestDateRange (
162
173
start_date = start_date ,
163
174
end_date = end_date
164
175
)
165
176
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 )
171
178
pretty_print_backtest (backtest_report )
172
179
end_time = time .time ()
173
180
print (f"Execution Time: { end_time - start_time :.6f} seconds" )
0 commit comments