有点儿失眠,又静不下心写程序。所以来把最后一段说明下。
复制内容到剪贴板
代码:
int start()
{
int limit,counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int i=0; i<limit; i++)
{
Buffer1[i]=iMACD(NULL,0,Fast,Slow,Signal,PRICE_CLOSE,MODE_MAIN,i);
Buffer2[i]=iMACD(NULL,0,Fast,Slow,Signal,PRICE_CLOSE,MODE_SIGNAL,i);
Buffer3[i]=Buffer1[i] - Buffer2[i];
}
return(0);
}1、int limit,counted_bars=IndicatorCounted();//定义两个整形变量,并给counted_bars变量负值。
这里面IndicatorCounted()函数是mt4内置函数不需要参数,其返回值为已经计算过的指标数组数量。
如果指标错误则这个函数会返回一个负数。
2、if(counted_bars<0) return(-1); //如果条件成立说明指标调用运行错误。则退出程序。
3、if(counted_bars>0) counted_bars--;//从已经计算的指标中去除最后一条。
这条语句用来修正counted_bars使得已经计算的最后一个数值可以在接下来的运算中重新计算一次。
4、limit=Bars-counted_bars;//计算需要计算的指标数据的柱数。
这里需要说明。在mt4中指标数组的索引和K线的索引标记相同,是从后向前递增的从0开使的整数。
也就是说,最后一条K线的索引是0同时K线所对应的指标索引也是0。
那么倒数第2条的索引标记为1。倒数第三条的索引标记为2。
这一点一定要理解清楚。不然写程序的时候就会发生错误。
语句中的Bars是mt4预定义变量,其值是当前图表中K线的条数。
这里详细说下为什么有个counted_bars--;的语句,这个语句的意思是对变量counted_bars进行自减一操作。
因为主函数是每次价格变动就会运行一次。当运行完成后。IndicatorCounted()值应该等于Bars也就是K线的条数
如果没有上面的自减一操作,那么当价格变动有了新的收盘价但并没有生成新的K线。这时候计算limit的值将=0.
那么下面的for循环体将不会再计算最后一条k线相对应的指标数值。
实际上这个是需要计算的(因为有了新的收盘价)。而有了自减一的操作就可以对最有一个,也就是当前K线对应的指标值进行运算。
(不知道能看明白不自己慢慢捉摸捉摸)。这个自减一是必需的。
5、for(int i=0; i<limit; i++) for 循环语句(因为有了前面的自减一操作,这里limit最小等于1)
关于for循环的运行不作解释。各位自己找资料学习。
6、Buffer1[ i ]=iMACD(NULL,0,Fast,Slow,Signal,PRICE_CLOSE,MODE_MAIN,i); //调用MACD指标函数为Buffer1数组负值。
看下iMACD()这个内置指标函数的定义。
iMACD( string symbol, int timeframe, int fast_ema_period, int slow_ema_period, int signal_period, int applied_price, int mode, int shift)
symbol: 货币标识。通用指标用NULL常量。
timeframe: 计算所依据的图表时间周期 。0表示依据当前图表周期。
fast_ema_period: 快线周期
slow_ema_period: 慢线周期
signal_period: 信号线周期
applied_price: 计算所用价格模式
mode: 指标索引模式。MACD指标有两条线,因此这个位置有0,1两个选择。也可以用mt4预定义常量。
shift: 索引号
这里Buffer1取macd主线数据。Buffer2取macd信号线数据。
7、Buffer3[ i ]=Buffer1[ i ] - Buffer2[ i ];//计算MACD两条线之间的距离。
8、这里用到了外部变量。也可以叫用户自定义变量。这种变量在加载图表的时候可以修改。
复制内容到剪贴板
代码:
extern int Fast = 10;
extern int Slow = 22;
extern int Signal = 7;extern 关键字说明后面定义的变量是外部变量。
这里贴下iMACD()函数的英文说明。
double iMACD( string symbol, int timeframe, int fast_ema_period, int slow_ema_period, int signal_period, int applied_price, int mode, int shift)
Calculates the Moving averages convergence/divergence and returns its value. In the systems where OsMA is called MACD Histogram, this indicator is displayed as two lines. In the Client Terminal, the Moving Average Convergence/Divergence is drawn as a histogram.
Parameters:
symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.
timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.
fast_ema_period - Number of periods for fast moving average calculation.
slow_ema_period - Number of periods for slow moving average calculation.
signal_period - Number of periods for signal moving average calculation.
applied_price - Applied price. It can be any of Applied price enumeration values.
mode - Indicator line index. It can be any of the Indicators line identifiers enumeration value.
shift - Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).
Sample:
if(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0)) return(0);
英文好的自己看吧。我看着很累。
[
本帖最后由 xfxyldj 于 2007-9-16 02:44 编辑 ]