Hi I'm new to QuantConnect and I am building an RSI indicator from start with coarse selection.
However, after a years of backtest, it breaks and gives an error.
Runtime Error: Cannot implicitly convert type 'Python.Runtime.PyObject' to 'decimal' (Open Stacktrace)
I have attached my project here, any help would be appreciated :)
from customRSI import customRSI
class UncoupledVentralCoil(QCAlgorithm):
stopMarketTicket = None
stopMarketOrderFillTime = datetime.min
highestSPYPrice = 0
symbolsList = []
def Initialize(self):
self.SetStartDate(2000, 1, 1) # Set Start Date
self.SetEndDate(2020,1,1) #Set End Date
self.SetCash(100000) # Set Strategy Cash
# self.AddEquity("SPY", Resolution.Daily)
self.buylist = []
self.selllist = []
# self.Securities["SPY"].SetDataNormalizationMode(DataNormalizationMode.Raw)
# Create a chart for the indicator
crsiChart = Chart("Awesome", ChartType.Stacked)
crsiChart.AddSeries(Series('custom', SeriesType.Line))
self.coarse_count = 10
self.AddUniverse(self.CoarseSelectionFunction)
self.UniverseSettings.Resolution = Resolution.Daily
self.SetRiskManagement(TrailingStopRiskManagementModel(0.1))
def OnSecuritiesChanged(self, changes):
self.Debug(str(self.buylist))
for security in changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.1)
self.stopMarketTicket = self.StopMarketOrder(security.Symbol, -1.0 * self.CalculateOrderQuantity(security.Symbol, 0.1), 0.85 * self.Securities[security.Symbol].Close)
def CoarseSelectionFunction(self, coarse):
sym = sorted([x for x in coarse if x.HasFundamentalData and x.Price>0],
key = lambda x: x.DollarVolume, reverse = True)
syms = sym[:self.coarse_count]
for i in syms:
if not self.Securities.ContainsKey(i.Symbol):
self.symbolsList.append(i.Symbol)
for symbol in self.symbolsList:
if symbol not in self.buylist:
self.AddEquity(f"{symbol}", Resolution.Daily)
self.custom = customRSI(f"{symbol}")
self.RegisterIndicator(symbol, self.custom, Resolution.Daily)
if self.custom.Value < self.custom.h1:
self.buylist.append(symbol)
# if self.custom.Value > self.custom.h2:
# self.selllist.append(symbol)
return self.buylist
import pandas as pd
import numpy as np
import talib as ta
import math
from collections import deque
from datetime import datetime, timedelta
class customRSI(object):
def __init__(self, name):
self.Name = name
self.Time = datetime.min #minimum/earliest time, NOT MINUTE
self.cyclelen = 14
self.h1 = 30.0
self.h2 = 70.0
self.Value = 0
self.IsReady = False
self.queue = RollingWindow[float](self.cyclelen + 1) # +1 for difference cal
self.diffdeque = RollingWindow[float](self.cyclelen)
def __repr__(self):
return "{0} -> IsReady: {1}. Time: {2}. Value: {3}".format(self.Name, self.IsReady, self.Time, self.Value)
def Update(self, input):
#fill the queue
self.queue.Add(input.Close) # 15 day closing prices
# calc rsi
self.pricechange = self.change()
# initiate
count = self.queue.Count
self.Time = input.EndTime
self.IsReady = count == self.cyclelen + 1
# if its ready iniate these
if self.IsReady == True:
self.uplist = [0.0 if i < 0 else i for i in self.pricechange]
self.downlist = [0.0 if i > 0 else -i for i in self.pricechange]
self.rs = np.mean(self.uplist)/np.mean(self.downlist)
self.Value = rsi = 100.0 - 100.0 / (1 + self.rs)
else:
self.Value = 0
def change(self): # pine change fx
try:
self.diff = self.queue[0]-self.queue[1]
self.diffdeque.Add(self.diff)
except:
return
return self.diffdeque
Derek Melchin
Hi Lian,
In the code above, the custom indicator is registered to receive data updates but then over-written with each loop iteration in the `CoarseSelectionFunction`. To set things up properly, I recommend reviewing the EmaCrossUniverseSelectionModel and adding a manual warm-up for the indicator. Note that the universe selection function adds and removes data subscriptions automatically, so we shouldn't use `AddEquity` inside the selection method.
There are also several issues with the custom indicator implementation. To resolve these, refer to this related forum thread.
Best,
Derek Melchin
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.
Lian Sheng
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!