# emax_buy_strat_00f
# https://usethinkscript.com/threads/need-help-w-ema20-strategy.13144/
# Need help w/ ema20 strategy
def bn = barnumber();
def na = double.nan;
def h = high;
def l = low;
def o = open;
def c = close;
def v = volume;
def diffday = if getday() != getday()[1] then 1 else 0;
def aggday = AggregationPeriod.DAY;
def day_o = open(period = aggday);
def day_c = close(period = aggday);
def day_h = high(period = aggday);
def day_l = low(period = aggday);
# 390 min in a day
#input chart_stat_labels = no;
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
#AddLabel(chart_stat_labels, "chartmin " + chartmin, Color.MAGENTA);
def daybarqty = roundup(390 / chartmin, 0);
#AddLabel(chart_stat_labels, "bars per day " + daybarqty, Color.MAGENTA);
# on 10 minute bars.
def desired_time = 10;
def isagg = (chartmin == desired_time);
addlabel(!isagg, " CHART TIME IS NOT THE DESIRED TIME OF " + desired_time, color.cyan);
# first 30 minutes
input start = 0930;
input stop = 1600;
input t1 = 1500;
input t2 = 1000;
def firstbar = (secondsfromTime(start) == 0);
def first30 = if secondsfromTime(start) >= 0 and secondstillTime(t2) > 0 then 1 else 0;
# doesnt work
#def dayend = (secondsfromTime(stop) == 0);
def daylasthour = (secondsfromTime(t1) >= 0 and secondstillTime(stop) > 0);
def tradetime = (secondsfromTime(start) >= 0 and secondstillTime(t1) > 0);
def daytime = (secondsfromTime(start) >= 0 and secondstillTime(stop) > 0);
input ma1_len = 20;
input ma1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, close, ma1_len);
input show_lines = yes;
plot zma1 = if show_lines then ma1 else na;
zma1.setdefaultcolor(getcolor(1));
#zma1.setlineweight(1);
zma1.hidebubble();
def greenbar = close > open;
def redbar = close < open;
#---------------------------
# Here it is for bullish trades (inverse for bearish):
#Qualifiers:
# 1. Today's opening price > previous day closing price
def long_q1 = day_o > day_c[1];
# 2. Price within the first 3 bars (30 minutes) cannot be < or = to EMA (20)
def long_q2 = if !first30 then long_q2[1]
else if (first30 and (close <= ma1)) then 0
else 1;
# dayend doesnt work
# 3. At least 2 of the first 3 bars must be green (closing price > opening price)
def long_q3cnt = if firstbar and greenbar then 1
else if diffday then 0
else if first30 and greenbar then long_q3cnt[1] + 1
else long_q3cnt[1];
def long_q3 = (long_q3cnt >= 2);
input test_q_dots = yes;
input dot_size = 1;
def z1 = .006;
#plot q1 = if test_qs and long_q1 then (high*(1+(1*z1))) else na;
plot q1 = if test_q_dots then (day_h*(1+(1*z1))) else na;
q1.SetPaintingStrategy(PaintingStrategy.POINTS);
q1.AssignValueColor(if long_q1 then Color.magenta else color.dark_gray);
#q1.SetDefaultColor(Color.magenta);
q1.setlineweight(dot_size);
q1.hidebubble();
#plot q2 = if test_qs and long_q2 then (high*(1+(2*z1))) else na;
plot q2 = if test_q_dots then (day_h*(1+(2*z1))) else na;
q2.SetPaintingStrategy(PaintingStrategy.POINTS);
q2.AssignValueColor(if long_q2 then Color.cyan else color.dark_gray);
#q2.SetDefaultColor(Color.cyan);
q2.setlineweight(dot_size);
q2.hidebubble();
#plot q3 = if test_qs and long_q3 then (high*(1+(3*z1))) else na;
plot q3 = if test_q_dots then (day_h*(1+(3*z1))) else na;
q3.SetPaintingStrategy(PaintingStrategy.POINTS);
q3.AssignValueColor(if long_q3 then Color.orange else color.dark_gray);
#q3.SetDefaultColor(Color.orange);
q3.setlineweight(dot_size);
q3.hidebubble();
def long_q = (long_q1 and long_q2 and long_q3);
plot lq = if test_q_dots and tradetime then (day_h*(1+(4*z1))) else na;
lq.SetPaintingStrategy(PaintingStrategy.POINTS);
lq.AssignValueColor(if long_q then Color.blue else color.dark_gray);
#lq.SetDefaultColor(Color.orange);
lq.setlineweight(dot_size);
lq.hidebubble();
addchartbubble(0, low,
long_q1 + " Q1\n" +
long_q2 + " Q2\n" +
long_q3 + " Q3\n" +
long_q3cnt + " cnt\n" +
long_q + " Q"
, (if diffday then color.yellow else color.gray), no);
#Entry Point:
#EMA(20) > today's opening price
def long_e1 = (tradetime and long_q and (ma1 > day_o));
# find first bar that long_e1 is true
def long_e1b = if (!long_e1[1] and long_e1) then 1 else 0;
plot le = if test_q_dots and tradetime then (day_h*(1+(5*z1))) else na;
le.SetPaintingStrategy(PaintingStrategy.POINTS);
le.AssignValueColor(if long_e1 then Color.yellow else color.dark_gray);
#le.SetDefaultColor(Color.orange);
le.setlineweight(dot_size);
le.hidebubble();
# many bars
def long_en = (tradetime and long_e1);
# first bar , to buy
def long_buy_en = (tradetime and long_e1b);
input buy_bubbles = yes;
# buy-1
#Buy 1 unit when price is .15 (15 cents) > EMA(20)
input buy1_level = 0.15;
def long_b1 = ((long_buy_en and (close + buy1_level) > ma1 ));
# save the close price when a buy
def long_b1_pr = if bn == 1 then 0
else if diffday then 0
else if long_b1 then round(close, 2)
else long_b1_pr[1];
plot zb1 = if long_b1 then low*0.995 else na;
zb1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#zb1.SetPaintingStrategy(PaintingStrategy.POINTS);
zb1.AssignValueColor(if long_b1 then Color.green else color.dark_gray);
#zb1.SetDefaultColor(Color.orange);
zb1.setlineweight(dot_size);
zb1.hidebubble();
addchartbubble(buy_bubbles and long_b1, low *0.99,
"Buy1\n" + long_b1_pr, color.green, no);
# buy-2
# ? only after a buy1?
#Buy 1 unit when price = EMA(20)
def b2_tol = 0.1;
def b2 = (100*absvalue(close - ma1)/ma1) <= b2_tol;
def long_b2 = (tradetime and long_e1b and b2);
plot zb2 = if long_b2 then low*0.993 else na;
zb2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#zb2.SetPaintingStrategy(PaintingStrategy.POINTS);
zb2.AssignValueColor(if long_b2 then Color.cyan else color.dark_gray);
#zb2.SetDefaultColor(Color.orange);
zb2.setlineweight(dot_size);
zb2.hidebubble();
# buy-3
#Buy 1 unit when price is .15 (15 cents) < EMA(20)
#No entry in final 60 minutes
#======================================
#Exit Point:
#X = [ High of Day (HOD) – EMA (20) @ time of entry ] / 2
def long_b1_ema = if bn == 1 then 0
else if long_b1 then ma1
else long_b1_ema[1];
# bad number, too small
def x = (day_h - long_b1_ema)/2;
#def x = day_h - (day_h - long_b1_ema)/2;
# just pick some factor number, for a gain sell level
#def x = day_h * 0.996;
input test_lines_day_ema = no;
plot zzd = if test_lines_day_ema then day_h else na;
plot zzb1ema = if (!test_lines_day_ema or long_b1_ema == 0) then na else long_b1_ema;
zzd.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zzd.SetDefaultColor(Color.gray);
zzd.hidebubble();
zzb1ema.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zzb1ema.SetDefaultColor(Color.magenta);
zzb1ema.hidebubble();
#plot zzx = if (!test_lines or long_b1_ema == 0) then na else x;g
#zzx.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zzx.SetDefaultColor(Color.green);
#zzx.hidebubble();
#def stop_en = (daytime and long_e1);
def stop_en = if diffday then na else if long_b1_pr > 0 then 1 else na;
#Stop Loss:
#Stop Price = [ EMA(20) @ time of entry - X ] - .15 (15 cents)
input stoploss_offset = -0.15;
#def stoploss = (long_b1_ema - x) + stoploss_offset;
# chg stoploss, to use close at buy, instead of ema $$
def stoploss = ((long_b1_pr - x) + stoploss_offset);
# use the buy ema level for a loss sell level
# zzb1ema.SetDefaultColor(Color.magenta);
#Profit
#Sell Half @ X + EMA(20) @ time of entry
#def stopgain1 = (long_b1_ema + x);
# chg stoploss, to use close at buy, instead of ema $$
def stopgain1 = (long_b1_pr + x);
#Sell Half @ [ X * 1.5 ] + EMA(20) @ time of entry
#def stopgain2 = (long_b1_ema + (x*1.5));
# chg stoploss, to use close at buy, instead of ema $$
def stopgain2 = (long_b1_pr + (x*1.5));
#if long_en then
input stop_lines = yes;
plot zstoploss = if (stop_lines and stop_en and long_b1_ema > 0) then stoploss else na;
plot zstopgain1 = if (stop_lines and stop_en and long_b1_ema > 0) then stopgain1 else na;
plot zstopgain2 = if (stop_lines and stop_en and long_b1_ema > 0) then stopgain2 else na;
zstoploss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zstoploss.SetDefaultColor(Color.red);
#zstoploss.hidebubble();
zstopgain1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zstopgain1.SetDefaultColor(Color.green);
#zstopgain1.hidebubble();
zstopgain2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zstopgain2.SetDefaultColor(Color.light_green);
#zstopgain2.hidebubble();
def long_stop1 = (close crosses below zstoploss);
def long_sell1 = (close crosses above zstopgain1);
def long_sell2 = (close crosses above zstopgain2);
def lstop1 = if long_stop1 then round(zstoploss - long_b1_pr, 2) else 0;
def lgain1 = if long_sell1 then round(zstopgain1 - long_b1_pr, 2) else 0;
def lgain2 = if long_sell2 then round(zstopgain2 - long_b1_pr, 2) else 0;
addchartbubble(long_stop1, low, "STOP SELL\n" + lstop1, color.red, no);
addchartbubble(long_sell1, high, "SELL1\n" + lgain1, color.cyan, yes);
addchartbubble(long_sell2, high, "SELL2\n" + lgain2, color.cyan, yes);
#Exit Notes
#If Profit Exit Point 1 is reached, stop loss quantity adjustment required
#Entire position must be closed @ 15:50:00 EST if profit/loss exit points have not been reached - no overnight positions
#