Surely you have ever wanted to share one of your MT4 programs with a friend or with a potential partner or client. However, you are afraid that your program may be shared with third parties or may even be marketed without your consent. Learn to solve this in a very simple way even without being an MT4 programmer.
Table of contents
- 1 Code snippet to bond a program to a single MT4 Forex account
- 1.1 1. Open your MT4 program (Expert Advisor, Indicator or Script) in MetaEditor (press F4 in MetaTrader platform).
- 1.2 2. Copy the following code.
- 1.3 3. Modify the second line of the code replacing ‘0’ (zero) with the number of the account authorized to use your MT4 program.
- 1.4 4. Paste the above code between the brackets of ‘OnInit’ or ‘init’ (in old files) function of your MT4 program. It would look as follows.
- 1.5 5. Make sure you have this line of code (#property strict) at the start of the program as shown in the image below. If not, add it. Otherwise the code will not work.
- 1.6 6. Compile the MT4 program.
- 1.7 7. Share only the executable file (‘ex4’).
- 2 Download a sample of an Expert Advisor bond to a single MT4 Forex account
- 3 Code snippet to set a trial period
- 4 Download a sample of an Indicator with a trial period
- 5 Adding an extra layer of protection with MQL5 Cloud Protector
Code snippet to bond a program to a single MT4 Forex account
Here we show you a very simple way to limit the use of your MT4 program to a single MetaTrader account. This way you can send your Expert Advisor, Indicator or Script to whoever you want with the guarantee that it will not work on any other account except the one you previously authorized.
Simply follow the following steps:
1. Open your MT4 program (Expert Advisor, Indicator or Script) in MetaEditor (press F4 in MetaTrader platform).

2. Copy the following code.
//--- code begin
int AcctNbr = 0;
if(AcctNbr > 0 && AcctNbr != AccountNumber())
{Print("Account not allowed"); return(-1);}
//--- code end
//--- code begin int AcctNbr = 52068425; // <-- write here the authorized account number if(AcctNbr > 0 && AcctNbr != AccountNumber()) {Print("Account not allowed"); return(-1);} //--- code end
4. Paste the above code between the brackets of ‘OnInit’ or ‘init’ (in old files) function of your MT4 program. It would look as follows.

5. Make sure you have this line of code (#property strict) at the start of the program as shown in the image below. If not, add it. Otherwise the code will not work.

6. Compile the MT4 program.


Download a sample of an Expert Advisor bond to a single MT4 Forex account
The EA is linked by default to the fictitious MetaTrader 4 forex account 52068425. Change this number to the account you want to allow use of the program.
Code snippet to set a trial period
With this simple piece of code, you will be able to limit the use of your MT4 trading tool from a certain date. This is, the MT4 EA or Indicator will work until the date you set in the code. From this date on, the MetaTrader 4 program will stop working.
Follow the points above just replacing points 2 and 3 with the following ones:
2. Copy the following code.
//--- code begin
datetime TrialEndDate = 0;
if(TimeCurrent() > TrialEndDate)
{Print("Trial period has expired!"); return(-1);}
//--- code end
3. Modify the second line of the code replacing ‘0’ (zero) with the date you want the MT4 program to stop working.
//--- code begin datetime TrialEndDate = D'23.07.2020'; // <-- write here the date when you the MT4 program stops if(TimeCurrent() > TrialEndDate) {Print("Trial period has expired!"); return(-1);} //--- code end
Download a sample of an Indicator with a trial period
The indicator trial is limited by default to work until December 31, 2020 (format D’31.12.2020′). Change it to the date to which you want to limit the use of the program.
Adding an extra layer of protection with MQL5 Cloud Protector
The MetaTrader 4 trading platform contains a tool that adds an additional layer of protection to the programs. This important tool is MQL5 Cloud Protector.

MQL5 Cloud Protector is actually a free service that adds a high degree of protection to MT4 programs. In fact, this is the same protection system used with the trading apps marketed in MQL5 market.
It is advisable to always use this protection, especially for those cases where a high degree of protection is desired in the MT4 program that you want to share or sell. From our point of view, MQL5 Cloud Protector significantly reduces the need to use DLL files in program protection.
MQL5 Cloud Protector was launched at the end of 2017 on MT5 build 1700. You can see the details of this version in the post “MetaTrader 5 Build 1700: Synthetic Instruments, Shared Projects and MQL5 Cloud Protector“. Consequently, the tool was also available for MT4 sometime later.
Philip
Thank You for this, you have saved me a lot of time. I have not made an attempt yet, but I’m sure I can get the code provided to work.
Thanks Again,
Philip,
Phil_GMT
barmenteros FX
You’re welcome Philip! We’re glad we could help and that the information provided was useful to you. If you have any further questions or concerns, don’t hesitate to reach out.
Adam Ahmad
Big thanks for that simple code that you post and by the way, just one question if I change “{Print” to “{Alert”, the message will popup in the screen right?
barmenteros FX
Yes Adam. That’s correct. Regards
Robert Chen
Thanks for the great sharing.
Wondering can you share also how to protect or lock a MT4 template to certain MT4 account number or name ?
thanks in advance & hopefully can receive your reply
barmenteros FX
Hi Robert. Not sure of getting what you mean. Can you explain more in detail with an example, please? Regards
Robert Chen
can you share also how to protect a MT4 template also ?
Abbey
Hi,
Thanks for all you do. Is there a way to combine the two restrictions (bonding to one or two accounts and also setting a trial period)?
barmenteros FX
Yes, Abbey. You can use a combination of the 2 restrictions. Best regards
Dave Waters
Very helpful article – thank you.
barmenteros FX
Thanks for your comment, Dave. Really appreciate it!
Mathew Karanja
I have tried that code and did as instructed but an error msg keep popping up showing
‘{‘ – Function defination unexpected
‘}’ – not all control paths return a value
barmenteros FX
Hi. Send us a screenshot showing the code you added. Thanks
Mike Rossi
Hello, how can I make an indicator work on 2 pairs only (EURUSD, GBPUSD).
Indicator only not EA
Thank you
barmenteros FX
Hi. You must add a restriction in the code to limit the use of the indicator to only those pairs. You can send us the indicator and we’ll do it. Regards
JACK BE
I WANT TO USE THIS CODE IN MT5 BUT I DOES’T WORK CAN SOMEONE PROVIDE ME CODE FOR MQL5
barmenteros FX
Hi Jack, can you send us a screenshot showing the code you used? Thanks
Tomi
Adding restriction or locking an indicator to a particular account number as you explained above clearly, is it the same with indicator on mt5 platform sir? I will like you to (if not too much to ask sir) help me on how to go about it with indicator on mt5 platform sir.
Thanks
barmenteros FX
Hi Tomi,
Yes, basically it’s the same. Just dismiss ‘#property strict’ that’s not necessary in MQL5.
Best regards
Micheal
Hello sir worked perfect , what if I want to use password or key ?
barmenteros FX
Thanks for the feedback, Micheal. We’ll try to update the post to include password or key protection. Best regards
OPOH
The Codes is working perfectly and is so simple to implement
barmenteros FX
Thank you for the feedback. Glad to hear it has been helpful to you.
John
Hello,
Please I have an indicator in MT5 .mq5 , can you please assist me adding a code to lock the indicator to an account number?
Thank you
barmenteros FX
Hi John,
Please, explain what step from the post you are not able to understand / implement.
Thanks
Moe
I have tried Copied and pasted the code for my EA ,compile didn’t work
barmenteros FX
Hi Moe. Please, send us a screenshot showing the code you added and the compilation error you get. You can send it to [email protected]
Regards
Wongani Bulaya
hey guys, my software is working fine, but when i change the propety to strict thats when a number of erros are popping up
like undeclared identifier and ‘if’ expressions not being allowedon a global space.
what do i do?
barmenteros FX
Hi Wongani, the strict property is used to give compatibility with programs developed before the recent MT4 versions (build 600+). It also implies that the old codes are checked according to the new compilers standards. That is why using the strict property may cause new errors and warnings to appear when compiling. In some cases the warnings can be ignored. Instead, the errors must be corrected so that the code can be compiled successfully. Regards
khoa le
hi,
I pasted #property strict line in code but when compile got error :
“possible loss of data die type conversion”
Could you help me to solve it?
thank you
Barmenteros FX Staff
Hi,
That’s not an error but a warning so you can compile the file properly. Anyway, send us a screenshot showing the line triggering the warning so we can take a look.
Best regards
Chicco
Thank you! It works perfectly 🙂 I really appreciate your wworks.
What if I want to add 2 or more accounts. Should I use a comma or semi colon to separate multiple accounts?
Barmenteros FX Staff
Hi Chicco,
Find below a simple example:
//--- code begin
int AcctNbr1 = 52068425; // <-- write here the authorized account number if(AcctNbr1 > 0 && AcctNbr1 != AccountNumber())
{Print("Account not allowed"); return(-1);}
int AcctNbr2 = 26341416; // <-- write here the authorized account number if(AcctNbr2 > 0 && AcctNbr2 != AccountNumber())
{Print("Account not allowed"); return(-1);}
//--- code end
Best regards
Mohammed
hey dear thanks for your great efforts
i just follow your instruction for single account it is working well but when i add second account EA not working even i follow same code you post
{
//— code begin
int AcctNbr1 = 274282; // 0 && AcctNbr1 != AccountNumber())
{Print(“Account not allowed”); return(-1);}
int AcctNbr2 = 277166; // 0 && AcctNbr2 != AccountNumber())
{Print(“Account not allowed”); return(-1);}
//— code end
}
please need your advise
thanks
barmenteros FX
Hi. Your code does not have much to do with what we publish. In your case, use the following:
//— code begin
int AcctNbr1 = 274282;
int AcctNbr2 = 277166;
if(AcctNbr1 != AccountNumber() && AcctNbr2 != AccountNumber())
{Print(“Account not allowed”); return(-1);}
//— code end
Matimba
Hi, I did put the code as instructed but it gives me an error code ” onlnit- function already defined and has a body
This is the code i was trying to put on my indicator and ea. please help me out on how to fix this issue
int OnInit()
{
int AcctNbr = 21698831; // 0 &&
AcctNbr != AccountNumber())
{
Alert(“Account not allowed”);
return(-1);
}
return(INIT_SUCCEEDED);
}
Barmenteros FX Staff
Hi Matimba,
You get that error because you already have a function in the code with the same name (‘OnInit’), so you should delete this new ‘OnInit’ function you created, search the ‘OnInit’ function already existing in your code, and copy the snippet inside it. In the other hand, your code won’t run since it contains error. Please, recheck your code and make sure it’s the same as the code we published in this post.
Thanks
jaison
hi i got an error like this…
can u show me how to put that codes correctly..
Barmenteros FX Staff
Hi Jaison,
Please, tell us what’s not clear in our previous reply so we can provide a better explanation.
Regards
Troc
Hello Bamenteros,
Many thanks for your great help!
Could you show us how to modify the code slightly so instead of checking for account number it validates against the name of the account?
Many many thanks Barmenteros!
You guys are great!
Barmenteros FX Staff
Hi Troc,
Please, send us a screenshot highlighting what do you mean by account name. If it’s what we think, you should notice that the name of the account is the same for all the accounts belonging to the same broker server, so it won’t be a valid verification method.
Thanks
rosa
Alert(“acct numbr> ” + AccountNumber());
//— code begin
int AcctNbr = 452295; // 0 && AcctNbr != AccountNumber())
{
Alert(“No puedes usar este indicador. Contacta con [email protected]” + AcctNbr);
return(-1);
//— code end
}
Barmenteros FX Staff
Hi Rosa,
Not sure what you are trying to do but your code is different from the code we posted here.
Best regards
rosa
Hi,
AccountNumber() returns “0” so it doesn´t work if you already have the indicator on your chart, close mt4 and reopen it.
int OnInit() {
//— code begin
Alert(“accountnumber: ” + AccountNumber());
int AcctNbr = XXXXXX; // 0 && AcctNbr != AccountNumber())
{
Alert(“not allowed: ” + AcctNbr );
return(-1);
//— code end
}
}
Barmenteros FX Staff
Hi Rosa,
Not sure what you are trying to do but your code is different from the code we posted here.
Best regards
Francis
i tried to do this to restrict my indicator to a particular account but still its not working
Barmenteros FX Staff
Hi. Please, send us a screenshot of the code you added and where you placed it. Thanks
Lito Lapis
Hi,
Is it possible to create a code protector for MT4 that you can share to your client that will expire in an X amount of days or months?
Thank you.
Lito
Barmenteros FX Staff
Hi Lito. Yes, it’s possible. Greetings
gary
I read somewhere on your blog abt using vmprotect for extra protection, but cant seemed to find the post. Can u share a little more on your experience of using that? Thank you.
Barmenteros FX Staff
Hi Gary. Unfortunately, we don’t have any post related to VmProtect. Nevertheless, you can find a lot of related info on the web. Best regards