MQL4TUTORIAL.COM

MQL4TUTORIAL.COM

By MQL4TUTORIAL.COMBusinessInvesting
Download on the App Store

MQL4TUTORIAL.COM episodes

  • MQL4 TUTORIAL BASICS – 89 SIMPLE HIGH PRICE TREND LINE
    In this video we are going to create an expert advisor that is able to draw a line object from the highest candle to the high of the current candle, so let’s find out how to do that with mql4. To get started please click on a little icon here or press 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 high price line, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. As we only want to draw a line for the visible candles on the chart we first need to calculate the number of candles on the chart and that can be done by using the function, window first visible bar that will return the index of the first visible bar in the current chart window and we will store that value in the variable called, candles on chart and that makes it possible to calculate the highest candle. We can do that by using the function ihighest, and ihighest will return the shift of the maximum value over a specific number of bars depending on type. So what does that mean? Well, for the current chart and the currently selected period on that chart, we use mode underscore high and if you mark that you will learn that it will give us the highest prices for each candle, and we want to do that for all the candles on the chart starting with the current candle that is candle zero, and the result will be stored in the variable called, highest candle, this is actually the number of the highest candle, not the value. Now let’s continue and use object delete. At the very first time, we will not have such an object but afterwards, we want to delete the old line and draw a new one each time the price changes and to do that we use object create, this will take a few parameters, so let’s go through the values one by one. The first parameter stands for the current chart, the second parameter is the name for the object in our case we use simple high line trend, you could use any name you want but you need to use the same name everywhere. Parameter three is obj underscore trend and if we mark that and press F1 we see that it is an object type for a trend line. There are lots of other object types. Depending on the type you want to draw you would need to pass other parameters, we want our object line to be drawn in the main window that is windows zero – that’s the window with the candles – if you like to use oscillators like the MACD you know that oscillators are shown in a separate window below the candles. And the line should start at the time of the highest candle, this is the timeline, these are the values and the first point for our line is the time of the highest candle on the chart, and in the next parameter we say that we want it actually to start at the highest price of the highest candle on the chart, the line should be drawn to the point in time where we have the current candle zero, and actually, we want to go it through the highest price of candle zero. That’s it for the object create part. Now let’s change some properties, we use object set integer for the current chart and the object that is called simple high line trend to change the object property color and I would like to see a red line. In the next step we do almost the same, we once again use object set integer for the current chart and the same object name but this time we want to change the object property style, I use style underscore solid, if you mark that and press F1 you will see that this will give us a solid line, we also could use a dashed or a dotted line and there are a few other styles here. The next parameter is called object property width that’s the line thickness, I have set it to one and here is one that is called object property ray, this is a little bit different and it will help us to draw the line from the highest candle through the current candle and into the fut...
    8 min
  • MQL4 TUTORIAL BASICS – 88 SIMPLE ALLIGATOR INDICATOR
    In this video we are going to create an expert advisor to calculate this indicator, it is called alligator and it is an indicator created by Bill Williams, so let’s find out how to do that with mql4. To get started please click on the 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 alligator, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. The alligator indicator has three different lines that we need to calculate and to do that we use the included function called ialligator, it comes with mql4 and we need to define a lot of parameters here, so let’s go through the values one by one. The first parameter here is for the current symbol or currency pair on the chart, the second one underscore period will automatically find the current period on that chart for example thirty minutes or one-hour charts. Parameter three is the jaw period, we use thirteen candles here and when you right-click on the alligator properties you will see those values thirteen, eight and five. These are the number of candles for the jaws period, the teeth period and the lips period, all those values also have a shift value. The original ialligator is using shift values here but I have set it to zero because it will make it possible to check the current values by comparing them for the current candle. The next parameter is the teeth period, this is calculated based on eight candles, I also don’t use a shift value for this one. The next parameter is for the lips period, this is calculated based on five candles and once again we are not going to use a shift value here. The following parameter is for the moving average method, I’m using mode underscore sma for simple moving average, if you mark that and press F1 you will see that you can choose one of these methods here, this one is the original one, we want to calculate the results based on the close price, if you mark that and press F1 you will see that there are other options available I think this one was the one that Bill Williams used. The next parameter is for one of the three available values, mode underscore gator jaw will calculate the jaw value. Here comes another shift value, once again we use zero to calculate the values for the current candle and that’s about it, so let’s close the ialligator definition here. And now we need to repeat the whole thing two more times, the only difference is that we change the parameter from mode underscore gator jaw to mode underscore gator teeth to calculate the alligator teeth. And to calculate the alligator lips you need to change the particular parameter to mode underscore gator lips, please all in capital letters. Finally, I would like to create an output by using the comment statement, this will output the calculated values directly on our chart. In this video we are not going to do automated trades, we will just output the values and that’s about it. Well, 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 this basic video series or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button. This worked without any errors 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 we need to open an empty chart and click on insert, indicators, alligator, please pick thirteen candles for the jaws period, eight candles for the teeth period, five candles for the lips period, the method is simple, we want to calculate the values based on the close price and we have set every shift value to zero here. Now please click on okay! Here is the alligator indicator, now please right-click into the chart, select template,
    8 min
  • MQL4 TUTORIAL BASICS – 87 SIMPLE SELL BUTTON OBJECT
    In this video we are going to create a button object, this is a sell button it’s actually working and we want to find out how to code that in mql4. To do that please click on a little button 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 button, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. Inside of the ontick function, we start by using object create, object create can create different kinds of objects, so let’s mark that and press F1 and you will see it takes a lot of parameters depending on the object type and when you click on this entry you will see that we have a lot of different objects that can be created, in our case we want to create a button, so we use obj underscore button and that button will appear on the main chart with the candles. The first parameter is for the current currency pair on the chart, the second one is the name of the object, we use the name sell button, parameter three is for the object type, as I said we are going to use obj underscore button, parameter four is for the window that we are going to use, the one with the candles is the main window. We want our button to appear on the main window if you like to use any oscillators you might know that Metatrader is able to show oscillators in a separate window below the candles, we don’t need a value for the date time and for the price because we are not going to move the button, we did that in the video where we have created the horizontal line, for the highest price we needed to move the object, in that case, you would need to use the parameters but for the button that’s it. So now we have an object and we want to change a few properties, so we use object set integer for the current symbol on the chart and the object with the name sell button, the property I want to change is object property x distance and set that to two hundred pixels. To change the width of the button this is very similar we use object set integer for the current symbol on the chart and the object that is called sell button, we want to change the property object property underscore x size and set it to two hundred pixels. Here comes the next one, this time we want to change the y distance – that’s the vertical distance – and we will also use two hundred pixels here and to set the height of the button we change the property object property y size and we will use fifty pixels. And now we want to decide in which corner we want the object to show up, so we change the property object property corner, I have used corner three in this case and of course we want to change the text for the button, this time we use object set string and change the property object property underscore text and change the text to sell. Here I made a mistake, this one belongs to the end of the function, our object is ready but we don’t have any event handling so far, so we need to code that now. And to do that we use the function on chart event, this one comes with mql4, the first parameter will give us the event id, the other parameters will pass some values, interesting for us is this value sparam because it will tell us the name of the object but first, we want to check if the id for the event equals chart event underscore object underscore click, if you mark that and press F1 you will see that we have different event types here and it’s easy to confuse them. For example chart event click wouldn’t work because we need to find out if the click was for an object, so chart event object click is what we want and if that is true we also want to check if the value for the sparam that we have passed here equals sell button because that is our object name and if that is true we will use the comment statement to output the value for the sparam followed by the text, was pressed,
    8 min
  • MQL4 TUTORIAL BASICS – 86 SIMPLE BUY BUTTON OBJECT
    In this video we are going to create a simple buy button object directly on the chart, so let’s find out how to code 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 buy button, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. Inside of the ontick function, we are going to use object create, this will create an object of a defined type. The first parameter is for the current symbol on the chart, the second parameter is the name, the third parameter is obj underscore button if you mark that and press F1 you will see that we have lots of different object types and this is the one we are going to use, it will place a button on your chart. The next parameter here is the window, we would like to have the button in front of the candles that’s window zero. If you would use an oscillator like the MACD that would create a separate window below window zero, that would be window one but let’s place the button on the main chart, we have no values for the date time and for the price because we are going to set the placement for our button by setting a few properties now. To do that we use object set integer for the current symbol on the chart, the second parameter is the name of the object, in our case, it’s the buy button, the property that we want to change is object property x distance and we will set it to two hundred pixels. Afterwards, I would like to change the width, so let’s repeat that object set integer for the current symbol on the chart, the name of the object is the same, this time we are going to change a different property, this is called object property x size, let’s set that also to two hundred pixels. To set the distance from the upper or the lower border we repeat the whole thing that we have done here but this time we change the property object property y distance, let’s set that also to two hundred pixels. Of course, we also need to define the button height that’s done by using obj properties y size, and I have used fifty pixels here. We can pick one of four corners for our button that is done by changing obj property underscore corner in my case I have chosen corner three and to make the button useful we need to set some object property text. Let’s remove the bracket here because this is for the end of the function. Now you could actually compile the code but the button wouldn’t do anything and to change that we need to add some event handling, therefore, we need to use a predefined function that is called on chart event. It uses several parameters. The first one is for the event id and the other parameters are used to pass values depending on the object type. Let’s find out what kind of event happened and to do that we check if the id equals chart event underscore object underscore click. If you mark that one and press F1 you will see that we have lots of different events, for example, key down for the keyboard, this would be a click on the chart but we need this one because we want to find out if the object was clicked. That’s where we can use the sparam value because it will pass the name of the object, in our case it’s the buy button, so let’s create a comment statement that will output the value for the s parameter, so it should say buy button was pressed and afterwards we use order send for the current symbol on the chart to open a buy position for ten micro lot, so please don’t do that on your real account, use a demo account. You can create one from right inside of Metatrader and one more thing, the on chart event will not work in the strategy tester, so you actually need to drag the expert advisor onto a real chart. Let’s close the if statement and the function and that’s about it. If this was too fast for you or if you have no...
    8 min
  • MQL4 TUTORIAL BASICS – 85 SIMPLE PROFIT POINT TEST
    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 chan...
    7 min
  • MQL4 TUTORIAL BASICS – 84 SIMPLE BOLLINGER BANDS SELL TRAILING STOP
    In this video we are going to create a sell trailing stop for the upper bollinger band, whenever the bollinger bands are going down we will see the trailing stop is moved, 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 bollinger band sell trailing stop, click on continue, continue and finish. Now you can delete everything above the ontick function and let’s also remove the two comment lines. We start by creating a static double variable and we will call it last stop bollinger value, this is going to hold the last value so that we can compare it with the current one. And to move a stop loss we need to open a test position, we use order send for the current symbol on the chart, we use op sell to open a sell position for ten micro lot and we also want to reset the last stop bollinger value to zero here. Now we can use the included ibands function that comes with mql4 for the current symbol on the chart and the currently selected period, we want to calculate the result for twenty candles, the deviation is two, we don’t use a shift value and we want to calculate the prices based on the close price once for the lower and once for the upper bollinger band, we do that for candle one. Now that we have the value we pass the value for the upper bollinger band to a user-defined function called check bollinger sell trailing stop. This function doesn’t exist so far, so we need to create it in a few seconds but before we do that we take the value for the upper bollinger band and save it as the last stop bollinger value because this is the value that we are going to use the next time we call the function. So let’s continue with the user-defined function, we use void because we don’t need a return value, the name of the function is check bollinger sell trailing stop and we pass the value for the upper bollinger band as a parameter. Inside of the function we use a for loop to go through all the open positions, we want to select each order that is done by using order select for the current order number, that’s the counter of the for loop here. Now let’s check if the current order symbol is the same as the symbol on our chart, symbol equals currency pair in our case. In the next step, we want to find out if the order type is op sell. That would mean we have a sell trade and let’s find out if the current order stop loss is bigger than the value for the upper bollinger band because that’s when we want to adjust the sell trailing stop. And if that is true we use order modify for the current order ticket, we don’t change the order open price but we do change the stop loss and assign the value for the upper bollinger band, the order take profit is also unchanged and that is true for the expiration date and for the color. Finally, we want to close the if loop, the for loop and the function and that’s about it. Well, if this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button or press F7, I don’t have any errors 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 pick an empty chart, click on insert, indicators, trend, bollinger bands, we want to select a period for twenty candles, the deviation value is two, applied to the close price, no shift value, now pick a color and click on okay and here are the bollinger bands, now let’s right-click select template, save template and save it as tester dot tpl because this is the template that we are going to see in the next strategy test. So let’s start one and click on view,
    7 min
  • MQL4 TUTORIAL BASICS – 83 SIMPLE BOLLINGER BANDS BUY TRAILING STOP
    In this video, we are going to create an expert advisor that is able to move a buy trailing stop for the bollinger bands indicator, 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 bollinger bands buy trailing stop, click on continue, continue and finish. Now you can remove everything above the ontick function and we also delete the two comment lines here. First, we want to create a static variable, it’s a double variable and it is called last stop bollinger value, and to have something that we can modify we need a position, so let’s check if we have no open positions – that would be the case when orders total equals zero – and in that case, we will open a test position by using order send. I would like to open a buy position for ten micro lot, and whenever we do that we want to set the last stop bollinger value to zero because this is a new position so we need a new stop value. Let’s continue and calculate the lower bollinger band and the upper bollinger band that is done by using the ibands function that comes with mql4, for the current symbol on the chart and the currently selected period, we want to calculate the result based on twenty candles, the deviation value is two, we don’t need any shift values here, the calculation should be done based on the close price and we use either mode lower or mode upper to calculate the lower or the upper bollinger band for candle one – that’s the last finished candle on the chart – and afterwards we want to try to move the stop loss. We try it because it will not be changed every time we want to do that, that depends on a few things so you might see these order modify errors here. For example, if the current price is too close to the lower bollinger band we cannot move the stop loss upwards because of the spread and whenever we want to try to change the stop loss we want to call a function called check bollinger buy trailing stop and we pass the value for the lower bollinger band as a parameter here. Afterwards, we want to assign the value for the lower bollinger band to the last stop bollinger value because the current value will be the last value the next time we call the function so let’s actually create that function. We use void because we don’t have a return type here. The name of the function is check bollinger buy trailing stop and we pass the parameter for the lower bollinger bands. Afterwards, we use a for loop to go through all the open orders. Now we want to use order select to select each of those orders. In the next step, we want to find out if the if the current order symbol is equal to the symbol on the chart. We check if the order type equals op buy for a buy order and if all those conditions apply we want to find out if the current order stop loss is below the lower bollinger band and if this is true we use order modify for the current order ticket, we don’t change the order open price but we will try to change the stop loss to the value for the lower bollinger band, the order take profit, the expiration and the color values are not modified here. Finally, we need to close the loops and the function and that’s about it. If you have no idea what all the code here does or if this was too fast for you or if 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, for now, please click on the compile button or press F7, you shouldn’t get any errors here and if that is the case you can press the little button here or press F4 on your keyboard to go back to Metatrader. And in Metatrader we want to open a blank chart and click on insert, indicators, trend, bollinger bands, please select a period for twenty candles, the deviation value is two,
    7 min
  • MQL4 TUTORIAL BASICS – 82 SIMPLE SMA TRAILING STOP LOSS
    In this video, we are going to create a sell trailing stop for a simple moving average, 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 sma sell trailing stop, click on continue, continue and finish. Now you can remove everything above the ontick function and we can delete the two comment lines here. First, we want to create a static double variable for the last stop moving average value and to use a trailing stop we also need to have open positions, so when orders total equals zero that means we have no open positions and that’s when we use order send to open a test position that is ten micro lot, we use op underscore sell to open a sell position and afterwards we set the last stop moving average value to an unrealistic high value of one thousand, so when we call this function the next time you could also use zero here but you would need to implement another condition check, so for this simple example we go with this solution. Now we can use the integrated ima function for the current symbol on the chart and the currently selected period on that chart to create a simple moving average for five hundred candles, we don’t use a shift value here, we use the parameter mode underscore sma because this stands for simple moving average and we would like to have the results based on the close price for candle one. Actually, this part needs to be outside of this if condition, now we can check if the current stop moving average value is below the last stop moving average value and we also want to know if the current value is bigger than the ask price otherwise it will be hard to set the stop loss and if both conditions are true we want to call a function called check sma sell trailing stop and pass the stop moving average value as a parameter. This function doesn’t exist, so we need to create it in a few seconds but before we do that we assign the current stop moving average value to the variable last stop moving average value because the next time we call this function the current value will be the last value. Now let’s continue with our new function called check sma sell trailing stop we use void here because we don’t have a return value and we pass the stop moving average value as a parameter. Afterwards, we use a for loop to go through all the open orders and with order select, we select each position, we want to check if the order symbol is equal to the symbol on the chart, we also want to know if the current order type is a sell order and finally we need to check if the current order stop loss is above the stop moving average value that we have calculated otherwise we won’t change anything and if all those conditions are true we use order modify for the current order ticket, we don’t change the order open price but we do change the stop moving average value for the stop loss value here, we leave the order take profit as it is and we also don’t want to change the value for the expiration or for the color. Now let’s close the for loop and the function here 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, for now, please click on the compile button or press F7, I didn’t get any errors 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 click on insert, indicators, trend, moving average, please pick a period for five hundred candles, select the method simple for simple sma, apply to the close price, pick a color and click on okay, now let’s zoom into the chart and here is the simple moving average. Now we right-click,
    7 min
  • MQL4 TUTORIAL BASICS – 81 SIMPLE SMA BUY TRAILING STOP
    In this video, we are going to create an expert advisor that is able to create a buy trailing stop for a simple moving average, 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, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start by creating a static variable and we call it last stop moving average value. To have something that we can use with a buy trailing stop we need to create a test buy position so whenever we have no open orders we use order send to open a buy position for ten micro lots, please don’t do that on a real account, this is just for testing purposes, so let’s calculate the stop moving average value that is done by using the ima function for the current symbol on the chart and the currently selected period on that chart, I will use a simple moving average for five hundred candles, we don’t use a shift value, the mode is for sma simple moving average, the calculation will be based on the close price for candle one. Now we want to check if the current value for the stop moving average is bigger than the last value and the second condition will be if the stop moving average value is below the current bid price and if that is the case we want to try to change the stop loss that’s done by calling a function check sma buy trailing stop and we are going to pass the stop moving average value that we have calculated here. And afterwards, we want to assign the stop moving average value to the variable for the last stop moving average value for the next time we will check it because the current one will be the last one the next time we check it. So far so simple, but this function doesn’t exist so far, so we need to create it now, the name of the function is check sma buy trailing stop, we don’t return a value so we use void and we will pass a parameter called stop moving average value from the main function, we use a for loop to go through all the open orders, each order is selected for the current counter value that we have used here in the for loop. The next condition we need to check is if the current order symbol is equal to the symbol on the chart and as we are going to create a buy trailing stop we also want to check if the order type equals op underscore buy, we also want to know if the current order stop loss for the position is below the stop moving average value that we have calculated, and if all the conditions are true we use order modify for the current order ticket, we don’t change the order open price but we try to change the stop loss value here, we leave the other options unchanged, now let’s close the if statement and the for loop, and finally the function, and that’s about it. Well, if this was too fast for you or if you have no idea what all the code here does maybe you want to watch the other videos in this basic video series or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button or press F7, I have messed up one of the braces here so let’s recompile now, and this time we have no errors, please notice that we have two warnings here, order send and order modify should be checked, there are several conditions when we will not be able to modify anything, for example, if we try to replace the stop moving average value with something that the broker is not accepting or if we use the same value twice. I will show you the modify errors in the journal. To get back to Metatrader please click on a little icon here or press F4 on your keyboard, now you should see the Metatrader window and here you want to create a new chart, click on insert, indicators, trend, moving average, please pick a period for five hundred candles for a simple moving average applied to the close price,
    8 min
  • MQL4 TUTORIAL BASICS – 80 SIMPLE SPREAD CHECKER
    In this video, we will create an expert advisor that is able to calculate the current spread and find out what the lowest and the highest spread was for a certain currency pair, so let’s find out how to do that with mql4. To get started please click on a little icon here or press the 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 spread checker, 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 double variable called lowest spread value and we give it an initial value of one hundred thousand, this is definitely not the lowest spread value that you will see. Now let’s create another variable called highest spread value and this one will have a value of zero and that’s definitely also not the highest value that you will see. Inside of the ontick function, we want to calculate the current candle spread that can be done by using symbol info integer for the current symbol on the chart and we use symbol underscore spread – all in capital letters – and if the current candle spread is below the value that we have for lowest spread value, we want to save it as the lowest spread value that we have seen, so we assign the value for current candle spread to our variable called lowest spread value. In the other case if the current candle spread is bigger than the highest spread value we have seen, so far we want to store the value for the current candle spread as the highest spread value. And finally, we want to use the comment statement to create a few outputs for the current spread, the lowest spread and the highest spread for our currency pair. That’s about it. If you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you. Actually, this was an idea of a premium course member and if you are already a premium course member and have an idea for a video like this one please let me know but for now please click on the compile button or press F7, you shouldn’t get 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, we need to open an empty chart, please navigate to the new file called, simple spread checker and drag it on the chart, click okay and when the next tick comes in you should see that we have some values here, I will make it a line chart, so it’s better to read, so the lowest spread value so far was sixteen, the current spread value is sixteen and the highest spread value is seventeen. Now we see a few price changes and now the current spread is changing and we see that the highest spread value now is seventeen, the lowest one is fourteen, so our expert advisor actually works as we expected but there is one more thing to mention, please click on view, strategy tester or press control and r, please pick the new file simple spread checker dot ex4, mark the visual mode and start a test. And this time you will see no changes at all and that’s because mql4 does not store the values for the spread for history data so if you press F2 you will see that we have a history center and everything inside is open, high, low, close and volume, there are no values for the spread and that’s why this expert advisor will not be useful inside of the strategy tester but it works on real-time charts and in this little video you have learned how to find out the current, the lowest and the highest spread for a certain currency pair and you have coded it yourself with a few lines of mql4 code.
    6 min

About MQL4TUTORIAL.COM

From the publisher's feed

MQL4 Tutorial for Metatrader 4 or MT4 about automated trading, trading robots, MQL 4 and MQL programming