Back

Error

Hi there, 

I am getting an error:

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


if self.sma20.Current.Value < self.sma150.Current.Value:
if security.Invested:
self.Liquidate(security.Symbol)

Runtime Error: UnboundLocalError : local variable 'security' referenced before assignment
at OnData in main.py:line 119
UnboundLocalError : local variable 'security' referenced before assignment (Open Stacktrace)

**Line 119 corresponds to "if security.Invested:"

I was wondering if someone would be able to help me out, thanks.

Update Backtest







You need to loop through the list of securities that are supposed to be liquidated in this part: 

if self.sma20.Current.Value < self.sma150.Current.Value:
for secutity in list_of_securities_to_liquidate:
if security.Invested:
self.Liquidate(security.Symbol)

Note that you have a similar case above:

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

but with that snippet, we cannot tell which securities you want to liquidate.
If all:

if self.sma20.Current.Value < self.sma150.Current.Value:
self.Liquidate()
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