CMx_Oscillator

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Moving average indicatorMovement directional index
0 Views
0 Downloads
0 Favorites
CMx_Oscillator
ÿþ//+------------------------------------------------------------------+

//|                                               CMx_Oscillator.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "CMx Oscillator"

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   1

//--- plot CMX

#property indicator_label1  "CMX"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrSteelBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input uint     InpPeriod   =  12;      // Period

input double   InpK        =  1.682;   // K

input double   InpL        =  18.0;    // L

//--- indicator buffers

double         BufferCMX[];

double         BufferDiff[];

double         BufferEMA[];

double         BufferKMA[];

double         BufferADX[];

//--- global variables

double         pK;

double         pL;

int            period_ema;

int            period_k;

int            handle_ema;

int            handle_kma;

int            handle_adx;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_ema=int(InpPeriod<2 ? 2 : InpPeriod);

   pK=(InpK<0.2 ? 0.2 : InpK);

   pL=(InpL==0 ? 0.1 : InpL);

   period_k=(int)floor(period_ema*pK+0.5);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferCMX,INDICATOR_DATA);

   SetIndexBuffer(1,BufferDiff,INDICATOR_CALCULATIONS);

   SetIndexBuffer(2,BufferADX,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferEMA,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferKMA,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"CMx Osc ("+(string)period_ema+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,8);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,423.8);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,261.8);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,161.8);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,3,61.8);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,4,-61.8);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,5,-161.8);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,6,-261.8);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,7,-423.8);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferCMX,true);

   ArraySetAsSeries(BufferDiff,true);

   ArraySetAsSeries(BufferADX,true);

   ArraySetAsSeries(BufferEMA,true);

   ArraySetAsSeries(BufferKMA,true);

//--- create MA handle

   ResetLastError();

   handle_ema=iMA(NULL,PERIOD_CURRENT,period_ema,0,MODE_EMA,PRICE_CLOSE);

   if(handle_ema==INVALID_HANDLE)

     {

      Print(__LINE__,": The iMA (",(string)period_ema,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_kma=iMA(NULL,PERIOD_CURRENT,period_k,0,MODE_EMA,PRICE_CLOSE);

   if(handle_kma==INVALID_HANDLE)

     {

      Print(__LINE__,": The iMA (",(string)period_k,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_adx=iADX(NULL,PERIOD_CURRENT,period_ema);

   if(handle_adx==INVALID_HANDLE)

     {

      Print("The iADX (",(string)period_ema,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//---

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

  {

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<fmax(period_ema,4)) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-period_k-1;

      ArrayInitialize(BufferCMX,EMPTY_VALUE);

      ArrayInitialize(BufferDiff,0);

      ArrayInitialize(BufferADX,0);

      ArrayInitialize(BufferEMA,0);

      ArrayInitialize(BufferKMA,0);

     }



//--- >43>B>2:0 40==KE

   int count=(limit>0 ? rates_total : 1),copied=0;

   copied=CopyBuffer(handle_ema,0,0,count,BufferEMA);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_kma,0,0,count,BufferKMA);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_adx,MAIN_LINE,0,count,BufferADX);

   if(copied!=count) return 0;



//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      double n=0;

      double mean=0;

      double meandev=0;

      double meandevp=0;

      BufferDiff[i]=BufferEMA[i]-BufferKMA[i];

      for(int j=(i+period_k-1); j>=i; j--)

        {

         mean+=BufferDiff[j];

         n++;

        }

      mean/=n;



      for(int j=(i+period_k-1); j>=i; j--)

         meandevp+=pow(BufferDiff[j]-mean,2);

      meandevp/=(n-(n>1 ? 1 : 0));

      meandev=sqrt(meandevp);

      double CCI=(meandev>0 ? (BufferDiff[i]-mean)/(meandev*0.015) : 0);

      double v=(pL*pK);

      BufferCMX[i]=(v!=0 ? CCI*BufferADX[i]/v : 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 ---