iKarakatica

Author: Copyright © 2018, Vladimir Karputov
Indicators Used
Movement directional index
0 Views
0 Downloads
0 Favorites
iKarakatica
ÿþ//+------------------------------------------------------------------+

//|                                                  iKarakatica.mq5 |

//|                              Copyright © 2018, Vladimir Karputov |

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

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

#property copyright "Copyright © 2018, Vladimir Karputov"

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

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   2

//--- plot BUY 

#property indicator_label1  "BUY" 

#property indicator_type1   DRAW_ARROW 

#property indicator_color1  clrBlue 

#property indicator_width1  1 

//--- plot SELL

#property indicator_label2  "SELL" 

#property indicator_type2   DRAW_ARROW 

#property indicator_color2  clrRed

#property indicator_width2  1  

//--- input parameters 

input int      Inp_ADX_adx_period= 7;     // averaging period

input bool     Inp_Filter        = true ; // filter

//--- indicator buffers 

double         BuyBuffer[];

double         SellBuffer[];

double         DI_plusBuffer[];

double         DI_minusBuffer[];



int    handle_iADX;        // variable for storing the handle of the iADX indicator

int    bars_calculated=0;  // we will keep the number of values in the Average Directional Movement Index indicator 

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

//| Custom indicator initialization function                         | 

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

int OnInit()

  {

//--- assignment of arrays to indicator buffers 

   SetIndexBuffer(0,BuyBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SellBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,DI_plusBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,DI_minusBuffer,INDICATOR_CALCULATIONS);

//--- Define the symbol code for drawing in PLOT_ARROW 

   PlotIndexSetInteger(0,PLOT_ARROW,241);

   PlotIndexSetInteger(1,PLOT_ARROW,242);

//--- Set the vertical shift of arrows in pixels 

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,5);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-5);

//--- Set as an empty value 0 

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- create handle of the indicator iADX

   handle_iADX=iADX(Symbol(),Period(),Inp_ADX_adx_period);

//--- if the handle is not created 

   if(handle_iADX==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code 

      PrintFormat("Failed to create handle of the iADX indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//--- name of the indicator on a chart 

   string short_name=StringFormat("iKarakatica(%d)",Inp_ADX_adx_period);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- normal initialization of the indicator     

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

  {

//--- number of values copied from the iADX indicator 

   int values_to_copy;

//--- determine the number of values calculated in the indicator 

   int calculated=BarsCalculated(handle_iADX);

   if(calculated<=0)

     {

      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());

      return(0);

     }

//--- if it is the first start of calculation of the indicator or if the number of values in the iADX indicator changed 

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history) 

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the iADXBuffer array is greater than the number of values in the iADX indicator for symbol/period, then we don't copy everything  

      //--- otherwise, we copy less than the size of indicator buffers 

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate() 

      //--- for calculation not more than one bar is added 

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the array with values of the Average Directional Movement Index indicator 

//--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation 

   if(!FillArraysFromBuffers(DI_plusBuffer,DI_minusBuffer,handle_iADX,values_to_copy))

      return(0);

//--- memorize the number of values in the Average Directional Movement Index indicator 

   bars_calculated=calculated;

   int limit=prev_calculated-1;

   if(limit<0)

      limit=0;



   if(prev_calculated==0)

      for(int i=0;i<Inp_ADX_adx_period;i++)

        {

         BuyBuffer[i]=EMPTY_VALUE;

         SellBuffer[i]=EMPTY_VALUE;

        }



   if(limit<Inp_ADX_adx_period)

      limit=Inp_ADX_adx_period;



   static int ltr;

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

     {

      if(Inp_Filter)

        {

         if(BuyBuffer[i-1]!=0.0 && BuyBuffer[i-1]!=EMPTY_VALUE)

            ltr=1;

         if(SellBuffer[i-1]!=0.0 && SellBuffer[i-1]!=EMPTY_VALUE)

            ltr=2;

        }



      if(close[i]>close[i-Inp_ADX_adx_period] && DI_plusBuffer[i]>DI_minusBuffer[i] && ((Inp_Filter && ltr!=1) || !Inp_Filter))

         BuyBuffer[i]=low[i];

      else

         BuyBuffer[i]=EMPTY_VALUE;



      if(close[i]<close[i-Inp_ADX_adx_period] && DI_plusBuffer[i]<DI_minusBuffer[i] && ((Inp_Filter && ltr!=2) || !Inp_Filter))

         SellBuffer[i]=high[i];

      else

         SellBuffer[i]=EMPTY_VALUE;

     }

//--- return the prev_calculated value for the next call 

   return(rates_total);

  }

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

//| Filling indicator buffers from the iADX indicator                | 

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

bool FillArraysFromBuffers(double &DIplus_values[],   // indicator buffer for DI+ 

                           double &DIminus_values[],  // indicator buffer for DI- 

                           int ind_handle,            // handle of the iADX indicator 

                           int amount                 // number of copied values 

                           )

  {

//--- reset error code 

   ResetLastError();



//--- fill a part of the DI_plusBuffer array with values from the indicator buffer that has index 1 

   if(CopyBuffer(ind_handle,PLUSDI_LINE,0,amount,DIplus_values)<0)

     {

      //--- if the copying fails, tell the error code 

      PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated 

      return(false);

     }



//--- fill a part of the DI_minusBuffer array with values from the indicator buffer that has index 2 

   if(CopyBuffer(ind_handle,MINUSDI_LINE,0,amount,DIminus_values)<0)

     {

      //--- if the copying fails, tell the error code 

      PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated 

      return(false);

     }

//--- everything is fine 

   return(true);

  }

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

//| Indicator deinitialization function                              | 

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

void OnDeinit(const int reason)

  {

//--- clear the chart after deleting the indicator 

   Comment("");

  }

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

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