MQL4 TUTORIAL BASICS 104 SIMPLE SELL CROSSOVER STOP LOSS
>> YOUR LINK HERE: ___ http://youtube.com/watch?v=kLSYwA_iabY
https://mql4tutorial.com • With MQL4 for Metatrader4 we create an Expert Advisor that is able to open sell trades and close them based on a crossover of two moving averages. • In this video, we are going to create an Expert Advisor that is able to close open sell positions when the trend changes and we see a crossover. 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 sell cross over, stop, click on continue, continue and finish. Now you can delete everything above the Ontick function and the two comment lines here. We start by creating a string variable for the signal. This variable is also called signal and we don’t assign a value here because we are going to calculate it later. • • The next step is to calculate the small moving average for candle one. That is done by using the IMA function. For the current symbol on the chart and for the currently selected period on that chart. We want to calculate the IMA for 20 candles, we don’t use a shift value here. And because we want to calculate a simple moving average, we are going to use Mode underscore SMA for an exponential moving average you could use Mode underscore EMA. • • The result should be calculated based on the close price for candle one. The calculation for the big moving average for candle one is pretty similar. The only difference is the name for the variable and we are going to calculate it for 50 candles in this case. Now that we know those values, we can find out if the big moving average one for candle one is bigger than the small moving average for candle one. And if that is the case, that would be a Sell signal. • • So we assign the word sell to our signal. In the other case, if the value for big moving average one is below the value for small moving average one, we would consider that to be a buy signal and then we would assign the word buy to our signal. And when our signal equals sell and the return value for OrdersTotal equals zero, that would mean we have a sell signal and no open orders. And now we use Ordersend to open a test position for 10 Microlot. • • Please keep in mind this is only a test position, we need to open a position so we can check if the moving average sell cross over Stop Loss is working. But you wouldn’t do that on a real account. In the other case, when we have a buy signal and we have open orders, that would mean that OrdersTotal is bigger than zero. We want to call a function that is called close sell position this pair and close all the open sell position. • • Let’s add a comment statement here that will output the text the current signal is followed by the calculated signal. And that’s it for the Ontick function, but to use this function, we need to create it now because it doesn’t exist. Our function uses void because we don’t need a return value. The name is close sell positions this pair and we are going to use For loop to go through all the open positions. We use orderselect to select the position for the current counter value. • • Now we want to find out the current currency pair that is done by using order symbol and that makes it possible to check if the current symbol on the chart matches the currency symbol. We also want to find out if the order type equals OP_SELL and if those conditions are true, we use orderclose for the current order ticket and the current order lot size to close the current position. Finally, we need to close the For loop and the function, and that’s about it.
#############################
