#Connors RSI https://www.tradingmarkets.com/media/2012/CRHS.pdf?mkt_tok=3RkMMJWWfF9wsRonvavLZKXonjHpfsX57%2BosW6%2Bg38431UFwdcjKPmjr1YIGTsZ0dvycMRAVFZl5nRhQH%2BOaco5P6OZaGU6nSA%3D%3D
input size = 1000;
input riskTolerance = 0.1;
#risk management
def tradeSize = (size / close);
def costBasis = tradeSize * EntryPrice();
def difference = costBasis * riskTolerance;
def maxLoss = costBasis - difference;
def stopLoss = (tradeSize * close) < maxLoss;
input rsiLength = 3;
input overBought = 70;
input overSold = 30;
input price = close;
input averageType = AverageType.WILDERS;
def uptrend = close > MovingAverage(AverageType.SIMPLE, close, 200);
# ConnorsRSI Indicator
input Price_RSI_Period = 3;
input Streak_RSI_Period = 2;
input Rank_Lookback = 100;
# Component 1: the RSI of closing price
#RSI(Close,3)
def priceRSI = reference RSI("price" = close, "length" = Price_RSI_Period, averageType = averageType);
# Component 2: the RSI of the streak
def upDay = if close > close[1] then 1 else 0;
def downDay = if close < close[1] then -1 else 0;
def upStreak = if upDay != 0 then upStreak[1] + upDay else 0;
def downStreak = if downDay != 0 then downStreak[1] + downDay else 0;
def streak = upStreak + downStreak;
def streakRSI = reference RSI("price" = streak, "length" = Streak_RSI_Period);
# Component 3: The percent rank of the current return
def ROC1 = close / close[1] - 1;
def rank = fold i = 1 to Rank_Lookback + 1 with r = 0 do
r + (GetValue(ROC1, i, Rank_Lookback) < ROC1) ;
def pctRank = (rank / Rank_Lookback) * 100 ;
# The final ConnorsRSI calculation, combining the three components
def ConnorsRSI = (priceRSI + streakRSI + pctRank) / 3;
#def buy = uptrend and connorsRsi < overSold;
def buy = connorsRsi < overSold;
def sell = close > EntryPrice() and connorsRsi > overBought;
AddOrder(OrderType.BUY_AUTO, buy[-1], close[-1], tradeSize = tradeSize, name = "oversold", Color.orange);
AddOrder(OrderType.SELL_TO_CLOSE, stopLoss or sell[-1], close[-1], tickcolor = color.white, arrowcolor = color.white, name = "overbought");