2222
2323class BasicTemplateFutureOptionAlgorithm (QCAlgorithm ):
2424
25- def Initialize (self ):
26- '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
27- self .SetStartDate (2022 , 1 , 1 )
28- self .SetEndDate (2022 , 2 , 1 )
29- self .SetCash (100000 )
25+ def initialize (self ):
26+ '''initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
27+ self .set_start_date (2022 , 1 , 1 )
28+ self .set_end_date (2022 , 2 , 1 )
29+ self .set_cash (100000 )
3030
31- gold_futures = self .AddFuture (Futures .Metals .Gold , Resolution .Minute )
32- gold_futures .SetFilter (0 , 180 )
33- self .symbol = gold_futures .Symbol
34- self .AddFutureOption (self .symbol , lambda universe : universe .Strikes (- 5 , + 5 )
35- .CallsOnly ()
36- .BackMonth ()
37- .OnlyApplyFilterAtMarketOpen ())
31+ gold_futures = self .add_future (Futures .Metals .GOLD , Resolution .MINUTE )
32+ gold_futures .set_filter (0 , 180 )
33+ self ._symbol = gold_futures .symbol
34+ self .add_future_option (self ._symbol , lambda universe : universe .strikes (- 5 , + 5 )
35+ .calls_only ()
36+ .back_month ()
37+ .only_apply_filter_at_market_open ())
3838
3939 # Historical Data
40- history = self .History (self .symbol , 60 , Resolution .Daily )
41- self .Log (f"Received { len (history )} bars from { self .symbol } FutureOption historical data call." )
40+ history = self .history (self ._symbol , 60 , Resolution .DAILY )
41+ self .log (f"Received { len (history )} bars from { self ._symbol } FutureOption historical data call." )
4242
43- def OnData (self , data ):
44- '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
43+ def on_data (self , data ):
44+ '''on_data event is the primary entry point for your algorithm. Each new data point will be pumped in here.
4545 Arguments:
4646 slice: Slice object keyed by symbol containing the stock data
4747 '''
4848 # Access Data
49- for kvp in data .OptionChains :
50- underlying_future_contract = kvp .Key . Underlying
51- chain = kvp .Value
49+ for kvp in data .option_chains :
50+ underlying_future_contract = kvp .key . underlying
51+ chain = kvp .value
5252
5353 if not chain : continue
5454
5555 for contract in chain :
56- self .Log (f"""Canonical Symbol: { kvp .Key } ;
56+ self .log (f"""Canonical Symbol: { kvp .key } ;
5757 Contract: { contract } ;
58- Right: { contract .Right } ;
59- Expiry: { contract .Expiry } ;
60- Bid price: { contract .BidPrice } ;
61- Ask price: { contract .AskPrice } ;
62- Implied Volatility: { contract .ImpliedVolatility } """ )
58+ Right: { contract .right } ;
59+ Expiry: { contract .expiry } ;
60+ Bid price: { contract .bid_price } ;
61+ Ask price: { contract .ask_price } ;
62+ Implied Volatility: { contract .implied_volatility } """ )
6363
64- if not self .Portfolio . Invested :
65- atm_strike = sorted (chain , key = lambda x : abs (chain .Underlying . Price - x .Strike ))[0 ].Strike
66- selected_contract = sorted ([contract for contract in chain if contract .Strike == atm_strike ], \
67- key = lambda x : x .Expiry , reverse = True )[0 ]
68- self .MarketOrder (selected_contract .Symbol , 1 )
64+ if not self .portfolio . invested :
65+ atm_strike = sorted (chain , key = lambda x : abs (chain .underlying . price - x .strike ))[0 ].strike
66+ selected_contract = sorted ([contract for contract in chain if contract .strike == atm_strike ], \
67+ key = lambda x : x .expiry , reverse = True )[0 ]
68+ self .market_order (selected_contract .symbol , 1 )
6969
70- def OnOrderEvent (self , orderEvent ):
71- self .Debug ("{} {}" .format (self .Time , orderEvent . ToString ()))
70+ def on_order_event (self , order_event ):
71+ self .debug ("{} {}" .format (self .time , order_event . to_string ()))
0 commit comments