FOREX TRADING FRAMEWORK – SIMPLE TAKE PROFIT OPTIMIZATION

This video here is going to show you how to test and optimize your target take profit value.
Our Expert Advisor is running in the background here and I have coded for testing the different take profit values. Right now it is testing the 100 point take profit value and it’s only making buy positions.
Once the equity becomes equal or above the balance the EA opens multiple positions. Please don’t try this on your real account.
So, let’s see now how to code the Expert Advisor that shall test the take profit value in MQL5.
Let’s begin by launching the Metaeditor from Metatrader. (Press F4 or use the little button here.)
If you see the Metaeditor window now please click on: “File/ New/ Expert Advisor (template)” from template, click “Next”, let’s give a name to our file: “SimpleTakeProfitTest”, click on “Next”, “Next” again then “Finish”.

We remove everything above the “OnTick” function even the two comment lines.

Our first thing to do is creating a “CTrade”. “CTrade” is part of the “Trade” library so we shall import it from there. Let’s create the new instance and we call it: “trade”.
Next we want to enable the user to make changes in the take profit value, so we use a variable for the user input; it’s an integer and let’s name it as: “MyTakeProfitValue” and we set 100 for the default value.
Alright, now a few more calculations we need to do.
Let’s get the Ask price first of all and we want to make sure we have the accurate number; so we need to define the number of digits accordingly. We use “SymbolInfoDouble” for the current symbol on our chart, then we get “SYMBOL_ASK”, after these “NormalizeDouble” and ”_Digits” helps us to obtain the correct number of digits behind the dot in the value for any type of currency pair we might select on the chart. This is because some pairs have 3 digits behind the dot and others have 5.
Next we need to obtain the latest account balance with the use of: “AccountInfoDouble (ACCOUNT_BALANCE)” and we also need to get the latest number for the equity by “AccountInfoDouble (ACCOUNT_EQUITY)”.
When we are done we continue with the creation of the simple buy signal. The buy condition is very simple; once the equity is equal or above the balance we are going to buy 10 micro lots of the selected Forex pair.
Let’s continue. Here you can see the profit value variable we have previously coded for the user input, you will see in a short while what shall we do with it but first we code the chart output to display the Balance, the Equity and the profit value (MyTakeProfitValue) on the chart.

So far so good, let’s compile our code. You can use the “Compile” button or the short key F7. If we have no errors or warnings we can get back to Metatrader. (Use the little button here or just hit F4 on your keyboard.)
When we are in Metatrader we want to test our Expert Advisor. Let’s start the Strategy Tester by clicking on “View/ Strategy Tester” or using “CTRL + R”, we select the file: “SimpleTakeProfitTest.ex5”, let’s pick any currency pair and any time frame, now we turn off the visual mode so the test will be running faster and start the test. Only a few seconds and we must see the “Results” tab signaling the test is finished. We got a profit of: $2.863. Here is the graph and here we click on “Inputs”, and as you can see we can change the take profit value (MyTakeProfitValue) and it is 100 at the moment. Let’s make it to 200 and run a new test. Now we got a profit of: $37.732,70.
It looks okay but here comes the exiting part; let’s optimize! Here we select “Optimization/ Fast Generic Based Algorithm”, click on the “Inputs” tab, we set the original value to 100, the start value also to 100, and the step value to 20 while we set the stop value to… let say: 300.
So what is going to happen here now is the algorithm runs the test with different values, starting from 100, the next is 120, then 140 and so on until reaching 300 then comparing them with each other. Here we define the value we are optimizing for then run a new test. (Not using visual mode.)
Here you see several agents at work now, the first ones are already done and we shall have all the results in a moment. Remember please we are only running a simple and quick test.
Now we can analyze the results, let’s sort by the take profit value (MyTakeProfitValue). We got the best profit when the take profit value was 240 points.
Alright, let’s see what happens if we go with a step value of: 10!
This Expert Advisor can help you find the best take profit value very fast. The test is finished and this time we sort by the profit column here and we can see that with a take profit value of 250 we got the best profit of: $109.502.
That’s all. In this video you have just learned how to code an Expert Advisor in MQL5 to test and find the best take profit level and you did it by yourself with only a few lines of code.

Download “Forex Trading Framework - Simple Profit Optimization” SimpleTakeProfitTest.txt – Downloaded 72 times – 395 B