Expert Advisor Programming

Before we begin with this mql4 tutorial, make sure you understand the following data types. Every variable we use has to be defined with a data type. Here are the most used:.

Before beginning, please note that after chapter 1 you maybe will not be able to run the EA. Not all brokers work the same way. If it does not work on your broker, please continue reading until the end of chapter 2, where we will make the EA compatible with all brokers. In the next window, we write the Expert Advisors Name and the Authors name with his website link. We also want to add our first variables. We add the standard variables that an Expert Advisor needs.

All types are int integer except the LotSize. The LotSize must have a decimal point in it so you have to define it as a double. This is because I am using an 5 Digits Broker. Because the EA will not automatically recognize a 4 or 5 Digits broker, we have to add a function which does this for him. But you will learn this later in the next MQL4 tutorial. For now, leave it like this. If you have a 4 Digits Broker, divide these 3 values by You can see 3 areas.

All code you write goes to one of these areas. We will put all the code we want to call if we attach the Expert Advisor to the chart into the OnInit area. And finally, the area in which we work most, the OnTick area. The OnTick function is called every time if we receive a new price tick from the broker for the Symbol Currency Pair the EA is attached to. If you try to attach it to a chart and you go to the Inputs tab you can now see our defined variables.

What we do in this mql4 tutorial: For this strategy, we call a predefined variable. The Close[] which is a double remember from above. The Close[] gives us a close price example 1. Close[0] means the current Close price; Close[1] the previous and so on.

Same with the Open[], Low[] and High[]. So when you look at the code, this logic should be clear now. I will put here the function to open a buy order.

The EA will ignore this. You will learn how to make it compatible for ECN Brokers in the next tutorial. Press F7 to compile and check the result in the StrategyTester. You will now actually get something like this.

The EA places a buy order on every tick if the 3 last bars where bullish. You have to know: MQL4 has already predefined function like the OrderSend function.

But we can also write our own function, in which we can call other custom or predefined functions. We write them at the very bottom of our file. This function is an integer int. It returns the value of open trade for this Pair and this MagicNumber. We use our MagicNumber we defined above. The function looks like this:. Do not forget to call the function in the OnTick area. We call our custom function. The function returns the total open trades for this Symbol and this MagicNumber. Quickly explained the TotalOpenOrders function: But now we still have an issue.

If the candle is long the EA open a second buy trade directly after the first buy trade gets closed by the StopLoss or TakeProfit. We have to add a second function. This function is a boolean bool.

This function will return a true if a new candle is generated in the Metatrader else it returns a false. This function is self-explanatory so I will not explain how it works in this mql4 tutorial. It will now look like this and work pretty well. Try now for yourself to add the opposite strategy called 3 black crows Sell after 3 bearish candles. Have questions about this mql4 tutorial? Write a comment or open a topic in the forum if there is not already an answer for it.

Download the source code. Thanks for your tutorial. Please could you check out the main part of my EA code and tell me why it is not working? Its suppose to open both buy and sell positions if candlestick crosses SMA. But if you follow my tutorial you should be able to programme such a simple EA. I had to use a different naming for the functions to calculate the point and slippage; i. The reason is that I was encountering errors when the orders were placed, and this was the solution to eliminate said errors…just an FYI.

It worked OK with these function renamings. Thank you for your practical step by step guidance. Even zero program knowledge like me can make my first EA. Gud tut to make me get started.. There are a load of programs that work with a drag and drop GUI.

Some I will use for sure, but I always was interested in learning to code so this might be the right time. My highly skilled programmer friend once told me I got the skills to become a very talented programmer in just 2 months time. But should I start with MQL4 language? I can google it, but I prefer hearing from an experienced mql programmer. You can immediately learn mql4 without knowing any other language. Nowadays mql4 and mql5 are very similar.

Some small things are quite different, but they are actually pretty much the same. But here again, I advise to first learn mql4 because most users use MT4 and you will find much more tutorials and help online than you would get with mql5. I am sure you figure it out how to make a trailing stop function if you take a look at the mql4 forum and the mql4 documentation.

Thank you for your reply. Please can you show me how yo modify code such that new trades will be open when ever a new signal is valid even if there is an existing trade, for instance if there is a buy trade and a sell signal is generated ea should open sell even if a buy trade is still open. You only need to modify the following code: Martingale i not advised for safety of your money, jut curious to know if i wanted to implement martigale in this chapter one tutorial what should be added?

You have to set an initial LotSize like 0. Each time the EA opens a trade he would have to check if the last closed trade same MagicNumber and symbol was a looser. If it was a looser, the lotsize of the next trade will be the lotsize of the last trade multiplied by X. If you will not mind can you assist me the code of checking whether the trade finished as a loser or winner. I am sure you will also find a lot in the mql4 forum.

Hi Yannick, Need help. Need an ea that places a trade at each candle, buy for a bull candle, sell for a bear candle. Exits all open trades when the candle changes direction. Example, if the trend was up and we buy at several times, we exit when a bear candle appears, and enter new sell. After this tutorial you should be able to program your entry strategy. I will explain how you can close trades in a next chapter.