Back

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

System.InvalidCastException: Specified cast is not valid.

OR

Runtime Error: Specified cast is not valid.

Why?

[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.SetStartDate(2017,10,1)
self.SetEndDate(2017,11,1)
self.SetCash(100000)
self.averages = {}
self.AddUniverse(self.CoarseSelection)

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 main.py:line 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)
'''
c.Log(diffs)
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)

Update Backtest







garyha, I would advice you to avoid using pandas.DataFrame and pandas.Series for such simple filtering and sorting operations. It is slower and consumes more memory than simple list operations:

def CoarseSelection(c, coarse):

# Filter coarse fundamental universe by price range and has fundamental data
coarse = [ cf for cf in coarse if cf.Price > .5 and cf.Price < .6 and cf.HasFundamentalData ]
# get the 99 smallest coarse fundamental universe by price
coarse = sorted(coarse, key = lambda cf: cf.Price)[:99]

diff = {}

for cf in coarse:
symbol = cf.Symbol
if symbol not in c.averages:
c.averages[symbol] = SymbolData(symbol)
c.averages[symbol].update(cf)
sma1 = c.averages[symbol].sma1.Current.Value
sma2 = c.averages[symbol].sma2.Current.Value
diff[symbol] = sma1 - sma2

return list(diff.keys())

It is also an known issue that using Symbol objects as pandas.Index doesn't work as we would expect, therefore you needed to convert it into str `z = str(sym)`. At this point, you have a string and this method should return a list of Symbol... 

In your code snippet, there is also a question about an exception thrown my this statement:
`c.Log(diffs.size)`:
The Log method accepts string or a non-native python object: it means that int, float, double, etc must be passed as a string: `c.Log(str(diffs.size))` or `c.Log(f"{diffs.size}")`.

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.


I like that first line for trimming coarse, simple:

        coarse = [ s for s in coarse if s.Price > 5.00 and s.Price < 10.00 and s.HasFundamentalData ]

A little more info on the error 'Specified cast is not valid'.

My return looked like ['DCIX UTEYWBKJIC11', 'ZBB TTK3TR0978MD', 'SYMX TXB02K4DOLLX']

In Alexandre's code using a dictionary, the symbol variable prior to it looks like DCIX UTEYWBKJIC11 but then those apparently land as keys in the dictionary like <QuantConnect.Symbol object at 0x7f2335874a58>, pointers or references.

0

The issue is that z = str(sym) will return the string value of the Symbol object: "DCIX UTEYWBKJIC11", for example. 
list(diffs.index) is a list of string where it should be a list of Symbol. 

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