Code a simple Trading Robot | Trading with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video i will show you how to code the training robot with python and how to run it through the matri5 platform this spring robot uses the simple wing average crossover it buys when the candle closes above the moving average and sells when account closes below the moving average on this chart we can see the price on your usd the blue line represents the simple wing average when the candle closes above the blue line it is a buy signal when a candle closes below the blue line it is a cell signal this is the code to create the simple moving average crossover strategy we import material 5 pandas date time and time for this code to make our live easier i've created functions such as market order which takes the symbol volume and order type as parameters and i work with the microfile library where i pass this request dictionary to order sent so empty five or send will send my request to the platform and executes the trade instead of having to type this lines or these lines of codes every time writing a single line with market order will save a lot of time similarly we have close order where we just have to specify the ticket of our open position and it will close the order so what this function does is it looks through all our positions and if there is a position that contains this ticket id that's passes parameter it will then create this request dictionary and pass it to order send to close this position otherwise if this position does not exist it will return ticket does not exist also i've created a function to get the exposure for certain symbol so if i want to know how many lots i've opened at the current moment on eurosd let's say five lots it will return five lots to get the exposure we are using empty five dot positions get and we only want to get positions for the symbols that are open and then if there are any positions i'm using panas data frame to go through the volume column and i'm summing up the exposure then we have the signal function that will look for training signals so here we have the simple logic for moving average crossover and what we need for this function is a symbol time frame and sma period to get the simple moving average we first need the candlestick data or the bar data for that i'm using md5 dot copyrights from pause and this returns a number of candles based on the position so it returns the candlestick data for this symbol this time frame and we're starting with the candle position one and then here's the number of cancelled i want to request and here we need the number uh equal to the sma period then i'm passing this data into panas data frame and from that i'm calculating the last close of the bar for that we take the bars data frame and then locating the last row of this data frame and taking the close value for the sma we take the close all the close values and calculate the average value with mean now to create the signal variable we create a velcro direction and first initially i'm assigning the value flat for direction but if the last close price is greater than the sma or a simple moving average the direction is by otherwise if last course price is below the simple moving average this is cell and this function will return the last closed price sma in the direction these are all the functions we need to create our strategy so now let's go to our if name equals main block and here in this block we are defining the settings of our strategy so the symbol for strategy is usd and i've decided to trade with a volume of one lot for a time frame just for testing purposes i'm using mt5.time frame m1 which is the m1 time frame but we can adjust the time frames and the sma period here based on our liking and preference so if you're a swing trader you can choose any higher time frames that you want lastly we have a deviation parameter that specifies the maximum slippage on the symbol we want to receive when trading now to connect our platform use mt5 to initialize and we are now running a loop to run the strategy inside this loop we check for a current exposure on the symbol and we also want to calculate the data which is the last clause as a main direction these are functions that we've defined previously up here here and here and they will be necessary to run the strategy so here's a train logic so if direction is equal to buy remember direction is generated by our signal function so if our signal tells us to buy we then look for any open positions that are short so we want to close all short positions and to close all short positions we use empty five positions get and if any of those position is a cell order and to check if the position cell mt5 assigns the type attribute to one that represents a cell order so if uh deposit order we want to close the order and for that we use the closed order function and pass the ticket number of that position remember close order is the function that we've created up here so instead of writing this entire code we can just write one line and it will close our position also if there are no open positions right so if the signals buy and we have no open positions i also want to create a buy order so i will use the function market order that we've defined here up here and if there are no open positions then this market order order will open a position on the symbol that we've defined which is usd with the volume 1.0 and with the direction which is by in this case the same logic applies to sell orders just the other way around so if it's a cell you want to close all by positions and if there are no positions open we want to create a market order that will sell lastly we have some print statements to see what the square is doing so we check the current time the exposure last close the sma and the signals in which direction we should buy or sell and also i've added uh the sleep function so i want to update the strategy every one second and this is it so let's see how to add the strategy to the metroid five platform and run this when your code is ready login to mt5 training account in my case i'm logged in to ic markets sc demo and to start a strategy make sure that algo trading is enabled and now to run a script we simply open our command prompt and go to the location where our python file is located in my case it is locating trigger pi algo trading and the command to run the strategy is python and then the file name which is sma crossover one dot pi so when you run this now we see that this rate is running and we are looping to check for new signals and if you've noticed we have a new position open which is short because currently price is below the simple moving average this is it for a video please like and subscribe if you've enjoyed it if you have any training ideas or strategies to be coded in python please add them to the comments section and i'll review it to make a potential video thank you for watching you
Info
Channel: TraderPy - Algorithmic Trading
Views: 677
Rating: undefined out of 5
Keywords: traderpy, trading, python, algo
Id: 4kdR6Ixt51U
Channel Id: undefined
Length: 9min 7sec (547 seconds)
Published: Fri Oct 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.