Building your own trading bot using MetaTrader 4 and MQL4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey it's Brian again in this video I'm going to show you how to program your own basic trading bot using metatrader4 and nql for this is a great skill to have in the trading business not only can you test in or automate your own trading strategies but if you just start out and looking to make some money to trade with there are all kinds of people out there that are willing to pay you to build their BOTS for them Metatrader actually maintains an active website of people looking for freelancers that i've linked to in the description of this video knowing how to code will surely help you get through this video the language you know how to code in doesn't matter much as the MQL for programming language is pretty basic and experienced in any other modern language should allow you to allow you to jump right in even if you have no programming experience so feel free to follow along you can probably pick it up pretty quick part two of this series is going to focus on back testing and you won't need any programming knowledge for that once again a disclaimer the bots we're building this for demonstration purposes only please don't try using the spot for real it's very simple will not make you any money but let this bot be a starting point for learning how to build bots that will make you a lot of money so Metatrader has a programming language built right into the trading client called MQL for we can write MQL for programs using what they call meta editor there are three types of programs you can make with meta editor scripts indicators and expert advisors today we'll be creating an expert advisor our simple bot is going to trade based on a currencies moving average to wrap your mind around how moving averages work check out my video on moving averages that should pop up on a car here or you can look in the video's description for a link to it here's the strategy or about is going to trade with when the price on this chart drops below the moving average we're going to be in a potential buy position once the price starts increasing again we're going to enter into a buy position also when the price on the chart rises above the moving average we're going to be in a potential sell position when the price turns around and starts dropping we're going to enter it position one more time this trading strategy is absolute garbage and will not make you money don't trade using it however it is simple enough that we can get the code up and running quickly all right so we've got the main meta trader window up and open I recommend you do all of your development on a test account I've got one of my real test accounts here it's connected to the live server but as you see has almost no money in it so I highly recommend you do all your development on an account like that so to get to the meta editor in order to code our bot you're going to go to tools medical language editor and that should pop it right up here we go in the top left you'll see an icon for new create a new document today we're going to be creating an expert advisor a couple other things you can do are custom indicators and scripts scripts are for they just run one time over the data you have on a chart custom indicators will draw things on the charts there are things such as moving averages and stuff like that an expert advisor can actually enter and exit trades so that's what we want to do today click on next come up within a name for this yeah whatever you wanted now in future videos we'll get into what some of these event handlers do for now just go ahead and click through and you see it writes all the boilerplate code for you this is really all you need it sets up first of all the properties that we put in on that first dialog box version number you can update that if you want as you make changes there's a couple functions that it loads automatically for us here now on in it just gets run once when the expert advisor is first loaded into the system and and similarly on D in it is run just once when it's removed from the chart on tick is where the meat of what we're doing is going to be written this could is called every tick a tick in Metatrader is every time the price is updated so there's potentially you know several per second this is not a period this has nothing to do with whether the charge is said on the 4-hour chart or the 1-minute chart ticket every time the price gets updated so let's run a little test here to see if this is all put together and working correctly we're going to write basically the hello world of spots so when I write alert hello world and that should do the trick you'll see what alert does it kind of it's a good way to see debugging information and things in Metatrader so to get this to show up in Metatrader we'll simply go ahead and hit compile says zero air for errors zero warnings and it created the YouTube NQ four file which we can then transfer to our experts folder and we should be able to see this appear in the expert advisors list now yep here it is occasionally the first time you do it you'll have to right-click on this and say refresh but once you get it there it should just be there you should be fine so let's test this out in order to use an expert advisor you simply drag it onto the chart you want to use it on so do you know yes we do okay that's the stuff we put in that click okay ah see I'm first tick all right so the first thing we want to do for our bodies have a calculated moving average as we go along now luckily for us luckily for us mq Beaufort has a built-in moving average function we don't have to write that function ourselves so let's go ahead and add that the function is called I MA and then once you type that it'll tell you everything you need so the symbol is if you put in all of the symbol of the current chart putting zero on the time frame means the current time frame [Music] period for the moving average I'm just going to arbitrarily set this to 15 I'll be going to into what shift is in a future video about more advanced moving average concepts for now just set that to nothing and then the next variable is the method we want to use the average as in simple arithmetic moving average or exponential moving average we're just going to go simple today then the next variable is which price we want to use basically we're on the candlestick we want to say is the price of this period I'll link to my video about candlesticks it feel a little bit a little bit confused about that concept for now we're just going to go ahead and put media and once again another shifts variable and do you ever move an average let's go ahead and display that and see if this is working as expected go ahead and compile now we got an error why did we get this error well moving average undeclared identifier people using some more modern programming languages may not be familiar with what it means to clear a variable you have to tell it what type it is whether it's a number or a string of characters in this case our moving average is going to be a floating-point number but it's actually going to be a little bit bigger than that a double so we can go into more advanced concepts like this in a future video but for now just know you have to clear a variable and then assign it however mql 4 does allow us to do this in one step if we want like that so now it should compile it correctly so now should compile correctly yep 0 0 1 AIDS let's go ahead and put that into our experts under the plate please go back to metatrader4 just to be safe let's go ahead and refresh this to make sure it's the newest version drag this onto our chart there you go sure enough it's calculating our moving average as would be expected the other two numbers we need for our trading strategy are the current price and the price on the previous candlestick MQL 4 makes it very easy to get these numbers as a special array called open that contains all of the previous price data so let's go ahead and get current price now the way this works is the current price is in position 0 on the array one candlestick back is one two bars back would be two etc etc we just need to go one back for our for our strategy let's go ahead and put that information out there and compile and see if it works as expected yep as expected all right now we have just have to put in the depth of our trading strategy so put it in exactly as we defined it you all right so that's basically the logic of our body as we defined it at the beginning of this video sermon language mql 4 has pretty standard if statements you see basically we're saying if the current price is above the moving average but it's lower than the last price meaning it just turned around and it's starting to the down towards back towards the moving average we're going to put in a sell order or if the current price is beneath the moving average but it took an uptick and it's starting to go up now we're going to place a buy order so now finally the moment we've all been waiting for how to make the actual sell and buy orders in NPO for now one thing that's important to notice within this on tick function any of the variables we declare are basically going to go away after this function is done running it as we've seen it gots called one it one per tick so if we want variables to persist from tick to tick what we need to do is to clear them outside the scope of that function so up here at the top so if we want to store say in order that we create an order number of the order we open got to create that outside this function let's put it up here now that variable work will persist outside of these on ernet on DNA and on type functions to create our selves order the order those order send this function we want to use for this there's a lot of stuff that goes into this we're not going to go over it all in detail today just know that it's basically like you've seen so far so know once again means on the current symbol the command will be okey so since we want to enter a sell order again I'm if this were more sophisticated but we'd have to you know determine what a healthy amount of volume was things like that you want to price we want to be at the current price that's it that will create an order as I've said before the spot this trading strategy is not very good so I'm not actually going to run this and lose my money but you can see how it would be done let's go ahead and rate what the buy order would be real quick on this one the operation would be by now even if we're not going to run those let's go ahead and compile it to make sure there's no syntax errors or anything like that zero errors so we're good to go now what order send returns is your order number which we recover which we're storing in this order number in this order and this order variable so that's a very basic bow once again it's not a very good one don't run this you'll lose money but those are the basics of how to create an NPO for expert advisor that acts as about four trades before letting the spot loose on the wild we're going to want to back test it metatrader4 allows us to test any scripts indicators or expert advisors using historical data to make sure it works as expected I've got an entire video on back testing and metatrader4 so click somewhere around here to watch that next see it
Info
Channel: Cryptocurrency Trading
Views: 285,739
Rating: undefined out of 5
Keywords: metatrader 4, trading bots, mql4, cryptocurrency trading bots
Id: t-TmsV0hV8Y
Channel Id: undefined
Length: 15min 45sec (945 seconds)
Published: Sun Apr 23 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.