我正在尝试在名为ATREx的阵列上创建一个标准偏差的上下波段。下面的第一个while循环创建了数组ATREx。我相信这部分是正确编码的。
然而,用于在ATREx阵列上创建上下波段的第二个while循环似乎没有给出正确的结果。
知道我错过了什么吗?
Counted_bars=IndicatorCounted();
i=Bars-Counted_bars-1;
while(i>0)
{
Moving_Average_Close=iMA(NULL,0,40,0,MODE_EMA,PRICE_CLOSE,i);
Moving_Average_Low=iMA(NULL,0,40,0,MODE_EMA,PRICE_LOW,i);
Moving_Average_High=iMA(NULL,0,40,0,MODE_EMA,PRICE_HIGH,i);
ATR=iATR(NULL,0,14,i);
if(close[i]<Moving_Average_Close)
{
ATREx[i]=(((low[i]-Moving_Average_Low)/close[i])*100)*((ATR/close[i])*100);
//ATREx_200[i]=(((low[i]-Moving_Average_Low_200)/close[i])*100)*((ATR/close[i])*100);
}
else
ATREx[i]=(((high[i]-Moving_Average_High)/close[i])*100)*((ATR/close[i])*100);
i--;
}
j=Bars-Counted_bars-1;
while (j>0)
{
ATREx_Upper[j]=iMAOnArray(ATREx,0,40,0,MODE_EMA,j)+iStdDevOnArray(ATREx,0,200,0,MODE_EMA,j);
ATREx_Lower[j]=iMAOnArray(ATREx,0,40,0,MODE_EMA,j)-iStdDevOnArray(ATREx,0,200,0,MODE_EMA,j);
j--;
}
答案 0 :(得分:0)
{ iMAOnArray | iStdDevOnArray() | ... }
指标从左到右计算。 要访问数组元素的系列数组(即从右到左), 必须使用ArraySetAsSeries()
功能。
double iMAOnArray( double array[], // array with data <--------------
int total, // number of elements <-----------
int ma_period, // MA averaging period
int ma_shift, // MA shift
int ma_method, // MA averaging method
int shift // shift
)
double iStdDevOnArray( double array[], // array with data <--------------
int total, // number of elements <-----------
int ma_period, // MA averaging period
int ma_shift, // MA shift
int ma_method, // MA averaging method
int shift // shift
)
CustomIndicator
实例的共享独奏主题。所以在CustomIndicator
代码中要多次小心:
Counted_bars = IndicatorCounted();
i = Bars - Counted_bars - 1;
while( i > 0 )
{ Moving_Average_Close = iMA( _Symbol, PERIOD_CURRENT, 40, 0, MODE_EMA, PRICE_CLOSE, i );
Moving_Average_Low = iMA( _Symbol, PERIOD_CURRENT, 40, 0, MODE_EMA, PRICE_LOW, i );
Moving_Average_High = iMA( _Symbol, PERIOD_CURRENT, 40, 0, MODE_EMA, PRICE_HIGH, i );
ATR = iATR( _Symbol, PERIOD_CURRENT, 14, i );
if ( close[i] < Moving_Average_Close )
{ ATREx[i] = ( ( ( low[i] - Moving_Average_Low ) / close[i] ) * 100 ) * ( ( ATR / close[i] ) * 100 );
// ATREx_200[i] = ( ( ( low[i] - Moving_Average_Low_200 ) / close[i] ) * 100 ) * ( ( ATR / close[i] ) * 100 );
}
else ATREx[i] = ( ( ( high[i] - Moving_Average_High ) / close[i] ) * 100 ) * ( ( ATR / close[i] ) * 100 );
i--;
}
int nBARs = 40; // //?40 vvv // NEVER LEAVE ANY FULL-DEPTH PROCESSING aFeasibleDEPTH WILL SUITE ENOUGH
double aCPY[]; // ||
ArraySetAsSeries(); // ||
// ||
j = Bars - Counted_bars - 1; // ||
while( j > 0 ) // || ?40 // OUGHT BE 40?
{ ArrayCopy( aCPY, ATREx, 0, j, nBARs );// vv vvv
double aStDev = iStdDevOnArray( aCPY, nBARs, 200, 0, MODE_EMA, 0 ), // NEVER LEAVE ANY EXTENSIVE PROCESSING PIECE OF CODE IN INDICATOR (a blocking solo-thread for *ALL* MT4.Graphs !!! )
aMeanL = iMAOnArray( aCPY, nBARs, 40, 0, MODE_EMA, 0 ); // NEVER LEAVE ANY EXTENSIVE PROCESSING PIECE OF CODE IN INDICATOR ( a blocking solo-thread for *ALL* MT4.Graphs !!! )
ATREx_Upper[j] = aMean + aStDev; // REUSE VALUEs NEVER RE-PROCESS THE SAME THING, THAT HAS ALREADY BEEN COMPUTED
ATREx_Lower[j] = aMean - aStDev; // REUSE VALUEs NEVER RE-PROCESS THE SAME THING, THAT HAS ALREADY BEEN COMPUTED
// *******************************************************************************************************************
// WAS 4x MORE EXPENSIVE:
// *******************************************************************************************************************
// ATREx_Upper[j] = iMAOnArray( ATREx, 0, 40, 0, MODE_EMA, j ) + iStdDevOnArray( ATREx, 0, 200, 0, MODE_EMA, j );
// ATREx_Lower[j] = iMAOnArray( ATREx, 0, 40, 0, MODE_EMA, j ) - iStdDevOnArray( ATREx, 0, 200, 0, MODE_EMA, j );
// *******************************************************************************************************************
j--;
}
double aCPY[]; // best put MEM .ALLOC in file-scope declarations
ArraySetAsSeries( aCPY, True ); // lock a TimeSeries ordering right there
虽然 nBARs
可能需要一些定量建模来满足最佳精度/计算_费用余额,但强烈建议不要复制 WHOLE_ARRAY
,除非有任何确实的(数学支持的)全深度反向步进卷积(其中[0]的任何变化是由于以多米诺方式计算后向传播,更深入回到timeDOMAIN历史 - 这显然是因为非常相反的行为,恰恰相反,基于数量的技术分析很乐意与之合作。)因此,阿尔伯特·爱因斯坦(Albert EINSTEIN)为现代物理学创造了如此可爱的东西,而不是复制品。&#34; 按需要复制,尽可能复制,但不是单一的&#34; - 那你就有最好的表现。
虽然重复使用iMAOnArray()
和iStdDevOnArray()
,但它在数值和计算上并不像使用滑动窗口计算一样高效,从不重新计算任何值,但通过重新提高速度 - 使用所有已经计算过的值 - iMAOnArray( ATTREx, WHOLE_ARRAY, 3, 0, MODE_SMA, j);
是其中一个示例,由{strong> [j]
在while(){...}
中重新循环。