BPT_ATR_EVO

Author: Copyright 2023, Giorgio Scarabello - Blueprint Trading
0 Views
0 Downloads
0 Favorites
BPT_ATR_EVO
//+------------------------------------------------------------------+
//|                                                  BPT_ATR_EVO.mq5 |
//|           Copyright 2023, Giorgio Scarabello - Blueprint Trading |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Giorgio Scarabello - Blueprint Trading"
#property version   "1.00"
#property description   "BPT Average true range"
#property description   "by Blueprint Trading"
#property description   "Home of profitability"
//--- indicator settings
#property indicator_separate_window

#property indicator_buffers 6
#property indicator_plots   2


#property indicator_type1   DRAW_COLOR_HISTOGRAM
#property indicator_color1  clrDodgerBlue, clrRoyalBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  5
#property indicator_label1  "BPT_ATR_EVO A"
//
#property indicator_type2   DRAW_COLOR_LINE
#property indicator_color2  clrChartreuse, clrAzure
#property indicator_style2  STYLE_DOT
#property indicator_width2  1
#property indicator_label2  "BPT_ATR_EVO B"

//--- input parameters
input int InpAtrPeriod=3;     // ATR period A
input int InpAtrPeriodB=89;   // ATR period B

//--- indicator buffers
double    ATRBuffer[];
double    ATRBufferCol[];
double    TRBuffer[];
double    ATRBufferB[];
double    ATRBufferBCol[];
double    TRBufferB[];

int       PeriodATR;
int       PeriodATRB;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- check for input value
   if(InpAtrPeriod<=0 || InpAtrPeriod>=InpAtrPeriodB){
      PeriodATR=3;
      PrintFormat("Incorrect input parameter ATR period A = %d. Indicator will use value %d for calculations.",InpAtrPeriod,PeriodATR);
   }
   else{
      PeriodATR=InpAtrPeriod;
   }
   
   if(InpAtrPeriodB<=0){
      PeriodATRB=34;
      PrintFormat("Incorrect input parameter ATR period B = %d. Indicator will use value %d for calculations.",InpAtrPeriodB,PeriodATRB);
   }
   else{
      PeriodATRB=InpAtrPeriodB;
   }
   
//--- indicator buffers mapping
   SetIndexBuffer(0,ATRBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ATRBufferCol,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,ATRBufferB,INDICATOR_DATA);
   SetIndexBuffer(3,ATRBufferBCol,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(4,TRBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,TRBufferB,INDICATOR_CALCULATIONS);
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpAtrPeriod);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpAtrPeriodB);
//--- name for DataWindow and indicator subwindow label
   string short_name=StringFormat("BPT_ATR_EVO(%d - %d)",PeriodATR, PeriodATRB);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,"BPT_ATR_EVO A("+IntegerToString(PeriodATR,0,' ')+")");
   PlotIndexSetString(1,PLOT_LABEL,"BPT_ATR_EVO B("+IntegerToString(PeriodATRB,0,' ')+")");
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                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[])
  {
   if(rates_total<=PeriodATR || rates_total<=PeriodATRB) return(0);

   int i,start;
//--- preliminary calculations
   if(prev_calculated==0){
      ArrayInitialize(ATRBuffer,0.0);
      ArrayInitialize(ATRBufferCol,0.0);
      ArrayInitialize(TRBuffer,0.0);
      ArrayInitialize(ATRBufferB,0.0);
      ArrayInitialize(ATRBufferBCol,0.0);
      ArrayInitialize(TRBufferB,0.0);
      //
      start=PeriodATRB+1;
   }
   else{
      start=prev_calculated-1;
   }
   
//--- the main loop of calculations
   for(i=start; i<rates_total && !IsStopped(); i++){
      TRBuffer[i]=MathAbs(MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]));
      ATRBuffer[i]=ATRBuffer[i-1]+(TRBuffer[i]-TRBuffer[i-PeriodATR])/PeriodATR;
      //
      TRBufferB[i]=TRBuffer[i];
      ATRBufferB[i]=ATRBufferB[i-1]+(TRBufferB[i]-TRBufferB[i-PeriodATRB])/PeriodATRB;
      //
      if(ATRBuffer[i]>ATRBufferB[i]){
         ATRBufferCol[i]=0.0;
         ATRBufferBCol[i]=1.0;
      }
      else if(ATRBuffer[i]<=ATRBufferB[i]){
         ATRBufferCol[i]=1.0;
         ATRBufferBCol[i]=0.0;
      }
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---