Back

How can I filter stocks using their standard deviations for the last year

Hello, when I launch this algorithm it returns me an error.However I don't see what can be wrong.Can Someone help me please

 

Thank You 

class DynamicHorizontalChamber(QCAlgorithm):


def Initialize(self):
self.SetStartDate(2019, 1, 7)
self.SetEndDate(2019, 4, 1)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction)
self.averages = { }

def SelectionFunction(self, universe):
selected = []
universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True)
universe = [c for c in universe if c.Price > 100][:100]

for coarse in universe:
symbol = coarse.Symbol

if symbol not in self.averages:

history = self.History(symbol, 200, Resolution.Daily)

self.averages[symbol] = Selection(history)

self.averages[symbol].update(self.Time, coarse.AdjustedPrice)

if self.averages[symbol].is_ready() and self.averages[symbol].stt < 20:
selected.append(symbol)

return selected[100:]

def OnSecuritiesChanged(self, changes):
self.changes = changes
self.Log(f"OnSecuritiesChanged({self.Time}):: {changes}")

for security in self.changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)

for security in self.changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.10)

class Selection():
def __init__(self,history):

self.stt = StandardDeviation(200)
for bar in history.itertuples():
self.stt.Update(bar.Index[1],bar.close)


def is_ready(self):
return self.stt.IsReady

def update(self, time, price):
self.stt.Update(time,price)

 

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.


HI, I replaced self.CoarseSelectionFunction by SelectionFunction but it still doesn't work

0

Hey Wawes123,

stt represents the StandardDeviation indicator object and its current value can be found with stt.Current.Value.

This means for a given symbol you can access it's StandardDeviation value with

self.averages[symbol].stt.Current.Value

You also need to slice the selected list from the front rather than the end.
selected[100:] to selected[:100] , this ensures the selected symbols are properly returned by universe selection.

Here is a backtest with the changes.

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