EMA Crossover strategy help

merc2226

Member
VIP
Can someone help me with this strategy, I can't find it on the site.

1. 7 and 13 ema crossover.
2 long only once candle clears 13, 48 and 200 ema
3 short only once candle clears 13, 48 and 200 ema
4 close once candle crosses over the 13 ema in the opposite direction of the current position.
5 open position again if all the above is met
6 no trades if candles are between the 13, 48, 200 ema.
7 close all positions at 4:00
8 open position at 9:30 or once above criteria is met.

Thank you
 
Solution
Can someone help me with this strategy, I can't find it on the site.

1. 7 and 13 ema crossover.
2 long only once candle clears 13, 48 and 200 ema
3 short only once candle clears 13, 48 and 200 ema
4 close once candle crosses over the 13 ema in the opposite direction of the current position.
5 open position again if all the above is met
6 no trades if candles are between the 13, 48, 200 ema.
7 close all positions at 4:00
8 open position at 9:30 or once above criteria is met.

Thank you


here is my guess on your rules.
can choose to close trades at the end of the day or not.
i used used open[-1] to initiate an order, so they happen on the next bar, from finished, confirmed signals.

this is a strategy

Code:
#...
Can someone help me with this strategy, I can't find it on the site.

1. 7 and 13 ema crossover.
2 long only once candle clears 13, 48 and 200 ema
3 short only once candle clears 13, 48 and 200 ema
4 close once candle crosses over the 13 ema in the opposite direction of the current position.
5 open position again if all the above is met
6 no trades if candles are between the 13, 48, 200 ema.
7 close all positions at 4:00
8 open position at 9:30 or once above criteria is met.

Thank you


here is my guess on your rules.
can choose to close trades at the end of the day or not.
i used used open[-1] to initiate an order, so they happen on the next bar, from finished, confirmed signals.

this is a strategy

Code:
# strat_rules_emas13_48_200

#https://usethinkscript.com/threads/ema-crossover-strategy-help.15621/

#1. 7 and 13 ema crossover.
#2 long only once candle clears 13, 48 and 200 ema
#3 short only once candle clears 13, 48 and 200 ema
#4 close once candle crosses over the 13 ema in the opposite direction of the current position.
#5 open position again if all the above is met
#6 no trades if candles are between the 13, 48, 200 ema.
#7 close all positions at 4:00
#8 open position at 9:30 or once above criteria is met.


def na = Double.NaN;
def bn = BarNumber();
def price = close;

#-------------------
# lastbar_of_day
# https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
# Finding the First and Last Bar of the day in ThinkorSwim
# korygill  Aug 23, 2019  
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBarOfDay[-1]) then 1 else 0;
#-------------------

input avg_type =  AverageType.EXPONENTIAL;
input ema1_len = 7;
input ema2_len = 13;
input ema3_len = 48;
input ema4_len = 200;
def ema7 = MovingAverage(avg_type, price, ema1_len);
def ema13 = MovingAverage(avg_type, price, ema2_len);
def ema48 = MovingAverage(avg_type, price, ema3_len);
def ema200 = MovingAverage(avg_type, price, ema4_len);

#-------------------
# Long
def long_open1 = ema7 crosses above ema13;
def long_open2 = close > ema13 and close > ema48 and close > ema200;

def long_close1 = close crosses below ema13;

def long_openx = long_open1 and long_open1;
def long_closex = long_close1;

#-------------------
# short
def short_open1 = ema7 crosses below ema13;
def short_open2 = close < ema13 and close < ema48 and close < ema200;

def short_close1 = close crosses above ema13;

def short_openx = short_open1 and short_open1;
def short_closex = short_close1;

#-------------------

def long_trade = if bn == 1 then 0
 else if long_closex then 0
 else if long_openx then 1
 else long_trade[1];

def short_trade = if bn == 1 then 0
 else if short_closex then 0
 else if short_openx then 1
 else short_trade[1];


input close_trades_on_last_bar_of_day = yes;
def long_buy = !long_trade[1] and long_trade;
def long_sell = (long_trade[1] and !long_trade) or (close_trades_on_last_bar_of_day and lastBarOfDay);

def short_buy = !short_trade[1] and short_trade;
def short_sell = (short_trade[1] and !short_trade) or (close_trades_on_last_bar_of_day and lastBarOfDay);


input show_long_trades = yes;
input show_short_trades = yes;

input trade_size = 1;
addOrder(OrderType.BUY_TO_OPEN, show_long_trades and long_buy, open[-1], trade_size, color.green, color.green, "LONG");
addOrder(OrderType.SELL_TO_CLOSE, show_long_trades and long_sell, open[-1], trade_size, color.green, color.green, "close");

addOrder(OrderType.SELL_TO_open, show_short_trades and short_buy, open[-1], trade_size, color.red, color.red, "SHORT");
addOrder(OrderType.buy_to_close, show_short_trades and short_sell, open[-1], trade_size, color.red, color.red, "SHORT");


#AddOrder ( type, condition, price, tradeSize, tickColor, arrowColor, name); 


#-------------------
# test stuff


input show_avg_lines = no;
plot z1 = if show_avg_lines then ema7 else na;
plot z2 = if show_avg_lines then ema13 else na;
plot z3 = if show_avg_lines then ema48 else na;
plot z4 = if show_avg_lines then ema200 else na;
z1.SetDefaultColor(Color.cyan);
z2.SetDefaultColor(Color.yellow);
z3.SetDefaultColor(Color.yellow);
z4.SetDefaultColor(Color.yellow);

#-------------

input show_long_vert_signal_lines = no;
addverticalline(show_long_vert_signal_lines and long_openx, "Long buy", color.green);
addverticalline(show_long_vert_signal_lines and long_closex, "Long sell", color.cyan);

input show_short_vert_signal_lines = no;
addverticalline(show_short_vert_signal_lines and short_openx, "short buy", color.red);
addverticalline(show_short_vert_signal_lines and short_closex, "short sell", color.yellow);

#-------------

input test_first_last_bubbles = no;
AddChartBubble(firstBarOfDay and test_first_last_bubbles, close,
    "First Bar of Day",
    Color.GREEN, yes);

AddChartBubble(lastBarOfDay and test_first_last_bubbles, close,
    "Last Bar of Day",
    Color.GREEN, no);

#

jSU3UgG.jpg



----------------

some thoughts to help you (and others) in future posts
if you use the correct terms when creating code rules, it will make more sense to you and others and be easier to create a study. write all the long rules together, then all the short rules.

1. 7 and 13 ema crossover.
..when is rule #1 supposed to be applied? before #2 and before #3? any others?
..crossover in which direction, for long and short?


2 long only once candle clears 13, 48 and 200 ema
..saying 'only once' , at first sounds like you want something to happen only one time.
..'clears' isn't a price movement
..instead, say what you mean.
....close > ema13 and close > ema48 and close > ema200


3 short only once candle clears 13, 48 and 200 ema
..close < .....


4 close once candle crosses over the 13 ema in the opposite direction of the current position.
..don't combine 2 rules in 1 line.
..write out each rule.


5 open position again if all the above is met
6 no trades if candles are between the 13, 48, 200 ema.
..if the conditions are true, the previous rules will happen. don't need to write this.


7 close all positions at 4:00
..it is unrealistic to try to close a trade at the time the day closes.
..maybe during the last bar of the day, would be doable.


8 open position at 9:30 or once above criteria is met.
..? why have rules if you are going to blindly open a position at 9:30?
..do you mean to trade only during normal hours?
 
Solution
Thank you so much for this and the help with the terms to help you with the codes.
I appreciate all the help you guys provide on the site. see my answers and 2 examples below.
I hope I cleared some of my request up.

I use it for day trading and don't hold any positions overnight so that is why open and close at the start and end of the trading session.

1. 7 and 13 ema crossover.
..when is rule #1 supposed to be applied? before #2 and before #3? any others?
..crossover in which direction, for long and short?
if candle closes above all 4 ema's, the 7,13, 48 and 200 then a position would be opened, if the candle is above all 4 ema's a call would be purchased, if it closes below all 4 ema's a put would be purchased.
If candle is between all 4 then no trade is open.
The trade is then closed as soon as the candle crosses the 13 ema.
see picture, my candles are gray when the candles are not above or below all of the ema's.
the arrows represent the 7 and 13 ema cross, that is my indication to open or close my position.
the arrow could trigger when the candle is still in between the ema's if that is the case it is opened as soon as it closes above or below all 4, then closed when the 7 and 13 ema cross.
if the trading session opens and all the criteria's are met that would open a new position at the start of the day, lets say I would open the position at 9:32.
I hope they is clearer and can help fill in the missing pieces.
thank you so much
Screen Shot 2023-05-30 at 7.50.24 AM.png


Screen Shot 2023-05-30 at 7.56.39 AM.png
 
Thank you so much for this and the help with the terms to help you with the codes.
I appreciate all the help you guys provide on the site. see my answers and 2 examples below.
I hope I cleared some of my request up.

I use it for day trading and don't hold any positions overnight so that is why open and close at the start and end of the trading session.

1. 7 and 13 ema crossover.
..when is rule #1 supposed to be applied? before #2 and before #3? any others?
..crossover in which direction, for long and short?
if candle closes above all 4 ema's, the 7,13, 48 and 200 then a position would be opened, if the candle is above all 4 ema's a call would be purchased, if it closes below all 4 ema's a put would be purchased.
If candle is between all 4 then no trade is open.
The trade is then closed as soon as the candle crosses the 13 ema.
see picture, my candles are gray when the candles are not above or below all of the ema's.
the arrows represent the 7 and 13 ema cross, that is my indication to open or close my position.
the arrow could trigger when the candle is still in between the ema's if that is the case it is opened as soon as it closes above or below all 4, then closed when the 7 and 13 ema cross.
if the trading session opens and all the criteria's are met that would open a new position at the start of the day, lets say I would open the position at 9:32.
I hope they is clearer and can help fill in the missing pieces.
thank you so much
View attachment 18787


View attachment 18789
hi...interesting Strat for scalping.... question for you - does the 7/13 crossover needs to happen above 48 and 200 ema or it can happen below and the price move above all emas after that and u take the trade? because in my manual back testing, there are a lot of fake-outs in 2mins and not much move. Thanks
 
hi...interesting Strat for scalping.... question for you - does the 7/13 crossover needs to happen above 48 and 200 ema or it can happen below and the price move above all emas after that and u take the trade? because in my manual back testing, there are a lot of fake-outs in 2mins and not much move. Thanks
if above and all signed up calls, if below and all signed up in order puts, if in between no play.
 
here is my guess on your rules.
can choose to close trades at the end of the day or not.
i used used open[-1] to initiate an order, so they happen on the next bar, from finished, confirmed signals.

this is a strategy

Code:
# strat_rules_emas13_48_200

#https://usethinkscript.com/threads/ema-crossover-strategy-help.15621/

#1. 7 and 13 ema crossover.
#2 long only once candle clears 13, 48 and 200 ema
#3 short only once candle clears 13, 48 and 200 ema
#4 close once candle crosses over the 13 ema in the opposite direction of the current position.
#5 open position again if all the above is met
#6 no trades if candles are between the 13, 48, 200 ema.
#7 close all positions at 4:00
#8 open position at 9:30 or once above criteria is met.


def na = Double.NaN;
def bn = BarNumber();
def price = close;

#-------------------
# lastbar_of_day
# https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
# Finding the First and Last Bar of the day in ThinkorSwim
# korygill  Aug 23, 2019 
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBarOfDay[-1]) then 1 else 0;
#-------------------

input avg_type =  AverageType.EXPONENTIAL;
input ema1_len = 7;
input ema2_len = 13;
input ema3_len = 48;
input ema4_len = 200;
def ema7 = MovingAverage(avg_type, price, ema1_len);
def ema13 = MovingAverage(avg_type, price, ema2_len);
def ema48 = MovingAverage(avg_type, price, ema3_len);
def ema200 = MovingAverage(avg_type, price, ema4_len);

#-------------------
# Long
def long_open1 = ema7 crosses above ema13;
def long_open2 = close > ema13 and close > ema48 and close > ema200;

def long_close1 = close crosses below ema13;

def long_openx = long_open1 and long_open1;
def long_closex = long_close1;

#-------------------
# short
def short_open1 = ema7 crosses below ema13;
def short_open2 = close < ema13 and close < ema48 and close < ema200;

def short_close1 = close crosses above ema13;

def short_openx = short_open1 and short_open1;
def short_closex = short_close1;

#-------------------

def long_trade = if bn == 1 then 0
 else if long_closex then 0
 else if long_openx then 1
 else long_trade[1];

def short_trade = if bn == 1 then 0
 else if short_closex then 0
 else if short_openx then 1
 else short_trade[1];


input close_trades_on_last_bar_of_day = yes;
def long_buy = !long_trade[1] and long_trade;
def long_sell = (long_trade[1] and !long_trade) or (close_trades_on_last_bar_of_day and lastBarOfDay);

def short_buy = !short_trade[1] and short_trade;
def short_sell = (short_trade[1] and !short_trade) or (close_trades_on_last_bar_of_day and lastBarOfDay);


input show_long_trades = yes;
input show_short_trades = yes;

input trade_size = 1;
addOrder(OrderType.BUY_TO_OPEN, show_long_trades and long_buy, open[-1], trade_size, color.green, color.green, "LONG");
addOrder(OrderType.SELL_TO_CLOSE, show_long_trades and long_sell, open[-1], trade_size, color.green, color.green, "close");

addOrder(OrderType.SELL_TO_open, show_short_trades and short_buy, open[-1], trade_size, color.red, color.red, "SHORT");
addOrder(OrderType.buy_to_close, show_short_trades and short_sell, open[-1], trade_size, color.red, color.red, "SHORT");


#AddOrder ( type, condition, price, tradeSize, tickColor, arrowColor, name);


#-------------------
# test stuff


input show_avg_lines = no;
plot z1 = if show_avg_lines then ema7 else na;
plot z2 = if show_avg_lines then ema13 else na;
plot z3 = if show_avg_lines then ema48 else na;
plot z4 = if show_avg_lines then ema200 else na;
z1.SetDefaultColor(Color.cyan);
z2.SetDefaultColor(Color.yellow);
z3.SetDefaultColor(Color.yellow);
z4.SetDefaultColor(Color.yellow);

#-------------

input show_long_vert_signal_lines = no;
addverticalline(show_long_vert_signal_lines and long_openx, "Long buy", color.green);
addverticalline(show_long_vert_signal_lines and long_closex, "Long sell", color.cyan);

input show_short_vert_signal_lines = no;
addverticalline(show_short_vert_signal_lines and short_openx, "short buy", color.red);
addverticalline(show_short_vert_signal_lines and short_closex, "short sell", color.yellow);

#-------------

input test_first_last_bubbles = no;
AddChartBubble(firstBarOfDay and test_first_last_bubbles, close,
    "First Bar of Day",
    Color.GREEN, yes);

AddChartBubble(lastBarOfDay and test_first_last_bubbles, close,
    "Last Bar of Day",
    Color.GREEN, no);

#

jSU3UgG.jpg

I try to understand thinkscript even though I cannot produce it. Can someone please explain lines 48 and 58?

Line 48: long_openx = long_open1 and long_open1;
Why is long_open1 repeated twice in the statement?

Line 58: short_openx = short_open1 and short_open1;
Again, why is short_open1 needed twice in the statement?

Thank you in advance for anyone who can demystify this for me :)
 
Thank you so much for this and the help with the terms to help you with the codes.
I appreciate all the help you guys provide on the site. see my answers and 2 examples below.
I hope I cleared some of my request up.

I use it for day trading and don't hold any positions overnight so that is why open and close at the start and end of the trading session.

1. 7 and 13 ema crossover.
..when is rule #1 supposed to be applied? before #2 and before #3? any others?
..crossover in which direction, for long and short?
if candle closes above all 4 ema's, the 7,13, 48 and 200 then a position would be opened, if the candle is above all 4 ema's a call would be purchased, if it closes below all 4 ema's a put would be purchased.
If candle is between all 4 then no trade is open.
The trade is then closed as soon as the candle crosses the 13 ema.
see picture, my candles are gray when the candles are not above or below all of the ema's.
the arrows represent the 7 and 13 ema cross, that is my indication to open or close my position.
the arrow could trigger when the candle is still in between the ema's if that is the case it is opened as soon as it closes above or below all 4, then closed when the 7 and 13 ema cross.
if the trading session opens and all the criteria's are met that would open a new position at the start of the day, lets say I would open the position at 9:32.
I hope they is clearer and can help fill in the missing pieces.
thank you so much
View attachment 18787


View attachment 18789
Hey Merc2226, Can you post the code you use for the 2 examples you posted? Code for the EMA's with arrows and gray candles in between. Also, your strategy lists 4 EMA's 7, 13, 48, 200. I only see 3 EMA's in you example pics? Thanks in advance bud!
 
Last edited:
Last edited by a moderator:

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
539 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