Global Variable Problem

 

I am using multiple charts all with the same EA attached to each chart. I want to take my profit by closing all trades on all charts. To do this as soon as any of the charts finds that the account equity is at a set level i just close all the trades. However, while closing all the trades other charts continue opening further trades which i do not want to happen. So using the trade context code as per https://www.mql5.com/en/articles/1412 I replaced all the TradeisBusy statements with StopCharts and the TradeIsNotBusy statments with StartCharts. The idea being that when any chart got the set level to close all trades it would also activate the StopCharts (TradeIsBusy) to stop all charts executing the attached EA until such time as all the trades were closed and then use StartCharts (TradeIsNotBusy) to allow all EAs attached to all charts to start trading again.

To do this the check for all charts needs the if (StopCharts < 0) statement (TradeIs Busy) right at the start of the EA to stop it executing during the closing of all trades.

Sounds good to me but does not work!!! The if (StopCharts < 0) statement does not allow any chart to execute as the global variable gets set to 1.

I have also tried this with the TradeIsBusy as per https://www.mql5.com/en/articles/1412 by putting the if statement at the start of any EA and the TradeIs Busy gets set to 1 as well.

Anyone any idea why this happens or how i can achieve what I want to do

 
BigAl:

I am using multiple charts all with the same EA attached to each chart. I want to take my profit by closing all trades on all charts. To do this as soon as any of the charts finds that the account equity is at a set level i just close all the trades. However, while closing all the trades other charts continue opening further trades which i do not want to happen. So using the trade context code as per https://www.mql5.com/en/articles/1412 I replaced all the TradeisBusy statements with StopCharts and the TradeIsNotBusy statments with StartCharts. The idea being that when any chart got the set level to close all trades it would also activate the StopCharts (TradeIsBusy) to stop all charts executing the attached EA until such time as all the trades were closed and then use StartCharts (TradeIsNotBusy) to allow all EAs attached to all charts to start trading again.

To do this the check for all charts needs the if (StopCharts < 0) statement (TradeIs Busy) right at the start of the EA to stop it executing during the closing of all trades.

Sounds good to me but does not work!!! The if (StopCharts < 0) statement does not allow any chart to execute as the global variable gets set to 1.

I have also tried this with the TradeIsBusy as per https://www.mql5.com/en/articles/1412 by putting the if statement at the start of any EA and the TradeIs Busy gets set to 1 as well.

Anyone any idea why this happens or how i can achieve what I want to do


sorry forgot the code in question - The EA never get as far as the level of equity to execute the closing of trades as for some unknown reason the first if chartstop displays the comment and nothing happens on any chart

int start()
 {
  if (ChartStop() < 0)
   {
    Comment("Waiting for TakeProfit to Complete");
    return(0);
   }
 
//some code here for trading 
   
  if (AccountEquity() > OldEquity * Percentage)
   {
    if (ChartsStop() < 0)
     {
      Comment("StopCharts is already busy");
      return(0);
     }
    TakeProfit();  //close all trades
    OldEquity = AccountEquity();
    StartCharts();
    return(0);
   }

//more code for trading here

  return(0);
 
BigAl:

sorry forgot the code in question - The EA never get as far as the level of equity to execute the closing of trades as for some unknown reason the first if chartstop displays the comment and nothing happens on any chart

  if (ChartStop() < 0)
   {
    Comment("Waiting for TakeProfit to Complete");
    return(0);
   }
  1. What part of return is unclear? Nothing is going to happen until that changes. Does the function return positive when there are no open orders?
  2.     if (ChartsStop() < 0)
         {
          Comment("StopCharts is already busy");
          return(0);
         }
    This will never be executed because you already returned.
 

If one of th EA instances in one chart decides it's time to close all trades, you set a global variable with function:

GlobalVariableSet("close_all_trades",1);



The other instances of the EA catch this change this way:

if(GlobalVariableGet("close_all_trades")==1) { call_routine_to_close_all_trades(); }


Attention:

- this variable is permanent and can only be changed by GlobalVariableDel("close_all_trades") or GlobalVariableSet("close_all_trades",0).

- this variable is persistent. That is, if therminal is finished and restarted, its value is still there.

- In the terminal, press F3 to see the global variables list defined in your terminal-


Regards,

AM.

 
abstract_mind:



If one of th EA instances in one chart decides it's time to close all trades, you set a global variable with function:

GlobalVariableSet("close_all_trades",1);



The other instances of the EA catch this change this way:

if(GlobalVariableGet("close_all_trades")==1) { call_routine_to_close_all_trades(); }


Attention:

- this variable is permanent and can only be changed by GlobalVariableDel("close_all_trades") or GlobalVariableSet("close_all_trades",0).

- this variable is persistent. That is, if therminal is finished and restarted, its value is still there.

- In the terminal, press F3 to see the global variables list defined in your terminal-


Regards,

AM.




 
ok guys thanks for your input abstract mind solution works fine
Reason: