VWAP library [Sam4Cok] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
KKjPwEz.png


Multiple VWAP options can select from. (Traditional, smoothed and preVWAP).

CODE:
CSS:
#// VWAP library
# Created by Sam3Cok@Samer800     - 03/2023

input VWAP_src = close;#
input VWAP_smoothing = 6;
input timeFrame = {default DAY, WEEK, MONTH};
input VwapType = {default "Traditional VWAP" , "Smoothed VWAP", "Pre Vwap", "Cloud VWAP"};
input showStdvBand_1  = yes;    # "Show Standard Deviation Bands #1"
input stdevMult_1     = 1.0;    # "Bands Multiplier #1"
input showStdvBand_2  = no;     # "Show Standard Deviation Bands #2"
input stdevMult_2     = 2.0;    # "Bands Multiplier #2"
input showStdvBand_3  = no;     # "Show Standard Deviation Bands #3"
input stdevMult_3     = 3.0;    # "Bands Multiplier #3"


def na = Double.NaN;
#--- Color
DefineGlobalColor("green1" , CreateColor(0, 216,08));
DefineGlobalColor("green2" , CreateColor(0, 196,08));
DefineGlobalColor("green3" , CreateColor(0, 177,07));
DefineGlobalColor("green4" , CreateColor(0, 137,05));
DefineGlobalColor("green5" , CreateColor(0, 118,05));
DefineGlobalColor("green6" , CreateColor(0, 98,04));
DefineGlobalColor("green7" , CreateColor(0, 78,03));

DefineGlobalColor("red1" , CreateColor(255,17,0));
DefineGlobalColor("red2" , CreateColor(216,14,0));
DefineGlobalColor("red3" , CreateColor(196,13,0));
DefineGlobalColor("red4" , CreateColor(177,12,0));
DefineGlobalColor("red5" , CreateColor(137,9,0));
DefineGlobalColor("red6" , CreateColor(118,8,0));
DefineGlobalColor("red7" , CreateColor(98,7,0));


def TradVwap = VwapType == VwapType."Traditional VWAP";
def SmooVwap = VwapType == VwapType."Smoothed VWAP";
def PrevVwap = VwapType == VwapType."Pre Vwap";
def AllVwap  = VwapType == VwapType."Cloud VWAP";
def last = TradVwap or SmooVwap or AllVwap or isNaN(close);
def day = GetDay();
def week = GetWeek();
def month =  GetMonth();
def VWAP_TF; def prevClose;
switch (timeFrame) {
case DAY:
    VWAP_TF = day;
    prevClose = close(Period=AggregationPeriod.DAY);
case WEEK:
    VWAP_TF = week;
    prevClose = close(Period=AggregationPeriod.WEEK);
case MONTH:
    VWAP_TF = month;
    prevClose = close(Period=AggregationPeriod.MONTH);
}

def cap = GetAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

#export f_Vwap(simple string tf, float src, float src_v) =>
script f_Vwap {
    input tf = close;
    input src = close;
    def src_v = Volume;
    def start0 = tf - tf[1];
    def sumSrc0 = src * src_v;
    def sumVol0 = src_v;
    def sumSrc2 = src_v * Sqr(src);
    def sumSrc1 = CompoundValue(1, if start0 then sumSrc0 else sumSrc0 + sumSrc1[1], sumSrc0);
    def sumVol1 = CompoundValue(1, if start0 then sumVol0 else sumVol0 + sumVol1[1], sumVol0);
    def sumVol2 = CompoundValue(1, if start0 then sumSrc2 else sumSrc2 + sumVol2[1], sumSrc2);
    def Vwap = sumSrc1 / sumVol1;
    def deviation = Sqrt(Max(sumVol2 / sumVol1 - Sqr(Vwap), 0));
    plot wap = Vwap;
    plot dev = deviation;
}
def Vwap    = f_Vwap(VWAP_TF, VWAP_src).wap;
def devVwap = f_Vwap(VWAP_TF, VWAP_src).dev;
    
#export f_smoothed_Vwap(simple string tf, float src, float src_v, simple int smoothing) =>
script f_smoothed_Vwap {
    input tf = close;
    input src = close;
    input smoothing = 6;
    def src_v = Volume;
    def start0 = tf - tf[1];
    def sumSrc0 = src * src_v;
    def sumVol0 = src_v;
    def sumSrc2 = src_v * Sqr(src);
    def sumSrc1 = if start0 then sumSrc0 else sumSrc0 + sumSrc1[1];
    def sumVol1 = if start0 then sumVol0 else sumVol0 + sumVol1[1];
    def sumVol2 = CompoundValue(1, if start0 then sumSrc2 else sumSrc2 + sumVol2[1], sumSrc2);
    def Vwap = sumSrc1 / sumVol1;
    def Vwap_smoothed = Average(Vwap, smoothing);
    def deviation = Average(Sqrt(Max(sumVol2 / sumVol1 - Sqr(Vwap), 0)), smoothing);
    plot wap = Vwap_smoothed;
    plot dev = deviation;
}
def Vwap_smoothed = f_smoothed_Vwap(VWAP_TF, VWAP_src, VWAP_smoothing).wap;
def Vwap_smooDev = f_smoothed_Vwap(VWAP_TF, VWAP_src, VWAP_smoothing).dev;

#export f_Pre_Vwap(simple string tf, float Vwap, int occurrence) =>
script f_Pre_Vwap {
    input tf = close;
    input src = close;
    def src_v = Volume;
    def start0 = tf - tf[1];
    def sumSrc0 = src * src_v;
    def sumVol0 = src_v;
    def sumSrc2 = src_v * Sqr(src);
    def sumSrc1 = CompoundValue(1, if start0 then sumSrc0 else sumSrc0 + sumSrc1[1], sumSrc0);
    def sumVol1 = CompoundValue(1, if start0 then sumVol0 else sumVol0 + sumVol1[1], sumVol0);
    def sumVol2 = CompoundValue(1, if start0 then sumSrc2 else sumSrc2 + sumVol2[1], sumSrc2);
    def Vwap = sumSrc1 / sumVol1;
    def deviation = Sqrt(Max(sumVol2 / sumVol1 - Sqr(Vwap), 0));
    def Pre_Vwap = CompoundValue(1, if start0 then Vwap[1] else Pre_Vwap[1], Vwap);
    def Dev_Vwap = CompoundValue(1, if start0 then deviation[1] else Dev_Vwap[1], deviation);
    plot wap = Pre_Vwap;
    plot dev = Dev_Vwap;
}
def Pre_Vwap = f_Pre_Vwap(VWAP_TF, VWAP_src).wap;
def PredevVwap = f_Pre_Vwap(VWAP_TF, VWAP_src).dev;

#-- VWAP Dev Calc
def vwapDevUp1 = Vwap + devVwap * stdevMult_1;
def vwapDevUp2 = Vwap + devVwap * stdevMult_2;
def vwapDevUp3 = Vwap + devVwap * stdevMult_3;
def vwapDevLo1 = Vwap - devVwap * stdevMult_1;
def vwapDevLo2 = Vwap - devVwap * stdevMult_2;
def vwapDevLo3 = Vwap - devVwap * stdevMult_3;

def vwapSmooDevUp1 = Vwap_smoothed + Vwap_smooDev  * stdevMult_1;
def vwapSmooDevUp2 = Vwap_smoothed + Vwap_smooDev  * stdevMult_2;
def vwapSmooDevUp3 = Vwap_smoothed + Vwap_smooDev  * stdevMult_3;
def vwapSmooDevLo1 = Vwap_smoothed - Vwap_smooDev  * stdevMult_1;
def vwapSmooDevLo2 = Vwap_smoothed - Vwap_smooDev  * stdevMult_2;
def vwapSmooDevLo3 = Vwap_smoothed - Vwap_smooDev  * stdevMult_3;

#---- Plot
plot vwapLine = if AllVwap or TradVwap then Vwap else na;
vwapLine.SetHiding(AllVwap);

plot vwapUp1 = if showStdvBand_1 and vwapLine then vwapDevUp1 else na;
vwapUp1.SetHiding(AllVwap);
plot vwapLo1 = if showStdvBand_1 and vwapLine then vwapDevLo1 else na;
vwapLo1.SetHiding(AllVwap);
plot vwapUp2 = if showStdvBand_2 and vwapLine then vwapDevUp2 else na;
vwapUp2.SetHiding(AllVwap);
plot vwapLo2 = if showStdvBand_2 and vwapLine then vwapDevLo2 else na;
vwapLo2.SetHiding(AllVwap);
plot vwapUp3 = if showStdvBand_3 and vwapLine then vwapDevUp3 else na;
vwapUp3.SetHiding(AllVwap);
plot vwapLo3 = if showStdvBand_3 and vwapLine then vwapDevLo3 else na;
vwapLo3.SetHiding(AllVwap);

plot vwapSmoo = if AllVwap or SmooVwap then Vwap_smoothed else na;
vwapSmoo.SetHiding(AllVwap);

plot vwapSmooUp1 = if showStdvBand_1 and vwapSmoo then vwapSmooDevUp1 else na;
vwapSmooUp1.SetHiding(AllVwap);
plot vwapSmooLo1 = if showStdvBand_1 and vwapSmoo then vwapSmooDevLo1 else na;
vwapSmooLo1.SetHiding(AllVwap);
plot vwapSmooUp2 = if showStdvBand_2 and vwapSmoo then vwapSmooDevUp2 else na;
vwapSmooUp2.SetHiding(AllVwap);
plot vwapSmooLo2 = if showStdvBand_2 and vwapSmoo then vwapSmooDevLo2 else na;
vwapSmooLo2.SetHiding(AllVwap);
plot vwapSmooUp3 = if showStdvBand_3 and vwapSmoo then vwapSmooDevUp3 else na;
vwapSmooUp3.SetHiding(AllVwap);
plot vwapSmooLo3 = if showStdvBand_3 and vwapSmoo then vwapSmooDevLo3 else na;
vwapSmooLo3.SetHiding(AllVwap);

def dir = prevClose>Pre_Vwap;

plot Prevwap = if last then na else Pre_Vwap;
Prevwap.SetPaintingStrategy(PaintingStrategy.POINTS);
Prevwap.AssignValueColor(if Vwap> Pre_Vwap then GlobalColor("green1") else GlobalColor("red1"));

plot PrevwapUp1 = if last or !showStdvBand_1 then na else Pre_Vwap + PredevVwap * stdevMult_1;
PrevwapUp1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PrevwapUp1.AssignValueColor(if vwapDevUp1>PrevwapUp1 then GlobalColor("green2") else GlobalColor("red2"));
plot PrevwapUp2 = if last or !showStdvBand_2 then na else Pre_Vwap + PredevVwap * stdevMult_2;
PrevwapUp2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PrevwapUp2.AssignValueColor(if vwapDevUp2>PrevwapUp2 then GlobalColor("green3") else GlobalColor("red3"));
plot PrevwapUp3 = if last or !showStdvBand_3 then na else Pre_Vwap + PredevVwap * stdevMult_3;
PrevwapUp3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PrevwapUp3.AssignValueColor(if vwapDevUp3>PrevwapUp3 then GlobalColor("green4") else GlobalColor("red4"));

plot PrevwapLo1 = if last or !showStdvBand_1 then na else Pre_Vwap - PredevVwap * stdevMult_1;
PrevwapLo1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PrevwapLo1.AssignValueColor(if vwapDevLo1>PrevwapLo1 then GlobalColor("green2") else GlobalColor("red2"));
plot PrevwapLo2 = if last or !showStdvBand_2 then na else Pre_Vwap - PredevVwap * stdevMult_2;
PrevwapLo2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PrevwapLo2.AssignValueColor(if vwapDevLo2>PrevwapLo2 then GlobalColor("green3") else GlobalColor("red3"));
plot PrevwapLo3 = if last or !showStdvBand_3 then na else Pre_Vwap - PredevVwap * stdevMult_3;
PrevwapLo3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PrevwapLo3.AssignValueColor(if vwapDevLo3>PrevwapLo3 then GlobalColor("green4") else GlobalColor("red4"));

vwapLine.AssignValueColor(if Vwap>Vwap_smoothed then GlobalColor("green1") else GlobalColor("red1"));

vwapUp1.AssignValueColor(if vwapUp1>vwapSmooDevUp1 then GlobalColor("green2") else  GlobalColor("red2"));
vwapUp2.AssignValueColor(if vwapUp2>vwapSmooDevUp2 then GlobalColor("green3") else  GlobalColor("red3"));
vwapUp3.AssignValueColor(if vwapUp3>vwapSmooDevUp3 then GlobalColor("green4") else  GlobalColor("red4"));
vwapLo1.AssignValueColor(if vwapLo1>vwapSmooDevLo1 then GlobalColor("green2") else  GlobalColor("red2"));
vwapLo2.AssignValueColor(if vwapLo2>vwapSmooDevLo2 then GlobalColor("green3") else  GlobalColor("red3"));
vwapLo3.AssignValueColor(if vwapLo3>vwapSmooDevLo3 then GlobalColor("green4") else  GlobalColor("red4"));

vwapSmoo.AssignValueColor(if Vwap>Vwap_smoothed then  GlobalColor("green1") else  GlobalColor("red1"));

vwapSmooUp1.AssignValueColor(if vwapDevUp1>vwapSmooUp1 then GlobalColor("green2") else GlobalColor("red2"));
vwapSmooUp2.AssignValueColor(if vwapDevUp2>vwapSmooUp2 then GlobalColor("green3") else GlobalColor("red3"));
vwapSmooUp3.AssignValueColor(if vwapDevUp3>vwapSmooUp3 then GlobalColor("green4") else GlobalColor("red4"));
vwapSmooLo1.AssignValueColor(if vwapDevLo1>vwapSmooLo1 then GlobalColor("green2") else  GlobalColor("red2"));
vwapSmooLo2.AssignValueColor(if vwapDevLo2>vwapSmooLo2 then GlobalColor("green3") else  GlobalColor("red3"));
vwapSmooLo3.AssignValueColor(if vwapDevLo3>vwapSmooLo3 then GlobalColor("green4") else  GlobalColor("red4"));

#AddCloud(Vwap, vwapSmoo, Color.CYAN, Color.MAGENTA);
AddCloud(if AllVwap then Vwap else na, Vwap_smoothed,  GlobalColor("green4"), GlobalColor("red4"), yes);
AddCloud(if AllVwap then vwapUp1 else na, vwapSmooUp1, GlobalColor("green5"), GlobalColor("red5"));
AddCloud(if AllVwap then vwapUp2 else na, vwapSmooUp2, GlobalColor("green6"), GlobalColor("red6"));
AddCloud(if AllVwap then vwapUp3 else na, vwapSmooUp3, GlobalColor("green7"), GlobalColor("red7"));

AddCloud(if AllVwap then vwapLo1 else na, vwapSmooLo1, GlobalColor("green5"), GlobalColor("red5"));
AddCloud(if AllVwap then vwapLo2 else na, vwapSmooLo2, GlobalColor("green6"), GlobalColor("red6"));
AddCloud(if AllVwap then vwapLo3 else na, vwapSmooLo3, GlobalColor("green7"), GlobalColor("red7"));
#AddCloud(vwapLo1, vwapSmooLo1, Color.DARK_GREEN, Color.DARK_RED);

#---- END CODE
 
Thank you for sharing. It is intresting. Can you explain how to use it?
this is a custom VWAP library to be used with other tools, either to use it as normal VWAP, smoothed or preVWAP based on the last closed.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
339 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