MetaTrader 5 / 程序库
MovingAverages - MetaTrader 5程序库
4620
MovingAverages库是 MetaTrader 5客户端标准包的一部分。
该库包含了计算不同类型的 移动平均线的函数。总共有8个函数。它们可划分为相同类型的2个组,每组含4个。
第一组函数接收到一个数组,但仅返回移动平均线在指定位置的某个数值:
- SimpleMA() - 计算简单移动平均线的某个数值;
- ExponentialMA() - 计算 指数平均线的某个数值;
- SmoothedMA() - 计算 平滑平均线的某个数值;
- LinearWeightedMA() - 计算 线性加权平均线的某个数值。
这些函数本意只为获取一次一个数组的均值,没有针对多次调用进行优化。如果你需要循环使用这一组的任一个函数(计算多个平均值,然后将每个计算值写入一个数组),你就必须组织一个优化算法。
第二组函数本意是为了将基于含初始值的数组的均线值填充到被接收的数组中:
- SimpleMAOnBuffer() - 将price[]数组的简单均线值填充到输出数组buffer[];
- ExponentialMAOnBuffer() - 将price[]数组的指数均线值填充到输出数组buffer[];
- SmoothedMAOnBuffer() - 将price[]数组的平滑均线值填充到输出数组buffer[];
- LinearWeightedMAOnBuffer() - 将price[]数组的线性加权均线值填充到输出数组buffer[];。
函数:
//+------------------------------------------------------------------+ //| MovingAverages.mqh | //| Copyright 2009, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "https://www.mql5.com" //+------------------------------------------------------------------+ //| 简单移动平均线 | //+------------------------------------------------------------------+ double SimpleMA(const int position,const int period,const double &price[]) //+------------------------------------------------------------------+ //| 指数移动平均线 | //+------------------------------------------------------------------+ double ExponentialMA(const int position,const int period,const double prev_value,const double &price[]) //+------------------------------------------------------------------+ //| 平滑移动平均线 | //+------------------------------------------------------------------+ double SmoothedMA(const int position,const int period,const double prev_value,const double &price[]) //+------------------------------------------------------------------+ //| 线性加权移动平均线 | //+------------------------------------------------------------------+ double LinearWeightedMA(const int position,const int period,const double &price[]) //+------------------------------------------------------------------+ //| 价格数组的简单移动平均 | //+------------------------------------------------------------------+ int SimpleMAOnBuffer(const int rates_total,const int prev_calculated,const int begin, //+------------------------------------------------------------------+ //| 价格数组的指数移动平均 | //+------------------------------------------------------------------+ int ExponentialMAOnBuffer(const int rates_total,const int prev_calculated,const int begin, const int period,const double& price[],double& buffer[]) //+------------------------------------------------------------------+ //| 价格数组的平滑移动平均 | //+------------------------------------------------------------------+ int SmoothedMAOnBuffer(const int rates_total,const int prev_calculated,const int begin, const int period,const double& price[],double& buffer[]) //+------------------------------------------------------------------+ //| 价格数组的线性加权移动平均 | //+------------------------------------------------------------------+ int LinearWeightedMAOnBuffer(const int rates_total,const int prev_calculated,const int begin, const int period,const double& price[],double& buffer[])
例子:
实际用例请参加文章 "MQL5: 创建你自己的指标"
由MetaQuotes Ltd译自俄语
原代码: https://www.mql5.com/ru/code/77