//--- 输入参数
input int InpPeriod = 10; // 移动平均线计算周期
input ENUM_MA_METHOD InpMethod = MODE_SMA; // 移动平均线计算方法
input ENUM_APPLIED_PRICE InpPrice = PRICE_CLOSE; // 移动平均线计算价格
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 如果移动平均线周期设置为小于1的值,则使用默认值(10)
int period=(InpPeriod<1 ? 10 : InpPeriod);
//--- 创建移动平均线指标句柄
int handle=iMA(Symbol(),Period(),period,0,InpMethod,InpPrice);
if(handle==INVALID_HANDLE)
{
Print("Failed to create the Moving Average indicator handle. Error ",GetLastError());
return;
}
//--- 获得当前卖价
double bid=0;
ResetLastError();
if(!SymbolInfoDouble(Symbol(),SYMBOL_BID,bid))
{
Print("Failed to get Bid price. Error ",GetLastError());
return;
}
//--- 获得当前柱形图的移动平均线值
double array[1];
int copied=CopyBuffer(handle,0,0,1,array);
if(copied!=1)
{
Print("Failed to get Moving Average data. Error ",GetLastError());
return;
}
//--- 获取两者中最高的价格(卖价和移动平均线值),并在日志中显示结果数据
double max_price=MathMax(bid,array[0]);
PrintFormat("Bid: %.*f, Moving Average: %.*f, highest price of the two: %.*f",_Digits,bid,_Digits,array[0],_Digits,max_price);
PrintFormat("Bid price %s moving average",(bid>array[0] ? "higher" : bid<array[0] ? "lower" : "equal to"));
}
|