В MQL5 существует несколько вариантов вызова индикаторов, и осуществляются они в основном при помощи функций IndicatorCreate() и iCustom(). Причем эти функции лишь возвращают хендл индикатора, и дальнейшая работа с индикаторами ведется именно через него. Так что же такое хендл? Как работать с функциями IndicatorCreate() и iCustom()? И как...
//+------------------------------------------------------------------+//| Example iEnvelopes values on a chart.mq5 |//| Copyright 2020, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2020, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.00"//--- input parametersinputint Input1=9;
//---int handle_iEnvelopes; // variable for storing the handle of the iEnvelopes indicator//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- create handle of the indicator iEnvelopes
handle_iEnvelopes=iEnvelopes(Symbol(),Period(),3,0,MODE_LWMA,PRICE_OPEN,0.03);
//--- if the handle is not createdif(handle_iEnvelopes==INVALID_HANDLE)
{
//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iEnvelopes indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped earlyreturn(INIT_FAILED);
}
//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//---
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
//---string text="";
double upper[],lower[];
ArraySetAsSeries(upper,true);
ArraySetAsSeries(lower,true);
int start_pos=0,count=3;
if(!iGetArray(handle_iEnvelopes,UPPER_LINE,start_pos,count,upper) ||
!iGetArray(handle_iEnvelopes,LOWER_LINE,start_pos,count,lower))
{
return;
}
string text_upper="",text_lower="";
for(int i=count-1; i>=0; i--)
{
text_upper = text_upper + "Upper"+"["+IntegerToString(i)+"]"+" "+DoubleToString(upper[i],Digits()+1) +" | ";
text_lower = text_lower + "Lower"+"["+IntegerToString(i)+"]"+" "+DoubleToString(lower[i],Digits()+1) +" | ";
}
Comment(text_upper+"\n"+text_lower);
}
//+------------------------------------------------------------------+//| Get value of buffers |//+------------------------------------------------------------------+bool iGetArray(constint handle,constint buffer,constint start_pos,
constint count,double &arr_buffer[])
{
bool result=true;
if(!ArrayIsDynamic(arr_buffer))
{
PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
return(false);
}
ArrayFree(arr_buffer);
//--- reset error codeResetLastError();
//--- fill a part of the iBands array with values from the indicator bufferint copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
if(copied!=count)
{
//--- if the copying fails, tell the error codePrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
__FILE__,__FUNCTION__,count,copied,GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculatedreturn(false);
}
return(result);
}
//+------------------------------------------------------------------+
我最初依靠的是这篇文章:https://www.mql5.com/ru/articles/43
但那里的代码只到检查和获取指标数据到缓冲区数组的时刻,此外,没有如何处理收到的指标数据的例子。我翻了翻参考书,里面主要有指标的代码例子,我从中挑选了setindexbuffer。我想知道该往哪边 "挖"。我昨天就厌倦了,我已经在这里呆了半天了。
已经试过了。但在向导中,它在那里生成了类。我至少想以一种简单的方式理解代码--代码我至少了解一点儿。能否请您告知如何纠正代码,以便在EA中获得指标值?
示例:如何从EA获得iEnvelopes指标值
结果。
例子:如何从EA中获得iEnvelopes指标值
非常感谢您!我将研究一下!
Vladimir,请告诉我:它从哪里得到UPPER_LINE和LOWER_LINE?如果我们只得到了指示器的手柄,我们是否直接从手柄上得到了它?
Vladimir,请告诉我:它从哪里得到UPPER_LINE和LOWER_LINE?如果我们只得到了指示器的手柄,那么我们是否也一下子得到了手柄?
我不明白你的问题。它是一组没有意义的字母。请重新表述你的问题--不要急于打字,仔细想想你的问题,冷静地打字。
我不明白你的问题。一组没有意义的字母。请重新表述你的问题--不要急于打字,考虑一下你的问题,冷静地打字。
在该函数的参数中。
iGetArray(handle_iEnvelopes,UPPER_LINE,start_pos,count,upper)
你使用了UPPER_LINE。
这个参数值来自哪里,来自你之前收到的句柄。
?
在函数参数中。
你使用了UPPER_LINE。
这个参数值从何而来,来自于之前收到的握手。
?
不,该值取自iEnvelopes 帮助。
下午。当在GBPUSD货币上运行EA时,我在代码中为EURUSD设置了指标,EA在启动时崩溃了。然而,如果我在欧元兑美元上运行专家顾问,并指定英镑兑美元为指标,一切都能正常工作。它能与什么有关?
下午。在运行GBPUSD货币的EA时,我在代码中为EURUSD设置了一个指标,EA在启动时崩溃了。然而,如果我在欧元兑美元上运行EA,并指定英镑兑美元为指标,一切都能正常工作。这可能是由于什么原因?
1.使用向导MQL5生成一个EA - 例如,为EA指定 "GBPUSD",为信号模块指定 "EURUSD"。你将得到一个正确的示例代码。研究代码。在未来,只要避免手动编辑,使用MQL5向导生成代码。
2.如果您为专家顾问 指定"GBPUSD",您应该只在"GBPUSD " 上运行该EA。
1.使用向导MQL5生成一个EA - 例如为EA指定 "GBPUSD",为信号模块指定 "EURUSD"。你将得到一个正确的示例代码。研究代码。在未来,只要避免手动编辑,使用MQL5向导生成代码。
2.如果您为专家顾问 指定了"GBPUSD",那么您应该只在"GBPUSD " 上运行该EA。
Vladimir,我使用MQL5向导生成了代码。我试过你的专家顾问,当我在GBPUSD货币上运行时,它也不对我起作用。
它在日志中说,如下。