Skip to content

Commit

Permalink
Updated samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbeced committed Jan 17, 2025
1 parent 0a7325c commit 989f1a5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion samples/backtest_bbands.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ async def main():
strategy = bbands.Strategy(event_dispatcher, period=30, std_dev=2)
exchange.subscribe_to_bar_events(pair, strategy.on_bar_event)

# Connect the position manager to the strategy signals and to bar events.
# Connect the position manager to different types of events.
position_mgr = position_manager.PositionManager(
exchange, position_amount, pair.quote_symbol, stop_loss_pct
)
strategy.subscribe_to_trading_signals(position_mgr.on_trading_signal)
exchange.subscribe_to_bar_events(pair, position_mgr.on_bar_event)
exchange.subscribe_to_order_events(position_mgr.on_order_event)

# Setup chart.
chart = charts.LineCharts(exchange)
Expand Down
3 changes: 2 additions & 1 deletion samples/backtest_pairs_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ async def main():
event_dispatcher, pair_1, pair_2, window_size=24 * 10, z_score_window_size=24 * 10,
p_value_threshold=p_value_threshold, z_score_entry_ge=2.3, z_score_exit_lt=1.5
)
# Connect the position manager to the strategy signals.
# Connect the position manager to different types of events.
trading_strategy.subscribe_to_trading_signals(position_mgr.on_trading_signal)
exchange.subscribe_to_order_events(position_mgr.on_order_event)

chart = charts.LineCharts(exchange)

Expand Down
3 changes: 2 additions & 1 deletion samples/backtest_rsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ async def main():
strategy = rsi.Strategy(event_dispatcher, 7, oversold_level, overbought_level)
exchange.subscribe_to_bar_events(pair, strategy.on_bar_event)

# Connect the position manager to the strategy signals and to bar events. Borrowing is disabled in this example.
# Connect the position manager to different types of events. Borrowing is disabled in this example.
position_mgr = position_manager.PositionManager(
exchange, position_amount=Decimal(1000), quote_symbol=pair.quote_symbol, stop_loss_pct=Decimal(6),
borrowing_disabled=True
)
strategy.subscribe_to_trading_signals(position_mgr.on_trading_signal)
exchange.subscribe_to_bar_events(pair, position_mgr.on_bar_event)
exchange.subscribe_to_order_events(position_mgr.on_order_event)

# Load bars from the CSV file.
exchange.add_bar_source(csv.BarSource(pair, "bitstamp_btcusd_day.csv", "1d"))
Expand Down
3 changes: 2 additions & 1 deletion samples/backtest_sma.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ async def main():
strategy = sma.Strategy(event_dispatcher, period=12)
exchange.subscribe_to_bar_events(pair, strategy.on_bar_event)

# Connect the position manager to the strategy signals and to bar events. Borrowing is disabled in this example.
# Connect the position manager to different types of events. Borrowing is disabled in this example.
position_mgr = position_manager.PositionManager(
exchange, position_amount=Decimal(1000), quote_symbol=pair.quote_symbol, stop_loss_pct=Decimal(5),
borrowing_disabled=True
)
strategy.subscribe_to_trading_signals(position_mgr.on_trading_signal)
exchange.subscribe_to_bar_events(pair, position_mgr.on_bar_event)
exchange.subscribe_to_order_events(position_mgr.on_order_event)

# Load bars from the CSV file.
exchange.add_bar_source(csv.BarSource(pair, "binance_btcusdt_day.csv", "1d"))
Expand Down
6 changes: 6 additions & 0 deletions samples/backtesting/position_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ async def on_bar_event(self, bar_event: bs.BarEvent):
self._last_check_loss = bar_event.when
await self.check_loss()

async def on_order_event(self, order_event: backtesting_exchange.OrderEvent):
order = order_event.order
logging.info(StructuredMessage(
"Order udpated", id=order.id, is_open=order.is_open, amount=order.amount,
amount_filled=order.amount_filled, avg_fill_price=order.fill_price
))

def signed_to_position(signed):
if signed > 0:
Expand Down

0 comments on commit 989f1a5

Please # to comment.