Battle of the Bands Indicator for ThinkorSwim

horserider

Well-known member
VIP
Just reviewing some old charts and ran across this one. BB,ATR and IV bands. Has some labels also. If anyone can make us of it here you go.

Code:
# Battle of the Bands
# Implied Volatility Bands
# ATR Bands
# Bollinger Bands
# Mobius
# Added ATR label. Added conditional color for labels. Altered bands on lower aggs. to 100% IV spread from mean. Altered coding structure to be more efficient.
# V02.10.17.2013

# Hide Study Name and Inputs ###########################
plot X = Double.NaN;                                   #
X.SetDefaultColor(CreateColor ( 0, 0, 0 ));            #
########################################################

declare hide_on_daily;

input n = 21;
input IvDailyBands = no;
input IvHourBands = no;
input IV15minBands = yes;
input AtrBands = no;
input BollingerBands = yes;
input DisplayLabels = yes;

def o = open;
def h = high;
def l = low;
def c = close;
def period = AggregationPeriod.DAY;
def hd = high(period = period);
def ld = low(period = period);
def cd = close(period = period);
def DaysMean = hl2(period = period);
def HoursMean = hl2(period = AggregationPeriod.HOUR);
def IVd = imp_Volatility(period = period);
def IV = if isNaN(IVd) then IV[1] else IVd;
def hIV = Highest(IV, 252);
def lIV = Lowest(IV, 252);
def IVpercentile = (IV - lIV) / (hIV - lIV);
AddLabel(DisplayLabels, "IV " + AsPercent(IV) +
                         "  IV Percentile: " + AsPercent(IVpercentile)
                       , if IsAscending(IV)
                         then Color.GREEN
                         else if IsDescending(IV)
                         then Color.RED
                         else Color.WHITE);
def ATR = Average(TrueRange(h, c, l), n);
def ATRd = Average(TrueRange(hd, cd, ld), n);
def SDatr = StDev(ATRd, 252);
def hATR = Highest(SDatr, 252);
def lATR = Lowest(SDatr, 252);
def ATRpercentile = (SDatr - lATR) / (hATR - lATR);
def DaysIV = IV / Sqrt(252);
def DaysIVSD = StDev(DaysIV, n);
def DaysIVValue = Round((DaysMean * DaysIV) / TickSize(), 0) * TickSize();
AddLabel(DisplayLabels, Concat("Days IV " + AsPercent(DaysIV), " Value $" + DaysIVValue),
                    if IsAscending(DaysIV)
                    then Color.GREEN
                    else if IsDescending(DaysIV)
                    then Color.RED
                    else Color.WHITE);
def HoursIV = IV / Sqrt(5796);
def HoursIVSD = StDev(HoursIV, n);
def HoursIVValue = Round((HoursMean * HoursIV) / TickSize(), 0) * TickSize();
AddLabel(DisplayLabels, Concat("Hours IV " + AsPercent(HoursIV), " Value $" + HoursIVValue),
                    if IsAscending(HoursIV)
                    then Color.GREEN
                    else if IsDescending(HoursIV)
                    then Color.RED
                    else Color.WHITE);
def IV15min = IV / Sqrt(23184);
def IV15minSD = StDev(IV15min, n);
def IV15mValue = Round((c * IV15min) / TickSize(), 0) * TickSize();
AddLabel(DisplayLabels, Concat("15min IV " + AsPercent(IV15min), " Value $" + IV15mValue),
                     if IsAscending(IV15min)
                     then Color.GREEN
                     else if IsDescending(IV15min)
                     then Color.RED
                     else Color.WHITE);

plot DaysIVhigh = DaysMean + ((DaysIVValue) / 2);
DaysIVhigh.SetDefaultColor(Color.ORANGE);
DaysIVhigh.SetHiding(!IvDailyBands);
DaysIVhigh.SetPaintingStrategy(PaintingStrategy.DASHES);
plot DaysIVlow = DaysMean - ((DaysIVValue) / 2);
DaysIVlow.SetDefaultColor(Color.ORANGE);
DaysIVlow.SetHiding(!IvDailyBands);
DaysIVlow.SetPaintingStrategy(PaintingStrategy.DASHES);
plot HoursIVhigh = Average(HoursMean + HoursIVValue, n);
HoursIVhigh.SetDefaultColor(Color.CYAN);
HoursIVhigh.SetHiding(!IvHourBands);
plot HoursIVlow = Average(HoursMean - HoursIVValue, n);
HoursIVlow.SetDefaultColor(Color.CYAN);
HoursIVlow.SetHiding(!IvHourBands);
plot IV15minhigh = Inertia(c + (c * IV15minSD), n);
IV15minhigh.SetDefaultColor(Color.YELLOW);
IV15minhigh.SetHiding(!IV15minBands);
plot IV15minlow = Inertia(c - (c * IV15minSD), n);
IV15minlow.SetDefaultColor(Color.YELLOW);
IV15minlow.SetHiding(!IV15minBands);
plot ATRup = Inertia(c + ATR, n);
ATRup.SetHiding(!AtrBands);
plot ATRdn = Inertia(c - ATR, n);
ATRdn.SetHiding(!AtrBands);

def SD = StDev(close, n);
def Avg = Average(close, n);

plot SDup = Avg + (2 * SD);
SDup.SetDefaultColor(Color.GREEN);
SDup.SetHiding(!BollingerBands);
plot SDdn = Avg + (-2 * SD);
SDdn.SetDefaultColor(Color.GREEN);
SDdn.SetHiding(!BollingerBands);

#AssignPriceColor(if (SDup < IV15minhigh) or
#                     (SDdn > IV15minlow)
#                  then Color.PLUM
#                  else Color.CURRENT);
AddLabel(DisplayLabels, "ATR Chart Agg. = $" + (Round(ATR / TickSize(), 0) * TickSize()) +
                        "  ATR Daily $" + (Round(ATRd / TickSize(), 0) * TickSize()) +
                        "  ATR Percentile: " + AsPercent(ATRpercentile),
                                  if ATR > ATR[1]
                                  then Color.GREEN
                                  else if ATR < ATR[1]
                                  then Color.RED
                                  else Color.WHITE);

# End Code Battle of the Bands
 
Can any make this in ThinkScript?
I am trying to make ATR Bands and I found some code but it isn't in ThinkScript. The parameters are below and here is an article that got my attention on the indicator https://www.incrediblecharts.com/indicators/atr_average_true_range_bands.php
  • Average True Range is calculated in accordance with J. Welles Wilder's formula.
  • The bands are calculated by adding/subtracting a multiple of Average True Range to the daily closing price.
  • For the HighLow option, the multiple of ATR is added to the daily Low, and subtracted from the daily High.
  • A ratchet mechanism ensures that the lower band only moves up in an up-trend and the upper band only moves down in a down-trend.
  • Plots are projected one day forward (e.g. the stop caculated from today's closing price is plotted for tomorrow)
Code:
//---- input parameters
extern int       TimeFrame = 0;
extern int       Length    = 21;
extern int       MA_Type   = 0;
extern int       Shift     = 0;
extern int       Price     = 0;
extern double    TimesATR  = 4.0;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle ( 0, DRAW_LINE, STYLE_DOT );
   SetIndexBuffer( 0, ExtMapBuffer1 );
   SetIndexStyle ( 1, DRAW_LINE, STYLE_DOT );
   SetIndexBuffer( 1, ExtMapBuffer2 );
   SetIndexStyle ( 2, DRAW_LINE, STYLE_DOT );
   SetIndexBuffer( 2, ExtMapBuffer3 );
  
   SetIndexLabel ( 0, Length + " KChanUp" );
   SetIndexLabel ( 1, Length + " KChanMid");
   SetIndexLabel ( 2, Length + " KChanLow");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars = IndicatorCounted();
   int    limit        = Bars - counted_bars;
   int    i = 0;
  
//----Check for possible errors
   if ( counted_bars < 0 ) return ( -1 );
      
//----Last counted bar will be recounted
   if ( counted_bars > 0 ) counted_bars-- ;
  
   double MA = 0;   
   double ATR =0; iATR ( NULL, TimeFrame, Length, i );
  
//---Main calculation Loop
   for (i = 0; i < limit; i++ )
      
       {
         MA  = iMA ( NULL, TimeFrame, Length, Shift, MA_Type, Price, i );
         ATR = iATR( NULL, TimeFrame, Length, i );
              
         ExtMapBuffer1[i] = MA + ATR * TimesATR ;
        
         ExtMapBuffer2[i] = MA;
                                  
         ExtMapBuffer3[i] = MA - ATR * TimesATR ;
       }                         

  
   return(0);
  }
//+------------------------------------------------------------------+
 

Similar threads

Top