MT4指标函数移植到MT5上的意义
- 了解MQL4与MQL5语言的差别,加深学习理解,利于开发通用MT4与MT5的程序。
- 很多优秀的指标使用MQL4语言开发,无法直接运行mt5上,移值后可节省二次开发MT5指标的时间。
- MT5优于MT4,当运行复杂的程序时MT5比MT4更稳定,并且MT5具有底层改进和新功能,例如网络通讯和数据库。
- MT5的回测功能更强大,可实现更精细的多品种回测,MT4一次只能处理一个交易品种且准确性低。
为什么MT4指标无法在MT5上运行
- MT4中大量系统定义的变量在MT5中被取消,例如Ask,Bid等。
- MT5绘制指标线的函数与MT4完全不同,例如MT5中指标绘图均是以Plot开头的函数。
- MT4和MT5指标默认缓冲区的数组序列方向不一样,MT4是从最新到最旧,0号是最新数据,而MT5恰恰相反,0号是最旧数据。
- 旧版MT4的事件函数start()已无法在MT5中运行,需要重新替换。
MT4指标函数在MT5上的重定义与代码实现
#define MODE_ASCEND 0
#define MODE_DESCEND 1
#define MODE_OPEN 0
#define MODE_LOW 1
#define MODE_HIGH 2
#define MODE_CLOSE 3
#define MODE_VOLUME 4
#define MODE_TIME 5
#define MODE_REAL_VOLUME 6
#define MODE_TICKVOLUME 7
#define MODE_REALVOLUME 8
#define MODE_BID 9
#define MODE_ASK 10
#define MODE_POINT 11
#define MODE_DIGITS 12
#define MODE_SPREAD 13
#define MODE_STOPLEVEL 14
#define MODE_LOTSIZE 15
#define MODE_TICKVALUE 16
#define MODE_TICKSIZE 17
#define MODE_SWAPLONG 18
#define MODE_SWAPSHORT 19
#define MODE_STARTING 20
#define MODE_EXPIRATION 21
#define MODE_TRADEALLOWED 22
#define MODE_MINLOT 23
#define MODE_LOTSTEP 24
#define MODE_MAXLOT 25
#define MODE_SWAPTYPE 26
#define MODE_PROFITCALCMODE 27
#define MODE_MARGINCALCMODE 28
#define MODE_MARGININIT 29
#define MODE_MARGINMAINTENANCE 30
#define MODE_MARGINHEDGED 31
#define MODE_MARGINREQUIRED 32
#define MODE_FREEZELEVEL 33
#define MODE_CLOSEBY_ALLOWED 34
#define MODE_MAIN (0)
#define MODE_SIGNAL (1)
#define MODE_PLUSDI (1)
#define MODE_MINUSDI (2)
#define MODE_UPPER (1)
#define MODE_LOWER (2)
#define MODE_GATORJAW (1)
#define MODE_GATORTEETH (2)
#define MODE_GATORLIPS (3)
#define MODE_TENKANSEN (1)
#define MODE_KIJUNSEN (2)
#define MODE_SENKOUSPANA (3)
#define MODE_SENKOUSPANB (4)
#define MODE_CHIKOUSPAN (5)
double _Open[];
double _Close[];
double _High[];
double _Low[];
double _Ask,_Bid;
int _CountedMQL4;
void InitMql4(void) export
{
ArraySetAsSeries(_Open, true);
ArraySetAsSeries(_Close, true);
ArraySetAsSeries(_High, true);
ArraySetAsSeries(_Low, true);
}
void GetMQL4Tick() export
{
MqlRates rates[1];
CopyRates(Symbol(), 0, 0, 1, rates);
_Open[0]=rates[0].open;
_Close[0]=rates[0].close;
_High[0]=rates[0].high;
_Low[0]=rates[0].low;
MqlTick tick;
SymbolInfoTick(Symbol(), tick);
_Ask = tick.ask;
_Bid = tick.bid;
_CountedMQL4 = ArraySize(_Close);
}
void GetMQL4(void) export
{
datetime end_tm = 0;
datetime start_tm = TimeTradeServer();
CopyOpen(Symbol(), Period(), start_tm, end_tm, _Open);
CopyHigh(Symbol(), Period(), start_tm, end_tm, _High);
CopyLow(Symbol(), Period(), start_tm, end_tm, _Low);
CopyClose(Symbol(), Period(), start_tm, end_tm, _Close);
MqlTick tick;
SymbolInfoTick(Symbol(), tick);
_Ask = tick.ask;
_Bid = tick.bid;
_CountedMQL4=0;
}
int MQL4Run(int rates_total, int prev_calculated) export
{
int uncount = rates_total - prev_calculated;
if (uncount == 0)
GetMQL4Tick();
else
GetMQL4();
return ( ArraySize(_Close) );
}
int CountedMQL4(void) export
{
return (_CountedMQL4);
}
void SetIndexStyleMQL4(int index, int type, int style=EMPTY, int width=EMPTY, color clr=CLR_NONE) export
{
if (width > -1) PlotIndexSetInteger(index, PLOT_LINE_WIDTH, width);
if (clr != CLR_NONE) PlotIndexSetInteger(index, PLOT_LINE_COLOR, clr);
switch (type)
{
case 0: PlotIndexSetInteger(index, PLOT_DRAW_TYPE, DRAW_LINE);
case 1: PlotIndexSetInteger(index, PLOT_DRAW_TYPE, DRAW_SECTION);
case 2: PlotIndexSetInteger(index, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
case 3: PlotIndexSetInteger(index, PLOT_DRAW_TYPE, DRAW_ARROW);
case 4: PlotIndexSetInteger(index, PLOT_DRAW_TYPE, DRAW_ZIGZAG);
case 12: PlotIndexSetInteger(index, PLOT_DRAW_TYPE, DRAW_NONE);
default: PlotIndexSetInteger(index, PLOT_DRAW_TYPE, DRAW_LINE);
}
switch (style)
{
case 0: PlotIndexSetInteger(index, PLOT_LINE_STYLE, STYLE_SOLID);
case 1: PlotIndexSetInteger(index, PLOT_LINE_STYLE, STYLE_DASH);
case 2: PlotIndexSetInteger(index, PLOT_LINE_STYLE, STYLE_DOT);
case 3: PlotIndexSetInteger(index, PLOT_LINE_STYLE, STYLE_DASHDOT);
case 4: PlotIndexSetInteger(index, PLOT_LINE_STYLE, STYLE_DASHDOTDOT);
default: return;
}
}
void SetLevelStyleMQL4(int draw_style, int line_width, color clr=CLR_NONE) export
{
IndicatorSetInteger(INDICATOR_LEVELWIDTH, line_width);
if (clr != CLR_NONE) IndicatorSetInteger(INDICATOR_LEVELCOLOR, clr);
switch (draw_style)
{
case 0: IndicatorSetInteger(INDICATOR_LEVELSTYLE, STYLE_SOLID);
case 1: IndicatorSetInteger(INDICATOR_LEVELSTYLE, STYLE_DASH);
case 2: IndicatorSetInteger(INDICATOR_LEVELSTYLE, STYLE_DOT);
case 3: IndicatorSetInteger(INDICATOR_LEVELSTYLE, STYLE_DASHDOT);
case 4: IndicatorSetInteger(INDICATOR_LEVELSTYLE, STYLE_DASHDOTDOT);
default: return;
}
}
bool SetIndexBufferMQL4(int index, double &buffer[], ENUM_INDEXBUFFER_TYPE data_type=INDICATOR_DATA) export
{
return ::SetIndexBuffer(index, buffer, data_type);
}
void SetIndexDrawBeginMQL4(int index, int begin) export
{
PlotIndexSetInteger(index, PLOT_DRAW_BEGIN, begin);
}
void SetIndexLabelMQL4(int index, string text) export
{
PlotIndexSetString(index, PLOT_LABEL, text);
}
void SetIndexShiftMQL4(int buffer, int shift) export
{
PlotIndexSetInteger(buffer, PLOT_SHIFT, shift);
}
int ArrayCopyRatesMQL4(MqlRates &rates_array[], string symbol=NULL, int timeframe=0) export
{
Print("Error: ArrayCopyRates() - Not implemented stub.");
return (-1);
}
int ArrayCopyRatesMQL4(double &dest_array[][], string symbol=NULL, int timeframe=0) export
{
Print("Error: ArrayCopyRates() - Not implemented stub.");
return (-1);
}
int ArrayCopySeriesMQL4(double &array[], int series_index, string symbol=NULL, int tf=0) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int count = Bars(symbol, timeframe);
switch (series_index)
{
case MODE_OPEN:
return (CopyOpen(symbol, timeframe, 0, count, array));
case MODE_LOW:
return (CopyLow(symbol, timeframe, 0, count, array));
case MODE_HIGH:
return (CopyHigh(symbol, timeframe, 0, count, array));
case MODE_CLOSE:
return (CopyClose(symbol, timeframe, 0, count, array));
default: return (0);
}
return (0);
}
int ArrayDimensionMQL4(double &array[]) export
{
Print("Error: ArrayDimension() - Not implemented stub.");
return (-1);
}
int ArrayMaximumMQL4(double &array[], int count=WHOLE_ARRAY, int start=0) export
{
return ( ArrayMaximum(array, start, count) );
}
int ArrayMinimumMQL4(double &array[], int count=WHOLE_ARRAY, int start=0) export
{
return ( ArrayMinimum(array, start, count) );
}
bool ArraySortMQL4(double &array[], int count=WHOLE_ARRAY, int start=0, int direction=MODE_ASCEND) export
{
switch (direction)
{
case MODE_ASCEND: ArraySetAsSeries(array, true);
case MODE_DESCEND: ArraySetAsSeries(array, false);
default: ArraySetAsSeries(array, true);
}
ArraySort(array);
return (false);
}
ENUM_TIMEFRAMES TFMigrate(int tf) export
{
switch (tf)
{
case 0: return (PERIOD_CURRENT);
case 1: return (PERIOD_M1);
case 5: return (PERIOD_M5);
case 15: return (PERIOD_M15);
case 30: return (PERIOD_M30);
case 60: return (PERIOD_H1);
case 240: return (PERIOD_H4);
case 1440: return (PERIOD_D1);
case 10080: return (PERIOD_W1);
case 43200: return (PERIOD_MN1);
case 2: return (PERIOD_M2);
case 3: return (PERIOD_M3);
case 4: return (PERIOD_M4);
case 6: return (PERIOD_M6);
case 10: return (PERIOD_M10);
case 12: return (PERIOD_M12);
case 16385: return (PERIOD_H1);
case 16386: return (PERIOD_H2);
case 16387: return (PERIOD_H3);
case 16388: return (PERIOD_H4);
case 16390: return (PERIOD_H6);
case 16392: return (PERIOD_H8);
case 16396: return (PERIOD_H12);
case 16408: return (PERIOD_D1);
case 32769: return (PERIOD_W1);
case 49153: return (PERIOD_MN1);
default: return (PERIOD_CURRENT);
}
}
ENUM_MA_METHOD MethodMigrate(int method) export
{
switch (method)
{
case 0: return (MODE_SMA);
case 1: return (MODE_EMA);
case 2: return (MODE_SMMA);
case 3: return (MODE_LWMA);
default: return (MODE_SMA);
}
}
ENUM_APPLIED_PRICE PriceMigrate(int price) export
{
switch (price)
{
case 1: return (PRICE_CLOSE);
case 2: return (PRICE_OPEN);
case 3: return (PRICE_HIGH);
case 4: return (PRICE_LOW);
case 5: return (PRICE_MEDIAN);
case 6: return (PRICE_TYPICAL);
case 7: return (PRICE_WEIGHTED);
default: return (PRICE_CLOSE);
}
}
ENUM_STO_PRICE StoFieldMigrate(int field) export
{
switch (field)
{
case 0: return (STO_LOWHIGH);
case 1: return (STO_CLOSECLOSE);
default: return (STO_LOWHIGH);
}
}
double CopyBufferMQL4(int handle, int index, int shift) export
{
double buf[];
switch (index)
{
case 0: if (CopyBuffer(handle,0,shift,1,buf)>0)
return (buf[0]); break;
case 1: if (CopyBuffer(handle,1,shift,1,buf)>0)
return (buf[0]); break;
case 2: if (CopyBuffer(handle,2,shift,1,buf)>0)
return (buf[0]); break;
case 3: if (CopyBuffer(handle,3,shift,1,buf)>0)
return (buf[0]); break;
case 4: if (CopyBuffer(handle,4,shift,1,buf)>0)
return (buf[0]); break;
default: break;
}
return (EMPTY_VALUE);
}
int BarsMQL4(void) export
{
return (Bars(_Symbol, _Period));
}
double MarketInfoMQL4(string symbol, int type) export
{
switch(type)
{
case MODE_LOW:
return (double)(SymbolInfoDouble(symbol, SYMBOL_LASTLOW));
case MODE_HIGH:
return (double)(SymbolInfoDouble(symbol, SYMBOL_LASTHIGH));
case MODE_TIME:
return (double)(SymbolInfoInteger(symbol, SYMBOL_TIME));
case MODE_BID:
return (double)(SymbolInfoDouble(symbol, SYMBOL_BID));
case MODE_ASK:
return (double)(SymbolInfoDouble(symbol, SYMBOL_ASK));
case MODE_POINT:
return (double)(SymbolInfoDouble(symbol, SYMBOL_POINT));
case MODE_DIGITS:
return (double)(SymbolInfoInteger(symbol, SYMBOL_DIGITS));
case MODE_SPREAD:
return (double)(SymbolInfoInteger(symbol, SYMBOL_SPREAD));
case MODE_STOPLEVEL:
return (double)(SymbolInfoInteger(symbol, SYMBOL_TRADE_STOPS_LEVEL));
case MODE_LOTSIZE:
return (double)(SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE));
case MODE_TICKVALUE:
return (double)(SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE));
case MODE_TICKSIZE:
return (double)(SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE));
case MODE_SWAPLONG:
return (double)(SymbolInfoDouble(symbol, SYMBOL_SWAP_LONG));
case MODE_SWAPSHORT:
return (double)(SymbolInfoDouble(symbol, SYMBOL_SWAP_SHORT));
case MODE_STARTING:
return (double)(SymbolInfoInteger(symbol, SYMBOL_START_TIME));
case MODE_EXPIRATION:
return (double)(SymbolInfoInteger(symbol, SYMBOL_EXPIRATION_TIME));
case MODE_TRADEALLOWED:
return (double)(SymbolInfoInteger(symbol, SYMBOL_TRADE_MODE) != SYMBOL_TRADE_MODE_DISABLED);
case MODE_MINLOT:
return (double)(SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN));
case MODE_LOTSTEP:
return (double)(SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP));
case MODE_MAXLOT:
return (double)(SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX));
case MODE_SWAPTYPE:
return (double)(SymbolInfoInteger(symbol, SYMBOL_SWAP_MODE));
case MODE_PROFITCALCMODE:
case MODE_MARGINCALCMODE:
return (double)(SymbolInfoInteger(symbol, SYMBOL_TRADE_CALC_MODE));
case MODE_MARGINREQUIRED:
case MODE_MARGININIT:
return (double)(SymbolInfoDouble(symbol, SYMBOL_MARGIN_INITIAL));
case MODE_MARGINMAINTENANCE:
return (double)(SymbolInfoDouble(symbol, SYMBOL_MARGIN_MAINTENANCE));
case MODE_MARGINHEDGED:
return (double)(SymbolInfoDouble(symbol, SYMBOL_MARGIN_HEDGED));
case MODE_FREEZELEVEL:
return (double)(SymbolInfoInteger(symbol, SYMBOL_TRADE_FREEZE_LEVEL));
default: return(0);
}
return(0);
}
double iADMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = (int)iAD(symbol, timeframe, VOLUME_TICK);
if (handle < 0)
{
Print( "此 iAD 对象不能创建: 错误", GetLastError() );
return (-1);
}
else
return (CopyBufferMQL4(handle, 0, shift));
}
double iADXMQL4(string symbol, int tf, int period, int price, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iADX(symbol, timeframe, period);
if (handle < 0)
{
Print("The iADX object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle, mode, shift) );
}
double iAlligatorMQL4(string symbol, int tf, int jaw_period, int jaw_shift,
int teeth_period, int teeth_shift, int lips_period, int lips_shift,
int method, int price, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_MA_METHOD ma_method = MethodMigrate(method);
ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
int handle = iAlligator(symbol, timeframe, jaw_period, jaw_shift,
teeth_period, teeth_shift, lips_period, lips_shift,
ma_method, applied_price);
if (handle < 0)
{
Print("The iAlligator object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle, mode-1, shift) );
}
double iAOMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iAO(symbol, timeframe);
if (handle < 0)
{
Print("The iAO object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
double iBearsPowerMQL4(string symbol, int tf, int period, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iBearsPower(symbol, timeframe, period);
if (handle < 0)
{
Print("The iBearsPower object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle, 0, shift) );
}
double iBullsPowerMQL4(string symbol, int tf, int period, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iBullsPower(symbol, timeframe, period);
if (handle < 0)
{
Print("The iBullsPower object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle, 0, shift) );
}
double iBWMFIMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle=(int)iBWMFI(symbol,timeframe,VOLUME_TICK);
if (handle < 0)
{
Print("The iBWMFI object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
double iATRMQL4(string symbol, int tf, int period, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iATR(symbol, timeframe, period);
if (handle < 0)
{
Print("The iATR object is not created: Error",GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
double iBandsMQL4(string symbol, int tf, int period, double deviation, int bands_shift, int method, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_MA_METHOD ma_method = MethodMigrate(method);
int handle = iBands(symbol,timeframe,period,
bands_shift,deviation,ma_method);
if (handle < 0)
{
Print("The iBands object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,mode,shift));
}
int iBarsMQL4(string symbol, int tf) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
return (Bars(symbol, timeframe));
}
int iBarShiftMQL4(string symbol, int tf, datetime time, bool exact=false) export
{
if (time < 0) return (-1);
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
datetime arr[], time1;
CopyTime(symbol, timeframe, 0, 1,arr);
time1 = arr[0];
if (CopyTime(symbol,timeframe,time,time1,arr) > 0)
{
if (ArraySize(arr) > 2) return (ArraySize(arr)-1);
if (time < time1)
return (1);
else
return(0);
}
else
return(-1);
}
double iCCIMQL4(string symbol, int tf, int period, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_APPLIED_PRICE applied_price = PriceMigrate(price);
int handle = iCCI(symbol, timeframe, period, price);
if (handle < 0)
{
Print("The iCCI object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle, 0, shift) );
}
double iCloseMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
double array[1];
int copied = CopyClose(symbol, timeframe, shift, 1, array);
if (copied != 1) return (0);
return (array[0]);
}
double iDeMarkerMQL4(string symbol, int tf, int period, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iDeMarker(symbol,timeframe,period);
if (handle < 0)
{
Print("The iDeMarker object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle,0,shift) );
}
double iEnvelopesMQL4(string symbol, int tf, int ma_period, int method, int ma_shift, int price, double deviation, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_MA_METHOD ma_method = MethodMigrate(method);
ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
int handle = iEnvelopes(symbol,timeframe,
ma_period,ma_shift,ma_method,
applied_price,deviation);
if (handle < 0)
{
Print("The iEnvelopes object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,mode-1,shift));
}
double iForceMQL4(string symbol, int tf, int period, int method, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_MA_METHOD ma_method = MethodMigrate(method);
int handle = iForce(symbol,timeframe,period,ma_method,VOLUME_TICK);
if (handle < 0)
{
Print("The iForce object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
double iFractalsMQL4(string symbol, int tf, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iFractals(symbol,timeframe);
if (handle < 0)
{
Print("The iFractals object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,mode-1,shift));
}
double iGatorMQL4(string symbol, int tf, int jaw_period, int jaw_shift, int teeth_period, int teeth_shift, int lips_period, int lips_shift, int method, int price, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_MA_METHOD ma_method = MethodMigrate(method);
ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
int handle = iGator(symbol,timeframe,jaw_period,jaw_shift,
teeth_period,teeth_shift,
lips_period,lips_shift,
ma_method,applied_price);
if (handle < 0)
{
Print("The iGator object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,mode-1,shift));
}
double iHighMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
double array[1];
int copied = CopyHigh(symbol, timeframe, shift, 1, array);
if (copied != 1) return (0);
return (array[0]);
}
int iHighestMQL4(string symbol, int tf, int type, int count=WHOLE_ARRAY, int start=0) export
{
if (start < 0) return (-1);
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
if (count<=0) count = Bars(symbol,timeframe);
if (type<=MODE_OPEN)
{
double open[];
ArraySetAsSeries(open, true);
CopyOpen(symbol, timeframe, start, count, open);
return (ArrayMaximum(open,0,count) + start);
}
if (type==MODE_LOW)
{
double low[];
ArraySetAsSeries(low, true);
CopyLow(symbol, timeframe, start, count, low);
return (ArrayMaximum(low,0,count) + start);
}
if (type==MODE_HIGH)
{
double high[];
ArraySetAsSeries(high, true);
CopyHigh(symbol, timeframe, start, count, high);
return (ArrayMaximum(high,0,count) + start);
}
if (type==MODE_CLOSE)
{
double close[];
ArraySetAsSeries(close, true);
CopyClose(symbol, timeframe, start, count, close);
return (ArrayMaximum(close,0,count) + start);
}
if (type==MODE_VOLUME)
{
long volume[];
ArraySetAsSeries(volume, true);
CopyTickVolume(symbol, timeframe, start, count, volume);
return (ArrayMaximum(volume,0,count) + start);
}
if (type>=MODE_TIME)
{
datetime time[];
ArraySetAsSeries(time, true);
CopyTime(symbol, timeframe, start, count, time);
return (ArrayMaximum(time,0,count) + start);
}
return (-1);
}
double iLowMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
double array[1];
int copied = CopyLow(symbol,timeframe,shift,1,array);
if (copied!=1) return (0);
return (array[0]);
}
double iOpenMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
double array[1];
int copied = CopyOpen(symbol, timeframe, shift, 1, array);
if (copied != 1) return (0);
return (array[0]);
}
double iRSIMQL4(string symbol, int tf, int period, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_APPLIED_PRICE applied_price = PriceMigrate(price);
static int handle = INVALID_HANDLE;
if (handle == INVALID_HANDLE)
{
handle = iRSI(symbol, timeframe, period, applied_price);
}
if (handle < 0)
{
Print("The iRSI object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle,0,shift) );
}
double iRVIMQL4(string symbol, int tf, int period, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iRVI(symbol, timeframe, period);
if (handle < 0)
{
Print("The iRVI object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle, mode, shift) );
}
double iSARMQL4(string symbol, int tf, double step, double maximum, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iSAR(symbol, timeframe, step, maximum);
if (handle < 0)
{
Print("The iSAR object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle, 0, shift) );
}
double iStdDevMQL4(string symbol, int tf, int ma_period, int ma_shift, int method, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_MA_METHOD ma_method = MethodMigrate(method);
ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
int handle = iStdDev(symbol,timeframe,ma_period,ma_shift,
ma_method,applied_price);
if (handle < 0)
{
Print("The iStdDev object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
datetime iTimeMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
datetime array[1];
int copied = CopyTime(symbol, timeframe, shift, 1, array);
if (copied != 1) return (0);
return (array[0]);
}
int iLowestMQL4(string symbol, int tf, int type, int count=WHOLE_ARRAY, int start=0) export
{
if(start<0) return(-1);
ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
if(count<=0) count=Bars(symbol,timeframe);
if(type<=MODE_OPEN)
{
double open[];
ArraySetAsSeries(open,true);
CopyOpen(symbol,timeframe,start,count,open);
return(ArrayMinimum(open,0,count)+start);
}
if(type==MODE_LOW)
{
double low[];
ArraySetAsSeries(low,true);
CopyLow(symbol,timeframe,start,count,low);
return(ArrayMinimum(low,0,count)+start);
}
if(type==MODE_HIGH)
{
double high[];
ArraySetAsSeries(high,true);
CopyHigh(symbol,timeframe,start,count,high);
return(ArrayMinimum(high,0,count)+start);
}
if(type==MODE_CLOSE)
{
double close[];
ArraySetAsSeries(close,true);
CopyClose(symbol,timeframe,start,count,close);
return(ArrayMinimum(close,0,count)+start);
}
if(type==MODE_VOLUME)
{
long volume[];
ArraySetAsSeries(volume,true);
CopyTickVolume(symbol,timeframe,start,count,volume);
return(ArrayMinimum(volume,0,count)+start);
}
if(type>=MODE_TIME)
{
datetime time[];
ArraySetAsSeries(time,true);
CopyTime(symbol,timeframe,start,count,time);
return(ArrayMinimum(time,0,count)+start);
}
return(0);
}
void EMA(double &array[], int InpMAPeriod) export
{
int count=ArraySize(array)-1;
double pr=2.0/(InpMAPeriod+1);
count--;
while (count >= 0)
{
array[count]=array[count]*pr+array[count+1]*(1-pr);
count--;
}
}
void SimpleMA(double &price[], double &ExtLineBuffer[], int InpMAPeriod) export
{
int rates_total=ArraySize(price);
int i,ii;
double Value;
for (ii=0;ii<(rates_total-InpMAPeriod);ii++)
{
Value=0;
for (i=0;i<InpMAPeriod;i++) Value+=price[ii+i];
Value/=InpMAPeriod;
ExtLineBuffer[ii]=Value;
}
}
void SmoothedMA(double &price[], double &ExtLineBuffer[], int InpMAPeriod) export
{
int rates_total=ArraySize(price)-1;
int i,limit;
limit=rates_total-InpMAPeriod;
double firstValue=0;
for (i=rates_total;i>limit;i--) firstValue+=price[i];
firstValue/=InpMAPeriod;
ExtLineBuffer[limit+1]=firstValue;
for (i=limit;i>=0;i--) ExtLineBuffer[i]=(ExtLineBuffer[i+1]*(InpMAPeriod-1)+price[i])/InpMAPeriod;
}
void LWMA(double &price[], double &ExtLineBuffer[], int InpMAPeriod) export
{
int rates_total=ArraySize(price)-1;
int limit=rates_total-InpMAPeriod;
int i,weightsum;
weightsum=0;
double sum;
double firstValue=0;
for (i=limit;i<=rates_total;i++)
{
int k=i-limit;
weightsum+=k;
firstValue+=k*price[i];
}
firstValue/=weightsum;
ExtLineBuffer[limit+1]=firstValue;
for (i=limit; i>=0; i--)
{
sum=0;
for (int j=0;j<InpMAPeriod;j++)
sum+=(InpMAPeriod-j)*price[i+j];
ExtLineBuffer[i]=sum/weightsum;
}
}
double iIchimokuMQL4(string symbol, int tf, int tenkan_sen, int kijun_sen, int senkou_span_b, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iIchimoku(symbol,timeframe, tenkan_sen,kijun_sen,senkou_span_b);
if (handle < 0)
{
Print("The iIchimoku object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,mode-1,shift));
}
double iMACDMQL4(string symbol, int tf, int fast_ema_period, int slow_ema_period, int signal_period, int price, int mode, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
int handle = iMACD(symbol,timeframe, fast_ema_period,slow_ema_period, signal_period,applied_price);
if (handle < 0)
{
Print("The iMACD object is not created: Error ", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle, mode, shift));
}
double iMFIMQL4(string symbol, int tf, int period, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle=(int)iMFI(symbol,timeframe,period,VOLUME_TICK);
if (handle < 0)
{
Print("The iMFI object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
double iMomentumMQL4(string symbol, int tf, int period, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
int handle = iMomentum(symbol,timeframe,period,applied_price);
if (handle < 0)
{
Print("The iMomentum object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
double iOBVMQL4(string symbol, int tf, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iOBV(symbol,timeframe,VOLUME_TICK);
if (handle < 0)
{
Print("The iOBV object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
double iOsMAMQL4(string symbol, int tf, int fast_ema_period, int slow_ema_period, int signal_period, int price, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
int handle = iOsMA(symbol,timeframe,
fast_ema_period,slow_ema_period,
signal_period,applied_price);
if (handle < 0)
{
Print("The iOsMA object is not created: Error", GetLastError());
return (-1);
}
else
return (CopyBufferMQL4(handle,0,shift));
}
long iVolumeMQL4(string symbol, int tf, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
long array[1];
int copied = CopyTickVolume(symbol, timeframe, shift, 1, array);
if (copied != 1) return (0);
return (array[0]);
}
double iWPRMQL4(string symbol, int tf, int period, int shift) export
{
ENUM_TIMEFRAMES timeframe = TFMigrate(tf);
int handle = iWPR(symbol, timeframe, period);
if (handle < 0)
{
Print("The iWPR object is not created: Error", GetLastError());
return (-1);
}
else
return ( CopyBufferMQL4(handle, 0, shift) );
}
double iBandsOnArrayMQL4(double &array[], int total, int period, int deviation, int bands_shift, int mode, int shift) export
{
Print("Error: iBandsOnArray() - Not implemented stub.");
return (0);
}
double iCCIOnArrayMQL4(double &array[], int total, int period, int shift) export
{
Print("Error: iCCIOnArray() - Not implemented stub.");
return (0);
}
double iEnvelopesOnArrayMQL4(double &array[], int total, int ma_period, int ma_method, int ma_shift, double deviation, int mode, int shift) export
{
Print("Error: iEnvelopesOnArray() - Not implemented stub.");
return (-1);
}
double iRSIOnArrayMQL4(double &array[], int total, int period, int shift) export
{
if (total == 0) total = ArraySize(array);
int stop = total-shift;
if (period<=1 || shift<0 || stop<=period) return (0);
bool is_series = ArrayGetAsSeries(array);
if(is_series)
ArraySetAsSeries(array,false);
int i;
double SumP=0;
double SumN=0;
for (i=1; i<=period; i++)
{
double diff = array[i]-array[i-1];
if (diff > 0)
SumP += diff;
else
SumN += -diff;
}
if (!MathIsValidNumber(SumP) || !MathIsValidNumber(SumN))
{
return MathSqrt(-1);
}
double AvgP = SumP/period;
double AvgN = SumN/period;
for (; i<stop; i++)
{
double diff = array[i]-array[i-1];
AvgP = (AvgP*(period-1)+(diff>0?diff:0))/period;
AvgN = (AvgN*(period-1)+(diff<0?-diff:0))/period;
}
double rsi;
if(AvgN==0.0)
{
rsi = (AvgP==0.0 ? 50.0 : 100.0);
}
else
{
rsi = 100.0-(100.0/(1.0+AvgP/AvgN));
}
if (is_series) ArraySetAsSeries(array, true);
return (rsi);
}
double iMAOnArrayMQL4(double &array[], int total, int period, int ma_shift, int ma_method, int shift) export
{
double buf[],arr[];
if(total==0) total=ArraySize(array);
if(total>0 && total<=period) return(0);
if(shift>total-period-ma_shift) return(0);
switch(ma_method)
{
case MODE_SMA :
{
total=ArrayCopy(arr,array,0,shift+ma_shift,period);
if(ArrayResize(buf,total)<0) return(0);
double sum=0;
int i,pos=total-1;
for(i=1;i<period;i++,pos--)
sum+=arr[pos];
while(pos>=0)
{
sum+=arr[pos];
buf[pos]=sum/period;
sum-=arr[pos+period-1];
pos--;
}
return(buf[0]);
}
case MODE_EMA :
{
if(ArrayResize(buf,total)<0) return(0);
double pr=2.0/(period+1);
int pos=total-2;
while(pos>=0)
{
if(pos==total-2) buf[pos+1]=array[pos+1];
buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
pos--;
}
return(buf[shift+ma_shift]);
}
case MODE_SMMA :
{
if(ArrayResize(buf,total)<0) return(0);
double sum=0;
int i,k,pos;
pos=total-period;
while(pos>=0)
{
if(pos==total-period)
{
for(i=0,k=pos;i<period;i++,k++)
{
sum+=array[k];
buf[k]=0;
}
}
else sum=buf[pos+1]*(period-1)+array[pos];
buf[pos]=sum/period;
pos--;
}
return(buf[shift+ma_shift]);
}
case MODE_LWMA :
{
if(ArrayResize(buf,total)<0) return(0);
double sum=0.0,lsum=0.0;
double price;
int i,weight=0,pos=total-1;
for(i=1;i<=period;i++,pos--)
{
price=array[pos];
sum+=price*i;
lsum+=price;
weight+=i;
}
pos++;
i=pos+period;
while(pos>=0)
{
buf[pos]=sum/weight;
if(pos==0) break;
pos--;
i--;
price=array[pos];
sum=sum-lsum+price*period;
lsum-=array[i];
lsum+=price;
}
return(buf[shift+ma_shift]);
}
default: return(0);
}
return(0);
}
double iMomentumOnArrayMQL4(double &array[], int total, int period, int shift) export
{
Print("Error: iMomentumOnArray() - Not implemented stub.");
return -1;
}
double iStdDevOnArrayMQL4(double &array[], int total, int ma_period, int ma_shift, int ma_method, int shift) export
{
Print("Error: iStdDevOnArray() - Not implemented stub.");
return (-1);
}
void HideTestIndicatorsMQL4(bool hide) export
{
Print("Error: HideTestIndicators() - Not implemented stub.");
}
void IndicatorBuffersMQL4(int count) export
{
Print("Error: IndicatorBuffers() - Not implemented stub.");
}
int IndicatorCountedMQL4(void) export
{
Print("Error: IndicatorCounted() - Not implemented stub.");
return (0);
}