Need help on allow EA only run on Backtest and Demo account

 

Hello community,

I am writing a new EA and want to allow it running on Demo and Tester only. I have tried the script:


void OnTick()
{
// Check if running in backtest mode
    bool isBacktest = MQLInfoInteger(MQL_TESTER) == 1;

    // Check if the account is a demo account
    bool isDemoAccount = AccountInfoInteger(ACCOUNT_TRADE_MODE) == ACCOUNT_TRADE_MODE_DEMO;

    // If not in backtest mode and not a demo account, stop execution
    if (!isBacktest && !isDemoAccount)
    {
        Print("Demo version: This EA only runs on demo accounts or backtesting.");
        return;
    }


But it still running trades on live account. Does someone know how to overcome this issue please help me. Thank you.

Tim.

 
Tim:
void OnTick()
{
// Check if running in backtest mode
    bool isBacktest = MQLInfoInteger(MQL_TESTER) == 1;

    // Check if the account is a demo account
    bool isDemoAccount = AccountInfoInteger(ACCOUNT_TRADE_MODE) == ACCOUNT_TRADE_MODE_DEMO;

    // If not in backtest mode and not a demo account, stop execution
    if (!isBacktest && !isDemoAccount)
    {
        Print("Demo version: This EA only runs on demo accounts or backtesting.");
        return;
    }

you need an or check , if its not backtest or if its not demo account 

 
Lorentzos Roussos #:

you need an or check , if its not backtest or if its not demo account 

Hello I have already write if (!isBacktest),  can you spoil what wrong with the script or what else have to be added. Thanks so much
 
Tim #:
Hello I have already write if (!isBacktest),  can you spoil what wrong with the script or what else have to be added. Thanks so much

yeah , call ExpertRemove too on the non init functions

#define ISDEMO (bool)(AccountInfoInteger(ACCOUNT_TRADE_MODE)==ACCOUNT_TRADE_MODE_DEMO)
#define ISBACKTEST (bool)(MQLInfoInteger(MQL_TESTER))
int OnInit()
  {
  if(!ISDEMO||!ISBACKTEST){return(INIT_FAILED);}
  return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  }
void OnTick()
  {
  if(!ISDEMO||!ISBACKTEST){ExpertRemove();return;}
  }
void OnTimer()
  {
  if(!ISDEMO||!ISBACKTEST){ExpertRemove();return;}
  }
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  if(!ISDEMO||!ISBACKTEST){ExpertRemove();return;}
  }
 
Lorentzos Roussos #:

yeah , call ExpertRemove too on the non init functions

Hello friend, now it's worked, thank you very muchh