Thanks Pete for explaining about recursion.
With that code, I was finally able to create my triggers in a STUDY using SlowRSI, MoneyFlowIndex and MACD Crossovers.
declare lower;
input emaLength = 6;
input rsiLength = 14;
input over_bought = 80;
input over_sold = 20;
input mfilength = 14;
input movingAvgLength = 1;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input crossingType = {default “Positive to Negative”, “Negative to Positive”};
def ema = ExpAverage(close, emaLength);
def netChgAvg = WildersAverage(close – ema, rsiLength);
def totChgAvg = WildersAverage(AbsValue(close – ema), rsiLength);
def chgRatio = if totChgAvg != 0 then netChgAvg / totChgAvg else 0;
def SlowRSI = 50 * (chgRatio + 1);
def OverBought = over_bought;
def MiddleLine = 50;
def OverSold = over_sold;
def MFI = Average(moneyflow(high, close, low, volume, mfilength), movingAvgLength);
def Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
#Conditions for Long Trigger
def SlowRSI_OverSold = SlowRSI <= OverSold;
rec SlowRSI_OverSold_Trigger = CompoundValue(1, if SlowRSI_OverSold then 1 else if SlowRSI_OverSold_Trigger[1] and SlowRSI < MiddleLine then 1 else 0,0);
def MFI_OverSold = MFI <= OverSold;
rec MFI_OverSold_Trigger = CompoundValue(1, if MFI_OverSold then 1 else if MFI_OverSold_Trigger[1] and MFI < MiddleLine then 1 else 0,0);
def MACD_Long_Trigger = crosses(Diff, 0, crossingType == crossingType.”Positive to Negative”);
plot Long_Trigger = SlowRSI_OverSold_Trigger and MFI_OverSold_Trigger and MACD_Long_Trigger;
#Conditions for Short Trigger
def SlowRSI_OverBought = SlowRSI >= OverBought;
rec SlowRSI_OverBought_Trigger = CompoundValue(1, if SlowRSI_OverBought then 1 else if SlowRSI_OverBought_Trigger[1] and SlowRSI > MiddleLine then 1 else 0,0);
def MFI_OverBought = MFI >= OverBought;
rec MFI_OverBought_Trigger = CompoundValue(1, if MFI_OverBought then 1 else if MFI_OverBought_Trigger[1] and MFI > MiddleLine then 1 else 0,0);
def MACD_Short_Trigger = crosses(Diff, 0, crossingType == crossingType.”Negative to Positive”);
plot Short_Trigger = SlowRSI_OverBought_Trigger and MFI_OverBought_Trigger and MACD_Short_Trigger;
Short_Trigger.SetDefaultColor(color.YELLOW);
However, now I’m having difficulty turning this into a useable automated STRATEGY.
You eluded to this in your answer, “This code will NOT work in Study Alerts or Conditional Orders.”
Is this why I’m unable to turn this Study into a Strategy?
Any help is much appreciated.
You say you had trouble converting this to a Strategy. But you did not describe what sort of trouble you had. I was able to convert it to a Strategy by adding two lines of code, removing the declare lower statement and converting the plots to defs.
Here are the two lines of code I added:
AddOrder(OrderType.BUY_AUTO, open[-1], name = ”Long Entry $” + open[-1]);
AddOrder(OrderType.SELL_AUTO, open[-1], name = ”LongEntry $” + open[-1]);