# ----------------------------------------------------------------
EQUITIES = ['AAPL','TSLA']; CRYPTOS = ['BTCUSD', 'ETHUSD']; CFDS = ['WTICOUSD']; RSI_PERIOD = 14;
# ----------------------------------------------------------------
class IndicatorForEquitiesAndCryptos(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) #Set Start Date
self.SetEndDate(2021, 2, 1) #Set End Date
self.SetCash(100000)
self.equities = [self.AddEquity(ticker, Resolution.Daily).Symbol for ticker in EQUITIES]
self.cryptos = [self.AddCrypto(ticker, Resolution.Daily).Symbol for ticker in CRYPTOS]
self.cfds = [self.AddCfd(ticker, Resolution.Daily).Symbol for ticker in CFDS]
self.assets = self.equities + self.cryptos + self.cfds
self.rsi = {}
self.current = {}
self.entry_price = {}
self.highestPrice = {}
for sec in self.assets:
self.rsi[sec] = self.RSI(sec, RSI_PERIOD, MovingAverageType.Simple, Resolution.Daily)
self.current[sec] = None
self.entry_price[sec] = None
self.highestPrice[sec] = 0
#self.SetWarmUp(RSI_PERIOD + 1, Resolution.Daily)
def OnData(self, data):
#if self.IsWarmingUp: return
# self.Log(self.assets)
count = 0
for sec in self.assets:
#self.Plot("RSI", sec, self.rsi[sec].Current.Value)
self.current[sec] = self.Securities[sec].Close
if self.current[sec] == None: continue
#trading conditions, only when we are not invested
if self.rsi[sec].Current.Value > 60:
self.Debug("RSI is more than 60")
if not self.Securities[sec].Invested:
self.SetHoldings(sec, 0.1) #invested 10% of our capital
self.entry_price[sec] = self.current[sec]
self.Debug(str(sec) + " Entry at: " +str(self.entry_price[sec]))
#exit conditions, only if we are invested
if self.Securities[sec].Invested:
if self.current[sec] <= self.entry_price[sec] - (self.entry_price[sec] * 0.05): #cut loss at 5%
self.Liquidate(sec, "stop-loss")
self.Debug(str(sec) + " stopped out at: " + str(self.current[sec]))
elif self.current[sec] >= self.entry_price[sec] + (self.entry_price[sec]*0.1): # set tp > 10%
self.Liquidate(sec, "take-profit")
self.Debug(str(sec) + " Tp at: "+ str(self.current[sec]))
count = count + 1
self.Debug("Count at " + str(count))
Hi, any ideas why is this algorithm executing twice a day? once at 00:00 hrs and another 19:00 hrs
Vladimir
Piggyinu
Thank you for choosing my framework for your algorithm.
Here is "RSI for equities, cryptos and cfds with Stop Loss and Take Profit",
less expensive, and also trades no more than once a day.
If you are satisfied with my answer, please accept it and don't forget to like it
Piggyinu
wow Vladimir You are a Genius! Thank you so much once again!😊
Piggyinu
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!