Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
0 Views
0 Downloads
0 Favorites
Repulse
ÿþ//+------------------------------------------------------------------+

//|                                                      Repulse.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 "Repulse oscillator"

#property indicator_separate_window

#property indicator_buffers 10

#property indicator_plots   2

//--- plot RP1

#property indicator_label1  "Repulse"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot RP2

#property indicator_label2  "Signal"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input uint     InpPeriod1  =  5;    // Repulse period

input uint     InpPeriod2  =  15;   // Signal period

//--- indicator buffers

double         BufferRP1[];

double         BufferRP2[];

double         BufferPos1[];

double         BufferNeg1[];

double         BufferPos2[];

double         BufferNeg2[];

double         BufferPos1tmp[];

double         BufferNeg1tmp[];

double         BufferPos2tmp[];

double         BufferNeg2tmp[];

//--- global variables

int            period1;

int            period2;

//--- includes

#include <MovingAverages.mqh>

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period1=int(InpPeriod1<1 ? 1 : InpPeriod1);

   period2=int(InpPeriod2<1 ? 1 : InpPeriod2);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferRP1,INDICATOR_DATA);

   SetIndexBuffer(1,BufferRP2,INDICATOR_DATA);

   SetIndexBuffer(2,BufferPos1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferNeg1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferPos2,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferNeg2,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferPos1tmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,BufferNeg1tmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferPos2tmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferNeg2tmp,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Repulse("+(string)period1+","+(string)period2+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferRP1,true);

   ArraySetAsSeries(BufferRP2,true);

   ArraySetAsSeries(BufferPos1,true);

   ArraySetAsSeries(BufferNeg1,true);

   ArraySetAsSeries(BufferPos2,true);

   ArraySetAsSeries(BufferNeg2,true);

   ArraySetAsSeries(BufferPos1tmp,true);

   ArraySetAsSeries(BufferNeg1tmp,true);

   ArraySetAsSeries(BufferPos2tmp,true);

   ArraySetAsSeries(BufferNeg2tmp,true);

//---

   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 =0 <8=8<0;L=>5 :>;85AB2> 10@>2 4;O @0AGQB0

   if(rates_total<4 || Point()==0) return 0;

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

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

   int limit=rates_total-prev_calculated;

   int max=fmax(period1,period2);

   if(limit>1)

     {

      limit=rates_total-max-1;

      ArrayInitialize(BufferRP1,EMPTY_VALUE);

      ArrayInitialize(BufferRP2,EMPTY_VALUE);

      ArrayInitialize(BufferPos1,0);

      ArrayInitialize(BufferNeg1,0);

      ArrayInitialize(BufferPos2,0);

      ArrayInitialize(BufferNeg2,0);

      ArrayInitialize(BufferPos1tmp,0);

      ArrayInitialize(BufferNeg1tmp,0);

      ArrayInitialize(BufferPos2tmp,0);

      ArrayInitialize(BufferNeg2tmp,0);

     }

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

   double MinPrice=0,MaxPrice=0;

   int bl=WRONG_VALUE,bh=WRONG_VALUE;

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

     {

      if(close[i]==0) continue;

      bl=Lowest(period1,i);

      bh=Highest(period1,i);

      if(bl==WRONG_VALUE || bh==WRONG_VALUE) continue;

      MinPrice=low[bl];

      MaxPrice=high[bh];

      BufferPos1tmp[i]=100.*(3.*close[i]-2.*MinPrice-open[i])/close[i];

      BufferNeg1tmp[i]=100.*(open[i]+2.*MaxPrice-3.*close[i])/close[i];

      //---

      bl=Lowest(period2,i);

      bh=Highest(period2,i);

      if(bl==WRONG_VALUE || bh==WRONG_VALUE) continue;

      MinPrice=low[bl];

      MaxPrice=high[bh];

      BufferPos2tmp[i]=100.*(3.*close[i]-2.*MinPrice-open[i+period2])/close[i];

      BufferNeg2tmp[i]=100.*(open[i+period2]+2.*MaxPrice-3.*close[i])/close[i];

     }

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

   ExponentialMAOnBuffer(rates_total,prev_calculated,0,5*period1,BufferPos1tmp,BufferPos1);

   ExponentialMAOnBuffer(rates_total,prev_calculated,0,5*period1,BufferNeg1tmp,BufferNeg1);

   ExponentialMAOnBuffer(rates_total,prev_calculated,0,5*period2,BufferPos2tmp,BufferPos2);

   ExponentialMAOnBuffer(rates_total,prev_calculated,0,5*period2,BufferNeg2tmp,BufferNeg2);

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

     {

      BufferRP1[i]=(BufferPos1[i]-BufferNeg1[i])/Point();

      BufferRP2[i]=(BufferPos2[i]-BufferNeg2[i])/Point();

     }



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

   return(rates_total);

  }

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

//| >72@0I05B 8=45:A <0:A8<0;L=>3> 7=0G5=8O B09<A5@88 High          |

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

int Highest(const int count,const int start)

  {

   double array[];

   ArraySetAsSeries(array,true);

   if(CopyHigh(Symbol(),PERIOD_CURRENT,start,count,array)==count)

      return ArrayMaximum(array)+start;

   return WRONG_VALUE;

  }

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

//| >72@0I05B 8=45:A <8=8<0;L=>3> 7=0G5=8O B09<A5@88 Low            |

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

int Lowest(const int count,const int start)

  {

   double array[];

   ArraySetAsSeries(array,true);

   if(CopyLow(Symbol(),PERIOD_CURRENT,start,count,array)==count)

      return ArrayMinimum(array)+start;

   return WRONG_VALUE;

  }

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

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