//--- input parameters
input int InpPeriod = 10; // Moving average calculation period
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Moving average calculation method
input ENUM_APPLIED_PRICE InpPrice = PRICE_CLOSE; // Moving average calculation price
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- if the moving average period is set to a value less than 1, then the default value (10) is used
int period=(InpPeriod<1 ? 10 : InpPeriod);
//--- create the Moving Average indicator handle
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;
}
//--- get the current Bid price
double bid=0;
ResetLastError();
if(!SymbolInfoDouble(Symbol(),SYMBOL_BID,bid))
{
Print("Failed to get Bid price. Error ",GetLastError());
return;
}
//--- get the moving average value on the current bar
double array[1];
int copied=CopyBuffer(handle,0,0,1,array);
if(copied!=1)
{
Print("Failed to get Moving Average data. Error ",GetLastError());
return;
}
//--- get the highest price of the two (Bid price and Moving Average value) and display the resulting data in the journal
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"));
}
|