Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    ValBands Indicator

    Indicators/Strategies/Analyzers
    1
    2
    203
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      dasch last edited by

      Here is the valbands indicator converted from mq5.

      import backtrader as bt
      
      
      class ValBandsIndicator(bt.Indicator):
      
          '''
          Candlesticks length volatility indicator
      
          - histo - candlestick length in pips
          - lines are averages from the candlesticks length with
            standard deviation
      
          Settings are analogous to the Bollinger Channel
      
      
          Usage:
          https://www.mql5.com/en/code/687
          http://forex-strategies-revealed.com/money-management/keep-every-pip
      
              It is useful for tracking gaps on the market. The gap
              contains everything that is above the top line
              - crossing the upper border - a) �� (towards a position)
                                              b) SL (against a position)
                                              c) entry prohibition at this point
              - crossing the lower border - flat
              - crossing the lower border upwards - beginning of a trend
      
              Histo: the length of the candle in pips.
              Bands: the average of the candle length with standard deviation.
              - Top: reading above it suggests that a candle is taller than usual,
                  and it's the time to Take profit. (Also, don't open any new
                  trades there).
              - Mid: reading close to it - an active trend.
              - Bot: reading below it - ranging market. (Crossing the pink line
                  from below - beginning of a trend).
      
          Source:
      
              //+------------------------------------------------------------------------+
              //|                                                         Val_Bands.mq5  |
              //|                                             Copyright � 2010, Fomenko  |
              //|                                                http://faa1947@mail.ru  |
              //+------------------------------------------------------------------------+
              //|   Candlesticks length volatility indicator:                            |
              //|      - histogram - candlestick length in pips;                         |
              //|      - lines are averages from the candlesticks length with standard   |
              //|        deviation;                                                      |
              //|    Settings are analogous to the Bollinger Channel;                    |
              //|    Usage. It is useful for tracking gaps on the market. The gap        |
              //|                     contains everything that is above the yellow line; |
              //|      - crossing the upper border - �) �� (towards a position);         |
              //|                                    b) SL (against a position);         |
              //|                                    c) entry prohibition at this point; |
              //|      - crossing the lower border - flat;                               |
              //|      - crossing the lower border upwards - beginning of a trend.       |
              //+------------------------------------------------------------------------+
              #property copyright "Copyright � 2010, Fomenko"
              #property link      "http://faa1947@mail.ru"
              //---- indicator version
              #property version   "1.00"
              //---- drawing the indicator in a separate window
              #property indicator_separate_window
              //---- number of indicator buffers 5
              #property indicator_buffers 5
              //---- four plots are used in total
              #property indicator_plots   4
              //+-----------------------------------+
              //|  Indicator drawing parameters     |
              //+-----------------------------------+
              //---- drawing the indicator as a color histogram
              #property indicator_type1 DRAW_COLOR_HISTOGRAM
              //---- the following colors are used in the four color histogram
              #property indicator_color1 Gray,Red,Lime,Yellow
              //---- indicator line is a solid one
              #property indicator_style1 STYLE_SOLID
              //---- Indicator line width is equal to 2
              #property indicator_width1 2
              //---- displaying the indicator label
              #property indicator_label1 "Val Histogram"
      
              //---- drawing the indicator as a line
              #property indicator_type2 DRAW_LINE
              //---- lime color is used for the line
              #property indicator_color2 Lime
              //---- the indicator line is a dash-dotted curve
              #property indicator_style2 STYLE_DASHDOTDOT
              //---- indicator line width is equal to 1
              #property indicator_width2 1
              //---- displaying the line label
              #property indicator_label2  "Mov"
      
              //---- drawing the indicator as a line
              #property indicator_type3 DRAW_LINE
              //---- yellow color is used for the line
              #property indicator_color3 Yellow
              //---- the indicator line is a dash-dotted curve
              #property indicator_style3 STYLE_DASHDOTDOT
              //---- indicator line width is equal to 1
              #property indicator_width3 1
              //---- displaying the line label
              #property indicator_label3  "Up"
      
              //---- drawing the indicator as a line
              #property indicator_type4 DRAW_LINE
              //---- use red color for the line
              #property indicator_color4 Red
              //---- the indicator line is a dash-dotted curve
              #property indicator_style4 STYLE_DASHDOTDOT
              //---- indicator line width is equal to 1
              #property indicator_width4 1
              //---- displaying the line label
              #property indicator_label4  "Down"
      
              //+-----------------------------------+
              //|  Declaration of constants         |
              //+-----------------------------------+
              #define RESET  0 // the constant for getting the command for the indicator recalculation back to the terminal
      
              //+-----------------------------------+
              //|  Indicator input parameters       |
              //+-----------------------------------+
              input int BandsPeriod=14;         // Calculation period
              input double BandsDeviations=1.0; // Standard deviation size
              //+-----------------------------------+
              //---- declaration of the integer variables for the start of data calculation
              int min_rates_total;
              //---- declaration of integer variables for the indicators handles
              int HMA_Handle,LMA_Handle;
              //---- declaration of dynamic arrays that
              //---- will be used as indicator buffers
              double UpBuffer[],DownBuffer[],MaBuffer[],ValBuffer[],ColorValBuffer[];
              //+------------------------------------------------------------------+
              //| Val_Bands indicator initialization function                      |
              //+------------------------------------------------------------------+
              void OnInit()
              {
              //---- initialization of variables of the start of data calculation
              min_rates_total=BandsPeriod;
      
              //---- getting handle of the iMA indicator
              HMA_Handle=iMA(NULL,0,BandsPeriod,0,MODE_SMA,PRICE_HIGH);
              if(HMA_Handle==INVALID_HANDLE) Print(" Failed to get handle of the iMA indicator");
      
              //---- getting handle of the iMA indicator
              LMA_Handle=iMA(NULL,0,BandsPeriod,0,MODE_SMA,PRICE_LOW);
              if(LMA_Handle==INVALID_HANDLE) Print(" Failed to get handle of the iMA indicator");
      
              //---- set ValBuffer[] dynamic array as an indicator buffer
              SetIndexBuffer(0,ValBuffer,INDICATOR_DATA);
              //---- performing the shift of the beginning of the indicator drawing
              PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
              //---- setting the indicator values that won't be visible on a chart
              PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
              //---- indexing the elements in buffers as timeseries
              ArraySetAsSeries(ValBuffer,true);
      
              //---- set ColorValBuffer[] dynamic array as an indicator buffer
              SetIndexBuffer(1,ColorValBuffer,INDICATOR_COLOR_INDEX);
              //---- performing the shift of the beginning of the indicator drawing
              PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
              //---- indexing the elements in buffers as timeseries
              ArraySetAsSeries(ColorValBuffer,true);
      
              //---- set MABuffer[] dynamic array as an indicator buffer
              SetIndexBuffer(2,MaBuffer,INDICATOR_DATA);
              //---- performing the shift of the beginning of the indicator drawing
              PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,min_rates_total);
              //---- setting the indicator values that won't be visible on a chart
              PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
              //---- indexing the elements in buffers as timeseries
              ArraySetAsSeries(MaBuffer,true);
      
              //---- set UpBuffer[] dynamic array as an indicator buffer
              SetIndexBuffer(3,UpBuffer,INDICATOR_DATA);
              //---- performing the shift of the beginning of the indicator drawing
              PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,min_rates_total);
              //---- setting the indicator values that won't be visible on a chart
              PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);
              //---- indexing the elements in buffers as timeseries
              ArraySetAsSeries(UpBuffer,true);
      
              //---- set DownBuffer[] dynamic array as an indicator buffer
              SetIndexBuffer(4,DownBuffer,INDICATOR_DATA);
              //---- performing the shift of the beginning of the indicator drawing
              PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,min_rates_total);
              //---- setting the indicator values that won't be visible on a chart
              PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,EMPTY_VALUE);
              //---- indexing the elements in buffers as timeseries
              ArraySetAsSeries(DownBuffer,true);
      
              //---- initializations of a variable for the indicator short name
              string shortname;
              StringConcatenate(shortname,"Val_Bands( ",BandsPeriod,", ",BandsDeviations," )");
              //---- creating a name for displaying in a separate sub-window and in a tooltip
              IndicatorSetString(INDICATOR_SHORTNAME,shortname);
              //---- determination of accuracy of displaying the indicator values
              IndicatorSetInteger(INDICATOR_DIGITS,0);
              //---- initialization end
              }
              //+------------------------------------------------------------------+
              //| Val_Bands iteration function                                     |
              //+------------------------------------------------------------------+
              int OnCalculate(const int rates_total,    // number of bars in history at the current tick
                              const int prev_calculated,// number of bars calculated at previous call
                              const datetime &time[],
                              const double &open[],
                              const double &high[],
                              const double &low[],
                              const double &close[],
                              const long &tick_volume[],
                              const long &volume[],
                              const int &spread[])
              {
              //---- checking the number of bars to be enough for the calculation
              if(BarsCalculated(HMA_Handle)<rates_total
                  || BarsCalculated(LMA_Handle)<rates_total
                  || rates_total<min_rates_total)
                  return(RESET);
      
              //---- declarations of local variables
              int to_copy,limit,bar,k;
              double HMA[],LMA[];
              double sum,oldval,newres,deviation;
      
              //---- calculations of the necessary amount of data to be copied
              //---- and the 'limit' starting index for the bars recalculation loop
              if(prev_calculated>rates_total || prev_calculated<=0)// checking for the first start of the indicator calculation
                  {
                  to_copy=rates_total;                 // calculated number of all bars
                  limit=rates_total-min_rates_total-1; // starting index for calculation of all bars
                  }
              else
                  {
                  limit=rates_total-prev_calculated;  // starting index for calculation of new bars
                  to_copy=limit+1;                    // calculated number of new bars only
                  }
      
              //--- copy newly appeared data in the arrays
              if(CopyBuffer(HMA_Handle,0,0,to_copy,HMA)<=0) return(RESET);
              if(CopyBuffer(LMA_Handle,0,0,to_copy,LMA)<=0) return(RESET);
      
              //---- indexing elements in arrays as timeseries
              ArraySetAsSeries(HMA,true);
              ArraySetAsSeries(LMA,true);
              ArraySetAsSeries(high,true);
              ArraySetAsSeries(low,true);
      
              //---- first indicator calculation loop
              for(bar=limit; bar>=0 && !IsStopped(); bar--)
                  {
                  ValBuffer[bar]=(high[bar]-low[bar])/_Point;
                  MaBuffer[bar]=(HMA[bar]-LMA[bar])/_Point;
                  }
      
              //---- the second indicator calculation loop
              for(bar=limit; bar>=0 && !IsStopped(); bar--)
                  {
                  sum=0.0;
                  k=bar+BandsPeriod-1;
                  oldval=MaBuffer[bar];
      
                  while(k>=bar)
                      {
                      newres=ValBuffer[k]-oldval;
                      sum+=newres*newres;
                      k--;
                      }
      
                  deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
                  UpBuffer[bar]=oldval+deviation;
                  DownBuffer[bar]=oldval-deviation;
      
                  ColorValBuffer[bar]=0;
      
                  if(ValBuffer[bar]>UpBuffer[bar]) ColorValBuffer[bar]=3;
                  else if(ValBuffer[bar]>MaBuffer[bar]) ColorValBuffer[bar]=2;
                  else if(ValBuffer[bar]>DownBuffer[bar]) ColorValBuffer[bar]=1;
                  }
              //----
              return(rates_total);
              }
              //+------------------------------------------------------------------+
      
          '''
      
          lines = ('histo', 'top', 'mid', 'bot',)
      
          params = dict(
              period=14,
              devfactor=1.0,
              point=0.0001  # 10**pip_location
          )
      
          plotlines = dict(
              histo=dict(_method='bar', alpha=0.5, width=0.1),
              top=dict(),
              mid=dict(),
              bot=dict()
          )
      
          def __init__(self):
              self.hma = bt.ind.SMA(
                  self.data.high, period=self.p.period)
              self.lma = bt.ind.SMA(
                  self.data.low, period=self.p.period)
              self.ma = (self.hma - self.lma) / self.p.point
              self.stddev = self.p.devfactor * bt.ind.StdDev(
                  self.data, self.ma, period=self.p.period)
              self.l.histo = (self.data.high - self.data.low) / self.p.point
              self.l.mid = self.ma
              self.l.top = self.ma + self.stddev
              self.l.bot = self.ma - self.stddev
      
      
      1 Reply Last reply Reply Quote 2
      • D
        dasch last edited by

        Bildschirmfoto 2021-08-30 um 10.09.07.png

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors