blconnell308
New member
I need an If then statement to set the BuySignal and SellSignal depending on which way the EMA is moving. I want to Go Long (BuySignal) if EMA is up and use BuySignal to get out of the sell if EMA is going down. Here is the code I'm using. I cannot seem to get it right and looking for some expert advice.
input iEMALength = 9;
input iEMA_SMALength = 9;
input iEMA_LMALength = 5;
# EMA or EMA Crossovers
def EMA = MovAvgExponential(CLOSE, iEMALength, 0);
def EMA_MA1 = MovAvgExponential(EMA, iEMA_SMALength, 0);
def EMA_MA2 = MovAvgExponential(EMA_MA1, iEMA_LMALength, 0);
# PLOT EMA ON CHART
plot pEMA = EMA;
pEMA.SetDefaultColor(Color.Gray);
plot pEMA_MA1 = EMA_MA1;
pEMA_MA1.SetDefaultColor(Color.Green);
plot pEMA_MA2 = EMA_MA2;
peMA_MA2.SetDefaultColor(Color.Yellow);
# EMA Logic
def LbEMA = EMA_MA1 crosses above EMA_MA2;
def LsEMA = EMA_MA1 crosses below EMA_MA2;
def LongEMA = EMA_MA1 > EMA_MA2;
def ShortEMA = EMA_MA1 < EMA_MA2;
# Heiken Ashi Logic
def xClose = ( open + high + low + close ) / 4;
rec xOpen = CompoundValue( 1, ( xOpen[1] + xClose[1] ) / 2, xClose );
def Bullish = if ( xClose - xOpen ) >= 0 then yes else no;
def Bearish = if ( xClose - xOpen ) <= 0 then yes else no;
def BullishChange = if ( xClose - xOpen ) >= 0 and ( xClose[1] - xOpen[1] ) <= 0 then yes else no;
def BearishChange = if ( xClose - xOpen ) <= 0 and ( xClose[1] - xOpen[1] ) >= 0 then yes else no;
#--- DISPLAY INFORMATION ---
#Long
#def BuySignal = (LongEMA and BullishChange) or (LBEma and Bullish);
#def SellSignal = (LongEMA and BearishChange) or (LSEma);
#Short
def SellSignal = (ShortEMA and BearishChange) or (LSEma and Bearish);
def BuySignal = (ShortEMA and BullishChange) or (LBEma);
input iEMALength = 9;
input iEMA_SMALength = 9;
input iEMA_LMALength = 5;
# EMA or EMA Crossovers
def EMA = MovAvgExponential(CLOSE, iEMALength, 0);
def EMA_MA1 = MovAvgExponential(EMA, iEMA_SMALength, 0);
def EMA_MA2 = MovAvgExponential(EMA_MA1, iEMA_LMALength, 0);
# PLOT EMA ON CHART
plot pEMA = EMA;
pEMA.SetDefaultColor(Color.Gray);
plot pEMA_MA1 = EMA_MA1;
pEMA_MA1.SetDefaultColor(Color.Green);
plot pEMA_MA2 = EMA_MA2;
peMA_MA2.SetDefaultColor(Color.Yellow);
# EMA Logic
def LbEMA = EMA_MA1 crosses above EMA_MA2;
def LsEMA = EMA_MA1 crosses below EMA_MA2;
def LongEMA = EMA_MA1 > EMA_MA2;
def ShortEMA = EMA_MA1 < EMA_MA2;
# Heiken Ashi Logic
def xClose = ( open + high + low + close ) / 4;
rec xOpen = CompoundValue( 1, ( xOpen[1] + xClose[1] ) / 2, xClose );
def Bullish = if ( xClose - xOpen ) >= 0 then yes else no;
def Bearish = if ( xClose - xOpen ) <= 0 then yes else no;
def BullishChange = if ( xClose - xOpen ) >= 0 and ( xClose[1] - xOpen[1] ) <= 0 then yes else no;
def BearishChange = if ( xClose - xOpen ) <= 0 and ( xClose[1] - xOpen[1] ) >= 0 then yes else no;
#--- DISPLAY INFORMATION ---
#Long
#def BuySignal = (LongEMA and BullishChange) or (LBEma and Bullish);
#def SellSignal = (LongEMA and BearishChange) or (LSEma);
#Short
def SellSignal = (ShortEMA and BearishChange) or (LSEma and Bearish);
def BuySignal = (ShortEMA and BullishChange) or (LBEma);