#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#
# Revisions by Tidan
# v1.3
declare lower;
# OBV
input useObvSmoothing = yes;
input SmoothingLength = 5;
input SmoothingType = AverageType.EXPONENTIAL;
input colorUsingMidline = yes;
input showBands = yes;
def OBVdata = TotalSum(Sign(close - close[1]) * volume);
def OBVSmoothed = MovingAverage(SmoothingType, OBVdata, SmoothingLength);
plot OBV = if useObvSmoothing then OBVSmoothed else OBVdata;
#plot obv = obvdata; #for testing discrepencies between obv and this version.
# BB
input bbDisplace = 0;
input bbLength = 20;
input bbNum_Dev_Dn = -2.0;
input bbNum_Dev_up = 2.0;
input bbAverageType = AverageType.SIMPLE;
def bbsDev = StDev(data = OBV[-bbDisplace], length = bbLength);
plot bbMidLine = MovingAverage(bbAverageType, data = OBV[-bbDisplace], length = bbLength);
plot bbLowerBand = bbMidLine + bbNum_Dev_Dn * bbsDev;
plot bbUpperBand = bbMidLine + bbNum_Dev_up * bbsDev;
bbLowerBand.AssignValueColor(Color.VIOLET);
bbLowerBand.setHiding(!showBands);
bbMidLine.SetDefaultColor(Color.LIGHT_GRAY);
bbMidLine.SetStyle(Curve.SHORT_DASH);
bbMidline.setHiding(!showBands);
bbUpperBand.AssignValueColor(Color.VIOLET);
bbUpperBand.setHiding(!showBands);
def obvBB = if !colorUsingMidline and OBV > bbUpperBand then 1 else if !colorUsingMidline and obv < bblowerBand then 2 else if !colorUsingMidline and obv > obv[1] then 3 else if !colorUsingMidline and OBV < obv[1] then 4 else 0;
def obvML = if colorUsingMidline and OBV > bbUpperBand then 1 else if colorUsingMidline and OBV < bbLowerBand then 2 else if colorUsingMidline and obv > bbMidLine then 3 else if colorUsingMidline and OBV < bbMidLine then 4 else 0;
OBV.AssignValueColor(if obvML == 1 then color.orange else if obvML == 2 then Color.orange else if obvML == 3 then color.green else if obvML == 4 then Color.red else if obvBB == 1 then color.orange else if obvBB == 2 then color.orange else if obvBB == 3 then color.green else if obvBB == 4 then color.red else color.gray);
# end of code