Wavetrend_Oscillator

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

//|                                         Wavetrend_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 "Wavetrend oscillator"

#property indicator_separate_window

#property indicator_buffers 9

#property indicator_plots   3

//--- plot WT1

#property indicator_label1  "WT1"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot WT2

#property indicator_label2  "WT2"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot WT3

#property indicator_label3  "WT3"

#property indicator_type3   DRAW_COLOR_HISTOGRAM

#property indicator_color3  clrBlue,clrCoral,clrDarkGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  8

//--- input parameters

input uint     InpPeriodCH    =  10;      // Channel period

input uint     InpPeriodAVG   =  21;      // Avg period

input uint     InpPeriodSig   =  4;       // Signal period

input double   InpOverbought2 =  53.0;    // Upper overbought

input double   InpOverbought1 =  50.0;    // Lower Overbought

input double   InpOversold1   = -50.0;    // Upper Oversold

input double   InpOversold2   = -53.0;    // Lower oversold

//--- indicator buffers

double         BufferWT1[];

double         BufferWT2[];

double         BufferWT3[];

double         BufferColors[];

double         BufferRaw1[];

double         BufferRaw2[];

double         BufferPrice[];

double         BufferEMA1[];

double         BufferEMA2[];

//--- global variables

double         overbought2;

double         overbought1;

double         oversold1;

double         oversold2;

int            period_ch;

int            period_avg;

int            period_sig;

int            period_max;

int            handle_ma1;

int            handle_maP;

//--- includes

#include <MovingAverages.mqh>

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_ch=int(InpPeriodCH<1 ? 1 : InpPeriodCH);

   period_avg=int(InpPeriodAVG<1 ? 1 : InpPeriodAVG);

   period_sig=int(InpPeriodSig<1 ? 1 : InpPeriodSig);

   period_max=fmax(period_avg,fmax(period_ch,period_sig));

   overbought1=(InpOverbought1<0 ? 0 : InpOverbought1);

   overbought2=(InpOverbought2<=overbought1 ? overbought1+1.0 : InpOverbought2);

   oversold1=(InpOversold1>0 ? 0 : InpOversold1);

   oversold2=(InpOversold2>=oversold1 ? oversold1-1.0 : InpOversold2);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferWT1,INDICATOR_DATA);

   SetIndexBuffer(1,BufferWT2,INDICATOR_DATA);

   SetIndexBuffer(2,BufferWT3,INDICATOR_DATA);

   SetIndexBuffer(3,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(4,BufferRaw1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferRaw2,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferPrice,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,BufferEMA1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferEMA2,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Wavetrend oscillator ("+(string)period_ch+","+(string)period_avg+","+(string)period_sig+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,4);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,overbought2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,overbought1);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,oversold1);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,3,oversold2);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferWT1,true);

   ArraySetAsSeries(BufferWT2,true);

   ArraySetAsSeries(BufferWT3,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferRaw1,true);

   ArraySetAsSeries(BufferRaw2,true);

   ArraySetAsSeries(BufferPrice,true);

   ArraySetAsSeries(BufferEMA1,true);

   ArraySetAsSeries(BufferEMA2,true);

//--- create MA's handles

   ResetLastError();

   handle_ma1=iMA(NULL,PERIOD_CURRENT,period_ch,0,MODE_EMA,PRICE_TYPICAL);

   if(handle_ma1==INVALID_HANDLE)

     {

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

      return INIT_FAILED;

     }

   ResetLastError();

   handle_maP=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,PRICE_TYPICAL);

   if(handle_maP==INVALID_HANDLE)

     {

      Print("The iMA(1) 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_max,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_max-2;

      ArrayInitialize(BufferWT1,EMPTY_VALUE);

      ArrayInitialize(BufferWT2,EMPTY_VALUE);

      ArrayInitialize(BufferWT3,EMPTY_VALUE);

      ArrayInitialize(BufferColors,2);

      ArrayInitialize(BufferRaw1,0);

      ArrayInitialize(BufferRaw2,0);

      ArrayInitialize(BufferPrice,0);

      ArrayInitialize(BufferEMA1,0);

      ArrayInitialize(BufferEMA2,0);

     }

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

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

   copied=CopyBuffer(handle_ma1,0,0,count,BufferEMA1);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_maP,0,0,count,BufferPrice);

   if(copied!=count) return 0;

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

     {

      BufferRaw1[i]=fabs(BufferPrice[i]-BufferEMA1[i]);

     }

   if(ExponentialMAOnBuffer(rates_total,prev_calculated,0,period_ch,BufferRaw1,BufferEMA2)==0)

      return 0;

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

     {

      BufferRaw2[i]=(BufferEMA2[i]!=0 ? (BufferPrice[i]-BufferEMA1[i])/(0.015*BufferEMA2[i]) : 0);

     }

   

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

   if(ExponentialMAOnBuffer(rates_total,prev_calculated,0,period_avg,BufferRaw2,BufferWT1)==0)

      return 0;

   if(SimpleMAOnBuffer(rates_total,prev_calculated,period_avg,period_sig,BufferWT1,BufferWT2)==0)

      return 0;

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

     {

      BufferWT3[i]=BufferWT1[i]-BufferWT2[i];

      BufferColors[i]=(BufferWT3[i]>BufferWT3[i+1] ? 0 : BufferWT3[i]<BufferWT3[i+1] ? 1 : 2);

     }

   

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