Problem plotting moving average MQL5

 

Hi All,

I've been trying to open a chart and then plot a moving average from inside a script.

This is my simple code:

void OnStart()
{
long _chart = ChartOpen("AUDUSD.a",PERIOD_H1);
int   _myMA=iMA(Symbol(),PERIOD_H1,21,0,MODE_SMA,PRICE_CLOSE);
ChartIndicatorAdd(_chart,0,_myMA);
}


But .... no joy.

What have I missed?

Cheers

 
Graham Webber:

Hi All,

I've been trying to open a chart and then plot a moving average from inside a script.

This is my simple code:


But .... no joy.

What have I missed?

Cheers

That symbol() is the problem perhaps
 
Thanks for the reply. I just tried _Symbol, too, but same result :(
 

@Graham from Brisbane@Graham Webber

It is against the rules to have multiple MQL5 Community Accounts. You can only have one account.

Please close down the extra account(s). You can contact the Service Desk and request that the it be deleted so that only one remains.

Terms of Use of MQL5 community

12.11. The User shall use only one unique MQL5 ID on the www.mql5.com website. In case additional/repeated User accounts are detected, all of them shall be closed. MetaQuotes Ltd also reserves the right to invalidate all payment transactions made from additional/repeated accounts.

 

Sorry about that. I've had the Graham from Brisbane account for years. Today is the first time I've seen the Graham Webber account. Very confusing. I think it happened as a result of signing in via Gmail. Anyhow, I don't want two accounts, either, it's too confusing. I'll delete that account as soon as I can.

Meanwhile, I've seen heaps of posts from you, perhaps you could give me a clue as to how to solve my problem. Wow, MQL4 to MQL5 isn't easy for a guy my age .....  :)

 
Graham from Brisbane #: Meanwhile, I've seen heaps of posts from you, perhaps you could give me a clue as to how to solve my problem.

Problems in your code:

  • Not checking return values
  • Not checking for error codes
  • Using different symbol for chart and indicator

Sample code (compiled and tested) ...

void OnStart() {
   string          sSymbol    = "EURUSD";
   ENUM_TIMEFRAMES eTimeFrame = PERIOD_H1;
   ResetLastError();
   long nChartID = ChartOpen( sSymbol, eTimeFrame );
   if( nChartID > 0 ) {
      ResetLastError();
      int hMA = iMA( sSymbol, eTimeFrame, 21, 0, MODE_SMA, PRICE_CLOSE );
      if( hMA != INVALID_HANDLE ) {
         ResetLastError();
         if( ChartIndicatorAdd( nChartID, 0, hMA ) ) {
            // do something
         } else
            Print( "Unable to add indicator handle with error: ", _LastError );
      } else
         Print( "Unable to obtain indicator handle with error: ", _LastError );
   } else
      Print( "Unable to open chart with error: ", _LastError );
};  
 
Graham from Brisbane #: Wow, MQL4 to MQL5 isn't easy for a guy my age .....  :)
I'm 55, but I don't believe age has anything to do with it. It simply has to do with aptitude and/or effort, but not age.
 

Thanks for the help, I've just learned a few new things.

BTW, 76 next February, but still trying hard ...  :)