#// Indicator for TOS
# indicator("Anchored TWAP with StDev Bands [MrShadow]", shorttitle="TWAP"
# converted by Sam4Cok@Samer800 - 02/2025
input colorBars = no;
input autoTwapColor = yes; # "Auto Color"
input showAnchorVerticalLine = yes;
input anchor = {"Custom", default "Day", "3 Days", "Week", "Month"}; # "Anchor"
@Date
input customAnchorDay = 20250101; #"Custom Anchor Day"
input customAnchorTime = 0830;
input source = ohlc4; # "Source"
input showStDevBands = yes; # "StDev Bands"
input stDevMultiplier = 1.0; # "Multiplier"
def na = Double.NaN;
def last = IsNaN(close);
def GAP = GetAggregationPeriod();
def day3 = if !last then close(Period = AggregationPeriod.THREE_DAYS) else day3[1];
def time = GetTime();
def yyyyMmDd = GetYYYYMMDD();
def tChange = (time - time[1]) != GAP;
def s = SecondsTillTime(customAnchorTime) < 0;
def t = Time >= RegularTradingStart(customAnchorDay);
def t1 = t and !t[1];
def s1 = s and !s[1];
def isStart = if (yyyyMmDd == customAnchorDay) then if tChange then t1 else s1 else
(yyyyMmDd >= customAnchorDay and yyyyMmDd[1] < customAnchorDay) or
(yyyyMmDd == First(yyyyMmDd) and yyyyMmDd >= customAnchorDay);
def isWeek = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
def isMonth = RoundDown(yyyyMmDd / 100, 0);
script rising {
input src = close;
input len = 20;
def rising = fold i = 0 to len with p=1 while p do
GetValue(src, i) > GetValue(src, i + 1);
plot out = rising;
}
script falling {
input src = close;
input len = 20;
def falling = fold i = 0 to len with p=1 while p do
GetValue(src, i) < GetValue(src, i + 1);
plot out = falling;
}
#//Define anchor point
def isAnchorTime;
switch (anchor) {
case "Custom" :
isAnchorTime = CompoundValue(1, isStart, na);
case "3 Days" :
isAnchorTime = CompoundValue(1, day3 != day3[1], yes);
case "Week" :
isAnchorTime = CompoundValue(1, isWeek != isWeek[1], yes);
case "Month" :
isAnchorTime = CompoundValue(1, isMonth != isMonth[1], yes);
default :
isAnchorTime = CompoundValue(1, yyyyMmDd != yyyyMmDd[1], yes);
}
#//Calculate TWAP
#//Anchor
def barsSinceAnchor;
def priceSumSinceAnchor;
def priceSquaredSum;
if isAnchorTime {
barsSinceAnchor = 0;
priceSumSinceAnchor = source;
priceSquaredSum = Sqr(source);
} else {
barsSinceAnchor = CompoundValue(1, barsSinceAnchor[1] + 1, na);
priceSumSinceAnchor = CompoundValue(1, priceSumSinceAnchor[1] + source, na);
priceSquaredSum = CompoundValue(1, priceSquaredSum[1] + Sqr(source), na);
}
def timeSinceAnchor = barsSinceAnchor + 1;
def twap = priceSumSinceAnchor / timeSinceAnchor;
def stDev = Sqrt(Max(priceSquaredSum / timeSinceAnchor - Sqr(twap), 0));
#def stDev = Sqrt(variance);
def loBand = twap - stDev * stDevMultiplier;
def upBand = twap + stDev * stDevMultiplier;
#// Color
def twapUp = rising(twap, 3);
def twapDn = falling(twap, 3);
def col = if autoTwapColor then if twapUp then 1 else if twapDn then -1 else 0 else na;
# -- plot
plot twapLine = if twap then twap else na;
plot upperBand = if showStDevBands and upBand then upBand else na; # "Upper Band"
plot lowerBand = if showStDevBands and loBand then loBand else na; # "Lower Band"
twapLine.AssignValueColor(if IsNaN(col) then GetColor(0) else
if col > 0 then Color.GREEN else if col < 0 then Color.RED else Color.GRAY);
upperBand.SetDefaultColor(GetColor(2));
lowerBand.SetDefaultColor(GetColor(4));
#-- Cloud
AddCloud(upperBand, lowerBand, CreateColor(43, 0, 43));
#- Vetical Line
AddVerticalLine(showAnchorVerticalLine and isAnchorTime, "Anchor", Color.WHITE);
#-- color Bars
AssignPriceColor(if !colorBars then Color.CURRENT else
if twapUp then Color.GREEN else if twapDn then Color.RED else Color.GRAY);
#-- END of CODE