- MQL5 Wizard: Development of trading robots for MetaTrader 5
- Download MetaTrader 5 build 1730 with new trader features
- MetaTrader Market - the largest store of trading applications
You're basically already giving the answer yourself: just enter the code as an input variable, then add an if statement in OnInit like "if (licensecode!="123456abcDEF"){Print("invalid license");return INIT_FAILED;}
other possibility: limit the EA usage to a list of accepted account numbers that you store in an array like license.array[] and maybe add an expiration date;
just to give an example to work with (the values for the members in the license struct need to be assigned with appropriate account numbers and a year/month of course):
//+------------------------------------------------------------------+ //| global scope | //+------------------------------------------------------------------+ struct LicenseData { ushort expiration_year; ushort expiration_month; long accounts[]; }; LicenseData license; //+------------------------------------------------------------------+ //| license check | //+------------------------------------------------------------------+ void license_check(void) { long account_login=AccountInfoInteger(ACCOUNT_LOGIN); MqlDateTime time; TimeToStruct(TimeCurrent(),time); for (int i=0;i<ArraySize(license.accounts);i++) { if (account_login==license.accounts[i]) { Print("license for this account number: verified"); if (time.year<license.expiration_year || (time.year==license.expiration_year && time.mon<=license.expiration_month)) {Print("license valid until ",license.expiration_month,"/",license.expiration_year);return;} else {Print("license EXPIRED ",license.expiration_month,"/",license.expiration_year);ExpertRemove();return;} break; } } Print("license INVALID"); ExpertRemove(); }
1)Members should input password
2)Test it in OnInit() section.
Let's say your password is "zErt123"
input string checkPassword=NULL; const string myPassword="zErt123" //+------------------------------------------------------------------+ int OnInit(){ if (myPassword!=checkPassword) { Alert("Password not matching. EA is removed") ExpertRemove(); return(INIT_FAILED); } return(INIT_SUCCEEDED); }
paul selvan:
1)Members should input password
2)Test it in OnInit() section.
Let's say your password is "zErt123"
Hey Paul, thanks for your reply. I am new to coding and haven't done much so just wondering, is this code something I can just add into my settings as an "input" and ready to use?
You're basically already giving the answer yourself: just enter the code as an input variable, then add an if statement in OnInit like "if (licensecode!="123456abcDEF"){Print("invalid license");return INIT_FAILED;}
other possibility: limit the EA usage to a list of accepted account numbers that you store in an array like license.array[] and maybe add an expiration date;
just to give an example to work with (the values for the members in the license struct need to be assigned with appropriate account numbers and a year/month of course):
Thanks for your reply Chris... I'm fairly new to coding so is this a script I can just paste into my mq4 EA code and ready to use or no? I will study what you gave, I just don't have a lot of time lol. Good stuff though!
1)Members should input password
2)Test it in OnInit() section.
Let's say your password is "zErt123"
Thanks for your reply Chris... I'm fairly new to coding so is this a script I can just paste into my mq4 EA code and ready to use or no? I will study what you gave, I just don't have a lot of time lol. Good stuff though!
1. If you're new to coding there are probably many things to learn. Of couse, you are you and can do what you want. So just as a thought: I would mostly concentrate on the stuff that all expert advisors share in common, i.e. you need modules for order operations, risk & money management (position sizing, drawdown protection..), stop loss and take profit management... After that you can use this common trunk and put it into specific strategies via definition of e.g. indicators and your buy/sell/flat criteria. Until then, the thing that you should be the least worried about is somebody stealing your code. Protection is for code that is truly unique and consumed a lot of your time. You probably wouldn't want to protect the recipe of how you made your coffee this morning like you would protect the Coca Cola recipe ;-)
2. Yes, this example was made for MT5, but should work with MT4, too. Please learn about how to use functions and what the different events like e.g. OnInit, OnTick,.. mean and which parts of the code belong where. This will answer your question.
3. Please post MT4-specific questions in the MT4 section. It's sometimes really annoying to read 10 posts only to realize then that we're talking about completely different program versions.
[4. As a general thought: if you're at the beginning, why not MT5? Sure, MT4 might not be dead yet, but MT5 has many big advantages and MT5 won't be much harder to learn if you don't come from MT4 but start from scratch; if the broker doesn't offer MT5 accounts: it's easier to change the broker than a once learned programming language]
1. If you're new to coding there are probably many things to learn. Of couse, you are you and can do what you want. So just as a thought: I would mostly concentrate on the stuff that all expert advisors share in common, i.e. you need modules for order operations, risk & money management (position sizing, drawdown protection..), stop loss and take profit management... After that you can use this common trunk and put it into specific strategies via definition of e.g. indicators and your buy/sell/flat criteria. Until then, the thing that you should be the least worried about is somebody stealing your code. Protection is for code that is truly unique and consumed a lot of your time. You probably wouldn't want to protect the recipe of how you made your coffee this morning like you would protect the Coca Cola recipe ;-)
2. Yes, this example was made for MT5, but should work with MT4, too. Please learn about how to use functions and what the different events like e.g. OnInit, OnTick,.. mean and which parts of the code belong where. This will answer your question.
3. Please post MT4-specific questions in the MT4 section. It's sometimes really annoying to read 10 posts only to realize then that we're talking about completely different program versions.
[4. As a general thought: if you're at the beginning, why not MT5? Sure, MT4 might not be dead yet, but MT5 has many big advantages and MT5 won't be much harder to learn if you don't come from MT4 but start from scratch; if the broker doesn't offer MT5 accounts: it's easier to change the broker than a once learned programming language]
Thanks I appreciate the help. Although I mentioned I was new to coding, I do have a bit of a grasp on some things. I took 6 weeks of Javascript classes and even though that's not the same type of coding done on Mt4 or Mt5, I still understand the underlying concepts of what functions are and what to look for somewhat. I already have my entire Expert Advisor created and done with others testing it currently, so creating it isn't something I'm worried about. I had purchased a program that allows me to easily create the EA and that's how I did it so I'm just trying to add to the code itself someway to make this password protection happen.
My goal:
- I am part of a group that has a trading training platform, and it's a membership site where we go over a specific strategy with members and are very focused on that strategy. I am creating a new version and more levels into the membership. My goal was to create this EA based on our strategy and it is working from the feedback I have heard from my testers. I want to plug this EA into one of the membership levels so that members have more of an incentive to stick it out and continue with the program. That is why I want it password protected, to eliminate other users who are not members from using it best as I can.
Last night I studied what some of these different functions are within the MQL4 coding and how they operate. However, When I go to edit my code to place in the additional code you or anyone has given me, I get some various errors. I feel I am close to getting it right, just not sure of a couple of things that I can't find answers for.
Example... When I compile, I get the error message: 'init' - function already defined and has body
- Not sure if I placed it wrong or if I need to swap something out or what not.
The software I purchased only allow me to create MT4 EA's, so naturally I cannot use MQL5 coding at this time but am willing to find out how to change the code to MQL5 also.
(I do apologize for not posting this in the MT4 section, I was completely unaware of being in the MT5 section. In the future, I will post in the appropriate sections. I understand.)
All that being said, my questions are as follows:
- Did I place the code in the wrong spot? Do I need to eliminate something that already exists?
- How do I find these answers, and what are some learning resources to follow without spending a bunch of money?
- Am I able to input a password code that enables only current members of our trading program to use the EA (now that I layed out the foundation of my goal)?
@tyfred :
It doesn't work like that. In order to really clean up garbage code that you can automatically generate with a few mouse-clicks you need to understand how to code. If on the other hand you learned how to code, you wouldn't use such automatic code generators in the first place.
If the whole purpose essentially just is the automation of a proven strategy without really being committed to coding yourself, this is a classic job for a freelancer. A deadline shouldn't be a problem if this is communicated with the freelancer.
Btw.: how can the EA be working according to feedback if it doesn't even compile?
Well i told you already this error is self explanatory .
It literally means you have two OnInit() functions in your code you can only have one.
Also i do not know what you mean by software you purchased, Because Meta Editor is free.I completely understand that function already exists and I have two, but can't find the other and don't know how to 'combine' them, such as putting in the new code inside of the other function that is already there or is that even possible?
By software, I mean.. I got some software (not metatrader) that allows me to create an EA easily and then gives me the source code for it. So, I'm taking that source code and adding a password protection to it. Make sense?
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use