How can I draw a simple moving average on a newly created chart?

 

Hi All,

I've searched everywhere for the answer to how to draw a MA on a newly opened chart.

The chart opens as expected, but no MA.

Here is the code, but I'm missing something somewhere.

void OnStart()
  {
long _longchart = ChartOpen("AUDUSD.a",60);       // Open the chart of interest on the requested time frame 
int  _mahandle=iMA("AUDUSD.a",0,60,0,MODE_SMA,PRICE_CLOSE);  // get the moving average handle   
      ChartIndicatorAdd(0,0,_mahandle); 
  
  }
Any help would be much appreciated.
Documentation on MQL5: Chart Operations / ChartOpen
Documentation on MQL5: Chart Operations / ChartOpen
  • www.mql5.com
ChartOpen - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Since your code is for a Script, as soon as it terminates, the indicator handle is released, and consequently the Indicator that was added by it, is removed.

EDIT: Seems my logic is incorrect! My apologies for the misinformation!

 
Fernando Carreiro #: Since your code is for a Script, as soon as it terminates, the indicator handle is released, and consequently the Indicator that was added by it, is removed.
No Fernando, the handle being released is not an issue.
 
Graham from Brisbane:

Hi All,

I've searched everywhere for the answer to how to draw a MA on a newly opened chart.

The chart opens as expected, but no MA.

Here is the code, but I'm missing something somewhere.

Any help would be much appreciated.

You are adding the indicator to the current chart.

You need to add it to the newly opened chart.

      ChartIndicatorAdd(_longchart,0,_mahandle); 
 
Alain Verleyen #: No Fernando, the handle being released is not an issue.
Thank you for correcting me!
 

Thankyou Gentlemen, all is revealed.

Have nice days

Cheers ..... G-fer

 

Hi All,

Thanks for your help with the moving average issue.

I would like to add a comment to the new chart, too.

This code compiles properly, but the comments is places on the chart that was open when the script is run.

Any ideas?

Cheers ...

void OnStart()
  {
        long _longchart = ChartOpen("AUDUSD.a",60);       // Open the chart of interest on the requested time frame 
        int  _mahandle=iMA("AUDUSD.a",0,60,0,MODE_SMA,PRICE_CLOSE);  // get the moving average handle   
        ChartIndicatorAdd(_longchart,0,_mahandle);  // Draw the indicator
        Comment("This is a test comment");
  }
 
Graham from Brisbane #: I would like to add a comment to the new chart, too. This code compiles properly, but the comments is places on the chart that was open when the script is run. Any ideas?

The Comment function always applies to the current chart. As you can see from the documentation, it does not have any parameter for selecting a target chart.

However, if you read the Comment documentation, you will notice that it instructs you to also read about ChartSetString and ChartGetString.

So, instead of Comment, use the ChartSetString function with CHART_COMMENT property.

ChartSetString

Sets the string value for a corresponding property of the specified chart

ENUM_CHART_PROPERTY_STRING

ID

Description

Property Type

CHART_COMMENT

Text of a comment in a chart

string

 

Thank you Mr. C,

I replaced

Comment("This is a test comment");

with

ChartSetString(_longchart,CHART_COMMENT,"This is a test comment");

and that worked perfectly.

Cheers ....

Reason: