Turtle Channel

Author: Copyright © 2019, Vladimir Karputov
Price Data Components
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Turtle Channel
ÿþ//+------------------------------------------------------------------+

//|                                               Turtle Channel.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot Upper

#property indicator_label1  "Upper"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrLawnGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot Lower

#property indicator_label2  "Lower"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDarkGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- input parameters

input int      Inp_Upper_ma_period=20; // Upper band averaging period

input int      Inp_Lower_ma_period=10; // Lower band averaging period

//--- indicator buffers

double         ExtUpperBuffer[];

double         ExtLowerBuffer[];

//---

int            ExtBiggest;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtUpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtLowerBuffer,INDICATOR_DATA);

//---

   if(Inp_Upper_ma_period<=0)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "0@0<5B@ \"Upper band averaging period\" =5 <>65B 1KBL <5=LH5 8;8 @025= =C;N!":

                      "The parameter \"Upper band averaging period\" can not be less than or equal to zero!";

      //--- when testing, we will only output to the log about incorrect input parameters

      if(MQLInfoInteger(MQL_TESTER))

        {

         Print(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_FAILED);

        }

      else // if the Expert Advisor is run on the chart, tell the user about the error

        {

         Alert(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_PARAMETERS_INCORRECT);

        }

     }

   if(Inp_Lower_ma_period<=0)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "0@0<5B@ \"Lower band averaging period\" =5 <>65B 1KBL <5=LH5 8;8 @025= =C;N!":

                      "The parameter \"Lower band averaging period\" can not be less than or equal to zero!";

      //--- when testing, we will only output to the log about incorrect input parameters

      if(MQLInfoInteger(MQL_TESTER))

        {

         Print(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_FAILED);

        }

      else // if the Expert Advisor is run on the chart, tell the user about the error

        {

         Alert(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_PARAMETERS_INCORRECT);

        }

     }

   if(Inp_Upper_ma_period>Inp_Lower_ma_period)

      ExtBiggest=Inp_Upper_ma_period;

   else

      ExtBiggest=Inp_Lower_ma_period;

//--- indicator digits

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtBiggest+1);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtBiggest+1);

//---

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//--- name for Dindicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,"Turtle Channel("+

                      IntegerToString(Inp_Upper_ma_period)+","+IntegerToString(Inp_Lower_ma_period)+")");

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| 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[])

  {

//--- check for data

   if(rates_total<ExtBiggest+1)

      return(0);



   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

//--- main cycle

   for(int i=limit;i<rates_total;i++)

     {

      double max=DBL_MIN;

      double min=DBL_MAX;

      //--- recursion

      for(int j=i-Inp_Upper_ma_period;j<=i;j++)

        {

         if(j<0)

           {

            ExtUpperBuffer[i]=0.0;

            continue;

           }

         //--- search for maximum prices

         if(high[j]>max)

            max=high[j];

        }

      if(max==DBL_MIN)

         ExtUpperBuffer[i]=0.0;

      else

         ExtUpperBuffer[i]=max;

      //--- recursion

      for(int j=i-Inp_Lower_ma_period;j<=i;j++)

        {

         if(j<0)

           {

            ExtLowerBuffer[i]=0.0;

            continue;

           }

         //--- search for minimum prices

         if(low[j]<min)

            min=low[j];

        }

      if(min==DBL_MAX)

         ExtLowerBuffer[i]=0.0;

      else

         ExtLowerBuffer[i]=min;

     }

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