Help with If then

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

this is a strategy , not a study

here is something to experiment with
i added addorder functtions

added bubbles to display values to help debug

Code:
#if_then_help_buy_strat

#https://usethinkscript.com/threads/help-with-if-then.20280/
#Help with If then
#blconnell308  Jan 18, 2025

#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 long_Buy = (LongEMA and BullishChange) or (LbEMA and Bullish);
def long_Sell = (LongEMA and BearishChange) or (LsEMA);

#Short
def short_Buy = (ShortEMA and BullishChange) or (LbEMA);
def short_Sell = (ShortEMA and BearishChange) or (LsEMA and Bearish);


input long_enable = no;
input short_enable = yes;
#AddOrder ( type , condition , price , tradeSize , tickColor , arrowColor , name );  

input trade_size = 1;
AddOrder(OrderType.BUY_TO_OPEN, long_enable and long_Buy, open[-1], tradeSize = trade_size, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "long buy");
AddOrder(OrderType.SELL_TO_CLOSE, long_enable and long_sell, open[-1], tradeSize = trade_size, tickcolor = Color.cyan, arrowcolor = Color.cyan, name = "long sell");


AddOrder(OrderType.sell_TO_OPEN, short_enable and short_Buy, open[-1], tradeSize = trade_size, tickcolor = Color.red, arrowcolor = Color.red, name = "short buy");
AddOrder(OrderType.buy_TO_CLOSE, short_enable and short_sell, open[-1], tradeSize = trade_size, tickcolor = Color.yellow, arrowcolor = Color.yellow, name = "short sell");


def y = 0.003;
input test_long = no;
#def long_Buy = (LongEMA and BullishChange) or (LBEma and Bullish);
AddChartBubble(test_long, low * (1-y),
 "long\n" +
 "buy\n" +
 (LongEMA and BullishChange) + "\n" +
 (LbEMA and Bullish) + "\n" +
 long_Buy + "\n" 
, (if long_Buy then Color.GREEN else Color.GRAY), no);


#def long_Sell = (LongEMA and BearishChange) or (LSEma);
AddChartBubble(test_long, low * (1-y),
 "long\n" +
 "sell\n" +
 (LongEMA and BearishChange) + "\n" +
 LsEMA + "\n" +
 long_Sell + "\n" 
, (if long_Sell then Color.CYAN else Color.GRAY), no);


input test_short = no;
#def short_Buy = (ShortEMA and BullishChange) or (LbEMA);
AddChartBubble(test_short, high * (1+y),
 "short\n" +
 "buy\n" +
 (ShortEMA and BullishChange) + "\n" +
 (LbEMA) + "\n" +
 short_Buy
, (if short_Buy then Color.red else Color.GRAY), yes);

#def short_Sell = (ShortEMA and BearishChange) or (LsEMA and Bearish);
AddChartBubble(test_short, high * (1+y),
 "short\n" +
 "sell\n" +
 (ShortEMA and BearishChange) + "\n" +
 (LsEMA and Bearish) + "\n" +
 short_Sell
, (if short_Sell then Color.yellow else Color.GRAY), yes);
#
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
222 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top