Hello Hello Mic test FOLLOW THE CAPTIONS TO LEARN MORE! [Music] this is how we can actually create some basic functions on creating and closing orders and we've already seen that how we I use these very basic functions to create Buy sell orders and close those positions in the mt5 platform on the right hand side hello Financial programmers I am Ritvik Dashora and I'm back with a new video and some new learnings this is the last video of this playlist on exploring mt5 in the first four videos I talked about how to use Tradingview or alerts webhook to automate trades on mt5 the second video was about exploring the mt5 python package the third video was on making a full-fledged trading board on python using mt5 as backend and the fourth video was an extension of the third video which was on how to extract real-time prices in Python using mt5 this is the last video where we'll be talking about how to send buy and sell signals to MP5 using python guys if you're new to my YouTube channel on this channel I turn all my viewers to successful Financial programmers here I upload videos related to trading boards implementation of AI in finance using some highly valuable Finance apis making some pile script trading strategies etc etc so if this is something that you are interested in then you should click on the Subscribe button and also hit on the Bell icon because there are many playlists that are coming on this YouTube channel very soon and I don't want you guys to miss out on those free content over here I upload videos every Saturday and it will help you a lot to grow your skill set in Python and finance the topics that we're going to cover today are as I mentioned we are learning how to send buy and sell orders to mt5 using python so what I'll be doing is I'll be summarizing the last video first where I talked about how to extract the real-time prices in Python through mt5 right and then I will be understanding the order send send function because this is the function that will be used to send buy or sell orders to mt5 using python script the third thing is that we will make a python function of creating orders on mt5 and the last thing is that we will be making a function on closing the orders as well so this is the entire structure of this video and without any further Ado let's start okay guys so in the last video we saw how to install a MetaTrader5 and then we saw how to initialize it and authenticate it right which is logging into it and then this is the code that we made in the last video which is a basic for loop on extracting the real-time prices of Bitcoin for any particular time frame yeah in that video we actually did it for one minute but we can make it for different time frames as well right so I'll come back to open the MetaTrader 5 documentation this is the pypi. org page of MetaTrader5 and this is how we actually copied it and paste it over there to install it I'll come to the documentation directly so here all these are the functions that are used in mt5 Python package but the thing that I am actually interested in is just the order send function so I'll just click on this one let's see what it means what it says so it sends a request to perform trading operation now what type of trading operation will check it out so I'll just open both of them in different tabs and Order send is basically it is used to send a request to perform trading operations from the terminal to the trade server our trade server is our python code and the localhost is basically our server we can keep it in any particular Cloud as well so suppose it's running on say AWS or Digital Ocean and in that case you don't need to keep your system I would say switched on all the time you can just put it over there it will be running 24 7 and you can do whatever you want at that time so yeah this is the function which is order send and there is a request which goes inside it now what is this request I think it's mentioned here right in the next tab that I opened so there are different things that we need to provide it inside request it's in a dictionary format where we provide different types of things things like symbol price stop loss take profit type of order right it's a buy order a seller order Etc and so on so I think there would be an example here which is showed here so yeah these are some of the examples that actually you can go through but I'll just quickly come to the order send only because this is something that I would like to explore and so here if you see what are the different inputs that you need to provide again it's very similar to the requests because it is it's basically running on request only thing that we saw here in the next tab so these are the inputs that we need to provide here we'll check it out and this is an example code here which gives all the information that we need to follow so I'll come back to the to our code here let's import MetaTrader5 as empty right now we need to create a dictionary here so request and then dictionary what are the things that needs to be provided as inputs the first thing is that in is the symbol so just clear I would say write symbol here inside string the symbol is BTC USD right in the last video if you remember I did it for BTC USD only right now it's not the US market time so I'm not doing it for U.
S stocks but you you guys can do it for your stocks as well a very important thing here although I have said it in almost all videos if you click on the golden dollar button here you can actually search for different tickers so for example if I just write TSLA so the ticket for Tesla is TSLA. NAS which will be different for your broker so you should check out the symbol and then provide that one only in the python script so in this case if I just write BTC USD is that ticker of Bitcoin if you guys are confused on how to login and how to download the MetaTrader5 platform then guys you should watch my first video because in that video I explain everything I made this I would say I downloaded the MetaTrader of IC markets broker you guys can have a different broker which is update you so this is the symbol now the next thing which is price is important so I'll just click here and then price is equal to it would be something I'll provide it later what else stop loss right let's provide a stop loss as well again something that I'll provide later and another one is TP which is take profit right type right order type is something important whether it's a buy or a sell order type we'll check it later what else I think time type time is important and the last one is type filling right this is important now there's another important and I would say important input here which is position which will be used while closing the order position is basically the order ID right now we have no rates placed in mt5 so let me just split my screen into two parts on the right hand side you can see mt5 on the left hand side I am making this particular code so the price of this particular outside trade actually I forgot one here which is action if you just go to the first one is action okay perfect so the first one in this case let's just scroll down a bit here so action is this one basically empty dot trade action deal right so I I have imported a MetaTrader 5 as empty so I'll just write empty here symbol is BTC USD for price I'll just write empty dot symbol tick info and in the inside this I'll provide BTC USD dot let's just say ask I am interested in the ask price so I'm basically placing a buy order here so buy order would actually look for the ask price the best sellers which are available in the market for sale order we look for bid price but again like it's up to you you can create some limit orders as well the stop loss is suppose currently Bitcoin is trading at 29 300 so the stop loss would be 28 000. 0 I should remove the comma here right make sure you provide this in float format you cannot write an integer here it will throw an error so just make sure it's into decimal format the the take profit is suppose 33 33 000 I can point zero type of order would be empty dot if you just write capital r o and then press tab it will give you a different recommendations I am actually interested in order type buy I would like to buy it right I forgot one more variable here which is volume let's go here and check yeah this is the volume which is the number of shares number of lots that we would like to trade so in this case let's just do it for 0.
01 Bitcoin right now let's see what is Type time right if you go here type time is basically this one order type by expiration the value can be one of the order type time so let's say click here and let's see what is this one right order time GTC the order stays in the queue until it is manually cancelled so actually this is only good so I'll just click copy this one let's come to our code I'll just write empty Dot and then paste it here let's do the same thing for a type filling as well I'll go back type filling type filling type the value can be one of the order type filling values let's see what are the different things yeah an agreement to execute a deal at a maximum volume available in the Market within the volume specified in the order so yeah this looks great I'll just copy this and come here I'll just write Mt Dot and then paste it like this one right let's do one more thing here which is comment so I'll come back here let's go back and I think there yeah yeah so comment often orders which is basically just for our reference so I'll just write python open position let's just write this one okay so okay so I have made a very silly mistake here it should not be equal to it should be a colon because it's a dictionary that's why every code requires a lot of testing guys noise happens so before running this I would like to actually initialize and authenticate mt5 as well so in mt5 dot initialize it will be true here and then this is the way we authenticated it's true here again we run this particular cell which is this we have made the entire dictionary we have action which is empty dot trade action deal we have a symbol BTCUSD the price is ask price right this is the stop loss trade take profit the order type here is buy so we'll have a buy position here in just one minute and then other things are like this one so let's do one thing now let's come to our documentation again if you scroll down a bit this is the code and over here you'll see this line which is actually using the order send function so I'll just copy it and paste it here result is equal to mt5 I'll just change mt5 to empty because I have imported MetaTrader5 as empty dot Auto send and then it has request inside this which is a dictionary after running this particular cell there should be an order which should be placed here on the right hand side so let's see let me just run this cell and you can see on the right hand side there is an order which is placed here so this is how when actually send the orders from python to MetaTrader5 but the more important thing here is that it's very manual or like whatever I have done it's very manual I have to make it automatically it so the only way to do is is to make functions to to make functions of create order in close order but before that let's see how to close this particular order right so in this case what I'm going to do here is I'll just copy and paste it here like this one right the action is the same only symbol is also same price as empty dot symbol infotech and it will be bid because we will take an opposite position here the type should be order type cell now right because again we are closing the position we don't need to provide the stop loss and take profit because look it's a close position volume is same only the time and filling type is same comment would be python close position and the the next input which is which is position if you remember I talked about it while showing it here so if you see there is an input here which is of position so in this right position and then here I need to provide the position which is basically the order ID so you can copy the order ID from here which is ticket the other way to do it is by using the position get or position total function here so position get is it will actually give us a tuple if we just select the first one right it will give you like this one and we can actually get it in dictionary format so I'll just write as dict and then provide parenthesis the entire thing is in a dictionary format this is the ticket this is the identifier which actually I'm interested in so I'll just write pick it now ticket yeah it's a ticket so this is the order ID which I would like to cancel so whichever which I would like to close so I just provided over there right so this is the new request now I'll come again here let's first run this particular thing and then after running this this position should be closed so let's run this thing and you will see that the already existing buy or a long position is now closed over there as well right so again I'll repeat this is very manual so we need to automate it so let's do it by making functions just before that guys I have a very important information to share recently I launched a course on python for financial programming so if you are trying to Learn Python for finance from scratch that this is something that you might be interested in just go to my website which is FB rithvik. com python over here you will see two buttons Indian candidates non-Indian candidates so you can click as per your location it will redirect you to this the main website where the entire course information is available you'll see that there are five sessions 10 chapters 22 videos 10 quizzes and three assignments with two live projects and this is the entire syllabus of this course for all the people who are interested to Learn Python from scratch and who are struggling learning python on their own I highly recommend it because it's very very affordable and I have covered a lot of things from a very very simplistic eyesight and in the end in session four and five you'll see stock analysis and Investment Portfolio creation projects which are my personal projects we did it using some financial analysis if you use rtk 40 code you'll get an instant 40 discount which is only available for the first hundred candidates so yes guys I'll see you in the course let's come back to our code so I'll just copy this right and paste it here which is creating order this is the first one and second one would be closing the orders I'll just write it here closing the positions and here there's a request I'll just write def which is how we I would say create a functions here so create orders and inside this I will provide things like ticker quantity order type price stop loss and take profit a colon and then I will up indent everything like this one so the action is the same only the ticker the symbol is BTC USD in this case you can automate this as well but yeah let's keep it like this only is of now the price is something that we would like to make here so what I'll do here is I'll just write Buy price is equal to this one so this is the buy Price Right sell price is equal to bid of this entire thing suppose the stop loss in percent is say five percent right and take profit n percent is say 10 percent so the buy stop loss level would be at Buy price multiply by simple calculation which is 1 minus SL PCT right same goes with the buy take profit as well which is Buy price multiply by 1 plus tp_pct right and then the similar things for cell as well sell stop loss is this it will be sell price multiplied by it is opposite which is 1 plus sl_pct and the cell take profit is equal equal to sell price multiplied by 1 minus take profit percent so these are the different things that we would like to call in the function here by order is equal to this one and then cell order is equal to cell just write ticker here and provide BDC USD I will have to provide quantity as well here which is 0. 01 and this will be q d y the type would be order type price would be this one because I will provide it inside the function inputs only so I'll just change it like this one PP is equal to TP order type I have already provided qty thicker is already provided and then other things like this one right let's run this and let's create some order so creating the order the first thing is sticker is thicker only because like I'll just keep it like this the second is qty which is also qty the third thing is order type which is equal to buy order right the fourth thing is price which is equal to buy price then another the second last thing is SL is equal to by SL and then comma TP is equal to by TP right if I run this particular function I should be getting a buy order here but yeah before that I will actually copy this particular thing because this is how we'll be sending the order over here and then let's print result right let's run this cell and then run this particular seller as well you'll see on the right hand side there's a buy order which is placed here if we change some of these things and let's make it for cell as well so I will change the order type from buy order to sell order the price from Buy price to sell price stop loss from buy stop loss to sell stop loss in the last one as well as sell take profit if you run this you'll see a sell position as well on the right hand side so yeah this is how we have actually created a function to buy and sell Bitcoin now let's do the same thing for closing as well so I'll just copy and paste it here this is the request which we have already used and so D def close order and inside this let's provide thicker quantity order type and the last thing is price right I will indent this as well these two things are exactly the same so I'll just copy and paste it like this right now here what different things needs to be checked changed so this is the ticker this one is price order type cell is basically order type right volume is quantity these two things Remains the Same and then python closed position is also the same the position is again something that we can get automatically right so you yes that's it so let's run this particular cell and let me just first close both these positions manually and let's one I would say create an another order here so it's a cell order which is placed here now I would like to close this order automatically from here so guys by the way if you are not writing this code with me or if you're if you're finding any difficulty like while writing the code it's absolutely fine I will save this particular code in my Google Drve and I'll tell you how you can become my Google Drve Community member just wait for some time but before that let's finish this thing so this is the close order I'll just copy and paste it here I need to change this so Quant article is the same quantity is same order type would be sell order type and price should be sell price if I run this cell there's a problem here so I think I'll just write cell okay this cell order and the price is I think again the same thing sell in price if I run this cell you will see nothing happened why nothing happened because there is a there's a cell order which is already placed over there will have to make it a buy order to I would say cancel or to to close this position so it should be Buy price and then the buy order and then the Buy price if I run this cell now this version will get so yeah this is how we can actually create some basic functions on creating and closing orders and you can see and you've already seen that how we I use this I would say very basic functions to create Buy sell orders and close those positions in the mt5 platform on the right hand side okay so how to become my Google Drve Community member but yeah before that guys if you liked even one thing about this particular video just click on the like button down below because it's really really helpful for the algorithm and also click on the Subscribe button because like these are simple two clicks for you but it really matters a lot of a lot for me to grow on YouTube because it requires a lot of efforts to make these videos and just like requesting these two clicks from your site and also for freelancing inquiries just write an email on this particular email address and I'll reach out to you as soon as possible how to become a Google Drve Community member guys just go to my YouTube channel and then over there just click on that access to my Google Drve button and then just follow some steps that are present over there you'll see that it's very very affordable to join my Google Drve Community it costs really less than a third of a coffee price and I'm charging even this small amount of money because I hired a couple of interns to help me provide the best possible material and services to my Google Drve Community member and I will save this particular code on that particular Google Drve and not only this code there are many more codes and study materials that are saved on that particular Google Drve which includes my trading bot some Pine script trading strategies etc etc so and yes guys the link of my course is in the description box I highly recommend you to check that course because it will definitely help you grow your career by 10x thank you so much for watching this video and this entire playlist guys you can click here to subscribe to my YouTube channel over there you'll see this entire playlist on mt5 and over there you'll see one of my most liked Pine script trading strategies which I uploaded a few weeks ago and until then and until then PEACE!