order condition for RSI strategy with 3 inputs

S

SVL1967NC

New member
Hi,

Could you please help me to modify slightly standard RSI Strategy to include not 2 inputs but 3 inputs ( input over_bought = 70, input_neutral=50, input over_sold = 30) and to allow only one trade since crossover.
Buy only once if RSI crosses above any of these 3 levels and Sell only once when RSI crosses below any of these 3 levels .
In other words, you stay in long position without crossing below any intermediate levels ( 30, 50, 70), but reverse the position immediately if price crosses below any of these 3 levels ( 30, 50, 70) and vice versa.
I can not figure out how to script this condition.
Thanks in advance.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2013-2020
#

input price = close;
input length = 14;
input over_bought = 70;
input over_sold = 30;
input rsiAverageType = AverageType.WILDERS;

def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);

AddOrder(OrderType.BUY_AUTO, rsi crosses above over_sold, tickColor = GetColor(0), arrowColor = GetColor(0), name = "RSI_LE");
AddOrder(OrderType.SELL_AUTO, rsi crosses below over_bought, tickColor = GetColor(1), arrowColor = GetColor(1), name = "RSI_SE");
 
Last edited by a moderator:
YungTraderFromMontana

YungTraderFromMontana

Well-known member
Code:
input price = close;
input length = 14;
input over_bought = 70;
input neutral= 50;
input oversold = 30;
input rsiAverageType = AverageType.WILDERS;

def x = (RSI[1] < (overbought or neutral or oversold)) and (RSI > (overbought or neutral or oversold));
def y = (RSI[1] > (overbought or neutral or oversold)) and (RSI < (overbought or neutral or oversold));



def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);

AddOrder(OrderType.BUY_AUTO, x, tickColor = GetColor(0), arrowColor = GetColor(0), name = "RSI_LE");
AddOrder(OrderType.SELL_AUTO, y, tickColor = GetColor(1), arrowColor = GetColor(1), name = "RSI_SE");
 
S

SVL1967NC

New member
YungTraderFromMontana, many thanks for your prompt reply.

TOS highlighted these 2 lines in red "Expected Double":
def x = (RSI[1] < (overbought or neutral or oversold)) and (RSI > (overbought or neutral or oversold));
def y = (RSI[1] > (overbought or neutral or oversold)) and (RSI < (overbought or neutral or oversold));

I figured out myself through Global Strategy Setting by allowing up to 1 entry in the same direction regardless of the entry that generated the order. The below script works fine:

Code:
input price = close;
input length = 14;
input over_bought = 70;
input neutral= 50;
input over_sold = 30;

input rsiAverageType = AverageType.WILDERS;

def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);

AddOrder(OrderType.BUY_AUTO, rsi crosses above over_sold, tickColor = GetColor(0), arrowColor = GetColor(0), name = "RSI_LE");
AddOrder(OrderType.BUY_AUTO, rsi crosses above neutral, tickColor = GetColor(0), arrowColor = GetColor(0), name = "RSI_LE");
AddOrder(OrderType.BUY_AUTO, rsi crosses above over_bought, tickColor = GetColor(0), arrowColor = GetColor(0), name = "RSI_LE");

AddOrder(OrderType.SELL_AUTO, rsi crosses below over_bought, tickColor = GetColor(1), arrowColor = GetColor(1), name = "RSI_SE");
AddOrder(OrderType.SELL_AUTO, rsi crosses below neutral, tickColor = GetColor(1), arrowColor = GetColor(1), name = "RSI_SE");
AddOrder(OrderType.SELL_AUTO, rsi crosses below over_sold, tickColor = GetColor(1), arrowColor = GetColor(1), name = "RSI_SE");
 

Similar threads

Top