//+------------------------------------------------------------------+
//|                                                 SvW Opposite.mq4 |
//|                                 Copyright 2021, Nondisclosure007 |
//|                                              https://no.link.yet |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Nondisclosure007"
#property link      "https://no.link.yet"
#property version   "1.00"
#property strict

#include <Currency Indicator Helper.mqh>
#include <Strings\String.mqh>

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Up
#property indicator_label1  "Up"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//--- plot Down
#property indicator_label2  "Down"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  3
//--- input parameters
input int      Threshold = 100;

int      MAPeriod = 4;

//--- indicator buffers
double         UpBuffer[];
double         DownBuffer[];

struct CurrencyStruct
  {
   string            sym;
   double            currentpips;
   double            lastpips;
   double            count;
  };

struct CurrencyDiff
  {
   double            updiff;
   double            downdiff;
   double            ma;
  };

CurrencyStruct Base, Counter;
CurrencyDiff Diff[3];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,UpBuffer);
   SetIndexBuffer(1,DownBuffer);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
//PlotIndexSetInteger(0,PLOT_ARROW,159);
//PlotIndexSetInteger(1,PLOT_ARROW,159);
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 233);
   SetIndexEmptyValue(0, 0.0);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 234);
   SetIndexEmptyValue(1, 0.0);
   CString Dsym;
   Dsym.Assign(Symbol());
   Base.sym = Dsym.Left(3);
   Counter.sym = Dsym.Right(3);
//---
   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[])
  {
//---
   int    i,limit;
   double distance = 50 * _Point;
   limit = rates_total - prev_calculated;
   if(prev_calculated > 0)
     {
      limit++;
     }
   for(i = limit; i >= 0; i--)
     {
      if(i > Bars(Symbol(),0) - MAPeriod * 2)
        {
         continue;
        }
      else
        {
         
         /*Base.currentpips = CCPips(Base.sym,i);
         Base.lastpips = CCPips(Base.sym,i + 1);
         Counter.currentpips = CCPips(Counter.sym,i);
         Counter.lastpips = CCPips(Counter.sym,i + 1);
         if(Base.currentpips > Base.lastpips &&
            Counter.currentpips < Counter.lastpips &&
            Base.currentpips > Counter.currentpips)
           {
            UpBuffer[i] = Low[i] - distance;
           }
         if(Base.currentpips < Base.lastpips &&
            Counter.currentpips > Counter.lastpips &&
            Base.currentpips < Counter.currentpips)
           {
            DownBuffer[i] = High[i] + distance;
           }
         */
         Diff[2].downdiff = iCustom(Symbol(),0,"Currency Diff",1,i + 2);
         Diff[2].updiff = iCustom(Symbol(),0,"Currency Diff",0,i + 2);
         Diff[2].ma = iCustom(Symbol(),0,"Currency Diff",3,i + 2);
         Diff[1].downdiff = iCustom(Symbol(),0,"Currency Diff",1,i + 1);
         Diff[1].updiff = iCustom(Symbol(),0,"Currency Diff",0,i + 1);
         Diff[1].ma = iCustom(Symbol(),0,"Currency Diff",3,i + 1);
         Diff[0].downdiff = iCustom(Symbol(),0,"Currency Diff",1,i);
         Diff[0].updiff = iCustom(Symbol(),0,"Currency Diff",0,i);
         Diff[0].ma = iCustom(Symbol(),0,"Currency Diff",3,i);
         if(Diff[2].updiff > Diff[2].ma && Diff[1].updiff > Diff[1].ma && Diff[0].updiff > Diff[0].ma &&
            Diff[2].updiff != 0 && Diff[1].updiff > 0 && Diff[0].updiff > 0)
           {
            UpBuffer[i] = Low[i] - distance;
           }
         if(Diff[2].downdiff < Diff[2].ma && Diff[1].downdiff < Diff[1].ma && Diff[0].downdiff < Diff[0].ma &&
            Diff[2].downdiff != 0 && Diff[1].downdiff < 0 && Diff[0].downdiff < 0)
           {
            DownBuffer[i] = High[i] + distance;
           }
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
