Password protection

Back to topics list To post a new topic, please log in or register
avatar
10
Boyan 2015.03.16 18:46
 

Hi,

I have a question about password protection.  I'm using this script for my indicator protection

const string allowed_broker = "MetaQuotes Software Corp.";
const long allowed_accounts[] = { 979890, 436290, 646490, 225690, 279260 };
                             
int password_status = -1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   string broker = AccountInfoString(ACCOUNT_COMPANY);
   long account = AccountInfoInteger(ACCOUNT_LOGIN);
   
   printf("The name of the broker = %s", broker);
   printf("Account number =  %d", account);
   
   if (broker == allowed_broker) 
      for (int i=0; i<ArraySize(allowed_accounts); i++)
       if (account == allowed_accounts[i]) { 
         password_status = 1;
         Print("EA account verified");
         break;
       }
   if (password_status == -1) Print("EA is not allowed to run on this account."); 
    
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  if (password_status == 1) 
  {
    // password correct
  } 
  }

 It works fine, except when restarting MT4 and the indicator is still attached on the chart it sets password_status==1. I see that it does not initialize the" broker" and "account" variables. It sets account = 0 and beoker ="  ". so I have to remove and reattach it so it runs again.

Any clue?

Thanks 


 

avatar
1109
Ovo 2015.03.16 19:11 #
 
Boyan:

Hi,

I have a question about password protection.  I'm using this script for my indicator protection

 It works fine, except when restarting MT4 and the indicator is still attached on the chart it sets password_status==1. I see that it does not initialize the" broker" and "account" variables. It sets account = 0 and beoker ="  ". so I have to remove and reattach it so it runs again.

Any clue?

Thanks 


 

Yes, it works like that, if the account is not yet connected.

In addition, once your indicator passes your protection with success, then you may change account to a non-listed one, and it goes on with the other account.

avatar
10
Boyan 2015.03.16 20:07 #
 
Thanks, maybe an explanation why is that? Why does not run AccountInfo() in the initialization function in this case?
avatar
1109
Ovo 2015.03.16 21:17 #
 
Boyan:
Thanks, maybe an explanation why is that? Why does not run AccountInfo() in the initialization function in this case?
It is because indicators do not re-init with account change. No idea why, but it has been like that for six years I know MT4.
avatar
10
Boyan 2015.03.20 19:47 #
 
Ovo:
It is because indicators do not re-init with account change. No idea why, but it has been like that for six years I know MT4.

I found the flaw! When restarting MT4 with AccountInfo() password protected indicator attached the indicator initializes instantly while MT4 is still establishing the server connection. Because there is no connection established at the time of initialization AccountInfo() returns 0 and and empty string. So you have to remove the indicator and attach it again when the connection is established. I tweaked the code so I removes it when there is no connection and saves you the pain of removing it by yourself.

int OnInit(void)
  {
 
   if(!IsConnected())
      {
         int Indi_Window = ChartWindowFind(ChartID(),"XXXX_XXXX_XXXX");
         ChartIndicatorDelete(ChartID(),Indi_Window,"XXXX_XXXX_XXXX");
      }

   string broker = AccountInfoString(ACCOUNT_COMPANY);
   long account = AccountInfoInteger(ACCOUNT_LOGIN);
   
   printf("The name of the broker = %s", broker);
   printf("Account number =  %d", account);
   
   if (broker == allowed_broker) 
     if (account == allowed_account) 
       { 
         password_status = 1;
         Print("Account verified");   
       }
   if (password_status == -1)
       {
        printf(Invalid "XXX_XXX_XXX license or failed server connection!!");
        printf(XXX_XXX_XXX is not allowed to run.");
        return(INIT_FAILED); 
       } 
}

 

 

avatar
735
honest_knave 2015.03.21 09:01 #
 
Just be aware that if you require a connection in order to use your EA, it will be difficult for people to backtest it offline with downloaded data.
avatar
1
codinho 2015.08.31 15:35 #
 
Boyan:

I found the flaw! When restarting MT4 with AccountInfo() password protected indicator attached the indicator initializes instantly while MT4 is still establishing the server connection. Because there is no connection established at the time of initialization AccountInfo() returns 0 and and empty string. So you have to remove the indicator and attach it again when the connection is established. I tweaked the code so I removes it when there is no connection and saves you the pain of removing it by yourself.

 

 

Bojan, the auto-refresh after new start from MetaTrader will not work by me. I always have to reload the Template. 

Can you Help?

Best regards, codinho 

Back to topics list  

To add comments, please log in or register