Connect Python to MetaTrader5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to this video in this video i will show you how we can trade with python through metatrader5 metatrader5 has an integration of python and it has its own documentation where you can request market data where you can do your own data analysis where you can get your account information such as balance and equity and where you can also send trades to the live market and trade using your algo strategy the documentation can be found on mql5.com and if you want to find any specific details about the python integration you can find the documentation here and look it up so what i'm going to show you are the most basic commands and features of the penetrator 5 library so for that we need to import metatrader5 as empty if you don't have it you can install using pip install matrix 5 and i will also import pandas and plotly to do some data analysis and data visualization lastly we will import datetime library to work with datetime objects so we can request for specific dates time periods etc so let's run the import and to start with a 35 from python you have to use initialize so if we type empty dot initialize the military five platform should start all right so on my side it just did so now my metatrader5 pattern is running please don't forget that to initialize the platform you have to download minute trader 5 first on mql5 next thing that we have to do is we have to log in to a trade account and i have prepared a demo account at ic markets with this login this password and this server and by typing empty dot login and passing these arguments we will then log in to our trade account so let me show you how it works so i'm logged in already but let's try to log out so now i'm logged out because i entered wrong credentials but if i run mt.login with these credentials i should be able to log in without without any issues so now we see that empty login works and that we're logged into our account to get account information we have mt.account info and what it does is it will return account specific information such as the login number the leverage the restrictions balance and equity which server it is on and even my name so we see that by typing dot account info we get this data and we when we only want to get specific data for let's say just the login number we can assign login number to account info dot login if we need the account balance we just select the balance and we need the equity we get the equity so now we print these values so login is six nine zero one one zero we can check that and that is correct the balance is ninety nine thousand nine hundred fourteen if we check it is correct and the equity when you run the script is 99 922 and that is almost correct as you can see i have an open position so equity has changed during this time but we see that requesting the account specific information is working next up is getting the number of symbols on our platform so mt.symbolstotal it will return the total number of symbols offered by your broker that are visible in the mt5 client terminal so you run this we see that the number is 1773 which is a very big number so i see markets is offering a wide range of symbols and you can find the symbols in your client room as well by going to a terminal and going through these symbols if we want to have the specifications of all symbols such as the margin requirement uh trading hours uh such as margin currency and profit currency contract sizes we can use empty dot symbols get and for every symbol here we are receiving data that might be used for us right so we have this we have the things such as simple name which is very important the profit currency the contract sizes when the session starts when the session closes so whatever you need to get from the symbol the to get data from the symbol you can find it in empty dot symbols get if we don't want to get data for all symbols but just for one specific symbol we can use empty dot symbol info and then pass in the symbol name and if we do that then we get data only for that symbol also if you don't want it in the mt5 format as it returns to you some to you something like that you can use as dick and it will return a dictionary so for example the euro usd here we have some details about the symbols such as you know when it was requested number of decimal digits the current spread etc etc so we can get all the data that we need on the euro usd using empty dot symbol info something that you will use very often is symbol info tick which is this one and this one will return the current price of the symbol so if i want to know the symbol prices on your esd here we have the time in unix timestamp we have the bit and ask prices so when you refresh the simple info take multiple times you will always get the most recent price data on that symbol now this one is very very useful so if we want to get the ohlc data or the candlestick data we can use empty dot copy rates range so what it does is it will return open high low close prices for your symbol and the parameters that we have to pass is the simple name the time frame in this case i specified the daily time frame and the start date time and the end in time so i'm requesting data starting 201 this year until now and i will also plot this data using plotly and i will close plot the close prices so we can see what the prices look on the chart so let's do that so now we see that these are the close prices on euro is the daily chart and here we also have the open high low close prices in the pandas data frame so now what we can do is we can perform data analysis calculator indicators and write our strategies based on this data to confirm that it works right we can also check the close prices in metatrader5 so this is what it looks like on in plotly and this is what it will look like in metatrader 5. so let's go to the daily chart let's zoom out a bit and go until start of the year of 2021 because that's what we requested so this should be this is january 2021 and this is now so we see that we went down went up and then back down again and we checked this in the jupiter notebook we see that the data here is the same as in mana trader 5 so this one worked if you're interested in going more into detail and not just look at the ohlc data but you want to check the actual ticks so every transactions that took place in metatrader 5 we can use mt dot copy ticks range and that will show you the history of all bid and ask prices in the platform so i run this now and i request for a request since october 4th and i'm requesting until now and this is what it looks like so we have to take data on euro usd so we know exactly what the spread was what the bid price was and what ask price was we see that these prices overlay because the spread here is zero but let's for example check the prices during less liquid times such as aftermarket market breaks so the bid price here is lower than the ask price but good thing is that ic markets has zero spread in most cases in usd that is why we see minimum difference between bit and ask for example here we see a slight uh deviation the bit from the ass that means that there was a spread on usd otherwise the prices on the at ic markets are very good now we are getting to the interesting part where we can interact the platform in terms of orders history and study sending trades remarket so to get the total number of current orders we can use mt.org total and that will return the number of open orders so we see that this returns one and we check the platform we have one buy stop order spending so if i close this one then now it should show zero it works let's try to open uh or place two by stop orders so we have one by stop and we place a second buy stop so now we have two pending orders open so if we go back to orders total we now see two because now we have two open buy supporters but if you want to get more details about the orders we can use empty dot orders get and that will give you the specific details of our orders so the orders have something like a ticket when the order was submitted and we see that it is a stop order on eurasi and the the price is set at 1.19 which we can also find here 1.19857 so uh next thing is we can also get number of positions so this means traits that are open and not closed yet in our platform if we check our mt5 platform we see that we have two lots open on usd so this should return one if we open another order that means that we're adding a new order so we have two openers now positions total should now return 2 so let's run this again and we see that we have 2 now if you want more details about our positions we can use empty dot positions get and if you run this we now see two positions and each position has the details that we need we see that it is again on usd one is volume one the other is volume 2. so if you need to get more details about your open positions you can use mt.positions get now we are getting to number of history orders so these are orders that we've request sent to the platform the past so these are all market orders limit orders and if you run this we see that we have 14 orders that we've placed from this date range if we check the platform we can go to history and go to orders and you will find a list of orders here so this data you can access with empty history or is total again if you want the specific details of our orders we can use history or just get so if you run this we get all the information about the orders that we've placed in the past and this data should reflect the same data that you find here in the client platform besides orders we also have something called deals so the difference between an order and deal is basically an order is basically a request that you sent to the broker and the deal is the confirmation or the transaction it basically tells you that the position has been opened or closed on your platform so when you send order it will create a deal and the history of deals or a number of deals you can get that using mt dot history deals total so this account has received 13 deals from the broker which you can also find in the platform by going to history and look at the deal section if you want to get the details of our deals we can use mt.historydeals get and exact data of our deals are now found here so this one is something you should be most excited about and here i will show you how you can open a trade with python through metatrader5 and to open a trade we have to create something called a request with certain parameters so the request in matrix 5 should have and a dollar about the action so if you specify empty the trade action deal means that you want to trade then you want to specify a symbol the volume how much of that symbol you want to buy or sell in type you specify if you want to buy or sell in this case i want to buy two lots on eur i want to buy at the current ask price on the usd so i specif i use empty symbol info take on your sd and get the ask price of that for this request i don't want to place any stop loss or take profit but if you want to feel free to add that if you specify 0.0 it means that it will not have any stop loss and will not have any take profit next we have something called deviation so when the request is sent the market price may move and may not be filled for the price that you specify but if you add a deviation it means that there is a tolerance for which you want the order to be filled so if there's a slip edge the deviation tells you okay if it's within the deviation i still want to open that trade next is magic so each order can be identified using a magic number that means if you have multiple expert advisors or multiple strategies you can uniquely identify them using this magic number and if you want to use comments you can also use them to also uh check for certain comments that might help you when checking the trades and client terminal the time in force of this order is good till cancelled and this is specified by mt.order time gtc and also the type filling that i'm using and this depends on brokers some brokers allow multiple time filling but at ic markets you have to use immediate or cancel so the type filling that i'm using here is mt.order filling ioc so this request is a dictionary that we've wrapped and we have to use empty dot order send and passing the request as the parameter so right now we see that we have two open positions so let's close them both and to also trade through python you have to make sure that algo trading is enabled so we click on algo trading and if we run this it should work right so we have an order result the return code is 1009 which means it's okay and here are the uh specific details that we've get from uh order sent so we get a deal id order id and here we see a comment request executed so this is a confirmative works and if we check our platform we now see that a position has been opened with python also you can see that here in the comments if i hover over this position you can see python script placed by expert id two three four zero zero zero so that is the magic number and the comment that we're talking about if i run this cell again i should see a second position opening which is the case so we see that it works last thing i want to show you is how to close a position so to close a position you also have to create a request dictionary but the closer position you simply have to specify the position number which is added here and if you want to close a position so we see that we have two buy orders to close position we have to sell and we want to sell using the full volume and to get the ticket we can just copy this these ticket numbers or what we can also do is we can go up here and go to positions get and we see that we have these positions open so if i for example take this ticket id and add it here and now the specifications are the same you know we're closing the position so same volume instead of buying we're selling so if i run this the position with the id 158631130 should now be closed so let's check this and we see that we have one position less which is great and if i scroll up to positions get this position should no longer be there if i want to close this last position i also have to copy the ticket id of this one so let's replace the position with this one it is success and if i check the platform now we have zero positions open all right so i hope that you've enjoyed this video if you have any questions about the monetary 5 documentation or any questions how to integrate your python scripts into manager 5 please let me know in the comments below and i will be back with another video thanks a lot bye
Info
Channel: TraderPy - Algorithmic Trading
Views: 1,976
Rating: undefined out of 5
Keywords: metatrader, python, trading, forex
Id: zu2q28h9Uvc
Channel Id: undefined
Length: 20min 52sec (1252 seconds)
Published: Mon Oct 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.