Hello all. I'm a very new coder/trader, and am attempting to learn through some YouTube videos.
def Initialize(self):
self.SetStartDate(2017, 11, 18)
self.SetEndDate(2023, 11, 18)
self.SetCash(100000)
self.AddRiskManagement(TrailingStopRiskManagementModel(0.05))
tickerList = ['GNRC', 'ABNB', 'MWW', 'PLL', 'CRL', 'DO', 'TDC', 'BG', 'KRFT', 'MNK', 'MI', 'XLNX', 'FL', 'WRB', 'DISCK', 'PANW', 'MGM', 'STE', 'SBAC', 'TSO', 'ATI', 'SWKS', 'CVG', 'MHK', 'COL', 'TLAB', 'TSG', 'HOT', 'QTRN', 'DG', 'TRB', 'APOL', 'URBN', 'ALLE', 'EXR','DLR', 'CHTR', 'GR', 'CEG', 'CFG', 'KORS', 'V', 'KDP', 'SRCL', 'DTV', 'XRX', 'QEP', 'TDG', 'GOOGL', 'AA', 'MHS', 'DXCM', 'CARR', 'SLM', 'DRE', 'CPRI', 'AMD', 'TDY', 'JNS', 'VRTX', 'TRGP', 'NKTR', 'PCL', 'NBL', 'IT', 'PAYC', 'CVH', 'LXK', 'CSGP', 'DFS', 'CHK', 'ABS', 'EMC', 'SIAL', 'TIF', 'TSCO', 'AAP', 'CFN', 'AME', 'CELG', 'PDCO', 'FRX', 'POOL', 'KVUE', 'UHS', 'TSS', 'POM', 'ESS', 'CTXS', 'UAL', 'UA', 'TFX', 'LEG', 'NCR', 'PCP', 'IDXX', 'MOH', 'XYL', 'AV', 'ABMD', 'LVS', 'DV', 'CERN', 'SWN', 'FFIV', 'FOX', 'SYF', 'BIG', 'WU', 'BIO', 'TYL', 'CDAY', 'BHF', 'NSM', 'RX', 'EVHC', 'WIN', 'SAI', 'TTWO', 'AVP', 'R', 'ACE', 'CVC', 'MRNA', 'SGP', 'FDS', 'IEX', 'KSS', 'FMC', 'LDW', 'ITT', 'CNC', 'VIAB', 'VTR', 'SIG', 'MSCI', 'GRMN', 'LSI', 'DF', 'SLE', 'S', 'WLP', 'ABBV', 'MTD', 'GPN', 'TRMB', 'Q', 'STJ', 'LYV', 'TWTR', 'CPRT', 'PCS', 'GMCR', 'CTLT', 'KEYS', 'CLF', 'ACAS', 'CPWR', 'TMC', 'NBR', 'HNZ', 'BCR', 'PSX', 'FANG', 'X', 'FLR', 'SE', 'COG', 'LM', 'MOS', 'OMX', 'AGN', 'TSLA', 'URI', 'EQIX', 'FRT', 'LUMN', 'MJN', 'RMD', 'BRCM', 'EP', 'REG', 'SNI', 'DYN', 'FSR', 'SCG', 'EQT', 'ADT', 'ANF', 'M', 'NWSA', 'BMS', 'HRB', 'HUBB', 'HP', 'STZ', 'DNR', 'ETFC', 'PODD', 'NWL', 'LDOS', 'FSLR', 'WB', 'FICO', 'AN', 'KBH', 'KSE', 'CBE', 'PYPL', 'BBBY', 'SVU', 'ENDP', 'AET', 'MUR', 'SPLS', 'PTC', 'WYN', 'SLR', 'JOYG', 'NWS', 'GNW', 'MXIM', 'ANDV', 'TER', 'AMG', 'INTU', 'ADS', 'NOV', 'INCY', 'CPGX', 'FLIR', 'DXC', 'YHOO', 'NDSN', 'BXLT', 'CTX', 'SBL', 'DLPH', 'HCBK', 'BJS', 'HSIC', 'BR', 'DPZ', 'REGN', 'JEF', 'SNDK', 'PBI', 'RSH', 'FB', 'ARG', 'ROST', 'CXO', 'ALK', 'GENZ', 'PCLN', 'CSRA', 'MPC', 'MNST']
for ticker in tickerList:
self.AddEquity(ticker, Resolution.Hour)
self.change = self.AddData(SPXChange, "change", Resolution.Hour).Symbol
def OnData(self, data: Slice):
if self.change in data:
targets = []
for ticker in data[self.change].Added:
if ticker in data:
targets.append(PortfolioTarget(ticker, 0.1))
for ticker in data[self.change].Removed:
if ticker in data:
targets.append(PortfolioTarget(ticker, -0.05))
self.SetHoldings(targets)
class SPXChange(PythonData):
def GetSource(self, config, date, isLive):
source = "https://www.dropbox.com/s/ksi5liwjja8...?dl=1"
return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile)
def Reader(self, config, line, date, isLive):
if not (line.strip() and line[0].isdigit()):
return None
data = line.split(',')
change = SPXChange()
try:
change.Symbol = config.Symbol
change.Time = datetime.strptime(data[0], "%Y-%m-%d") + timedelta(hours=10)
change.Value = 0
change["Added"] = str(data[1]).split(" ")
change["Removed"] = str(data[2]).split(" ")
except ValueError:
return None
return change
There is my code, however when I attempt to backtest it does not trade or really do anything.
Does anyone know what my issue might be?
Mia Alissi
Based on the provided code, there are a few potential issues that might be causing your algorithm not to execute trades:
Custom Data Source: You are using a custom data source from Dropbox. If the data source is not available or the data is not correctly formatted, your algorithm will not be able to make any trades. Make sure that the data source is available and the data is correctly formatted.
Data Reader: In your custom data class
SPXChange
, you are trying to parse the data in theReader
method. If there is an error in this method, it could cause your algorithm not to receive any data. Make sure that the data in your source file is correctly formatted and can be correctly parsed by yourReader
method.OnData Method: In your
OnData
method, you are checking ifself.change
is indata
. Ifself.change
is not indata
, your algorithm will not make any trades. Make sure thatself.change
is correctly set up and that it is included in your data slice.SetHoldings Method: You are using the
SetHoldings
method with a list ofPortfolioTarget
objects. If there is an error in the creation of these objects, it could cause your algorithm not to make any trades. Make sure that thePortfolioTarget
objects are correctly created.Without more information or the ability to run the code, it's hard to say exactly what the issue might be. I would recommend checking these potential issues and see if any of them are causing the problem. If none of these are the issue, you might want to ask for help on the QuantConnect forum, where other users might be able to provide more specific help.
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.
Oliver Bennett
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!