# VolumeProfile_Right_Justified_using_MonkeyBars
#
# TD Ameritrade IP Company, Inc. (c) 2010-2022
#
# TOS Monkey Bars modified to show it's volumeprofile in the expansion to pointing to the left. A volumeprofile snippet was added to display the valuearea, poc, vah and val in the chart area as the monkeybars needs to remain set to onexpansion.
input pricePerRowHeightMode = {AUTOMATIC, default TICKSIZE, CUSTOM};
input customRowHeight = 1.0;
input aggregationPeriod = {"1 min", "2 min", "3 min", "4 min", "5 min", "10 min", "15 min", "20 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "2 Days", "3 Days", "4 Days", "Week", "Month", "Quarter", default "Year"};
input timePerProfile = {CHART, MINUTE, HOUR, DAY, WEEK, MONTH, "OPT EXP", BAR, default YEAR};
input multiplier = 5;
input onExpansion = yes;
input profiles = 1000;
input showMonkeyBar = no;
input showThePlayground = no;
input thePlaygroundPercent = 70;
input opacity = 40;
input emphasizeFirstDigit = no;
input markOpenPrice = no;
input markClosePrice = no;
input volumeShowStyle = MonkeyVolumeShowStyle.ALL;
input showVolumeVA = no;
input showVolumePoc = no;
input theVolumePercent = 70;
input showInitialBalance = no;
input initialBalanceRange = 3;
def period;
def yyyymmdd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def year = GetYear();
def month = year * 12 + GetMonth();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def dom = GetDayOfMonth(yyyymmdd);
def dow = GetDayOfWeek(yyyymmdd - dom + 1);
def expthismonth = (if dow > 5 then 27 else 20) - dow;
def exp_opt = month + (dom > expthismonth);
def periodMin = Floor(seconds / 60 + day_number * 24 * 60);
def periodHour = Floor(seconds / 3600 + day_number * 24);
def periodDay = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
def periodWeek = Floor(day_number / 7);
def periodMonth = month - First(month);
def periodQuarter = Ceil(month / 3) - First(Ceil(month / 3));
def periodYear = year - First(year);
switch (timePerProfile) {
case CHART:
period = 0;
case MINUTE:
period = periodMin;
case HOUR:
period = periodHour;
case DAY:
period = periodDay;
case WEEK:
period = periodWeek;
case MONTH:
period = periodMonth;
case "OPT EXP":
period = exp_opt - First(exp_opt);
case BAR:
period = BarNumber() - 1;
case YEAR:
period = periodYear;
}
def count = CompoundValue(1, if period != period[1] then (GetValue(count, 1) + period - period[1]) % multiplier else GetValue(count, 1), 0);
def cond = CompoundValue(1, count < count[1] + period - period[1], yes);
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
height = PricePerRow.AUTOMATIC;
case TICKSIZE:
height = PricePerRow.TICKSIZE;
case CUSTOM:
height = customRowHeight;
}
def timeInterval;
def aggMultiplier;
switch (aggregationPeriod) {
case "1 min":
timeInterval = periodMin;
aggMultiplier = 1;
case "2 min":
timeInterval = periodMin;
aggMultiplier = 2;
case "3 min":
timeInterval = periodMin;
aggMultiplier = 3;
case "4 min":
timeInterval = periodMin;
aggMultiplier = 4;
case "5 min":
timeInterval = periodMin;
aggMultiplier = 5;
case "10 min":
timeInterval = periodMin;
aggMultiplier = 10;
case "15 min":
timeInterval = periodMin;
aggMultiplier = 15;
case "20 min":
timeInterval = periodMin;
aggMultiplier = 20;
case "30 min":
timeInterval = periodMin;
aggMultiplier = 30;
case "1 hour":
timeInterval = periodHour;
aggMultiplier = 1;
case "2 hours":
timeInterval = periodHour;
aggMultiplier = 2;
case "4 hours":
timeInterval = periodHour;
aggMultiplier = 4;
case "Day":
timeInterval = periodDay;
aggMultiplier = 1;
case "2 Days":
timeInterval = periodDay;
aggMultiplier = 2;
case "3 Days":
timeInterval = periodDay;
aggMultiplier = 3;
case "4 Days":
timeInterval = periodDay;
aggMultiplier = 4;
case "Week":
timeInterval = periodWeek;
aggMultiplier = 1;
case "Month":
timeInterval = periodMonth;
aggMultiplier = 1;
case "Quarter":
timeInterval = periodQuarter;
aggMultiplier = 1;
case "Year":
timeInterval = periodYear;
aggMultiplier = 1;
}
def agg_count = CompoundValue(1, if timeInterval != timeInterval[1] then (GetValue(agg_count, 1) + timeInterval - timeInterval[1]) % aggMultiplier else GetValue(agg_count, 1), 0);
def agg_cond = CompoundValue(1, agg_count < agg_count[1] + timeInterval - timeInterval[1], yes);
def digit = CompoundValue(1, if cond then 1 else agg_cond + GetValue(digit, 1), 1);
profile monkey = MonkeyBars(digit, "startNewProfile" = cond, "onExpansion" = onExpansion,
"numberOfProfiles" = profiles, "pricePerRow" = height, "the playground percent" = thePlaygroundPercent,
"emphasize first digit" = emphasizeFirstDigit, "volumeProfileShowStyle" = volumeShowStyle, "volumePercentVA" = theVolumePercent,
"show initial balance" = showInitialBalance, "initial balance range" = initialBalanceRange);
def con = CompoundValue(1, onExpansion, no);
def mbar = CompoundValue(1, if IsNaN(monkey.GetPointOfControl()) and con then GetValue(mbar, 1) else monkey.GetPointOfControl(), monkey.GetPointOfControl());
def hPG = CompoundValue(1, if IsNaN(monkey.GetHighestValueArea()) and con then GetValue(hPG, 1) else monkey.GetHighestValueArea(), monkey.GetHighestValueArea());
def lPG = CompoundValue(1, if IsNaN(monkey.GetLowestValueArea()) and con then GetValue(lPG, 1) else monkey.GetLowestValueArea(), monkey.GetLowestValueArea());
def hProfile = CompoundValue(1, if IsNaN(monkey.GetHighest()) and con then GetValue(hProfile, 1) else monkey.GetHighest(), monkey.GetHighest());
def lProfile = CompoundValue(1, if IsNaN(monkey.GetLowest()) and con then GetValue(lProfile, 1) else monkey.GetLowest(), monkey.GetLowest());
def plotsDomain = IsNaN(close) == onExpansion;
#plot MB = if plotsDomain then mbar else Double.NaN;
plot ProfileHigh = if plotsDomain then hProfile else Double.NaN;
plot ProfileLow = if plotsDomain then lProfile else Double.NaN;
DefineGlobalColor("Monkey Bar", GetColor(4));
DefineGlobalColor("The Playground", GetColor(3));
DefineGlobalColor("Open Price", GetColor(1));
DefineGlobalColor("Close Price", GetColor(1));
DefineGlobalColor("Volume", Color.CYAN);
DefineGlobalColor("Volume Value Area", GetColor(8));
DefineGlobalColor("Volume Point of Control", Color.RED);
DefineGlobalColor("Initial Balance", GetColor(7));
monkey.Show(color = Color.CYAN, "poc color" = Color.CURRENT, "va color" = Color.CURRENT, opacity = opacity, "open color" = Color.CURRENT, "close color" = Color.CURRENT, "ib color" =Color.CURRENT, "volume color" = color.cyan, "volume poc color" = Color.CURRENT);
ProfileHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfileLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfileHigh.SetDefaultColor(GetColor(3));
ProfileLow.SetDefaultColor(GetColor(3));
ProfileHigh.Hide();
ProfileLow.Hide();
#VolumeProfile snippet to display profile on last Timeperprofile on chart
input show_valuearea_color = yes;
profile vol = VolumeProfile(startnewprofile = cond, onExpansion = no, pricePerRow = PricePerRow.TICKSIZE, numberofprofiles = 1, "value area percent" = 70);
vol.Show(opacity = opacity, "volume va color" = Color.CURRENT, color = Color.CURRENT, "volume color" = Color.CURRENT, "volume poc color" = Color.DARK_RED, "va color" = if show_valuearea_color == yes then GetColor(8) else Color.CURRENT);
def pc = if IsNaN(vol.GetPointOfControl()) and con then pc[1] else vol.GetPointOfControl();
def hVA = if IsNaN(vol.GetHighestValueArea()) and con then hVA[1] else vol.GetHighestValueArea();
def lVA = if IsNaN(vol.GetLowestValueArea()) and con then lVA[1] else vol.GetLowestValueArea();
plot poc = pc;
plot vah = hVA;
plot val = lVA;
vah.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
val.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
poc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
vah.SetDefaultColor(Color.YELLOW);
val.SetDefaultColor(Color.YELLOW);
poc.SetDefaultColor(Color.RED);
#