```
#TOS Indicators
#Home of the Volatility Box
#More info regarding this indicator here: tosindicators.com/indicators/market-pulse
#Code written in 2019
#Full Youtube Tutorial here: https://youtu.be/Hku6dLR-m_A
# Second aggregation period added by horserider 5/31/2020
input ChartBubblesOn = no;
input price = close;
input length = 10;
def tmp1 = if price > price[1] then price - price[1] else 0;
def tmp2 = if price[1] > price then price[1] - price else 0;
def d2 = sum(tmp1, length);
def d4 = sum(tmp2, length);
def cond = d2 + d4 == 0;
def ad3 = if cond then 0 else (d2 - d4) / (d2 + d4) * 100;
def coeff = 2 / (length + 1) * AbsValue(ad3) / 100;
def asd = compoundValue("visible data" = coeff * price + (if IsNaN(asd[1]) then 0 else asd[1]) * (1 - coeff), "historical data" = price
);
plot VMA = asd;
VMA.setDefaultColor(GetColor(0));
def vwma8 = sum(volume * close, 8) / sum(volume, 8);
def vwma21 = sum(volume * close, 21) / sum(volume, 21);
def vwma34 = sum(volume * close, 34) / sum(volume, 34);
def bullish = if vwma8 > vwma21 and vwma21 > vwma34 then 1 else 0;
def bearish = if vwma8 < vwma21 and vwma21 < vwma34 then 1 else 0;
def distribution = if !bullish and !bearish then 1 else 0;
AddLabel(yes, if bullish then "Stage: Acceleration" else if bearish then "Stage: Deceleration" else if close>=VMA then "Stage: Accumulation" else "Stage: Distribution", if bullish then color.green else if bearish then color.red else if close >=VMA then color.yellow else color.orange);
VMA.AssignValueColor(if bearish and close<= VMA then color.red
else if bullish and close >= VMA then color.green
else color.gray);
def accumulationToAcceleration = if (bullish and close>=VMA) then 1 else 0;
AddChartBubble(ChartBubblesOn and accumulationToAcceleration and !accumulationToAcceleration[1], close, "Entering Acceleration", color.green);
def distributionToDeceleration = if (bearish and close <= VMA) then 1 else 0;
AddChartBubble(ChartBubblesOn and distributionToDeceleration and !distributionToDeceleration[1], close, "Entering Deceleration", color.red);
######### Second aggregation
input agg1 = aggregationPeriod.DAY;
def tmp1agg1 = if close(period = Agg1) > close(period = Agg1)[1] then close(period = Agg1) - close(period = Agg1)[1] else 0;
def tmp2agg1 = if close(period = Agg1)[1] > close(period = Agg1) then close(period = Agg1)[1] - close(period = Agg1) else 0;
def d2agg1 = sum(tmp1agg1, length);
def d4agg1 = sum(tmp2agg1, length);
def condagg1 = d2agg1 + d4agg1 == 0;
def ad3agg1 = if condagg1 then 0 else (d2agg1 - d4agg1) / (d2agg1 + d4agg1) * 100;
def coeffagg1 = 2 / (length + 1) * AbsValue(ad3agg1) / 100;
def asdagg1 = compoundValue("visible data" = coeffagg1 * price + (if IsNaN(asdagg1[1]) then 0 else asdagg1[1]) * (1 - coeffagg1), "historical data" = price
);
plot VMAagg1 = asdagg1;
VMAagg1.setDefaultColor(GetColor(0));
def vwma8agg1 = sum(volume(period = Agg1) * close(period = Agg1), 8) / sum(volume(period = Agg1), 8);
def vwma21agg1 = sum(volume(period = Agg1) * close(period = Agg1), 21) / sum(volume(period = Agg1), 21);
def vwma34agg1 = sum(volume(period = Agg1) * close(period = Agg1), 34) / sum(volume(period = Agg1), 34);
def bullishagg1 = if vwma8agg1 > vwma21agg1 and vwma21agg1 > vwma34agg1 then 1 else 0;
def bearishagg1 = if vwma8agg1 < vwma21agg1 and vwma21agg1 < vwma34agg1 then 1 else 0;
def distributionagg1 = if !bullishagg1 and !bearishagg1 then 1 else 0;
AddLabel(yes, if bullishagg1 then "Stage: Acceleration" else if bearishagg1 then "Stage: Decelerationagg1" else if close(period = Agg1)>=VMAagg1 then "Stage: Accumulationagg1" else "Stage: Distributionagg1", if bullishagg1 then color.green else if bearishagg1 then color.red else if close(period = Agg1) >=VMAagg1 then color.yellow else color.orange);
VMAagg1.AssignValueColor(if bearishagg1 and close(period = Agg1)<= VMAagg1 then color.red
else if bullishagg1 and close(period = Agg1) >= VMAagg1 then color.green
else color.gray);
def accumulationToAccelerationagg1 = if (bullishagg1 and close(period = Agg1)>=VMAagg1) then 1 else 0;
AddChartBubble(ChartBubblesOn and accumulationToAccelerationagg1 and !accumulationToAccelerationagg1[1], close(period = Agg1), "Entering Accelerationagg1", color.green);
def distributionToDecelerationagg1 = if (bearishagg1 and close(period = Agg1) <= VMAagg1) then 1 else 0;
AddChartBubble(ChartBubblesOn and distributionToDecelerationagg1 and !distributionToDecelerationagg1[1], close(period = Agg1), "Entering Decelerationagg1", color.red);
```