Hi all!
Actually trying to get the algos' programming logic, so I decided to build a very basic one but unfortunately, no trades are generated which is very frustrating:
Algo's Trading rule: Enter Long on TSLA if RSI > 51 and if RSI < 60. I'm pretty sure to miss something that might be an evidence for you, but I'm totally stuck with this.
I first thought that the RSI indicator wasn't "updated" but even when I write an algo that should trade if the stock's price is > X, it won't place any trades.
Also, if I can get this to work properly, I'll be able to understand who to manage orders, data and indicators, In other words, I would be able to build any strategies so your help would be really appreciated.
Cheers!
import numpy as np
class RSIalgo(QCAlgorithm):
def Initialize(self):
# Define a date range and strating capital for backtest
self.SetStartDate(2018, 6, 24) # Backtest starting date
self.SetEndDate(2018, 8, 24) # Last date = Today
self.SetCash(10000) # Define the initial capital
# Select the stock to trade
self.tesla = self.AddEquity("TSLA", Resolution.Daily)
def OnData(self, data):
# Make the algo work every days
self.Schedule.On(self.DateRules.EveryDay("SPY"))
# Calling the RSI Indicator
self.rsi = self.RSI("TSLA", 14, MovingAverageType.Simple, Resolution.Daily)
# If we are not invest in TSLA and RSI crosses above 30 go long
if not self.Portfolio["TSLA"].Invested:
if self.rsi > 51 and self.rsi < 60:
self.MarketOrder("TSLA", 22)
Gabriel Mercier
class RSIalgo(QCAlgorithm): def Initialize(self): # Define a date range and strating capital for backtest self.SetStartDate(2018, 6, 24) # Backtest starting date self.SetEndDate(2018, 8, 24) # Last date = Today self.SetCash(10000) # Define the initial capital # Select the stock to trade self.tesla = self.AddEquity("TSLA", Resolution.Daily) # Calling the RSI self.rsi = self.RSI("TSLA", 14) def OnData(self, data): # If we are not invest in TSLA and RSI is above 51 and below 60, go Long if not self.Portfolio["TSLA"].Invested: if self.rsi > 51 and self.rsi < 60: self.MarketOrder("TSLA", 22) if self.rsi > 80: self.Liquidate()
This is the code I wrote for the basic RSI algo, which is not working I don't know why.
When I execute this code, I get this error:
Runtime Error: TypeError : Cannot get managed object
at OnData in main.py:line 26
TypeError : Cannot get managed object (Open Stacktrace)
Gurumeher Sawhney
The reason as to why the TypeError occured is because the RelativeStrengthIndex object was being compared to a number. In order to get the value of the indicator, use indicator.Current.Value. The algorithm below has the necessary fix:
Gabriel Mercier
Thanks Gurumeher! It is working fine now!
Gabriel Mercier
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!