Author: Copyright 2022, qq:513394217
0 Views
0 Downloads
0 Favorites
PSY
ÿþ//+------------------------------------------------------------------+

//|                                                          PSY.mq4 |

//|                                     Copyright 2022, qq:513394217 |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2022, qq:513394217"

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

#property version   "1.00"

#property strict

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot PSY

#property indicator_label1  "PSY"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrWhite

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot PSYMA

#property indicator_label2  "PSYMA"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrYellow

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1



#property indicator_levelstyle STYLE_DOT

#property indicator_levelcolor clrSilver

#property indicator_level1  0

#property indicator_level2  20

#property indicator_level3  50

#property indicator_level4  80

#property indicator_level5  100



//--- input parameters

input int      N = 12;

input int      M = 6;

//--- indicator buffers

double         PSYBuffer[];

double         PSYMABuffer[];

double tmpcnt[];

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

//| Custom indicator initialization function                         |

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

int OnInit() {

//--- indicator buffers mapping

   IndicatorBuffers(3);

   SetIndexBuffer(0, PSYBuffer);

   SetIndexBuffer(1, PSYMABuffer);

   SetIndexBuffer(2, tmpcnt, INDICATOR_CALCULATIONS);

   SetIndexDrawBegin(0, N);

   SetIndexDrawBegin(1, M);

   SetIndexEmptyValue(1, 0);

   string name = "PSY(" + (string)N + "," + (string)M + ")";

   IndicatorShortName(name);

   IndicatorDigits(Digits);

   if(N <= 0 || M <= 0)

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

//---

   ArraySetAsSeries(PSYBuffer, false);

   ArraySetAsSeries(PSYMABuffer, false);

   ArraySetAsSeries(tmpcnt, false);

   ArraySetAsSeries(close, false);

   int i, start, cumsum = 0;

   start = prev_calculated - 1;

   if(start < 0)

      start = 0;

   for(i = start; i < rates_total; i++) {

      cumsum = 0;

      if(i == 0) {

         tmpcnt[i] = 0.0; }

      else {

         double cnt = 0.0;

         for(int j = i; j > 0; j--) {

            if(cumsum == N)

               continue;

            if(close[j] > close[j - 1])

               cnt++;

            cumsum++; }

         tmpcnt[i] = cnt; }

      double tmp1 = tmpcnt[i] / N * 100;

      PSYBuffer[i] = tmp1;

      cumsum = 0;

      double tmp2 = 0.0;

      for(int k = i; k >= M; k--) {

         if(cumsum == M)

            continue;

         tmp2 += PSYBuffer[k];

         cumsum++; }

      if(i >= M) {

         PSYMABuffer[i] = tmp2 / M; }

      else {

         PSYMABuffer[i] = 0.0; } }

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

   return(rates_total); }

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

Comments