Author: Copyright 2015, MetaQuotes Software Corp.
Indicators Used
MACD HistogramStochastic oscillatorRelative strength indexCommodity channel indexRelative Vigor indexDeMarker indicator
0 Views
0 Downloads
0 Favorites
arbitrage
//+------------------------------------------------------------------+
//|                                                    Arbitrage.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright  "Copyright 2015, MetaQuotes Software Corp."
#property link       "https://www.mql5.com"
#property description"Arbitrage by pipPod"
#property version    "2.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//---
#property indicator_color1  clrLimeGreen,clrFireBrick
#property indicator_width1  2
#property indicator_color2  clrFireBrick
#property indicator_width2  2
//---
#property indicator_levelcolor clrLightSlateGray
//---
double indicator_level1=   0;
double indicator_level2=  .2;
double indicator_level3= -.2;
double indicator_level4=  20;
double indicator_level5= -20;
double indicator_level6=  30;
double indicator_level7= -30;
double indicator_level8= 100;
double indicator_level9=-100;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum indicators
  {
   INDICATOR_MACD,         //Moving Average Convergence/Divergence
   INDICATOR_STOCHASTIC,   //Stochastic Oscillator
   INDICATOR_RSI,          //Relative Strength Index
   INDICATOR_CCI,          //Commodity Channel Index
   INDICATOR_RVI,          //Relative Vigor Index
   INDICATOR_DEMARK,       //DeMarker Oscillator
   INDICATOR_TRIX          //Triple Exponential Average
  };
//--- indicator to show
input indicators  Indicator=INDICATOR_MACD;
//--- indicator parameters
input string MACD;
input ushort FastEMA=12;                           //Fast EMA Period
input ushort SlowEMA=26;                           //Slow EMA Period
input ushort SignalSMA=9;                          //Signal SMA Period
input ENUM_APPLIED_PRICE MACDPrice=PRICE_CLOSE; //MACD Price
//---
input string Stochastic;
input ushort Kperiod=7;                            //K Period
input ushort Dperiod=3;                            //D Period
input ushort Slowing=3;
input ENUM_STO_PRICE PriceField=STO_LOWHIGH;    //Price Field
//---
input string RSI;
input ushort RSIPeriod=14;                        //RSI Period
input ENUM_APPLIED_PRICE RSIPrice=PRICE_CLOSE;  //RSI Price
//---
input string CCI;
input ushort CCIPeriod=14;                        //CCI Period
input ENUM_APPLIED_PRICE CCIPrice=PRICE_CLOSE;  //CCI Price
//---
input string RVI;
input ushort RVIPeriod=14;                        //RVI Period
//---
input string DeMarker;
input ushort DeMarkerPeriod=14;                   //DeMarker Period
//---
input string TriX;
input ushort TrixPeriod=14;                        //TriX Period
input ENUM_APPLIED_PRICE TrixPrice=PRICE_CLOSE;  //TriX Price
input string _;   //---
input bool ColorBars=true;    //Color Bars
input bool FillBuffers=true;  //Fill Buffers
//--- index buffers for drawing
double   BaseBuffer[];
double   QoteBuffer[];
//--- currency variables for calculation
#define  USDJPY   "USDJPY"
//---
string   base,
         quote,
         symbol1,
         symbol2,
         symbol3;
//---
double   Index1[],
         Index2[],
         Index3[],
         index1,
         index2,
         index3,
         baseIndex,
         qoteIndex;
//---
bool     baseUSD=false,
         quoteUSD=false,
         quoteJPY=false,
         quoteJPY1=false,
         quoteJPY2=false,
         quoteJPY3=false;
//---
string   shortName;
long     chartID=ChartID();
//--- indicator handles
int      indHandle1,
         indHandle2,
         indHandle3;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---set index colors
   color colorBase=indicator_color1,
   colorQuote=indicator_color2;
//---currencies to show
   base = StringSubstr(_Symbol,0,3);  //Base currency name
   quote= StringSubstr(_Symbol,3,3);  //Quote currency name
   if(_Symbol=="EURUSD")
     {
      SymbolSelect("EURJPY",true);
      SymbolSelect(USDJPY,true);
      colorBase=clrRoyalBlue;
      colorQuote=clrLimeGreen;
      quoteUSD= true;
      symbol1 = _Symbol;
      symbol2 = "EURJPY";
      quoteJPY2=true;
      symbol3=USDJPY;
      quoteJPY3=true;
     }
   if(_Symbol=="EURJPY")
     {
      SymbolSelect("EURUSD",true);
      SymbolSelect(USDJPY,true);
      colorBase=clrRoyalBlue;
      colorQuote=clrYellow;
      quoteJPY= true;
      symbol1 = _Symbol;
      quoteJPY1=true;
      symbol2 = "EURUSD";
      symbol3 = USDJPY;
      quoteJPY3=true;
     }
   if(_Symbol=="GBPUSD")
     {
      SymbolSelect("GBPJPY",true);
      SymbolSelect(USDJPY,true);
      colorBase=clrSilver;
      colorQuote=clrLimeGreen;
      quoteUSD= true;
      symbol1 = _Symbol;
      symbol2 = "GBPJPY";
      quoteJPY2=true;
      symbol3=USDJPY;
      quoteJPY3=true;
     }
   if(_Symbol=="GBPJPY")
     {
      SymbolSelect("GBPUSD",true);
      SymbolSelect(USDJPY,true);
      colorBase=clrSilver;
      colorQuote=clrYellow;
      quoteJPY= true;
      symbol1 = _Symbol;
      quoteJPY1=true;
      symbol2 = "GBPUSD";
      symbol3 = USDJPY;
      quoteJPY3=true;
     }
   if(_Symbol=="USDCAD")
     {
      SymbolSelect("CADJPY",true);
      SymbolSelect(USDJPY,true);
      colorBase=clrLimeGreen;
      colorQuote=clrWhiteSmoke;
      baseUSD = true;
      symbol1 = _Symbol;
      symbol2 = "CADJPY";
      quoteJPY2=true;
      symbol3=USDJPY;
      quoteJPY3=true;
     }
   if(_Symbol=="USDCHF")
     {
      SymbolSelect("CHFJPY",true);
      SymbolSelect(USDJPY,true);
      colorBase=clrLimeGreen;
      colorQuote=clrFireBrick;
      baseUSD = true;
      symbol1 = _Symbol;
      symbol2 = "CHFJPY";
      quoteJPY2=true;
      symbol3=USDJPY;
      quoteJPY3=true;
     }
   if(_Symbol==USDJPY)
     {
      SymbolSelect("EURUSD",true);
      SymbolSelect("EURJPY",true);
      colorBase=clrLimeGreen;
      colorQuote=clrYellow;
      quoteJPY= true;
      symbol1 = _Symbol;
      quoteJPY1=true;
      symbol2 = "EURUSD";
      symbol3 = "EURJPY";
      quoteJPY3=true;
     }
//---
   indHandle1 = INVALID_HANDLE;
   indHandle2 = INVALID_HANDLE;
   indHandle3 = INVALID_HANDLE;
   switch(Indicator)
     {
      case INDICATOR_MACD:
         indHandle1 = iMACD(symbol1,0,FastEMA,SlowEMA,SignalSMA,MACDPrice);
         indHandle2 = iMACD(symbol2,0,FastEMA,SlowEMA,SignalSMA,MACDPrice);
         indHandle3 = iMACD(symbol3,0,FastEMA,SlowEMA,SignalSMA,MACDPrice);
         shortName=StringFormat("Arbitrage MACD(%d,%d,%d)",
                                FastEMA,SlowEMA,SignalSMA);
         IndicatorSetInteger(INDICATOR_DIGITS,5);
         IndicatorSetInteger(INDICATOR_LEVELS,1);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
         break;
      case INDICATOR_STOCHASTIC:
         indHandle1 = iStochastic(symbol1,0,Kperiod,Dperiod,Slowing,MODE_SMA,PriceField);
         indHandle2 = iStochastic(symbol2,0,Kperiod,Dperiod,Slowing,MODE_SMA,PriceField);
         indHandle3 = iStochastic(symbol3,0,Kperiod,Dperiod,Slowing,MODE_SMA,PriceField);
         shortName=StringFormat("Arbitrage Stochastic(%d,%d,%d)",
                                Kperiod,Dperiod,Slowing);
         IndicatorSetInteger(INDICATOR_DIGITS,0);
         IndicatorSetInteger(INDICATOR_LEVELS,3);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level6);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,2,indicator_level7);
         break;
      case INDICATOR_RSI:
         indHandle1 = iRSI(symbol1,0,RSIPeriod,RSIPrice);
         indHandle2 = iRSI(symbol2,0,RSIPeriod,RSIPrice);
         indHandle3 = iRSI(symbol3,0,RSIPeriod,RSIPrice);
         shortName=StringFormat("Arbitrage RSI(%d)",
                                RSIPeriod);
         IndicatorSetInteger(INDICATOR_DIGITS,0);
         IndicatorSetInteger(INDICATOR_LEVELS,3);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level4);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,2,indicator_level5);
         break;
      case INDICATOR_CCI:
         indHandle1 = iCCI(symbol1,0,CCIPeriod,CCIPrice);
         indHandle2 = iCCI(symbol2,0,CCIPeriod,CCIPrice);
         indHandle3 = iCCI(symbol3,0,CCIPeriod,CCIPrice);
         shortName=StringFormat("Arbitrage CCI(%d)",
                                CCIPeriod);
         IndicatorSetInteger(INDICATOR_DIGITS,0);
         IndicatorSetInteger(INDICATOR_LEVELS,3);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level8);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,2,indicator_level9);
         break;
      case INDICATOR_RVI:
         indHandle1 = iRVI(symbol1,0,RVIPeriod);
         indHandle2 = iRVI(symbol2,0,RVIPeriod);
         indHandle3 = iRVI(symbol3,0,RVIPeriod);
         shortName=StringFormat("Arbitrage RVI(%d)",
                                RVIPeriod);
         IndicatorSetInteger(INDICATOR_DIGITS,3);
         IndicatorSetInteger(INDICATOR_LEVELS,3);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level2);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,2,indicator_level3);
         break;
      case INDICATOR_DEMARK:
         indHandle1 = iDeMarker(symbol1,0,DeMarkerPeriod);
         indHandle2 = iDeMarker(symbol2,0,DeMarkerPeriod);
         indHandle3 = iDeMarker(symbol3,0,DeMarkerPeriod);
         shortName=StringFormat("Arbitrage DeMarker(%d)",
                                DeMarkerPeriod);
         IndicatorSetInteger(INDICATOR_DIGITS,3);
         IndicatorSetInteger(INDICATOR_LEVELS,3);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level2);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,2,indicator_level3);
         break;
      case INDICATOR_TRIX:
         indHandle1 = iTriX(symbol1,0,TrixPeriod,TrixPrice);
         indHandle2 = iTriX(symbol2,0,TrixPeriod,TrixPrice);
         indHandle3 = iTriX(symbol3,0,TrixPeriod,TrixPrice);
         shortName=StringFormat("Arbitrage TriX(%d)",
                                TrixPeriod);
         IndicatorSetInteger(INDICATOR_DIGITS,5);
         IndicatorSetInteger(INDICATOR_LEVELS,1);
         IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
     }
   if(indHandle1==INVALID_HANDLE ||
      indHandle2==INVALID_HANDLE ||
      indHandle3==INVALID_HANDLE)
      return(INIT_FAILED);
//---
   IndicatorSetString(INDICATOR_SHORTNAME,shortName);
   int window=ChartWindowFind(chartID,shortName);
   uchar xStart=4;       //label coordinates
   uchar xIncrement=25;
   uchar yStart=16;
   ObjectCreate(base,window,xStart,yStart,colorBase);
   xStart+=xIncrement;
   ObjectCreate(quote,window,xStart,yStart,colorQuote);
//--- index buffers
   SetIndexBuffer(0,BaseBuffer);
   ArraySetAsSeries(BaseBuffer,true);
   SetIndexBuffer(1,QoteBuffer);
   ArraySetAsSeries(QoteBuffer,true);
//--- DRAW_FILLING / DRAW_LINE
   if(FillBuffers)
     {
      PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_FILLING);
      PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,colorBase);
      PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,colorQuote);
      PlotIndexSetString(0,PLOT_LABEL,base+";"+quote);
     }
   else
     {
      PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
      PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_LINE);
      PlotIndexSetInteger(0,PLOT_LINE_COLOR,colorBase);
      PlotIndexSetInteger(1,PLOT_LINE_COLOR,colorQuote);
      PlotIndexSetString(0,PLOT_LABEL,base);
      PlotIndexSetString(1,PLOT_LABEL,quote);
     }
//--- color bars
   if(ColorBars)
     {
      if(ChartGetInteger(0,CHART_COLOR_CANDLE_BULL)!=colorBase)
        {
         ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,colorBase);
         ChartSetInteger(0,CHART_COLOR_CHART_UP,colorBase);
        }
      if(ChartGetInteger(0,CHART_COLOR_CANDLE_BEAR)!=colorQuote)
        {
         ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,colorQuote);
         ChartSetInteger(0,CHART_COLOR_CHART_DOWN,colorQuote);
        }
     }
//---
   return(INIT_SUCCEEDED);
  }
//---
#define INVERT -1
//+------------------------------------------------------------------+
//| 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(time,true);
   int limit=rates_total-prev_calculated;
   if(prev_calculated>rates_total || prev_calculated<=0)
     {
      int barsWindow=(int)ChartGetInteger(chartID,CHART_VISIBLE_BARS)+100;
      PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rates_total-barsWindow);
      PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,rates_total-barsWindow);
      limit=barsWindow;
     }
   int toCopy=limit+1;
   int copied1 = CopyBuffer(indHandle1,0,time[0],toCopy,Index1);
   int copied2 = CopyBuffer(indHandle2,0,time[0],toCopy,Index2);
   int copied3 = CopyBuffer(indHandle3,0,time[0],toCopy,Index3);
   if(prev_calculated==0 && 
      (copied1!=toCopy ||
      copied2!=toCopy ||
      copied3!=toCopy))
      return(0);
   if(prev_calculated>0 && 
      (copied1!=toCopy ||
      copied2!=toCopy ||
      copied3!=toCopy))
      return(rates_total-1);
//---
   for(int i=limit,j=0;i>=0 && !IsStopped();i--,j++)
     {
      index1 = Index1[j];
      index2 = Index2[j];
      index3 = Index3[j];
      switch(Indicator)
        {
         case INDICATOR_MACD:
            if(quoteJPY1)
            index1/=100;
            if(quoteJPY2)
               index2/=100;
            if(quoteJPY3)
               index3/=100;
            break;
         case INDICATOR_STOCHASTIC:
            index1 -= 50;
            index2 -= 50;
            index3 -= 50;
            break;
         case INDICATOR_RSI:
            index1 -= 50;
            index2 -= 50;
            index3 -= 50;
            break;
         case INDICATOR_DEMARK:
            index1 -= .5;
            index2 -= .5;
            index3 -= .5;
            break;
         default:
            break;
        }
      //---
      if(quoteUSD)
        {
         baseIndex=(index1+index2)/2;
         index1*=INVERT;
         qoteIndex=(index1+index3)/2;
        }
      if(quoteJPY)
        {
         if(_Symbol==USDJPY)
            index2*=INVERT;
         baseIndex=(index1+index2)/2;
         index1 *= INVERT;
         index3 *= INVERT;
         qoteIndex=(index1+index3)/2;
        }
      if(baseUSD)
        {
         baseIndex=(index1+index3)/2;
         index1*=INVERT;
         qoteIndex=(index1+index2)/2;
        }
      //---
      BaseBuffer[i] = baseIndex;
      QoteBuffer[i] = qoteIndex;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
// delete labels
   ObjectDelete(chartID,base);
   ObjectDelete(chartID,quote);
// release indicators
   IndicatorRelease(indHandle1);
   IndicatorRelease(indHandle2);
   IndicatorRelease(indHandle3);
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ObjectCreate(string currency,
                  int window,
                  uchar x,
                  uchar y,
                  color clr)
  {
   ObjectCreate(chartID,currency,OBJ_LABEL,window,0,0);
   ObjectSetString(chartID,currency,OBJPROP_TEXT,currency);
   ObjectSetString(chartID,currency,OBJPROP_FONT,"Arial Bold");
   ObjectSetInteger(chartID,currency,OBJPROP_FONTSIZE,8);
   ObjectSetInteger(chartID,currency,OBJPROP_COLOR,clr);
   ObjectSetInteger(chartID,currency,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chartID,currency,OBJPROP_YDISTANCE,y);
  }
//-------------------------------------------------------------------+

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