order condition for RSI strategy with 3 inputs

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

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");
 

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");
 

Watchdogdaemon

New member
Hello, apologies if this has already been posted. I did search diligently trying to find answers but I am not sure what I am doing wrong. I am using a custom Buy order with OCO to try and trigger a buy order when the RSI crosses above the 60 and a sell order when the RSI crosses below either the 60 or 70.

Buy order script: RSI()."RSI" from 1 bars ago crosses above 60

Sell order script: RSI()."RSI" crosses below 70 or RSI()."RSI" crosses below 60

The buy order seems to be triggering when it should, however, the sell order is triggering 2 bars early and I have no explanation for it.
 

Similar threads

Top