9/28 EMA pullback entry strategy script

jte78

New member
New member here and highly impressed with the knowledge. I’m a long time user of ToS but only starting to gain knowledge of the coding side of things. I’m gaining expertise with indicator settings for custom setups and moving into the strategy and script side. Basically I know most anything you can imagine can be written, just don’t know how to do it yet. I’ve seen “almost” what I’m seeking using the search function, but have found it as an indicator vs a strategy. I’d like to be able to backtest the P/L and thus am looking for a strategy script. I’ve traded manually this way for a while but tend to take profits early. While it works ok I miss larger runs out of conservative tendencies. So the script I’m hoping for exits at the reverse crossover.

Inputs desired-
1. Upon the 9EMA crossing the 28EMA, wait for a pullback to the 9EMA and enter long or short appropriately. Could also wait for a pullback to the longer EMA, but this is my go-to as it provides more trades.
2. Exit is at the crossing of the 9/28 EMA in the opposite direction.

In the photo below-crossing occurs at the green arrow. Entry occurs at the green arrow. Exit occurs off screen when the 9ema crosses below the 28ema.

My goal is to gain knowledge how to manipulate this for a few other settings I trade with. ie- enter at the 28 vs 9, exit settings based on tick move vs ema cross, etc.
.
IMG_6245.jpeg

Thanks for any guidance
 
Last edited by a moderator:
Solution
New member here and highly impressed with the knowledge. I’m a long time user of ToS but only starting to gain knowledge of the coding side of things. I’m gaining expertise with indicator settings for custom setups and moving into the strategy and script side. Basically I know most anything you can imagine can be written, just don’t know how to do it yet. I’ve seen “almost” what I’m seeking using the search function, but have found it as an indicator vs a strategy. I’d like to be able to backtest the P/L and thus am looking for a strategy script. I’ve traded manually this way for a while but tend to take profits early. While it works ok I miss larger runs out of conservative tendencies. So the script I’m hoping for exits at the...
New member here and highly impressed with the knowledge. I’m a long time user of ToS but only starting to gain knowledge of the coding side of things. I’m gaining expertise with indicator settings for custom setups and moving into the strategy and script side. Basically I know most anything you can imagine can be written, just don’t know how to do it yet. I’ve seen “almost” what I’m seeking using the search function, but have found it as an indicator vs a strategy. I’d like to be able to backtest the P/L and thus am looking for a strategy script. I’ve traded manually this way for a while but tend to take profits early. While it works ok I miss larger runs out of conservative tendencies. So the script I’m hoping for exits at the reverse crossover.

Inputs desired-
1. Upon the 9EMA crossing the 28EMA, wait for a pullback to the 9EMA and enter long or short appropriately. Could also wait for a pullback to the longer EMA, but this is my go-to as it provides more trades.
2. Exit is at the crossing of the 9/28 EMA in the opposite direction.

In the photo below-crossing occurs at the green arrow. Entry occurs at the green arrow. Exit occurs off screen when the 9ema crosses below the 28ema.

My goal is to gain knowledge how to manipulate this for a few other settings I trade with. ie- enter at the 28 vs 9, exit settings based on tick move vs ema cross, etc.
.View attachment 22476
Thanks for any guidance
my simple suggestion is to use the already available knowledge in the site in the first place by using the search feature, looking for example for "pullback" or "crossing" and checking the available comments and coding you will find. In that way you can easily find some useful information you can put into practice. Of course, patience and persistence are key. good luck
 
New member here and highly impressed with the knowledge. I’m a long time user of ToS but only starting to gain knowledge of the coding side of things. I’m gaining expertise with indicator settings for custom setups and moving into the strategy and script side. Basically I know most anything you can imagine can be written, just don’t know how to do it yet. I’ve seen “almost” what I’m seeking using the search function, but have found it as an indicator vs a strategy. I’d like to be able to backtest the P/L and thus am looking for a strategy script. I’ve traded manually this way for a while but tend to take profits early. While it works ok I miss larger runs out of conservative tendencies. So the script I’m hoping for exits at the reverse crossover.

Inputs desired-
1. Upon the 9EMA crossing the 28EMA, wait for a pullback to the 9EMA and enter long or short appropriately. Could also wait for a pullback to the longer EMA, but this is my go-to as it provides more trades.
2. Exit is at the crossing of the 9/28 EMA in the opposite direction.

In the photo below-crossing occurs at the green arrow. Entry occurs at the green arrow. Exit occurs off screen when the 9ema crosses below the 28ema.

My goal is to gain knowledge how to manipulate this for a few other settings I trade with. ie- enter at the 28 vs 9, exit settings based on tick move vs ema cross, etc.
.
Thanks for any guidance

this did poor with almost all of the stocks i looked at.

here is a strategy, to experiment with.
load the floatingpl strategy, to see the profits/losses.

i made it so you can pick when to 'buy', on rule 1 or 2 or 3. default is 3.
can show vertical lines for the 3 rules.
can turn off long trades or short trades.

Long rules
i added rule 2
rule 1 - 9EMA crosses above 28EMA
rule 2 - first time low is greater than 9EMA (price has to go up before it can fall and pullback)
rule 3 - low crosses below 9EMA (pullback)

it exits the trade if,
..9EMA crosses below 28EMA
..or if high crosses below 9EMA.

long trades are green, short trades are red.
short trades follows rules opposite of the long rules.


Code:
#avg_cross_pullback2_strat

#https://usethinkscript.com/threads/9-28-ema-pullback-entry-strategy-script.19297/
#9/28 EMA pullback entry strategy script

#1. Upon the 9EMA crossing the 28EMA, wait for a pullback to the 9EMA and enter long or short appropriately. Could also wait for a pullback to the longer EMA, but this is my go-to as it provides more trades.
#2. Exit is at the crossing of the 9/28 EMA in the opposite direction.

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

input buy_rule = {one, two, default three };
input show_long_trades = yes;
input show_short_trades = yes;
input show_vertical_lines = no;

addlabel(1, " ", color.black);
addlabel(1, "Buy rule: " + buy_rule, color.yellow);

input avg1_type = AverageType.EXPONENTIAL;
#input avg1_type = AverageType.Simple;
input avg1_length = 9;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

input avg2_type = AverageType.EXPONENTIAL;
input avg2_length = 28;
def avg2 = MovingAverage(avg2_type, data, avg2_length );

def xup = avg1 crosses above avg2;
def xdwn = avg1 crosses below avg2;

input show_avg1_line = yes;
input show_avg2_line = yes;
plot zavg1 = if show_avg1_line then avg1 else na;
plot zavg2 = if show_avg2_line then avg2 else na;
zavg1.SetDefaultColor(Color.CYAN);
zavg1.SetLineWeight(1);
zavg1.HideBubble();
zavg2.SetDefaultColor(Color.YELLOW);
zavg2.SetLineWeight(1);
zavg2.HideBubble();

#--------------------------------
# rules -  long
def longr1 = xup;
def longr2 = low > avg1;
def longr3 = low crosses below avg1;

def longexit = xdwn;
def longcancel = high < avg1;

def longseq = if bn == 1 then 0
else if longseq[1] > 0 and longcancel then 0
else if longseq[1] == 3 and longexit then 0
else if longseq[1] == 0 and longr1 then 1
else if longseq[1] == 1 and longr2 then 2
else if longseq[1] == 2 and longr3 then 3
else longseq[1];

def lvert = show_long_trades and show_vertical_lines;
AddVerticalLine(lvert and longseq[1] == 0 and longseq == 1, "Long  1 ", Color.CYAN);
AddVerticalLine(lvert and longseq[1] == 1 and longseq == 2, "Long  2 ", Color.cyan);
AddVerticalLine(lvert and longseq[1] == 2 and longseq == 3, "Long  3 ", Color.cyan);
AddVerticalLine(lvert and longseq[1] == 3 and longseq == 0, "Long  sell ", Color.blue);


#--------------------------------
# rules - short
def shortr1 = xdwn;
def shortr2 = high < avg1;
def shortr3 = high crosses above avg1;

def shortexit = xup;
def shortcancel = low > avg1;

def shortseq = if bn == 1 then 0
else if shortseq[1] > 0 and shortcancel then 0
else if shortseq[1] == 3 and shortexit then 0
else if shortseq[1] == 0 and shortr1 then 1
else if shortseq[1] == 1 and shortr2 then 2
else if shortseq[1] == 2 and shortr3 then 3
else shortseq[1];

def svert = show_short_trades and show_vertical_lines;
AddVerticalLine(svert and shortseq[1] == 0 and shortseq == 1, "Short  1 ", Color.magenta);
AddVerticalLine(svert and shortseq[1] == 1 and shortseq == 2, "Short  2 ", Color.magenta);
AddVerticalLine(svert and shortseq[1] == 2 and shortseq == 3, "Short  3 ", Color.magenta);
AddVerticalLine(svert and shortseq[1] == 3 and shortseq == 0, "Short  sell ", Color.yellow);

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


def longbuyx;
def shortbuyx;
switch (buy_rule) {
case one:
 longbuyx = longseq[1] == 0 and longseq == 1;
 shortbuyx = shortseq[1] == 0 and shortseq == 1;
case two:
 longbuyx = longseq[1] == 1 and longseq == 2;
 shortbuyx = shortseq[1] == 1 and shortseq == 2;
case three:
 longbuyx = longseq[1] == 2 and longseq == 3;
 shortbuyx = shortseq[1] == 2 and shortseq == 3;
}


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

# place order
#AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "buy");
AddOrder(OrderType.BUY_to_open, show_long_trades and longbuyx, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "open");
AddOrder(OrderType.SELL_to_close, show_long_trades and (longexit or longcancel ), tickcolor = Color.green, arrowcolor = Color.green, name = "close");

AddOrder(OrderType.sell_to_open, show_short_trades and shortbuyx, tickcolor = Color.red, arrowcolor = Color.red, name = "open");
AddOrder(OrderType.buy_to_close, show_short_trades and (shortexit or shortcancel), tickcolor = Color.red, arrowcolor = Color.red, name = "close");
#
 

Attachments

  • 00c.JPG
    00c.JPG
    73.7 KB · Views: 99
Solution

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