What is the equivalent of iMAOnArray on MQL5 - page 2

 
Milko Vivaldi # :

Hello, me too I'm trying to create SMA on price but I can't understand what I have to insert as "position" variable... (the first parameters).

Example: if I want a SMA 200 periods writing SimpleMA(?, 200, close) where close is native array of OnCalculate function

Whether I put zero (as positoin) it doesn't work, even because into the code there is 

 so 0-1 isn't possible...

Thanks in advance.

AND why don't you try to pass with the ima() function?

 
Gerard Willia G J B M Dinh Sy #:

AND why don't you try to pass with the ima() function?

I'm trying but I can't understand what put as "position"!

 
Milko Vivaldi # :

I'm trying but I can't understand what put as "position"!

there is no "position" to give in ima()

iMA - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
The function returns the handle of the Moving Average indicator. It has only one buffer. Parameters symbol [in] The symbol name of the security...
 
Milko Vivaldi #:

I'm trying but I can't understand what put as "position"!

In MT5 you have to use the iMA part to get a handle , once , and then use the handle to request data

Here is one example

input int period=200;
input ENUM_MA_METHOD method=MODE_SMA;
input ENUM_APPLIED_PRICE price=PRICE_CLOSE;

//handle of the indicator 
  int moving_average_handle=INVALID_HANDLE;
int OnInit()
  {
  //initialize the handle of the indicator (s) - ideally when the symbol is ready
    moving_average_handle=iMA(_Symbol,_Period,period,0,method,price);
    if(moving_average_handle!=INVALID_HANDLE){
    /* this is how you get values from the indicator */
    double values[];//you need an array
    //and you need to call a function that will copy data with the indicator handle
    //into your values
    int copied_total=CopyBuffer(moving_average_handle,0,0,10,values);
    Print("Copied "+IntegerToString(copied_total)+" values");
    for(int i=0;i<copied_total;i++){
       Print("MA["+IntegerToString(i)+"] "+DoubleToString(values[i],_Digits));
       }
    }else{
    Print("Can't init ma");
    }
  return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  IndicatorRelease(moving_average_handle);
  }

 
Hello everyone, I've recently started learning MQL5, and I wrote a simple code for iMA, but unfortunately, I keep getting Error 2. Can anyone guide me on where my problem might be?

void OnStart()
  {
   int ma_handle = iMA(_Symbol, PERIOD_M15, 6, 0, MODE_SMMA, PRICE_OPEN);

   if(ma_handle == INVALID_HANDLE)
      Print("Error 1: Failed to create moving average indicator.");

   double maOpen[];
   ArraySetAsSeries(maOpen, true);
   ArrayFree(maOpen);
   ResetLastError();

   int start_pos = 0,count = 20;
   int copied = CopyBuffer(ma_handle, 0, start_pos, count, maOpen);
   if(copied != count)
      PrintFormat("Error 2: ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__, count, copied, GetLastError());

   string text = "";
   for(int i = 0; i < count; i++)
      text = text + IntegerToString(i) + ": " + DoubleToString(maOpen[i], Digits() + 1) + "\n";

   Print(text);
  }
 
Babak Ramezani #:
Hello everyone, I've recently started learning MQL5, and I wrote a simple code for iMA, but unfortunately, I keep getting Error 2. Can anyone guide me on where my problem might be?

note the comments regarding handles. check the comment above yours for OnInit.