pep8 conversion of python algorithms #3 (#7934)
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Report Generator Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled

* raw pep8 conversion

* Minor fixes

---------

Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
This commit is contained in:
Louis Szeto
2024-04-18 06:28:04 +08:00
committed by GitHub
parent 6d5f5dce58
commit ed351c8726
30 changed files with 788 additions and 705 deletions
@@ -23,55 +23,55 @@ from AlgorithmImports import *
class BasicTemplateOptionsHistoryAlgorithm(QCAlgorithm):
''' This example demonstrates how to get access to options history for a given underlying equity security.'''
def Initialize(self):
def initialize(self):
# this test opens position in the first day of trading, lives through stock split (7 for 1), and closes adjusted position on the second day
self.SetStartDate(2015, 12, 24)
self.SetEndDate(2015, 12, 24)
self.SetCash(1000000)
self.set_start_date(2015, 12, 24)
self.set_end_date(2015, 12, 24)
self.set_cash(1000000)
option = self.AddOption("GOOG")
option = self.add_option("GOOG")
# add the initial contract filter
# SetFilter method accepts timedelta objects or integer for days.
# The following statements yield the same filtering criteria
option.SetFilter(-2, +2, 0, 180)
# option.SetFilter(-2,2, timedelta(0), timedelta(180))
option.set_filter(-2, +2, 0, 180)
# option.set_filter(-2,2, timedelta(0), timedelta(180))
# set the pricing model for Greeks and volatility
# find more pricing models https://www.quantconnect.com/lean/documentation/topic27704.html
option.PriceModel = OptionPriceModels.CrankNicolsonFD()
option.price_model = OptionPriceModels.crank_nicolson_fd()
# set the warm-up period for the pricing model
self.SetWarmUp(TimeSpan.FromDays(4))
self.set_warm_up(TimeSpan.from_days(4))
# set the benchmark to be the initial cash
self.SetBenchmark(lambda x: 1000000)
self.set_benchmark(lambda x: 1000000)
def OnData(self,slice):
if self.IsWarmingUp: return
if not self.Portfolio.Invested:
for chain in slice.OptionChains:
volatility = self.Securities[chain.Key.Underlying].VolatilityModel.Volatility
for contract in chain.Value:
self.Log("{0},Bid={1} Ask={2} Last={3} OI={4} sigma={5:.3f} NPV={6:.3f} \
def on_data(self,slice):
if self.is_warming_up: return
if not self.portfolio.invested:
for chain in slice.option_chains:
volatility = self.securities[chain.key.underlying].volatility_model.volatility
for contract in chain.value:
self.log("{0},Bid={1} Ask={2} Last={3} OI={4} sigma={5:.3f} NPV={6:.3f} \
delta={7:.3f} gamma={8:.3f} vega={9:.3f} beta={10:.2f} theta={11:.2f} IV={12:.2f}".format(
contract.Symbol.Value,
contract.BidPrice,
contract.AskPrice,
contract.LastPrice,
contract.OpenInterest,
contract.symbol.value,
contract.bid_price,
contract.ask_price,
contract.last_price,
contract.open_interest,
volatility,
contract.TheoreticalPrice,
contract.Greeks.Delta,
contract.Greeks.Gamma,
contract.Greeks.Vega,
contract.Greeks.Rho,
contract.Greeks.Theta / 365,
contract.ImpliedVolatility))
contract.theoretical_price,
contract.greeks.delta,
contract.greeks.gamma,
contract.greeks.vega,
contract.greeks.rho,
contract.greeks.theta / 365,
contract.implied_volatility))
def OnSecuritiesChanged(self, changes):
for change in changes.AddedSecurities:
def on_securities_changed(self, changes):
for change in changes.added_securities:
# only print options price
if change.Symbol.Value == "GOOG": return
history = self.History(change.Symbol, 10, Resolution.Minute).sort_index(level='time', ascending=False)[:3]
if change.symbol.value == "GOOG": return
history = self.history(change.symbol, 10, Resolution.MINUTE).sort_index(level='time', ascending=False)[:3]
for index, row in history.iterrows():
self.Log("History: " + str(index[3])
self.log("History: " + str(index[3])
+ ": " + index[4].strftime("%m/%d/%Y %I:%M:%S %p")
+ " > " + str(row.close))