# macd_mtf_buysell_00e_upper
# https://usethinkscript.com/threads/assemble-in-one-indicator-1-2-3-4-5-minutes-macds.13297/
#mtf macd 5 times
#main chart arrows
#assemble in one indicator 1-2-3-4-5 minutes MACDs
## Five-Timeframe Aggregate MACD for tonydl
## Coded by Chemmy for usethinkscript.com
declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
input showline2 = no;
input showline3 = no;
input showline4 = no;
input showline5 = no;
## Aggregations
input agg1 = AggregationPeriod.MIN;
input agg2 = AggregationPeriod.TWO_MIN;
input agg3 = AggregationPeriod.THREE_MIN;
input agg4 = AggregationPeriod.FOUR_MIN;
input agg5 = AggregationPeriod.FIVE_MIN;
## Values and Averages
def Value1 = MovingAverage(averageType, close(period = agg1), fastLength) - MovingAverage(averageType, close(period = agg1), slowLength);
def Avg1 = MovingAverage(averageType, Value1, MACDLength);
def Value2 = MovingAverage(averageType, close(period = agg2), fastLength) - MovingAverage(averageType, close(period = agg2), slowLength);
def Avg2 = MovingAverage(averageType, Value1, MACDLength);
def Value3 = MovingAverage(averageType, close(period = agg3), fastLength) - MovingAverage(averageType, close(period = agg3), slowLength);
def Avg3 = MovingAverage(averageType, Value1, MACDLength);
def Value4 = MovingAverage(averageType, close(period = agg4), fastLength) - MovingAverage(averageType, close(period = agg4), slowLength);
def Avg4 = MovingAverage(averageType, Value1, MACDLength);
def Value5 = MovingAverage(averageType, close(period = agg5), fastLength) - MovingAverage(averageType, close(period = agg5), slowLength);
def Avg5 = MovingAverage(averageType, Value1, MACDLength);
## Diffs
def Diff1 = Value1 - Avg1;
def Diff2 = Value2 - Avg2;
def Diff3 = Value3 - Avg3;
def Diff4 = Value4 - Avg4;
def Diff5 = Value5 - Avg5;
#Diff1.SetHiding(!showline2);
#Diff2.SetHiding(!showline2);
#Diff3.SetHiding(!showline3);
#Diff4.SetHiding(!showline4);
#Diff5.SetHiding(!showline5);
def fulldiff = (Diff1 + Diff2 + Diff3 + Diff4 + Diff5) / 5;
def valavg = (Value1 + Value2 + Value3 + Value4 + Value5) / 5;
def fullavg = (Avg1 + Avg2 + Avg3 + Avg4 + Avg5) / 5;
def ZeroLine = 0;
## Signals
def UpSignal = if fulldiff crosses above ZeroLine then ZeroLine else Double.NaN;
def DownSignal = if fulldiff crosses below ZeroLine then ZeroLine else Double.NaN;
#UpSignal.SetHiding(!showBreakoutSignals);
#DownSignal.SetHiding(!showBreakoutSignals);
#valavg.SetDefaultColor(GetColor(1));
#fullavg.SetDefaultColor(GetColor(8));
#Diff1.SetDefaultColor(GetColor(5));
#Diff1.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#Diff1.SetLineWeight(3);
#Diff1.DefineColor("Positive and Up", Color.GREEN);
#Diff1.DefineColor("Positive and Down", Color.DARK_GREEN);
#Diff1.DefineColor("Negative and Down", Color.RED);
#Diff1.DefineColor("Negative and Up", Color.DARK_RED);
#Diff1.AssignValueColor(if Diff1 >= 0 then if Diff1 > Diff1[1] then Diff1.Color("Positive and Up") else Diff1.Color("Positive and Down") else if Diff1 < Diff1[1] then Diff1.Color("Negative and Down") else Diff1.Color("Negative and Up"));
#fulldiff.SetDefaultColor(GetColor(5));
#fulldiff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#fulldiff.SetLineWeight(3);
#fulldiff.DefineColor("Positive and Up", Color.GREEN);
#fulldiff.DefineColor("Positive and Down", Color.DARK_GREEN);
#fulldiff.DefineColor("Negative and Down", Color.RED);
#fulldiff.DefineColor("Negative and Up", Color.DARK_RED);
#fulldiff.AssignValueColor(if fulldiff >= 0 then if fulldiff > fulldiff[1] then fulldiff.Color("Positive and Up") else fulldiff.Color("Positive and Down") else if fulldiff < fulldiff[1] then fulldiff.Color("Negative and Down") else fulldiff.Color("Negative and Up"));
#ZeroLine.SetDefaultColor(GetColor(0));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#UpSignal.SetLineWeight(3);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#DownSignal.SetLineWeight(3);
#-------------------------------------------
def na = double.nan;
def bn = barnumber();
input back = 1;
def m1 = if value1 > value1[back] then 1
else if value1 < value1[back] then -1
else 0;
def m2 = if value2 > value2[back] then 1
else if value2 < value2[back] then -1
else 0;
def m3 = if value3 > value3[back] then 1
else if value3 < value3[back] then -1
else 0;
def m4 = if value4 > value4[back] then 1
else if value4 < value4[back] then -1
else 0;
def m5 = if value5 > value5[back] then 1
else if value5 < value5[back] then -1
else 0;
input exit = 3;
#def longclose = (m1 + m2 + m3 + m4 + m5 <= exit);
#def shortclose = (m1 + m2 + m3 + m4 + m5 >= -exit);
#def longopen = (m1 + m2 + m3 + m4 + m5 == 5);
#def shortopen = (m1 + m2 + m3 + m4 + m5 == -5);
# def longopen = (longopen[1] != 5) and (m1 + m2 + m3 + m4 + m5 == 5);
def msum = (m1 + m2 + m3 + m4 + m5);
def longclose = (msum[1] > exit) and (msum <= exit);
def shortclose = (msum[1] < -exit) and (msum >= -exit);
def longopen = (msum[1] != 5) and (msum == 5);
def shortopen = (msum[1] != -5) and (msum == -5);
# save a buy price level
# use to tell if in trade, >0
# compare value to close to see if trade is stopped for loss
# compare value to close to see if trade is stopped for gain
def longbuy = if bn == 1 then 0
else if longclose then 0
else if shortopen then 0
else if close < longbuy[1] * 0.999 then 0
else if close > (longbuy[1] * 1.02) then 0
else if longopen then close
else longbuy[1];
def shortbuy = if bn == 1 then 0
else if shortclose then 0
else if longopen then 0
else if close > shortbuy[1] * 1.001 then 0
else if close < (shortbuy[1] * 0.98 ) then 0
else if shortopen then close
else shortbuy[1];
def long = if bn == 1 then 0
else if longclose then 0
else if longopen then 1
else long[1];
def short = if bn == 1 then 0
else if shortclose then 0
else if shortopen then 1
else short[1];
# avg the long n short trades.. stretch them out , remove all the small blips
def trade = if bn ==1 then 0
else if long then 1
else if short then -1
else trade[1];
#--------------------------------
# lower plots
#plot zm1 = m1;
#plot zm2 = m2+2.2;
#plot zm3 = m3+4.4;
#plot zm4 = m4+6.6;
#plot zm5 = m5+8.8;
#plot r = 0;
#plot y1 = longopen+12;
#plot y2 = longclose+10;
#plot y3 = shortopen+8;
#plot y4 = shortclose+6;
#y1.SetDefaultColor(Color.green);
#y2.SetDefaultColor(Color.cyan);
#y3.SetDefaultColor(Color.red);
#y4.SetDefaultColor(Color.yellow);
#plot k = 14;
#k.SetDefaultColor(Color.white);
#k.setlineweight(3);
input vertlines = no;
addverticalline(vertlines and longopen, "-" , color.green);
addverticalline(vertlines and longclose, "-" , color.red);
addverticalline(vertlines and shortopen, "-" , color.cyan);
addverticalline(vertlines and shortclose, "-" , color.yellow);
#plot b = if longbuy > 0 then longbuy else na;
#plot s = if shortbuy > 0 then shortbuy else na;
#plot z1 = value1;
#plot z2 = value2;
#plot z3 = value3;
#plot z4 = value4;
#plot z5 = value5;
addchartbubble(0, -6,
msum + "\n" +
long + " L\n" +
short + " S\n" +
longopen + " LO\n" +
longclose + " LC\n" +
shortopen + " SO\n" +
shortclose + " SC\n"
, color.yellow, yes);
#plot zl = long+18;
#plot zs = short+16;
#zl.SetDefaultColor(Color.green);
#zs.SetDefaultColor(Color.red);
#plot t = trade+2;
#t.SetDefaultColor(Color.cyan);
#plot u = 2;
#u.SetDefaultColor(Color.white);
#u.setlineweight(2);
#--------------------------
# upper plots
# arrows don't line up? maybe a 2nd agg quirk
plot long_enter = if trade == 1 and trade[1] != 1 then low else na;
long_enter.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
long_enter.SetDefaultColor(Color.green);
long_enter.setlineweight(3);
long_enter.hidebubble();
plot long_exit = if trade != 1 and trade[1] == 1 then high else na;
long_exit.SetPaintingStrategy(PaintingStrategy.ARROW_down);
long_exit.SetDefaultColor(Color.red);
long_exit.setlineweight(3);
long_exit.hidebubble();
#------------------------------
def n =1000;
def h = highest(high, n);
def l = lowest(low, n);
def rng = h-l;
def mid = l + (rng/2);
# plot a pulse signal , representing the buy/sell trade signal
# scale and super impose on top of candles
# used highest, instead of highestall() , so study wouldn't become complex.
# len of 1000 causes signal to shift vertically at times
input show_trade_pulse_signal = yes;
plot zt = if show_trade_pulse_signal then (mid + (trade * (rng/8))) else na;
zt.AssignValueColor(if trade > 0 then color.green else color.red);
zt.setlineweight(2);
#