Download MetaTrader 5

Deleting an Indicator from an Expert Advisor...

To add comments, please log in or register
Take active part in the life of the community. Improve your rating!
cyberglassed
611
cyberglassed 2014.05.03 06:48 

Hello, I would like to delete an indicator from an Expert Advisor.

I can do it from an script but not from an EA.

Below you have two codes, an script and an EA. Basically they have the same code, but for some reason the script does it but the EA doesn't.

Retrieving the indicator name also fails on the EA.


I can delete an indicator from the following simple script:

int handle;

void OnStart() {
        handle = iBands(_Symbol , PERIOD_CURRENT, 20, 0, 2.0, PRICE_CLOSE);
        ChartIndicatorAdd(0, 0, handle);
        string  indicator_name = ChartIndicatorName(0, 0, 0);
        printf("Created the indicator: %s", indicator_name);
        printf("Before deleting the indicator: %s", indicator_name);
        IndicatorRelease(handle);
        ChartIndicatorDelete(0, 0, indicator_name);
}


But I can't delete an indicator from the following simple EA. Do you know why?

int handle;

int OnInit() {
        handle = iBands(_Symbol , PERIOD_CURRENT, 20, 0, 2.0, PRICE_CLOSE);
        ChartIndicatorAdd(0, 0, handle);
        string  indicator_name = ChartIndicatorName(0, 0, 0);
        printf("Created the indicator: %s", indicator_name);
        return (INIT_SUCCEEDED);
}

void OnTick() {
        static int ticks_count = 0;
        static int bars_count = 0;

        printf("ticks count: %d", ticks_count);
        if (isNewBar()) {
                printf("bars count: %d", bars_count);
                if (bars_count == 5) {
                        string  indicator_name = ChartIndicatorName(0, 0, 0);
                        printf("Before deleting the indicator: %s", indicator_name);
                        IndicatorRelease(handle);
                        ChartIndicatorDelete(0, 0, indicator_name);
                        string indicator_name_other = ChartIndicatorName(0, 0, 0);
                        printf("The indicator: %s persists", indicator_name_other);
                }
                bars_count++;
        }
        ticks_count++;
}

bool isNewBar() {
   static int last_bar = -1;
   MqlTick current_tick; 
   SymbolInfoTick(Symbol(), current_tick);
   int current_bar = (int)floor(current_tick.time/PeriodSeconds(PERIOD_CURRENT));
   if (current_bar > last_bar) {
      last_bar = current_bar;
      return true;
   }
   return false;
}


Thanks, Cyberglassed.

Alain Verleyen
Moderator
30707
Alain Verleyen 2014.05.03 11:15  
cyberglassed:

Hello, I would like to delete an indicator from an Expert Advisor.

I can do it from an script but not from an EA.

Below you have two codes, an script and an EA. Basically they have the same code, but for some reason the script does it but the EA doesn't.

Retrieving the indicator name also fails on the EA.


I can delete an indicator from the following simple script:


But I can't delete an indicator from the following simple EA. Do you know why?


Thanks, Cyberglassed.

What are the output (log) you get when running the EA ?
cyberglassed
611
cyberglassed 2014.05.03 11:16  

I did another experiment with the following very reduced Expert Advisor:

int OnInit() {

        int handle = iBands(_Symbol , PERIOD_CURRENT, 20, 0, 2.0, PRICE_CLOSE);
        ChartIndicatorAdd(0, 0, handle);
        string  indicator_name = ChartIndicatorName(0, 0, 0);
        printf("Created the indicator: %s", indicator_name);
        
        Sleep(3000);
        
        ChartIndicatorDelete(0, 0, indicator_name);
        IndicatorRelease(handle);
        printf("Deleted the indicator: %s", indicator_name);
        
        return (INIT_SUCCEEDED);
}

If you debug (development mode) or drag and drop the EA to the chart (production mode) then it does it correctly with an slightly bad behaviour (the chart needs to be refreshed somehow, ¿why?)

But if you run the EA with the Strategy Tester, then the problem occurs, and even the name of the chart is not displayed.

Summarizing even more the problem:

If you run the above EA normally, then the name of the indicator is displayed and later the indicator is deleted.

If you run the above EA with the Strategy Tester, then the name of the indicator is not displayed (indirectly I need this to work) nor the indicator is deleted.

Some ideas to get the indicator on the Strategy Tester be deleted?


Regards, Cyberglassed.

cyberglassed
611
cyberglassed 2014.05.03 11:25  
angevoyageur:
What are the output (log) you get when running the EA ?

With Strategy Tester the name is not displayed. But without it (normal execution of the EA) the name is displayed correctly.

cyberglassed
611
cyberglassed 2014.05.03 18:33  

Reducing even more the problem (I think if the following is solved, the rest is also solved),

if you create an EA with only the following code, then if you debug (development mode) or drag and drop the EA to the chart (production mode) then the name of the indicator is displayed correctly, but if you run the EA with the Strategy Tester then the name is not displayed. In other words the behaviour changes.

int OnInit() {
        int handle = iBands(_Symbol , PERIOD_CURRENT, 20, 0, 2.0, PRICE_CLOSE);
        ChartIndicatorAdd(0, 0, handle);
        printf("Indicator name: %s", ChartIndicatorName(0, 0, 0));
        return (INIT_SUCCEEDED);
}


Regards, Cyberglassed.

Alain Verleyen
Moderator
30707
Alain Verleyen 2014.05.03 19:45  
cyberglassed:

Reducing even more the problem (I think if the following is solved, the rest is also solved),

if you create an EA with only the following code, then if you debug (development mode) or drag and drop the EA to the chart (production mode) then the name of the indicator is displayed correctly, but if you run the EA with the Strategy Tester then the name is not displayed. In other words the behaviour changes.


Regards, Cyberglassed.

You will save you a lot of time by always checking the return value of functions :

   int handle=iBands(_Symbol,PERIOD_CURRENT,20,0,2.0,PRICE_CLOSE);
   if(!ChartIndicatorAdd(0,0,handle))
     {
      printf("Error (%i) trying to add an indicator to the chart",GetLastError());
     }
   else
     {
      printf("Indicator name: %s",ChartIndicatorName(0,0,0));
     }

When running with the Strategy Tester, gives :

2014.05.03 19:42:11    Core 1    2013.07.24 00:00:00   Error (4014) trying to add an indicator to the chart

This function CharIndicatorAdd() is not allowed with Strategy Tester.

cyberglassed
611
cyberglassed 2014.05.03 20:46  
angevoyageur:

You will save you a lot of time by always checking the return value of functions :

When running with the Strategy Tester, gives :

2014.05.03 19:42:11    Core 1    2013.07.24 00:00:00   Error (4014) trying to add an indicator to the chart

This function CharIndicatorAdd() is not allowed with Strategy Tester.

Hello angevoyageur, I think I don't understand exactly what you mean, because as you can see on the attached image on my previous post I could add an indicator on the Strategy Tester, so I thought the function: "ChartIndicatorAdd" worked on the Tester.


Thanks, Cyberglassed.

Alain Verleyen
Moderator
30707
Alain Verleyen 2014.05.03 21:33  
cyberglassed:

Hello angevoyageur, I think I don't understand exactly what you mean, because as you can see on the attached image on my previous post I could add an indicator on the Strategy Tester, so I thought the function: "ChartIndicatorAdd" worked on the Tester.


Thanks, Cyberglassed.

Indicator are automatically drawn when running with the Strategy Tester in visual mode.
cyberglassed
611
cyberglassed 2014.05.03 21:48  
angevoyageur:
Indicator are automatically drawn when running with the Strategy Tester in visual mode.

What do you mean with "automatically"?, because the indicator is drawn if I use the function: "ChartIndicatorAdd", if not, the indicator is not drawn. I could draw multiple indicators inside an EA by using that function. Before running the EA I don't associate any indicator to be drawn automatically when the EA starts.

cyberglassed
611
cyberglassed 2014.05.03 22:06  

Ok I found an article that explain some particularities about the Strategy Tester, including indicators.

I leave the article url here, maybe it helps somebody with the same issue as me:

https://www.mql5.com/en/docs/runtime/testing#indicators


Regards, Cyberglassed.

Documentation on MQL5: MQL5 programs / Testing Trading Strategies
Documentation on MQL5: MQL5 programs / Testing Trading Strategies
  • www.mql5.com
MQL5 programs / Testing Trading Strategies - Documentation on MQL5
To add comments, please log in or register