Indicators Used
0
Views
0
Downloads
0
Favorites
arbitrage_mtf
//+------------------------------------------------------------------+
//| Arbitrage_MTF.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 MTF 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;
//--- index timeframe
input ENUM_TIMEFRAMES TimeFrame=0;
//--- 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;
//---
datetime Time[1];
double Index1[1],
Index2[1],
Index3[1],
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;
}
//--- set timeframe
string timeFrame=StringSubstr(EnumToString(TimeFrame),7);
if(timeFrame=="CURRENT")
timeFrame= "";
//---
indHandle1 = INVALID_HANDLE;
indHandle2 = INVALID_HANDLE;
indHandle3 = INVALID_HANDLE;
switch(Indicator)
{
case INDICATOR_MACD:
indHandle1 = iMACD(symbol1,TimeFrame,FastEMA,SlowEMA,SignalSMA,MACDPrice);
indHandle2 = iMACD(symbol2,TimeFrame,FastEMA,SlowEMA,SignalSMA,MACDPrice);
indHandle3 = iMACD(symbol3,TimeFrame,FastEMA,SlowEMA,SignalSMA,MACDPrice);
shortName=StringFormat("Arbitrage MACD %s(%d,%d,%d)",
timeFrame,FastEMA,SlowEMA,SignalSMA);
IndicatorSetInteger(INDICATOR_DIGITS,5);
IndicatorSetInteger(INDICATOR_LEVELS,1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
break;
case INDICATOR_STOCHASTIC:
indHandle1 = iStochastic(symbol1,TimeFrame,Kperiod,Dperiod,Slowing,MODE_SMA,PriceField);
indHandle2 = iStochastic(symbol2,TimeFrame,Kperiod,Dperiod,Slowing,MODE_SMA,PriceField);
indHandle3 = iStochastic(symbol3,TimeFrame,Kperiod,Dperiod,Slowing,MODE_SMA,PriceField);
shortName=StringFormat("Arbitrage Stochastic %s(%d,%d,%d)",
timeFrame,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,TimeFrame,RSIPeriod,RSIPrice);
indHandle2 = iRSI(symbol2,TimeFrame,RSIPeriod,RSIPrice);
indHandle3 = iRSI(symbol3,TimeFrame,RSIPeriod,RSIPrice);
shortName=StringFormat("Arbitrage RSI %s(%d)",
timeFrame,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,TimeFrame,CCIPeriod,CCIPrice);
indHandle2 = iCCI(symbol2,TimeFrame,CCIPeriod,CCIPrice);
indHandle3 = iCCI(symbol3,TimeFrame,CCIPeriod,CCIPrice);
shortName=StringFormat("Arbitrage CCI %s(%d)",
timeFrame,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,TimeFrame,RVIPeriod);
indHandle2 = iRVI(symbol2,TimeFrame,RVIPeriod);
indHandle3 = iRVI(symbol3,TimeFrame,RVIPeriod);
shortName=StringFormat("Arbitrage RVI %s(%d)",
timeFrame,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,TimeFrame,DeMarkerPeriod);
indHandle2 = iDeMarker(symbol2,TimeFrame,DeMarkerPeriod);
indHandle3 = iDeMarker(symbol3,TimeFrame,DeMarkerPeriod);
shortName=StringFormat("Arbitrage DeMarker %s(%d)",
timeFrame,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,TimeFrame,TrixPeriod,TrixPrice);
indHandle2 = iTriX(symbol2,TimeFrame,TrixPeriod,TrixPrice);
indHandle3 = iTriX(symbol3,TimeFrame,TrixPeriod,TrixPrice);
shortName=StringFormat("Arbitrage TriX %s(%d)",
timeFrame,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;
}
//---
for(int i=limit;i>=0 && !IsStopped();i--)
{
if(CopyTime(symbol1,TimeFrame,time[i],1,Time)!=1 ||
CopyBuffer(indHandle1,0,Time[0],1,Index1)!=1 ||
CopyBuffer(indHandle2,0,Time[0],1,Index2)!=1 ||
CopyBuffer(indHandle3,0,Time[0],1,Index3)!=1)
return(0);
index1 = Index1[0];
index2 = Index2[0];
index3 = Index3[0];
//---
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,
int x,
int 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 Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---