This is the first code I have tried to do in ThinkScript.
I am trying to create a Strategy that will perform the following label a Call order when the RSI Crosses above 30 and within 4 bars of the MACD Histogram crossing above 0. Then label a Sell_Call when the RSI Crosses above 70 or MACD Crossing below 0.
Also label a Put order when the RSI Crosses below 70 and within 4 bars of the MACD Histogram crossing below 0 . Then signal a Sell_Put when the RSI Crosses below 30 or MACD Crossing above 0.
Basically trying to combine the RSICrossover and the MACDHistogramCrossover so they cross within 0-4 Bars of each other and place a label on the chart for both Buy_Long and Close_Long, and Buy_Short and Close_Short. Normally the RSI crosses then the MACD follows for buy signals.
CODE:
#Strategy
#Alert a Call order when the RSI Crosses above 30 and within 4 bars of the MACDHistogram crosses above 0
# and Alert a Sell_Call when the RSI Crosses above 70 or MACD Crosses crosses below 0
#Alert a Put order when the RSI Crosses below 70 and within 4 bars of the MACDHistogram crosses below 0
# and Alert a Sell_Put when the RSI Crosses below 30 or MACD Crosses crosses above 0
#RSI Inputs
input rsiLength = 14;
#input crossingType = {default above, below};
#input threshold = 30;
input RSIAverageType = AverageType.WILDERS;
input RSIPrice = close;
input RSIOverBought = 70;
input RSIOverSold = 30;
#MACD Inputs
input MACDFastLength = 12;
input MACDSlowLength = 26;
input MACDLength = 9;
input MACDAverageType = AverageType.EXPONENTIAL;
def rsi = reference RSI(price = RSIPrice, length = rsiLength, averageType = RSIAverageType).rsi;
def macdcross = reference MACD(MACDFastLength, MACDSlowLength, MACDLength, MACDAverageType).Diff;
#Buy or Call
AddOrder(OrderType.BUY_AUTO, rsi crosses above RSIOverSold and macdcross crosses above 0, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Buy_Call");
AddOrder(OrderType.SELL_TO_CLOSE, rsi > RSIOverBought or macdcross crosses above 0, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Close_Call");
#Short or Put
AddOrder(OrderType.BUY_AUTO, rsi crosses below RSIOverBought and macdcross crosses below 0, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "Buy_Put");
AddOrder(OrderType.SELL_TO_CLOSE, rsi < RSIOverSold or macdcross crosses below 0, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Close_Put");
#Alerts
#Alert(OrderType.BUY_AUTO, " ", Alert.Bar, Sound.Chimes);
#Alert(OrderType.SELL_TO_CLOSE, " ", Alert.Bar, Sound.Bell);
#END
I am trying to create a Strategy that will perform the following label a Call order when the RSI Crosses above 30 and within 4 bars of the MACD Histogram crossing above 0. Then label a Sell_Call when the RSI Crosses above 70 or MACD Crossing below 0.
Also label a Put order when the RSI Crosses below 70 and within 4 bars of the MACD Histogram crossing below 0 . Then signal a Sell_Put when the RSI Crosses below 30 or MACD Crossing above 0.
Basically trying to combine the RSICrossover and the MACDHistogramCrossover so they cross within 0-4 Bars of each other and place a label on the chart for both Buy_Long and Close_Long, and Buy_Short and Close_Short. Normally the RSI crosses then the MACD follows for buy signals.
CODE:
#Strategy
#Alert a Call order when the RSI Crosses above 30 and within 4 bars of the MACDHistogram crosses above 0
# and Alert a Sell_Call when the RSI Crosses above 70 or MACD Crosses crosses below 0
#Alert a Put order when the RSI Crosses below 70 and within 4 bars of the MACDHistogram crosses below 0
# and Alert a Sell_Put when the RSI Crosses below 30 or MACD Crosses crosses above 0
#RSI Inputs
input rsiLength = 14;
#input crossingType = {default above, below};
#input threshold = 30;
input RSIAverageType = AverageType.WILDERS;
input RSIPrice = close;
input RSIOverBought = 70;
input RSIOverSold = 30;
#MACD Inputs
input MACDFastLength = 12;
input MACDSlowLength = 26;
input MACDLength = 9;
input MACDAverageType = AverageType.EXPONENTIAL;
def rsi = reference RSI(price = RSIPrice, length = rsiLength, averageType = RSIAverageType).rsi;
def macdcross = reference MACD(MACDFastLength, MACDSlowLength, MACDLength, MACDAverageType).Diff;
#Buy or Call
AddOrder(OrderType.BUY_AUTO, rsi crosses above RSIOverSold and macdcross crosses above 0, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Buy_Call");
AddOrder(OrderType.SELL_TO_CLOSE, rsi > RSIOverBought or macdcross crosses above 0, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Close_Call");
#Short or Put
AddOrder(OrderType.BUY_AUTO, rsi crosses below RSIOverBought and macdcross crosses below 0, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "Buy_Put");
AddOrder(OrderType.SELL_TO_CLOSE, rsi < RSIOverSold or macdcross crosses below 0, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Close_Put");
#Alerts
#Alert(OrderType.BUY_AUTO, " ", Alert.Bar, Sound.Chimes);
#Alert(OrderType.SELL_TO_CLOSE, " ", Alert.Bar, Sound.Bell);
#END
Last edited: