Repaints Anchored TWAP with StDev Bands [MrShadow] For ThinkOrSwim

Repaints

Lerning2Code

New member
This TWAP band indicator would be utilized primarily for daytrading but can work for swings as well.
TWAP with:
  • Anchoring: Custom, Day, Week, Month, Quarter, Year
  • Standard Deviation Bands
  • Auto-coloring depending on the trend

BxVasy4.png


Here is the original Tradingview code:
https://www.tradingview.com/script/bcyVfwBS-Anchored-TWAP-with-StDev-Bands-MrShadow/


For the new ThinkOrSwim code, you must scroll down to the next post
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

https://www.tradingview.com/script/bcyVfwBS-Anchored-TWAP-with-StDev-Bands-MrShadow/

Looking to have this Twap band indicator converted, if possible. It would be utilized primarily for daytrading but can work for swings as well.
check the below:

CSS:
#// 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
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
1005 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top