Hello im trying to use a strategy that works with daily returns, when there is a divergence of returns then we buy and sell, its just academic, but at the moment im trying to apply returns and model it its not running goos saying
Runtime error: float object has no attribute shift. If anyone can help me would be great. Thanks!!e 'shift'
THE CODE
import numpy as np
import pandas as pd
class WTIBRENTSpread(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 1, 1)
self.SetCash(10000)
self.symbols = ["CDNS","SNPS"]
for symbol in self.symbols:
data = self.AddEquity(symbol, Resolution.Hour)
def OnData(self, data):
symbol1 = self.Symbol(self.symbols[0])
symbol2 = self.Symbol(self.symbols[1])
if symbol1 in data.Keys and symbol2 in data.Keys and data[symbol1] and data[symbol2]:
x = data[symbol1].Price
y = data[symbol2].Price
price_1=x/x.shift(1)-1
price_1=y/y.shift(1)-1
if x != 0 and y != 0:
spread=price1-price2
self.spread.Add(spread)
if self.spread.IsReady:
if (self.Time.date() - self.Securities[symbol1].GetLastData().Time.date()).days < 5 and (self.Time.date() - self.Securities[symbol2].GetLastData().Time.date()).days < 5:
if spread > 15:
self.SetHoldings(symbol1, -1)
self.SetHoldings(symbol2, 1)
elif spread < 15:
self.SetHoldings(symbol1, 1)
self.SetHoldings(symbol2, -1)
else:
self.Liquidate()
Adam W
Issue is this part here:
where `x` and `y` are floats but you run the (presumably pandas) `.shift()` on it. If you need to store the previous price, you can do something like
Ro García G
Thanks for the help! I think I made a mistake in the code I meant
if symbol1 in data.Keys and symbol2 in data.Keys and data[symbol1] and data[symbol2]:x = data[symbol1].Pricey = data[symbol2].Priceprice_1=x/x.shift(1)-1price_2=y/y.shift(1)-1
I try to run your algo but im not sure if iim having something wrong can you check it out? The code is not working
THANKS FOR THE HELP!!
class WTIBRENTSpread(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 1, 1)
self.SetCash(10000)
self.symbols = ["CDNS","SNPS"]
self.prevPrices = { symbol : None for symbol in self.symbols }
for symbol in self.symbols:
data = self.AddEquity(symbol, Resolution.Hour)
#data.SetLeverage(5)
def OnData(self, data):
symbol1 = self.Symbol(self.symbols[0])
symbol2 = self.Symbol(self.symbols[1])
for symbol in self.symbols:
if self.prevPrices[symbol] is None:
return
if not all([ data.ContainsKey(symbol) for symbol in self.symbols ]): return
prices = { symbol : data[symbol] for symbol in self.symbols }
price_1 = { symbol : ( prices[symbol1] / self.prevPrices[symbol1] )-1 for symbol in self.symbols }
price_2 = { symbol : ( prices[symbol2] / self.prevPrices[symbol2] )-1 for symbol in self.symbols }
self.prevPrices.update( prices )
if price1 != 0 and price2 != 0:
spread = price_1 - price_2
self.spread.Add(spread)
if self.spread.IsReady:
if (self.Time.date() - self.Securities[symbol1].GetLastData().Time.date()).days < 5 and (self.Time.date() - self.Securities[symbol2].GetLastData().Time.date()).days < 5:
current_spread = spreads[0]
if current_spread > 0.01:
self.SetHoldings(symbol1, -1)
self.SetHoldings(symbol2, 1)
elif current_spread < 0.01:
self.SetHoldings(symbol1, 1)
self.SetHoldings(symbol2, -1)
else:
self.Liquidate()
Adam W
Be careful with this part here:
Here's a more complete example attached. Try to make sure you understand what each line of code is doing so far before adding the trading logic.
Adam W
Reattaching backtest in case it didn't show up
Vladimir
Ro García G
Here is my attempt to implement your strategy with QC indicators.
If you are satisfied with my answer, please accept it and don't forget to like it.
Vladimir
Here is the code snippet of the attached backtest.
Ro García G
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!