declare lower;
declare zerobase;
input Agg1 = AggregationPeriod.THREE_MIN;
input Agg2 = AggregationPeriod.FIVE_MIN;
input Agg3 = AggregationPeriod.FIFTEEN_MIN;
input Agg4 = AggregationPeriod.THIRTY_MIN;
plot AO = Average(hl2, 5) - Average(hl2, 34);
def A0 = if AO > AO[1] then 1 else if AO < AO[1] then 0 else A0[1];
plot AO1 = if IsNaN(close) then Double.NaN else Average(hl2(period = Agg1), 5) - Average(hl2(period = Agg1), 34);
def A1 = if AO1 > AO1[1] then 1 else if AO1 < AO1[1] then 0 else A1[1];
plot AO2 = if IsNaN(close) then Double.NaN else Average(hl2(period = Agg2), 5) - Average(hl2(period = Agg2), 34);
def A2 = if AO2 > AO2[1] then 1 else if AO2 < AO2[1] then 0 else A2[1];
plot AO3 = if IsNaN(close) then Double.NaN else Average(hl2(period = Agg3), 5) - Average(hl2(period = Agg3), 34);
def A3 = if AO3 > AO3[1] then 1 else if AO3 < AO3[1] then 0 else A3[1];
plot AO4 = if IsNaN(close) then Double.NaN else Average(hl2(period = Agg4), 5) - Average(hl2(period = Agg4), 34);
def A4 = if AO4 > AO4[1] then 1 else if AO4 < AO4[1] then 0 else A4[1];
plot Zero = 0;
#AO.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
AO.SetLineWeight(2);
AO.DefineColor("Up", Color.GREEN);
AO.DefineColor("Down", Color.RED);
AO.AssignValueColor(if A0 == 1 then AO.Color("Up") else AO.Color("Down"));
AO1.SetLineWeight(2);
AO1.DefineColor("Up1", Color.GREEN);
AO1.DefineColor("Down1", Color.RED);
AO1.AssignValueColor(if A1 == 1 then AO1.Color("Up1") else AO1.Color("Down1"));
AO2.SetLineWeight(2);
AO2.DefineColor("Up2", Color.GREEN);
AO2.DefineColor("Down2", Color.RED);
AO2.AssignValueColor(if A2 == 1 then AO2.Color("Up2") else AO2.Color("Down2"));
AO3.SetLineWeight(2);
AO3.DefineColor("Up3", Color.GREEN);
AO3.DefineColor("Down3", Color.RED);
AO3.AssignValueColor(if A3 == 1 then AO3.Color("Up3") else AO3.Color("Down3"));
AO4.SetLineWeight(2);
AO4.DefineColor("Up4", Color.GREEN);
AO4.DefineColor("Down4", Color.RED);
AO4.AssignValueColor(if A4 == 1 then AO4.Color("Up4") else AO4.Color("Down4"));
Zero.SetDefaultColor(Color.LIGHT_GRAY);
input bubbles = yes;
def bubblemover = 1;
def mover = bubbles and IsNaN(close[bubblemover]) and !IsNaN(close[bubblemover + 1]);
AddChartBubble(mover, AO[bubblemover + 1], GetAggregationPeriod() / 60000 , if A0[bubblemover + 1] == 1 then AO.Color("Up") else AO.Color("Down"), no);
AddChartBubble(bubbles and IsNaN(close[bubblemover + 1]) and !IsNaN(close[bubblemover + 1 + 1]), AO1[bubblemover + 1 + 1], Agg1 / 60000 , if A1[bubblemover + 1 + 1] == 1 then AO1.Color("Up1") else AO1.Color("Down1"));
AddChartBubble(bubbles and IsNaN(close[bubblemover + 2]) and !IsNaN(close[bubblemover + 1 + 2]), AO2[bubblemover + 1 + 2], Agg2 / 60000 , if A2[bubblemover + 1 + 2] == 1 then AO2.Color("Up2") else AO2.Color("Down2"));
AddChartBubble(bubbles and IsNaN(close[bubblemover + 3]) and !IsNaN(close[bubblemover + 1 + 3]), AO3[bubblemover + 1 + 3], Agg3 / 60000 , if A3[bubblemover + 1 + 3] == 1 then AO3.Color("Up3") else AO3.Color("Down3"));
AddChartBubble(bubbles and IsNaN(close[bubblemover + 4]) and !IsNaN(close[bubblemover + 1 + 4]), AO4[bubblemover + 1 + 4], Agg4 / 60000 , if A4[bubblemover + 1 + 4] == 1 then AO4.Color("Up4") else AO4.Color("Down4"));
input labels = yes;
AddLabel(labels, GetAggregationPeriod() / 60000 + "m", if A0 == 1 then AO.Color("Up") else AO.Color("Down"));
AddLabel(labels, Agg1 / 60000 + "m", if A1 == 1 then AO1.Color("Up1") else AO1.Color("Down1"));
AddLabel(labels, Agg2 / 60000 + "m", if A2 == 1 then AO2.Color("Up2") else AO2.Color("Down2"));
AddLabel(labels, Agg3 / 60000 + "m", if A3 == 1 then AO3.Color("Up3") else AO3.Color("Down3"));
AddLabel(labels, Agg4 / 60000 + "m", if A4 == 1 then AO4.Color("Up4") else AO4.Color("Down4"));
#