Download MetaTrader 5
To add comments, please log in or register
How to test a program before buying?
Carl Schreiber
5966
Carl Schreiber 2014.03.24 10:06 

If my EA is stopped I want it to delete all its global variables!

If I start the debugger the glob.vars are set but nothing is deleted if I stop the debugger:

void OnDeinit( const int reason ){ delMyGlobs(); }
void delMyGlobs(){
        Print("DeInit called by: ",(string)UninitializeReason());
        int g = GlobalVariablesTotal();
        string gN;
        while(g>0){g--;
                gN = GlobalVariableName(g);
                if ( StringFind(gN, "setEA:") == 0 ) {
                        GlobalVariableDel(gN);
                        Print("GlobalVars.-Delete: ",gN);
                }
        }
}

As the initial Print is not printing and nothing is deleted I must assume DeInit() is not called and is not working at least in the debugger?

Beside that another question:

Due to the function call of DeInit();

void OnDeinit(const int reason);
// It must be declared as the void type and 
// should have one parameter of the const int type, 

How shall I use DeInit() that the ea deletes its glob.-vars by any reason the ea stops or is removed and the OnInit()- function will be called if re-started (is there a temporary stop of an EA?)?

Second if I stop the debugger I have these two line in the expert tab:

2014.03.24 10:58:59.509 myEA AUDUSD,M1: initialization failed (-1)
2014.03.24 10:58:59.509 debugging terminated

What does this now mean after the EA in debug-mode was stopped?

Carl Schreiber
5966
Carl Schreiber 2014.03.24 16:18  

Well this is the code that fails:

I start a script that opens several charts and installs an EA by applying a template.

The EA (just as an example) sets a global variable at OnInit() and than it removes itself with the intention that OnDeinit() delets the global variable of the EA.

//Script
void OnStart() {
        string ChartPair[5] = { "AUDUSD","USDCAD","EURCHF","EURUSD","GBPUSD" }
        long IDChart[5];
        for(c=0;c<5;c++){
           IDChart[c] = ChartOpen( ChartPair[c], PERIOD_M1 );
           if ( !ChartApplyTemplate( IDChart[c], fTPL ) ) {
                        Comment("ERROR(",_LastError,"): Can't apply Template to [",(string)c,"] ",ChartPair[c],"? Correct and Restart ",WindowExpertName()," - Installation stopped..");
                        return;
           }
        }
}

//EA:
string MyGlobVar;
void OnDeinit( const int reason ){ delMyGlobs(); }
void delMyGlobs(){
        Print("DeInit called by: ",(string)UninitializeReason());
        int g = GlobalVariablesTotal();
        string gN;
        while(g>0){g--;
                gN = GlobalVariableName(g);
                if ( MyGlobVar == gN ) {                        
                        Print("GlobalVars ",gN," Delete: ", GlobalVariableDel(gN) );
                }
        }
}

int OnInit(){
        MyGlobVar = "myEA: "+Symbol()+" "+TimeToString(TimeCurrent(),TIME_MINUTES);
        GlobalVariableSet( MyGlobVar, ChartID());
        ExpertRemove();
        return( INIT_FAILED );
}

But no global variable is deleted - what do I do wrong or is this not jet 'implemented' ??

/
To add comments, please log in or register