这是我在Stackoverflow上的第一篇文章,尽管我已经访问这里多年了。
我已经阅读了消息指南,因此我将尽可能简洁和具体。
我一直在尝试将自定义指标的代码直接嵌入到 EA 交易中,而无需调用 iCustom:
iCustom(Symbol(),60,"MB",3D,0,1)>0;
到目前为止,我已经失败了,虽然我相信这对许多人来说可能是一件微不足道的事情,但如果你不知道,你就不知道。
有问题的iCustom代码如下,我将不胜感激任何帮助:
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_width1 5
#property indicator_width2 5
extern int 3D= 5
double AIAIAI[];
double B1B1B1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexBuffer( 0, AIAIAI );
SetIndexEmptyValue( 0, 0.0 );
SetIndexStyle( 0, DRAW_ARROW );
SetIndexArrow( 0, 250 );
SetIndexLabel( 0, NULL );
SetIndexBuffer( 1, B1B1B1);
SetIndexEmptyValue( 1, 0.0 );
SetIndexStyle( 1, DRAW_ARROW );
SetIndexArrow( 1, 250 );
SetIndexLabel( 1, NULL );
IndicatorDigits( 5 );
//---- name for DataWindow and indicator subwindow label
IndicatorShortName( MB(" + 3D+ ")" );
return( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars = IndicatorCounted();
if (counted_bars < 0) return (-1);
if (counted_bars > 0) counted_bars--;
int intLimit = Bars - counted_bars;
int LO, HI;
for( int NINI = intLimit; NINI >= 0; NINI-- )
{
AIAIAI[NINI] = 0.0;
B1B1B1[NINI] = 0.0;
LO = iLowest( Symbol(), Period(), MODE_LOW, 3D, NINI );
if ( LO == NINI )
{
AIAIAI[NINI] = Low[NINI];
}
HI = iHighest( Symbol(), Period(), MODE_HIGH, 3D, NINI );
if ( HI == NINI )
{
B1B1B1[NINI] = High[NINI];
}
}
return( 0 );
}
谢谢
指标代码与编译的 EA 打包的最佳方法是将其作为资源包含并继续使用 icustom 调用它。以这种方式执行此操作时,无需重构和提取指标逻辑。
语法如下:
#resource "MyCustomIndicator.ex4"
double my_custom_zero_buffer(string symbol, int period, int setting, int i)
{
return iCustom(symbol, period, "::MyCustomIndicator.ex4", setting, 0, i);
}
当您编译此 EA 时,指标也将编译并打包在一起,以便您可以使用/分发它而不会暴露指标逻辑
如果您仅使用指标作为示例,这可能不是一个好的指标,因为它不使用缓冲区和整体非常简单的指标。当您需要时,从 ea 重新计算缓冲区值应该很容易。
double iCustomValue(const int param,const int buffer,const int shift)
{
switch(buffer)
{
case 0:
if(iLowest(_Symbol,0,MODE_LOW,param,shift)==shift)
return iLow(_Symbol,0,shift);
break;
case 1:
if(iHighest(_Symbol,0,MODE_HIGH,param,shift)==shift)
return iLow(_Symbol,0,shift);
break;
}
return(0.0);
}
并使用该函数代替您的指标。对于更复杂的指标 - 请记住,调用指标当然较慢,但可以更轻松地进行测试。
我已经意识到,这应该是有意义的,这只有在编译的指标 .ex4 文件与 EA 位于同一目录中时才有效。它似乎忽略了目录路径,尽管我已经得到了之前的工作,其中路径="c:\...\Indicators\mycustomindicator.ex4"。我认为这取决于MT4的版本,因为处理方式似乎有所不同。
有趣的是,推荐的方法根本不起作用!也许出于同样的原因,没有在正确的位置查找:查看"专家"文件夹而不是"指标"文件夹。
巧妙的伎俩,资源的东西!