Buyer to Seller Volume (BSV) For ThinkOrSwim

halcyonguy

Moderator - Expert
VIP
Lifetime
Converted from Tradingview:
https://usethinkscript.com/threads/converted-tradingview-buyer-to-seller-volume-bsv.13874/

here is a version without MTF

not sure if these formulas should be changed, to be averages, not just sums
def BuyAverage = sum(greencandlevolume, AvgLength);
def SellAverage = sum(redcandlevolume, AvgLength);


Code:
declare lower;


#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Steversteves
#//@version=5
#indicator("Buyer to Seller Volume (BSV)")

#BackgroundData(symbol, timeframe, data) =>
#    isLive = barstate.isrealtime
#    request.security(symbol, timeframe, data[isLive ? 1 : 0])[isLive ? 0 : 1]

#InSession(sessionTimes, sessionTimeZone=syminfo.timezone) =>
#    not na(time(timeframe.period, sessionTimes, sessionTimeZone))

def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
#addlabel(yes, chartmin + " min", color.yellow);


#higherTimeFrame = input.timeframe("D", title="Time Frame")
#AvgLength   = input.int(14, title="Average Length")
input AvgLength = 14;

#greencandle = (close > open)
#redcandle = (open > close)

def greencandle = (close > open);
def redcandle = (open > close);

#greencandlevolume = if greencandle
#    volume
#redcandlevolume = if redcandle
#    volume

def greencandlevolume = if greencandle then volume else 0;
def redcandlevolume = if redcandle then volume else 0;
  
#// Vol Over time

#BuyAverage = BackgroundData(syminfo.tickerid, higherTimeFrame, math.sum(greencandlevolume, AvgLength))
#SellAverage = BackgroundData(syminfo.tickerid, higherTimeFrame, math.sum(redcandlevolume, AvgLength))

def BuyAverage = sum(greencandlevolume, AvgLength);
def SellAverage = sum(redcandlevolume, AvgLength);


#buyvolmax = ta.highest(BuyAverage, AvgLength)
#buyvolmin = ta.lowest(BuyAverage, AvgLength)
#sellvolmax = ta.highest(SellAverage, AvgLength)
#sellvolmin = ta.lowest(SellAverage, AvgLength)

def buyvolmax = highest(BuyAverage, AvgLength);
def buyvolmin = lowest(BuyAverage, AvgLength);
def sellvolmax = highest(SellAverage, AvgLength);
def sellvolmin = lowest(SellAverage, AvgLength);

#bool buyvolmaxatmax = BuyAverage >= buyvolmax
#bool buyvolminatmin = BuyAverage <= buyvolmin
#bool sellvolmax2 = SellAverage >= sellvolmax
#bool sellvolmin2 = SellAverage >= sellvolmin

def buyvolmaxatmax = BuyAverage >= buyvolmax;
def buyvolminatmin = BuyAverage <= buyvolmin;
def sellvolmax2 = SellAverage >= sellvolmax;
def sellvolmin2 = SellAverage >= sellvolmin;


#// Color
#color volBullColorInput = input.color(color.green, "Bull")
#color volBearColorInput = input.color(color.red, "Bear")
#color neutralinput = input.color(color.gray, "Neutral")
#color maxbuyers = input.color(color.fuchsia, "Max Buyers")
#color minbuyers = input.color(color.black, "Max Sellers")
#color buyerneutral = input.color(color.black, "Buyers Neutral")


DefineGlobalColor("Bull", color.green);  #volBullColorInput
DefineGlobalColor("Bear", color.red);  #volBearColorInput
DefineGlobalColor("Neutral", color.gray);  #neutralinput
DefineGlobalColor("Max Buyers", color.magenta);  #maxbuyers
DefineGlobalColor("Max Sellers", color.black);  #minbuyers
DefineGlobalColor("Buyers Neutral", color.black);  #buyerneutral


#color buyavgcolor =  buyvolmaxatmax ? minbuyers : volBullColorInput
#color sellavgcolor = sellvolmax2 ? neutralinput : volBearColorInput


#// SMA Vol
#volavg = (BuyAverage + SellAverage) / 2
def volavg = (BuyAverage + SellAverage) / 2;

#sma = ta.sma(volavg, AvgLength)
input ma1_type =  AverageType.simple;
def sma = MovingAverage(ma1_type, volavg, AvgLength);

#plot(sma, "SMA", color=color.yellow)
plot zsma = sma;
zsma.SetDefaultColor(Color.yellow);
#zsma.setlineweight(1);
zsma.hidebubble();


#//plot(br, style=plot.style_histogram, color = pallette)

#plot(BuyAverage, "Buyer Volume", color = volBullColorInput, linewidth=2)
#plot(SellAverage, "Seller Volume", color = volBearColorInput, linewidth=2)

plot zBuyAverage = BuyAverage;
zBuyAverage.SetDefaultColor(GlobalColor("bull"));
zBuyAverage.setlineweight(2);
zBuyAverage.hidebubble();

plot zSellAverage = SellAverage;
zSellAverage.SetDefaultColor(GlobalColor("bear"));
zsellAverage.setlineweight(2);
zsellAverage.hidebubble();
 
Last edited by a moderator:
here is a version without MTF

not sure if these formulas should be changed, to be averages, not just sums
def BuyAverage = sum(greencandlevolume, AvgLength);
def SellAverage = sum(redcandlevolume, AvgLength);


Code:
declare lower;


#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Steversteves
#//@version=5
#indicator("Buyer to Seller Volume (BSV)")

#BackgroundData(symbol, timeframe, data) =>
#    isLive = barstate.isrealtime
#    request.security(symbol, timeframe, data[isLive ? 1 : 0])[isLive ? 0 : 1]

#InSession(sessionTimes, sessionTimeZone=syminfo.timezone) =>
#    not na(time(timeframe.period, sessionTimes, sessionTimeZone))

def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
#addlabel(yes, chartmin + " min", color.yellow);


#higherTimeFrame = input.timeframe("D", title="Time Frame")
#AvgLength   = input.int(14, title="Average Length")
input AvgLength = 14;

#greencandle = (close > open)
#redcandle = (open > close)

def greencandle = (close > open);
def redcandle = (open > close);

#greencandlevolume = if greencandle
#    volume
#redcandlevolume = if redcandle
#    volume

def greencandlevolume = if greencandle then volume else 0;
def redcandlevolume = if redcandle then volume else 0;
  
#// Vol Over time

#BuyAverage = BackgroundData(syminfo.tickerid, higherTimeFrame, math.sum(greencandlevolume, AvgLength))
#SellAverage = BackgroundData(syminfo.tickerid, higherTimeFrame, math.sum(redcandlevolume, AvgLength))

def BuyAverage = sum(greencandlevolume, AvgLength);
def SellAverage = sum(redcandlevolume, AvgLength);


#buyvolmax = ta.highest(BuyAverage, AvgLength)
#buyvolmin = ta.lowest(BuyAverage, AvgLength)
#sellvolmax = ta.highest(SellAverage, AvgLength)
#sellvolmin = ta.lowest(SellAverage, AvgLength)

def buyvolmax = highest(BuyAverage, AvgLength);
def buyvolmin = lowest(BuyAverage, AvgLength);
def sellvolmax = highest(SellAverage, AvgLength);
def sellvolmin = lowest(SellAverage, AvgLength);

#bool buyvolmaxatmax = BuyAverage >= buyvolmax
#bool buyvolminatmin = BuyAverage <= buyvolmin
#bool sellvolmax2 = SellAverage >= sellvolmax
#bool sellvolmin2 = SellAverage >= sellvolmin

def buyvolmaxatmax = BuyAverage >= buyvolmax;
def buyvolminatmin = BuyAverage <= buyvolmin;
def sellvolmax2 = SellAverage >= sellvolmax;
def sellvolmin2 = SellAverage >= sellvolmin;


#// Color
#color volBullColorInput = input.color(color.green, "Bull")
#color volBearColorInput = input.color(color.red, "Bear")
#color neutralinput = input.color(color.gray, "Neutral")
#color maxbuyers = input.color(color.fuchsia, "Max Buyers")
#color minbuyers = input.color(color.black, "Max Sellers")
#color buyerneutral = input.color(color.black, "Buyers Neutral")


DefineGlobalColor("Bull", color.green);  #volBullColorInput
DefineGlobalColor("Bear", color.red);  #volBearColorInput
DefineGlobalColor("Neutral", color.gray);  #neutralinput
DefineGlobalColor("Max Buyers", color.magenta);  #maxbuyers
DefineGlobalColor("Max Sellers", color.black);  #minbuyers
DefineGlobalColor("Buyers Neutral", color.black);  #buyerneutral


#color buyavgcolor =  buyvolmaxatmax ? minbuyers : volBullColorInput
#color sellavgcolor = sellvolmax2 ? neutralinput : volBearColorInput


#// SMA Vol
#volavg = (BuyAverage + SellAverage) / 2
def volavg = (BuyAverage + SellAverage) / 2;

#sma = ta.sma(volavg, AvgLength)
input ma1_type =  AverageType.simple;
def sma = MovingAverage(ma1_type, volavg, AvgLength);

#plot(sma, "SMA", color=color.yellow)
plot zsma = sma;
zsma.SetDefaultColor(Color.yellow);
#zsma.setlineweight(1);
zsma.hidebubble();


#//plot(br, style=plot.style_histogram, color = pallette)

#plot(BuyAverage, "Buyer Volume", color = volBullColorInput, linewidth=2)
#plot(SellAverage, "Seller Volume", color = volBearColorInput, linewidth=2)

plot zBuyAverage = BuyAverage;
zBuyAverage.SetDefaultColor(GlobalColor("bull"));
zBuyAverage.setlineweight(2);
zBuyAverage.hidebubble();

plot zSellAverage = SellAverage;
zSellAverage.SetDefaultColor(GlobalColor("bear"));
zsellAverage.setlineweight(2);
zsellAverage.hidebubble();
I Appreciate this! Will be able to learn from the code you left in there too, thanks!
 
Here is a way to make it MTF

Code:
input UseChartAgg = yes;
input MTFAgg= AggregationPeriod.FIVE_MIN;

def chartagg = if Usechartagg==0 then MTFagg else getaggregationperiod();


def Close = Close(period=Chartagg);
Def Open = Open(period=Chartagg);
def Volume = Volume(period=Chartagg);

declare lower;


#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Steversteves
#//@version=5
#indicator("Buyer to Seller Volume (BSV)")

#BackgroundData(symbol, timeframe, data) =>
#    isLive = barstate.isrealtime
#    request.security(symbol, timeframe, data[isLive ? 1 : 0])[isLive ? 0 : 1]

#InSession(sessionTimes, sessionTimeZone=syminfo.timezone) =>
#    not na(time(timeframe.period, sessionTimes, sessionTimeZone))

def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
#addlabel(yes, chartmin + " min", color.yellow);


#higherTimeFrame = input.timeframe("D", title="Time Frame")
#AvgLength   = input.int(14, title="Average Length")
input AvgLength = 14;

#greencandle = (close > open)
#redcandle = (open > close)

def greencandle = (close > open);
def redcandle = (open > close);

#greencandlevolume = if greencandle
#    volume
#redcandlevolume = if redcandle
#    volume

def greencandlevolume = if greencandle then volume else 0;
def redcandlevolume = if redcandle then volume else 0;
   
#// Vol Over time

#BuyAverage = BackgroundData(syminfo.tickerid, higherTimeFrame, math.sum(greencandlevolume, AvgLength))
#SellAverage = BackgroundData(syminfo.tickerid, higherTimeFrame, math.sum(redcandlevolume, AvgLength))

def BuyAverage = sum(greencandlevolume, AvgLength);
def SellAverage = sum(redcandlevolume, AvgLength);


#buyvolmax = ta.highest(BuyAverage, AvgLength)
#buyvolmin = ta.lowest(BuyAverage, AvgLength)
#sellvolmax = ta.highest(SellAverage, AvgLength)
#sellvolmin = ta.lowest(SellAverage, AvgLength)

def buyvolmax = highest(BuyAverage, AvgLength);
def buyvolmin = lowest(BuyAverage, AvgLength);
def sellvolmax = highest(SellAverage, AvgLength);
def sellvolmin = lowest(SellAverage, AvgLength);

#bool buyvolmaxatmax = BuyAverage >= buyvolmax
#bool buyvolminatmin = BuyAverage <= buyvolmin
#bool sellvolmax2 = SellAverage >= sellvolmax
#bool sellvolmin2 = SellAverage >= sellvolmin

def buyvolmaxatmax = BuyAverage >= buyvolmax;
def buyvolminatmin = BuyAverage <= buyvolmin;
def sellvolmax2 = SellAverage >= sellvolmax;
def sellvolmin2 = SellAverage >= sellvolmin;


#// Color
#color volBullColorInput = input.color(color.green, "Bull")
#color volBearColorInput = input.color(color.red, "Bear")
#color neutralinput = input.color(color.gray, "Neutral")
#color maxbuyers = input.color(color.fuchsia, "Max Buyers")
#color minbuyers = input.color(color.black, "Max Sellers")
#color buyerneutral = input.color(color.black, "Buyers Neutral")


DefineGlobalColor("Bull", color.green);  #volBullColorInput
DefineGlobalColor("Bear", color.red);  #volBearColorInput
DefineGlobalColor("Neutral", color.gray);  #neutralinput
DefineGlobalColor("Max Buyers", color.magenta);  #maxbuyers
DefineGlobalColor("Max Sellers", color.black);  #minbuyers
DefineGlobalColor("Buyers Neutral", color.black);  #buyerneutral


#color buyavgcolor =  buyvolmaxatmax ? minbuyers : volBullColorInput
#color sellavgcolor = sellvolmax2 ? neutralinput : volBearColorInput


#// SMA Vol
#volavg = (BuyAverage + SellAverage) / 2
def volavg = (BuyAverage + SellAverage) / 2;

#sma = ta.sma(volavg, AvgLength)
input ma1_type =  AverageType.simple;
def sma = MovingAverage(ma1_type, volavg, AvgLength);

#plot(sma, "SMA", color=color.yellow)
plot zsma = sma;
zsma.SetDefaultColor(Color.yellow);
#zsma.setlineweight(1);
zsma.hidebubble();


#//plot(br, style=plot.style_histogram, color = pallette)

#plot(BuyAverage, "Buyer Volume", color = volBullColorInput, linewidth=2)
#plot(SellAverage, "Seller Volume", color = volBearColorInput, linewidth=2)

plot zBuyAverage = BuyAverage;
zBuyAverage.SetDefaultColor(GlobalColor("bull"));
zBuyAverage.setlineweight(2);
zBuyAverage.hidebubble();

plot zSellAverage = SellAverage;
zSellAverage.SetDefaultColor(GlobalColor("bear"));
zsellAverage.setlineweight(2);
zsellAverage.hidebubble();
 
Last edited by a moderator:
@MP432 : Just a quick FYI...

should be:
def chartagg = if Usechartagg == 1 then MTFagg else getaggregationperiod();
Seems to be working fine for me.
You want "Usechartagg" to be negated (=0) for MTFagg to utilized,
else getaggregationperiod() returns you to the current Chart agg.
 
Seems to be working fine for me.
You want "Usechartagg" to be negated (=0) for MTFagg to utilized,
else getaggregationperiod() returns you to the current Chart agg.
That makes sense, but, for some strange reason, I only get MTFagg when UseChartAgg == 1...Looks like I'll have to dig a little deeper and see if I'm missing something...Thanks again for the contribution and the clarification...
 

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