Back

Separated OnData for different resolution

Hi, Collegues 

I have a problem please help with advise

I have two data resolusion subscriptions: Minute for option and Tick for Equity. I need to determine what type of data came in in onData and to process it differently.  How can I do this ?

When I procees bar data like a tick may lastprice is a oldest bar close price and it is too bad for my algo.

self.stock= "SPY"
self.equity = self.AddEquity(self.stock, Resolution.Tick)
self.equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
option = self.AddOption(self.stock, Resolution.Minute)
option.SetFilter(lambda universe: universe.IncludeWeeklys().Strikes(-15, +15).Expiration(timedelta(6), timedelta(7)))
# for greeks and pricer (needs some warmup) - https://github.com/QuantConnect/Lean/blob/21cd972e99f70f007ce689bdaeeafe3cb4ea9c77/Common/Securities/Option/OptionPriceModels.cs#L81
#option.PriceModel = OptionPriceModels.BlackScholes()
option.PriceModel = OptionPriceModels.CrankNicolsonFD() # both European & American, automatically



def OnData(self, data):

If it is ticks data:
do...

else
do....

 

 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


If you know that the option data will come in at minute frequency and the equity data at tick frequency, why not separate them based on what the data contains? I.e. if data contains a key named self.stock then you know it's the equity data.

0

Hi, Douglas,  unfortunately, both data will be contains a key named self.stock. 

0

I don`t know what is the problem but my simple code 

else:

ticks = data.Ticks
#if self.stock in ticks.Keys:
tradeBars = data.Bars
close = 0
try:
close = tradeBars[self.stock].Close
except: #"Do nothing"
pass

try:
tick = ticks[self.stock][-1]
except: #"Do nothing"
pass
else:
self.lastprice = tick.LastPrice
if close == self.lastprice: return
if abs(self.null_dh_level - self.lastprice) >= self.min_dh_step:
self.do_trade = False
self.get_greeks()
self.doDeltaHedge()




def doDeltaHedge(self):
#fut = self.Portfolio[self.stock].Quantity
#self.Debug("+++ time: "+ str(self.Time) +" delta is " + str(self.Delta) + " fut pos: " + str(fut))
if abs(self.Delta) > self.delta_treshold:
#self.Debug("+++ time: "+ str(self.Time) + "Go DH !!!")
self.Debug(" lastPrice is : --- {} " \
.format( self.lastprice) )
self.null_dh_level = self.lastprice
needfut = round(self.Delta*-100)
self.MarketOrder(self.stock,needfut)
self.Debug(str(self.Time) + " delta_hedging: self.Delta {} add fut contracts: {} by price: {} -- fut_vol: {}" \
.format(self.Delta, needfut, self.null_dh_level, round(self.vola *100,2) ))

 

is writing in debug messege that we buy 96 contract by price 241.4

https://ibb.co/pRfKV0C

but in order log we see that realy we buy it by price 257.92

https://ibb.co/nDVYXYS

 

Please, help me to understand where is the mistake, maybe it is lean engine bug, and maybe it is my code mistake 

I am completely confused...

0

Hi SLs,

Thank you for your question. Please use Securities[Symbol].Price to obtain the price to trade, instead of save the LastPrice, which may introduce bugs. If you need further help, please go to private support and provide the full codes of your algorithm.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed