BrainTrend2Stop

Author: Copyright � 2005, BrainTrading Inc.
0 Views
0 Downloads
0 Favorites
BrainTrend2Stop
//+------------------------------------------------------------------+
//|                                              BrainTrend2Stop.mq5 |
//|                               Copyright © 2005, BrainTrading Inc |
//|                                      http://www.braintrading.com |
//+------------------------------------------------------------------+
//---- author of the indicator
#property copyright "Copyright © 2005, BrainTrading Inc."
//---- link to the website of the author
#property link      "http://www.braintrading.com/"
//---- Indicator Version Number
#property version   "1.00"
//---- drawing the indicator in the main window
#property indicator_chart_window 
//----four buffers are used for calculation and drawing the indicator
#property indicator_buffers 4
//---- only four plots are used
#property indicator_plots   4
//+----------------------------------------------+
//|  Parameters of drawing the bearish indicator |
//+----------------------------------------------+
//---- drawing the indicator 1 as a symbol
#property indicator_type1   DRAW_ARROW
//---- red color is used as the color of a bearish indicator line
#property indicator_color1  Red
//---- thickness of line of the indicator 1 is equal to 1
#property indicator_width1  1
//---- displaying of the bearish label of the indicator
#property indicator_label1  "BrainSell"
//+----------------------------------------------+
//|  Parameters of drawing the bullish indicator |
//+----------------------------------------------+
//---- drawing the indicator 2 as a line
#property indicator_type2   DRAW_ARROW
//---- blue color is used as the color of a bullish indicator line
#property indicator_color2  Blue
//---- thickness of line of the indicator 2 is equal to 1
#property indicator_width2  1
//---- displaying of the bullish label of the indicator
#property indicator_label2 "BrainBuy"
//+----------------------------------------------+
//|  Parameters of drawing the bearish indicator |
//+----------------------------------------------+
//---- drawing the indicator 3 as a symbol
#property indicator_type3   DRAW_LINE
//---- red color is used as the color of a bearish indicator line
#property indicator_color3  Red
//---- thickness of line of the indicator 3 is equal to 1
#property indicator_width3  1
//---- Indicator line is a solid one
#property indicator_style3 STYLE_SOLID
//---- Indicator line width is equal to 2
#property indicator_width3 2
//---- displaying of the bearish label of the indicator
#property indicator_label3  "Brain2Sell"
//+----------------------------------------------+
//|  Parameters of drawing the bullish indicator |
//+----------------------------------------------+
//---- drawing the indicator 4 as a symbol
#property indicator_type4   DRAW_LINE
//---- blue color is used as the color of a bullish indicator line
#property indicator_color4  Blue
//---- thickness of line of the indicator 4 is equal to 1
#property indicator_width4  1
//---- Indicator line is a solid one
#property indicator_style4 STYLE_SOLID
//---- Indicator line width is equal to 2
#property indicator_width4 2
//---- displaying of the bullish label of the indicator
#property indicator_label4 "Brain2Buy"

//+----------------------------------------------+
//| Input parameters of the indicator            |
//+----------------------------------------------+
input int ATR_Period=7;
//+----------------------------------------------+

//---- declaration of dynamic arrays that further 
// will be used as indicator buffers
double SellStopBuffer[];
double BuyStopBuffer[];
double SellStopBuffer_[];
double BuyStopBuffer_[];
//---
bool   river=true,river_;
int    glava,glava_,StartBars,p,P_,OldTrend;
double r,R_,artp_2,s,dartp,cecf,Emaxtra,Emaxtra_,Values_[],Values[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- initialization of global variables 
   s = 1.5;
   cecf = 0.7;
   dartp=7.0;
//----
   artp_2 = 0.5 * ATR_Period * (ATR_Period + 1.0);
   StartBars=ATR_Period+2;
   
//---- memory distribution for variables' arrays   
   if(ArrayResize(Values,ATR_Period)<ATR_Period)
      Print("Failed to distribute the memory for Values array");
   if(ArrayResize(Values_,ATR_Period)<ATR_Period)
      Print("Failed to distribute the memory for Values array_");

//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(0,SellStopBuffer,INDICATOR_DATA);
//---- shifting the start of drawing of the indicator 1
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartBars);
//--- create label to display in DataWindow
   PlotIndexSetString(0,PLOT_LABEL,"Brain2SellStop");
//---- indicator symbol
   PlotIndexSetInteger(0,PLOT_ARROW,159);
//---- indexing elements in the buffer as in timeseries
   ArraySetAsSeries(SellStopBuffer,true);

//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(1,BuyStopBuffer,INDICATOR_DATA);
//---- shifting the start of drawing of the indicator 2
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,StartBars);
//--- create label to display in DataWindow
   PlotIndexSetString(1,PLOT_LABEL,"Brain2BuyStop");
//---- indicator symbol
   PlotIndexSetInteger(1,PLOT_ARROW,159);
//---- indexing elements in the buffer as in timeseries
   ArraySetAsSeries(BuyStopBuffer,true);
   
//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(2,SellStopBuffer_,INDICATOR_DATA);
//---- shifting the start of drawing of the indicator 3
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,StartBars);
//--- create label to display in DataWindow
   PlotIndexSetString(2,PLOT_LABEL,"Brain2SellStop");
//---- indexing elements in the buffer as in timeseries
   ArraySetAsSeries(SellStopBuffer_,true);
//---- restriction to draw empty values for the indicator
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(3,BuyStopBuffer_,INDICATOR_DATA);
//---- shifting the start of drawing of the indicator 4
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,StartBars);
//--- create label to display in DataWindow
   PlotIndexSetString(3,PLOT_LABEL,"Brain2BuyStop");
//---- indexing elements in the buffer as in timeseries
   ArraySetAsSeries(BuyStopBuffer_,true);
//---- restriction to draw empty values for the indicator
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

//---- Setting the format of accuracy of displaying the indicator
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---- name for the data window and for the label of sub-windows 
   string short_name="BrainTrend2Stop";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//----   
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---- checking the number of bars to be enough for the calculation
   if(rates_total<StartBars) return(0);

//---- declaration of local variables    
   int bar,J,limit,Curr,to_copy;
   double ATR,widcha,TR,Spread,r1;
   double Weight,Series1,High,Low,range2;

//--- calculations of the necessary amount of data to be copied
           //the limit starting number for loop of bars recalculation
                               // and variables start initialization
   if(prev_calculated>rates_total || prev_calculated<=0)// checking for the first start of calculation of an indicator
     {
      limit=rates_total-StartBars; // starting number for calculation of all bars
      to_copy=rates_total; // calculated number of all bars
      Emaxtra=close[limit+1];
      glava=0;
      double T_Series2=close[limit+2];
      double T_Series1=close[limit+1];
      if(T_Series2>T_Series1)
           river=true;
      else river=false;

      TR=spread[limit]+high[limit]-low[limit];

      if(MathAbs(spread[limit]+high[limit]-T_Series1)>TR)
         TR=MathAbs(spread[limit]+high[limit]-T_Series1);

      if(MathAbs(low[limit]-T_Series1)>TR)
         TR=MathAbs(low[limit]-T_Series1);

      ArrayInitialize(Values,TR);
     }
   else
     {
      limit=rates_total-prev_calculated; // starting number for calculation of new bars
      to_copy=rates_total-prev_calculated+1; // calculated number of new bars
     }

//---- indexing elements in arrays, as in timeseries
   ArraySetAsSeries(open,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(spread,true);
   ArraySetAsSeries(Values,true);
   ArraySetAsSeries(Values_,true);

//---- restore values of the variables
   p=P_;
   r=R_;
   glava=glava_;
   Emaxtra=Emaxtra_;
   river=river_;
   ArrayCopy(Values,Values_,0,WHOLE_ARRAY);

//---- main cycle of calculation of the indicator
   for(bar=limit; bar>=0; bar--)
     {
      //---- memorize values of the variables before running at the current bar
      if(rates_total!=prev_calculated && bar==0)
        {
         glava_=glava;
         Emaxtra_=Emaxtra;
         river_=river;
         ArrayCopy(Values_,Values,0,WHOLE_ARRAY);
        }

      BuyStopBuffer[bar]=0.0;
      SellStopBuffer[bar]=0.0;
      BuyStopBuffer_[bar]=0.0;
      SellStopBuffer_[bar]=0.0;

      Spread=spread[bar]*_Point;

      High=high[bar];
      Low=low[bar];
      Series1=close[bar+1];
      TR=Spread+High-Low;

      if(MathAbs(Spread+High-Series1)>TR)
         TR=MathAbs(Spread+High-Series1);

      if(MathAbs(Low-Series1)>TR)
         TR=MathAbs(Low-Series1);

      Values[glava]=TR;

      ATR=0;
      Weight=ATR_Period;
      Curr=glava;

      for(J=0; J<=ATR_Period-1; J++)
        {
         ATR+=Values[Curr]*Weight;
         Weight-=1.0;
         Curr--;
         if(Curr==-1) Curr=ATR_Period-1;
        }

      ATR=2.0*ATR/(dartp *(dartp+1.0));
      glava++;
      
      range2=ATR*s/4;

      if(glava==ATR_Period) glava=0;

      widcha=cecf*ATR;

      if(river && Low<Emaxtra-widcha)
        {
         river=false;
         Emaxtra=Spread+High;
        }

      if(!river && Spread+High>Emaxtra+widcha)
        {
         river=true;
         Emaxtra=Low;
        }

      if(river && Low>Emaxtra)
        {
         Emaxtra=Low;
        }

      if(!river && Spread+High<Emaxtra)
        {
         Emaxtra=Spread+High;
        }

      if(river)
        {
         r1=Low-range2;
         if(r1<r && OldTrend>0) r1=r;
         
         BuyStopBuffer[bar]=r1;
         BuyStopBuffer_[bar]=r1;
         
         if(bar!=0)
          {
           r=r1;
           OldTrend=+1;
          }
         
        }
      else
        {
         r1=High+range2;
         if(r1>r && OldTrend<0) r1=r;
         
         SellStopBuffer[bar]=r1;
         SellStopBuffer_[bar]=r1;
         
         if(bar!=0)
          {
           r=r1;
           OldTrend=-1;
          }
        }
     }

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