#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © LonesomeTheBlue
#study("Volume Multi Time Frame", "VMTF")
# Converted and mode by Sam4Cok@Samer800 - 11/2022
declare lower;
input ShowLines = yes;
input mode = {default "Auto", "User Defined"}; # "HTF Mode"
input ManualAgg = AggregationPeriod.DAY; # "Resolution (if HTF Mode is user-defined)"
def na = Double.NaN;
def agg = GetAggregationPeriod();
def day = GetDay();
def week = GetWeek();
def month = GetMonth();
def year = GetYear();
def oneMin = AggregationPeriod.MIN;
def fiveMin = AggregationPeriod.FIVE_MIN;
def fifttenMin = AggregationPeriod.FIFTEEN_MIN;
def hour = AggregationPeriod.HOUR;
def twoHr = AggregationPeriod.TWO_HOURS;
def fourHr = AggregationPeriod.FOUR_HOURS;
def day1 = AggregationPeriod.DAY;
def week1 = AggregationPeriod.WEEK;
def month1 = AggregationPeriod.MONTH;
def year1 = AggregationPeriod.YEAR;
#//auto higher time frame calculation
def HTFo = if agg == oneMin then fifttenMin / agg else
if agg <= fiveMin then hour / agg else
if agg <= twoHr then day1 / agg else
if agg <= fourHr then week1 / agg else
if agg <= day1 then month1 / agg else
if agg <= month1 then year1 / agg else day1 / agg;
def AutoTF = if agg < fourHr then day else
if agg < day1 then week else
if agg < month1 then month else
if agg < year1 then year else day;
def ManTF = if ManualAgg <= day1 then DAY else
if ManualAgg <= week1 then MONTH else
if ManualAgg < year1 then YEAR else day;
#--- Calc
def HTF = if mode == mode."Auto" then HTFo else ManualAgg / agg;
def TF = if mode == mode."Auto" then AutoTF else ManTF;
def count = If(count[1] > HTF - 1 or TF != TF[1], 1, count[1] + 1);
def htfvol = fold i = 0 to count with p do
p + GetValue(volume, i);
def OpenCandle = if count == 1 then open else OpenCandle[1];
def col = if close >= OpenCandle then 1 else 0;
def volValue = if htfvol > htfvol[1] then htfvol else na;
def line = CompoundValue(1, if htfvol > htfvol[1] then line[1] else volValue[1], volValue);
#--- Plot
def OpenLine = if count==1 then col[1] else OpenLine[1];
def Linecol = if OpenLine then 1 else 0;
plot volLine = if !ShowLines then na else line;
volLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
volLine.AssignValueColor(if Linecol then Color.GREEN else Color.RED);
# Plot Hist
AddChart(high = if col then htfvol else na , low = 0 , open = htfvol, close = 0,
type = ChartType.CANDLE, growcolor = CreateColor(0,152,0));
AddChart(high = if !col then htfvol else na , low = 0 , open = htfvol, close = 0,
type = ChartType.CANDLE, growcolor = CreateColor(128,0,0));
#--- End Code