Coverage for scripts/lean_smoke/main_option_order.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Lean smoke test of main.py's own multi-leg option order (V6.6.0, Problems.md #38). 

2 

3V6.5.0 ran main.py with an option asset on Lean's sample AAPL chain, but that month produced no signal, so the order path never 

4ran. This forces one signal: a subclass of the real algorithm calls `_apply_option_multi_leg_order` with a decision built by the 

5real sizing function once a chain has arrived. Run it with the config from `make_option_main_config.py`: 

6 

7 bash scripts/lean_smoke/run_main_option_order.sh 

8 

9The project holds the repo's main.py as `aq_main.py` and this file as main.py. It imports the module, not the class, so Lean finds 

10one algorithm class here. `SMOKE_JSON {...}` lines report what happened. 

11""" 

12 

13import dataclasses 

14import json 

15from builtins import Exception 

16 

17import aq_main 

18from AlgorithmImports import * # noqa: F403 

19 

20from portfolio.options_strategy import build_vertical_spread_position_sizing 

21 

22 

23class MainOptionOrderSmoke(aq_main.AetherQuantAlgorithm): 

24 _forced = False 

25 

26 def _smoke(self, **fields): 

27 self.Debug("SMOKE_JSON " + json.dumps(fields, default=str)) 

28 

29 def on_data(self, slice): 

30 super().on_data(slice) 

31 if self._forced or self.is_warming_up: 

32 return 

33 for symbol in self.symbols: 

34 asset = self.asset_lookup[str(symbol)] 

35 if asset.get("security_type") != "option": 

36 continue 

37 rows = (self.latest_options_chains_payload or {}).get(str(asset["underlying_ticker"])) or [] 

38 if not rows: 

39 continue 

40 nav = float(self.Portfolio.TotalPortfolioValue) 

41 decision = build_vertical_spread_position_sizing("buy", 0.8, rows, nav, max_vega_budget_pct_of_equity=0.02) 

42 if decision is None: 

43 self._smoke(kind="no_decision", rows=len(rows)) 

44 continue 

45 self._forced = True 

46 orders_before = self.Transactions.OrdersCount 

47 try: 

48 result = self._apply_option_multi_leg_order(symbol, str(symbol), decision, 0.01, float(rows[0].get("underlying_price") or 0.0), True, "smoke") 

49 except Exception as error: 

50 self._smoke(kind="order_raised", error=f"{type(error).__name__}: {error}") 

51 raise 

52 self._smoke( 

53 kind="forced_order", result=result, strategy=decision.strategy_name, contracts=decision.contracts, 

54 legs=[str(leg.contract_symbol) for leg in decision.legs], orders_placed=self.Transactions.OrdersCount - orders_before, 

55 ) 

56 # The limit above often rests unfilled on this thin sample, so one contract goes through the market branch as well: that one must fill. 

57 self.limit_orders_enabled = False 

58 orders_before = self.Transactions.OrdersCount 

59 result = self._apply_option_multi_leg_order( 

60 symbol, str(symbol), dataclasses.replace(decision, contracts=1), 0.01, float(rows[0].get("underlying_price") or 0.0), True, "smoke", 

61 ) 

62 self.limit_orders_enabled = True 

63 self._smoke(kind="forced_market_order", result=result, orders_placed=self.Transactions.OrdersCount - orders_before) 

64 return 

65 

66 def on_order_event(self, order_event): 

67 super().on_order_event(order_event) 

68 if order_event.Symbol.SecurityType == SecurityType.Option: # noqa: F405 

69 self._smoke(kind="option_fill_event", symbol=str(order_event.Symbol), status=str(order_event.Status), quantity=float(order_event.FillQuantity), price=float(order_event.FillPrice))