【发布时间】:2016-07-27 17:31:11
【问题描述】:
作为学习 MQL4 的练习,我正在编写一个自定义指标,它只是复制简单移动平均线,但没有使用内置的 iMA() 函数。下面的代码将指标绘制到图表上,但除结尾之外的线的每个部分都显示极低的值。例如,在 AUD/CAD 图表上,通常在 0.80-0.90 范围内,SMA 线在 0.20-0.30 附近。
该线与价格同步上下移动,最后一根柱线似乎具有准确的读数(因为 SMA 在该点向当前价格飙升),但其他一切都非常低。我已经用尽所有可能的原因来解释为什么会发生这种情况,但没有任何改变。有人可以帮我找出问题所在吗?
/*
SELF-CODED 5-DAY SMA
Set up buffer with style, indexbegin, and color
for each bar:
calculate closing price of last 4 bars and current price
use to find SMA value at that bar
update last value after every tick
lock each bar's SMA value once the bar has close
*/
#property strict
#property indicator_chart_window
#property indicator_buffers 1
double buffer[];
int OnInit()
{
IndicatorBuffers(1);
SetIndexBuffer(0, buffer);
SetIndexStyle(0, DRAW_LINE);
return(INIT_SUCCEEDED);
}
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[]
)
{
int limit = rates_total - prev_calculated;
buffer[0] = iClose(NULL, 0, 0);
for(int b = 1; b<5; b++)
{
buffer[0] = buffer[0] + iClose(NULL, 0, b);
}
buffer[0] = buffer[0]/5;
for(int i = 1; i<limit; i++)
{
buffer[i] = iClose(NULL, 0, i); // Placeholder in first for loop interation
for(int a = 1; a<4; a++) {
{
buffer[i] = buffer[i] + iClose(NULL, 0, i+a);
}
buffer[i] = buffer[i]/5.0;
}
}
return(rates_total);
}
【问题讨论】:
-
所提出的算法本身效率非常低,需要对算法思维进行一些更改,但 StackOverflow 提倡 MCVE 策略以将所有与代码相关的主题建立在C完整的基础上和 Verifiable(即可重复的,包括与数据相关的值)——所以请按照上面的建议更新您的帖子。不管怎样,欢迎来到 StackOverflow 和
MQL4的狂野世界 -
您介意发布一些定量输出吗?类似这样的东西:
Print( "SYMBOL:", _Symbol," PERIOD:", PERIOD_CURRENT, " Last 6 Bars Close[i] := [ ", Close[5], ", ", Close[4], ", ", Close[3], ", ", Close[2], ", ", Close[1], ", ", Close[0]," ], SmaToTEST[1] = ", iCustom( _Symbol, PERIOD_CURRENT, "SMA", 0, 1 ), "SmaToTEST[0] = ", iCustom( _Symbol, PERIOD_CURRENT, "SMA", 0, 0 ) );?
标签: mql4