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