//+------------------------------------------------------------------+ //| TimeLimitProtectedEA.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" datetime allowed_until = D'2012.02.11 00:00'; int password_status = -1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- printf("This EA is valid until %s", TimeToString(allowed_until, TIME_DATE|TIME_MINUTES)); datetime now = TimeCurrent(); if (now < allowed_until) Print("EA time limit verified, EA init time : " + TimeToString(now, TIME_DATE|TIME_MINUTES)); //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if (TimeCurrent() < allowed_until) { } else Print("EA expired."); }
If you use this logic (positive confirmation) allowed_until must be in the future.
I tested it with a future date and it worked.
datetime allowed_until = D'2024.02.11 00:00';
With past dates it detects expiry. This bit works ok too
if(TimeCurrent() < allowed_until)
Additionally, I will love to restrict the script to running only on some accounts numbers so it will not run with on every account except that which has been added in the source file. In help in this direction will be highly appreciated. thanks
Easy enough - the simplest way is to create a delimited string of the allowed accounts numbers and use StringFind() to check if the current account is in the list - you could do the opposite if you want to create an exclusion list.
An Array of account numbers is a bit more elegant, but needs a little more coding - if your list of account numbers is not too large, a string is easiest. If it runs just once during OnInit() it is unlikely to affect performance later on.
Easy enough - the simplest way is to create a delimited string of the allowed accounts numbers and use StringFind() to check if the current account is in the list - you could do the opposite if you want to create an exclusion list.
An Array of account numbers is a bit more elegant, but needs a little more coding - if your list of account numbers is not too large, a string is easiest. If it runs just once during OnInit() it is unlikely to affect performance later on.
//+------------------------------------------------------------------+ //| AccountProtectedEA.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" 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 } }
The first thing to do is use braces {} - omitting them has no benefit in my opinion
The MQL editor automatically generates code like this:
if(condition) { for(int i=0;i<total;i++) { if(condition) { } } }
This section of code should be made to follow that pattern - try that and see if it fixes the issue. If not, debug it
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; }
The first thing to do is use braces {} - omitting them has no benefit in my opinion
The MQL editor automatically generates code like this:
This section of code should be made to follow that pattern - try that and see if it fixes the issue. If not, debug it
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
lease I need some help in adding an expiration date to my script. I used the code below but even after the expiration time the script still runs. Additionally, I will love to restrict the script to running only on some accounts numbers so it will not run with on every account except that which has been added in the source file. In help in this direction will be highly appreciated. thanks