System.InvalidCastException: Specified cast is not valid.


Runtime Error: Specified cast is not valid.


[backtest doesn't show up on the list, will have to paste here instead of attach]


# System.InvalidCastException: Specified cast is not valid.
# Runtime Error: Specified cast is not valid.

import pandas as pd

class Zoo(QCAlgorithm):
def Initialize(self):
self.averages = {}

def CoarseSelection(c, coarse):
columns = ['Price', 'HasFundamentalData']
data_df = pd.DataFrame.from_records(
[[getattr(s, name) for name in columns] for s in coarse],
index = [s for s in coarse], columns = columns, coerce_float=True)
data_df = data_df.query("(Price > .50) and (Price < .60) and HasFundamentalData").nsmallest(99, 'Price')
diffs = pd.Series({})
for cf in data_df.index:
sym = cf.Symbol
if sym not in c.averages:
c.averages[sym] = SymbolData(sym) # initialize
c.averages[sym].update(cf) # Update the SymbolData object with current EOD price?
sma1 = c.averages[sym].sma1.Current.Value ; sma1 = float(sma1) if sma1 else 0.0
sma2 = c.averages[sym].sma2.Current.Value ; sma2 = float(sma2) if sma2 else 0.0
diff = sma1 - sma2
if not diff: continue
z = str(sym) # to avoid diffs[sym] = diff ... 'object is not callable' (another confusing error)
diffs[z] = diff
if len(diffs.index): c.Log(diffs.size)
And another confusing error:
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception.
To prevent the exception, ensure each parameter type matches those required by the Log method.
Please checkout the API documentation. at CoarseSelection in 32
TypeError : No method matches given arguments for Log (Open Stacktrace)
Huh? len() or .size ???? Expected any of these would work fine ...
c.Log(diffs) c.Log(len(diffs)) c.Log(diffs.size)
return list(diffs.index)

class SymbolData(object):
def __init__(self, symbol):
self.symbol = symbol
self.sma1 = SimpleMovingAverage(3)
self.sma2 = SimpleMovingAverage(10)
self.is_ready = False
def update(self, value):
self.is_ready = self.sma1.Update(value.EndTime, value.Price) and self.sma2.Update(value.EndTime, value.Price)