Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Initialization/HandleOrderEvents.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def Call(self):

context.executionTimer.start()

if self.context.LiveMode:
self.context.positions_store.store_positions()

if not (orderEvent.Status == OrderStatus.Filled or orderEvent.Status == OrderStatus.PartiallyFilled):
return

Expand Down
67 changes: 67 additions & 0 deletions Tests/specs/initialization/handle_order_events_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
self.algorithm.charting = MagicMock()
self.algorithm.recentlyClosedDTE = []
self.algorithm.logger = MagicMock()
self.algorithm.positions_store = MagicMock() # Always initialize positions_store

self.handler = HandleOrderEvents(self.algorithm, self.order_event)

Expand Down Expand Up @@ -89,6 +90,72 @@
self.algorithm.executionTimer.start.assert_called_once()
self.algorithm.executionTimer.stop.assert_called_once()

with it('stores positions in live mode'):
# Setup LiveMode
self.algorithm.LiveMode = True

# Setup mock position and order
position = MagicMock(
orderTag="TEST_POS",
legs=[MagicMock(symbol=self.order_event.Symbol)]
)
mock_order = MagicMock()
mock_order.Tag = "TEST_POS"
self.algorithm.Transactions.GetOrderById.return_value = mock_order

self.handler.getPositionFromOrderEvent = MagicMock(
return_value=(position, None, "close", mock_order)
)

self.handler.Call()

# Verify positions were stored
self.algorithm.positions_store.store_positions.assert_called_once()

with it('does not store positions in backtest mode'):
# Setup LiveMode
self.algorithm.LiveMode = False

# Setup mock position and order
position = MagicMock(
orderTag="TEST_POS",
legs=[MagicMock(symbol=self.order_event.Symbol)]
)
mock_order = MagicMock()
mock_order.Tag = "TEST_POS"
self.algorithm.Transactions.GetOrderById.return_value = mock_order

self.handler.getPositionFromOrderEvent = MagicMock(
return_value=(position, None, "close", mock_order)
)

self.handler.Call()

# Verify positions were not stored
self.algorithm.positions_store.store_positions.assert_not_called()

with it('handles missing positions_store in live mode'):
# Setup LiveMode but remove positions_store
self.algorithm.LiveMode = True
# No need to remove positions_store as it doesn't exist by default

# Setup mock position and order
position = MagicMock(
orderTag="TEST_POS",
legs=[MagicMock(symbol=self.order_event.Symbol)]
)
mock_order = MagicMock()
mock_order.Tag = "TEST_POS"
self.algorithm.Transactions.GetOrderById.return_value = mock_order

self.handler.getPositionFromOrderEvent = MagicMock(
return_value=(position, None, "close", mock_order)
)

# Should not raise an error
self.handler.Call()
# Test passes if no exception is raised

Comment on lines +137 to +158
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect test implementation for missing positions_store scenario.

The test claims to verify behavior when positions_store is missing, but positions_store is always initialized in before.each. This means the test isn't actually testing the intended scenario.

To properly test this scenario:

        with it('handles missing positions_store in live mode'):
            # Setup LiveMode but remove positions_store
            self.algorithm.LiveMode = True
-            # No need to remove positions_store as it doesn't exist by default
+            # Remove positions_store to test the scenario
+            delattr(self.algorithm, 'positions_store')
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
with it('handles missing positions_store in live mode'):
# Setup LiveMode but remove positions_store
self.algorithm.LiveMode = True
# No need to remove positions_store as it doesn't exist by default
# Setup mock position and order
position = MagicMock(
orderTag="TEST_POS",
legs=[MagicMock(symbol=self.order_event.Symbol)]
)
mock_order = MagicMock()
mock_order.Tag = "TEST_POS"
self.algorithm.Transactions.GetOrderById.return_value = mock_order
self.handler.getPositionFromOrderEvent = MagicMock(
return_value=(position, None, "close", mock_order)
)
# Should not raise an error
self.handler.Call()
# Test passes if no exception is raised
with it('handles missing positions_store in live mode'):
# Setup LiveMode but remove positions_store
self.algorithm.LiveMode = True
# Remove positions_store to test the scenario
delattr(self.algorithm, 'positions_store')
# Setup mock position and order
position = MagicMock(
orderTag="TEST_POS",
legs=[MagicMock(symbol=self.order_event.Symbol)]
)
mock_order = MagicMock()
mock_order.Tag = "TEST_POS"
self.algorithm.Transactions.GetOrderById.return_value = mock_order
self.handler.getPositionFromOrderEvent = MagicMock(
return_value=(position, None, "close", mock_order)
)
# Should not raise an error
self.handler.Call()
# Test passes if no exception is raised

with context('getPositionFromOrderEvent'):
with it('finds position by order tag'):
# Setup mock order and position
Expand Down
4 changes: 0 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ def OnOrderEvent(self, orderEvent):
self.executionTimer.stop()

def OnEndOfAlgorithm(self) -> None:
# store positions in live mode
if self.LiveMode:
self.positions_store.store_positions()

# Convert the dictionary into a Pandas Data Frame
# dfAllPositions = pd.DataFrame.from_dict(self.allPositions, orient = "index")
# Convert the dataclasses into Pandas Data Frame
Expand Down
Loading