In this video we are going to create an expert advisor that is able to trade automatically with a user-defined variable for the profit points, so let’s find out how to do that with mql4.
To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file, simple profit point test, click on continue, continue and finish.
Now you can delete everything above the ontick function and let’s also delete the two comment lines here.
We start by creating an input variable. This modifier input here will make it possible that the user can change the value from the expert advisor properties window.
Inside of the ontick function, we want to create a string variable for the signal but we don’t assign a value here because that is calculated later.
And in the next step we are going to use the included ima function for the current symbol on the chart and the currently selected period on that chart, we are going to calculate the value for twenty candles, we don’t use a shift value here, we use mode underscore sma for simple moving average, the result will be calculated based on the close price and we will do it for candle zero – that’s the current candle on the chart – and if the current moving average value is below the close price for candle zero – that’s actually the current price – we consider that to be a buy signal, so we assign the word buy to our signal.
Otherwise, if the moving average value is higher than the close price for candle zero we consider that to be a sell signal, so we assign the word sell to our signal.
Close zero is not actually the close price but current price, it becomes the close price as soon as candle zero is becoming candle one but for this simple example its good enough because now we can check if current signal equals buy and if orders total is zero – that would mean we have no open positions – and in that case, we use order send to buy ten micro lot.
And here is how we define our take profit, we take the current ask price and we multiply the user profit point value with this value, it’s called underscore point and that contains the point size of the current symbol.
Let’s repeat that for sell trades! So if the current signal equals sell and we have no open positions we use order send to sell ten micro lot, and here again, we use the user profit point value and multiply it with the point value of the currency pair.
Finally, we use the comment statement to create a chart output, this will output the current signal and the value for the user profit point value and that’s about it.
If you have no idea what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in the basic video series or maybe even the premium course on our website might be interesting for you.
Actually, this was another question from a premium course member and if you are a premium course member and have such a question please let me know, for now, please click on the compile button or press F7 on your keyboard, I don’t have any errors here and if that is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader you want to on click on insert, indicators, trend, moving average, I like to have fifty candles, no shift value, we want to calculate a simple moving average based on the close price.
Now let’s click on okay, here is the moving average and now we right-click on the chart and select template, save template and save it as tester dot tpl, you can overwrite the current one because tester tpl is what you will see when you start the strategy tester, so let’s click on view, strategy tester or press control and r, please pick the new file, simple profit point test dot ex4, mark the option for the visual mode here but before you start your test please go to expert properties and now we want to change the value to two twenty-nine, click on okay, start your test and here we are.
The expert advisor says that the current user profit point value is twenty-nine, so you can now speed the whole thing up, create results for a whole year and repeat that with a different user profit point value because in this little video you have learned how to create an expert advisor that is able to automatically trade with a take profit value that is based on a user input variable and you have coded it yourself with a few lines of mql4 code.