Skip to content

Commit

Permalink
Updated execution handler to update final orders ensuring an executio…
Browse files Browse the repository at this point in the history
…n order is created in the event of a single submission without a rebalance. (#392)
  • Loading branch information
juliettejames authored Apr 25, 2024
1 parent c370f42 commit 93eb8ae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions qstrader/execution/execution_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ def __call__(self, dt, rebalance_orders):
if self.submit_orders:
for order in final_orders:
self.broker.submit_order(self.broker_portfolio_id, order)
self.broker.update(dt)
17 changes: 17 additions & 0 deletions tests/integration/trading/fixtures/GHI.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Date,Open,Close,Adj Close
2015-11-06,209.740005,210.039993,180.776825
2015-11-09,209.309998,208.080002,179.089874
2015-11-10,207.509995,208.559998,179.503006
2015-11-11,208.880005,207.740005,178.797272
2015-11-12,206.5,204.839996,176.301285
2015-11-13,204.350006,202.539993,174.321686
2015-11-16,202.320007,205.619995,176.97261
2015-11-17,205.990005,205.470001,176.843521
2015-11-18,206.039993,208.729996,179.649323
2015-11-19,208.589996,208.550003,179.494431
2015-11-20,209.449997,209.309998,180.148499
2015-11-23,209.380005,209.070007,179.941971
2015-11-24,207.869995,209.350006,180.182999
2015-11-25,209.5,209.320007,180.15715
2015-11-27,209.429993,209.559998,180.363708
2015-11-30,209.75,208.690002,179.614899
30 changes: 30 additions & 0 deletions tests/integration/trading/test_backtest_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from qstrader.asset.universe.static import StaticUniverse
from qstrader.trading.backtest import BacktestTradingSession

from qstrader import settings


def test_backtest_sixty_forty(etf_filepath):
"""
Expand Down Expand Up @@ -127,3 +129,31 @@ def test_backtest_long_short_leveraged(etf_filepath):

pd.testing.assert_frame_equal(history_df, expected_df)
assert portfolio_dict == expected_dict


def test_backtest_buy_and_hold(etf_filepath, capsys):
"""
"""
settings.print_events=True
os.environ['QSTRADER_CSV_DATA_DIR'] = etf_filepath
assets = ['EQ:GHI']
universe = StaticUniverse(assets)
alpha_model = FixedSignalsAlphaModel({'EQ:GHI':1.0})

start_dt = pd.Timestamp('2015-11-09 14:30:00', tz=pytz.UTC)
end_dt = pd.Timestamp('2015-11-10 14:30:00', tz=pytz.UTC)

backtest = BacktestTradingSession(
start_dt,
end_dt,
universe,
alpha_model,
rebalance='buy_and_hold',
long_only=True,
cash_buffer_percentage=0.01,
)
backtest.run(results=False)

expected_execution_text = "(2015-11-09 14:30:00+00:00) - executed order:"
captured = capsys.readouterr()
assert expected_execution_text in captured.out

0 comments on commit 93eb8ae

Please sign in to comment.