Percent difference between price and vwap For ThinkOrSwim

Hi guys and gals,

I'm not well versed in Pinescript, so I'm hoping some coding guru can hopefully convert this TradingView script to thinkscript.
https://www.tradingview.com/script/o0mHbrYo-Difference-between-PRICE-and-VWAP-V2/

Code:
//@version=2
study(title="Percent difference between price and vwap")
BarcOn=input(false, title="Color bars/Candles?")
len=input(36)
TimeFrame = input('D')
start = security(tickerid, TimeFrame, time)

newSession = iff(change(start), 1, 0)

vwapsum = iff(newSession, hl2*volume, vwapsum[1]+hl2*volume)
volumesum = iff(newSession, volume, volumesum[1]+volume)
v2sum = iff(newSession, volume*hl2*hl2, v2sum[1]+volume*hl2*hl2)
myvwap = vwapsum/volumesum


src=input(close)
xSMA = myvwap
nRes = abs(src - xSMA) * 100 / src

nRes3 = sma(nRes,len)
plot(nRes3, color=blue, style=areabr,transp=90, histbase=0, title="Average")
level1=input(1.28)
level2=input(2.1)
level3=input(2.5)
level4=input(3.09)
level5=input(4.1)
color2=nRes>level1 and nRes<level2?navy: nRes>level2 and nRes<level3?blue: nRes>level3 and nRes<level4?orange: nRes>level4 and nRes<level5?red: nRes>level5?maroon: na
color=nRes>level1 and nRes<level2?navy: nRes>level2 and nRes<level3?blue: nRes>level3 and nRes<level4?orange: nRes>level4 and nRes<level5?red: nRes>level5?maroon: gray

plot(nRes, style=histogram,color=color)
barcolor(BarcOn?color2:na)
hline(0, title="Base Line", color=aqua, linestyle=solid)
a=hline(level1, title="1", color=aqua, linestyle=dotted)
b=hline(level2, title="2", color=blue, linestyle=dotted)
c=hline(level3, title="3", color=orange, linestyle=dotted)
d=hline(level4, title="4", color=red, linestyle=dotted)
e=hline(level5, title="5", color=maroon, linestyle=dotted)


Thank you!
check the below

CODE:

CSS:
#//@version=2
#study(title="Percent difference between price and vwap")
# request from usethinkscript.com memeber
#converted by sam4cok@Samer800 - 09/2022

declare lower;
def na = Double.NaN;
input BarcOn = no;#(false, title="Color bars/Candles?")
input len = 36;#(36)
input timeFrame = {default DAY, WEEK, 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");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

#start = security(tickerid, TimeFrame, time)

def newSession = isPeriodRolled;

def vwapsum = if(newSession, hl2*volume, vwapsum[1]+hl2*volume);
def volumesum = if(newSession, volume, volumesum[1]+volume);
def v2sum = if(newSession, volume*hl2*hl2, v2sum[1]+volume*hl2*hl2);
def myvwap = vwapsum/volumesum;


input src=close;#(close)
def xSMA = myvwap;
def nRes = AbsValue(src - xSMA) * 100 / src;

def nRes3 = SimpleMovingAvg(nRes,len);

plot AverageV = nRes3; #, color=blue, style=areabr,transp=90, histbase=0, title="Average")
#AverageV.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
addCloud(nRes3,0, Color.GRAY);
AverageV.SetDefaultColor(Color.WHITE);
input level1= 1.28;
input level2= 2.1;
input level3= 2.5;
input level4= 3.09;
input level5= 4.1;
def color2= if nRes>level1 and nRes<level2 then 2 else
            if nRes>level2 and nRes<level3 then 3  else
            if nRes>level3 and nRes<level4 then 4 else
            if nRes>level4 and nRes<level5 then 5 else
            if nRes>level5 then 0 else na;
def color=  if nRes>level1 and nRes<level2 then 2 else
            if nRes>level2 and nRes<level3 then 3 else
            if nRes>level3 and nRes<level4 then 4 else
            if nRes>level4 and nRes<level5 then 5 else
            if nRes>level5 then 0 else -1;

plot histo = nRes;#, style=histogram,color=color)
histo.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histo.AssignValueColor(if color ==2 then CreateColor(17,118,242) else
                       if color == 3 then CreateColor(15,68,245) else
                       if color == 4 then color.DARK_ORANGE else
                       if color == 5 then color.DARK_RED else
                       if color == 0 then color.RED else color.GRAY);

AssignPriceColor(if BarcOn then 
                       if color2 ==2 then CreateColor(17,118,242) else
                       if color2 == 3 then CreateColor(15,68,245) else
                       if color2 == 4 then color.DARK_ORANGE else
                       if color2 == 5 then color.DARK_RED else
                       if color2 == 0 then color.RED else color.GRAY else Color.CURRENT);

plot hline = if isNAN(close) then na else 0;     # "Base Line"
hline.SetDefaultColor(Color.DARK_GRAY);
plot a=level1;
a.SetDefaultColor(CreateColor(17,118,242));
a.SetPaintingStrategy(PaintingStrategy.DASHES);
plot b=level2;
b.SetDefaultColor(Color.BLUE);
b.SetPaintingStrategy(PaintingStrategy.DASHES);
plot c=level3;
c.SetDefaultColor(Color.ORANGE);
c.SetPaintingStrategy(PaintingStrategy.DASHES);
plot d=level4;
d.SetDefaultColor(Color.DARK_RED);
d.SetPaintingStrategy(PaintingStrategy.DASHES);
plot e=level5;
e.SetDefaultColor(Color.RED);
e.SetPaintingStrategy(PaintingStrategy.DASHES);

### END
 
check the below

CODE:

CSS:
#//@version=2
#study(title="Percent difference between price and vwap")
# request from usethinkscript.com memeber
#converted by sam4cok@Samer800 - 09/2022

declare lower;
def na = Double.NaN;
input BarcOn = no;#(false, title="Color bars/Candles?")
input len = 36;#(36)
input timeFrame = {default DAY, WEEK, 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");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

#start = security(tickerid, TimeFrame, time)

def newSession = isPeriodRolled;

def vwapsum = if(newSession, hl2*volume, vwapsum[1]+hl2*volume);
def volumesum = if(newSession, volume, volumesum[1]+volume);
def v2sum = if(newSession, volume*hl2*hl2, v2sum[1]+volume*hl2*hl2);
def myvwap = vwapsum/volumesum;


input src=close;#(close)
def xSMA = myvwap;
def nRes = AbsValue(src - xSMA) * 100 / src;

def nRes3 = SimpleMovingAvg(nRes,len);

plot AverageV = nRes3; #, color=blue, style=areabr,transp=90, histbase=0, title="Average")
#AverageV.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
addCloud(nRes3,0, Color.GRAY);
AverageV.SetDefaultColor(Color.WHITE);
input level1= 1.28;
input level2= 2.1;
input level3= 2.5;
input level4= 3.09;
input level5= 4.1;
def color2= if nRes>level1 and nRes<level2 then 2 else
            if nRes>level2 and nRes<level3 then 3  else
            if nRes>level3 and nRes<level4 then 4 else
            if nRes>level4 and nRes<level5 then 5 else
            if nRes>level5 then 0 else na;
def color=  if nRes>level1 and nRes<level2 then 2 else
            if nRes>level2 and nRes<level3 then 3 else
            if nRes>level3 and nRes<level4 then 4 else
            if nRes>level4 and nRes<level5 then 5 else
            if nRes>level5 then 0 else -1;

plot histo = nRes;#, style=histogram,color=color)
histo.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histo.AssignValueColor(if color ==2 then CreateColor(17,118,242) else
                       if color == 3 then CreateColor(15,68,245) else
                       if color == 4 then color.DARK_ORANGE else
                       if color == 5 then color.DARK_RED else
                       if color == 0 then color.RED else color.GRAY);

AssignPriceColor(if BarcOn then
                       if color2 ==2 then CreateColor(17,118,242) else
                       if color2 == 3 then CreateColor(15,68,245) else
                       if color2 == 4 then color.DARK_ORANGE else
                       if color2 == 5 then color.DARK_RED else
                       if color2 == 0 then color.RED else color.GRAY else Color.CURRENT);

plot hline = if isNAN(close) then na else 0;     # "Base Line"
hline.SetDefaultColor(Color.DARK_GRAY);
plot a=level1;
a.SetDefaultColor(CreateColor(17,118,242));
a.SetPaintingStrategy(PaintingStrategy.DASHES);
plot b=level2;
b.SetDefaultColor(Color.BLUE);
b.SetPaintingStrategy(PaintingStrategy.DASHES);
plot c=level3;
c.SetDefaultColor(Color.ORANGE);
c.SetPaintingStrategy(PaintingStrategy.DASHES);
plot d=level4;
d.SetDefaultColor(Color.DARK_RED);
d.SetPaintingStrategy(PaintingStrategy.DASHES);
plot e=level5;
e.SetDefaultColor(Color.RED);
e.SetPaintingStrategy(PaintingStrategy.DASHES);

### END
Will be playing with it over the weekend. Thanks a bunch!
 
Viewed some tickers on the 5min chart using this code on TOS. The results are totally different from what's shown on TradingView. Not sure where the problem lies.
 
Viewed some tickers on the 5min chart using this code on TOS. The results are totally different from what's shown on TradingView. Not sure where the problem lies.
i just checked it and seems working as expected. However, the volume in TOS and TV not the same specially if you enable pre/after market trading hours.

try to uncheck the extended hours and compare.
 
i just checked it and seems working as expected. However, the volume in TOS and TV not the same specially if you enable pre/after market trading hours.

try to uncheck the extended hours and compare.
Yep. That was the problem. Thanks again for your help.
 

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
407 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