Hi there, 

I am trying to get my algo to liquidate its equity holdings when the 20 day moving average goes below the 150 day moving average and then buy and hold bonds during that time. 

However, my algorithm continuously buys and sells the bonds during this time. How would I make it liquidate the equities and then buy and hold bonds?

def OnData(self, data): if self.sma20.Current.Value >= self.sma150.Current.Value: if self.flag3 > 0: if self.flag2 == 1: self.flag2 = 0 if self._changes == None: return for security in self._changes.RemovedSecurities: if security.Invested: self.Liquidate(security.Symbol) for security in self._changes.AddedSecurities: self.SetHoldings(security.Symbol, 0.8/float(len(self._changes.AddedSecurities))) self._changes = None else: self.Liquidate() self.SetHoldings("TLT", 1.0 )

Author