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