Build Algorithmic Trading Strategies with Python & ZeroMQ: Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so let's start writing function the function is you will recall as a worker function to a traders thread and it's called trader and we pass it two arguments a symbol and the maximum number of trades so let's start off by doing that we'll say def underscore trader underscore passing the symbol as well as the max trades and following that we need to start writing some logic so inside here first of all we're going to need to construct an order because that's the basis of anything here so we'll start off by calling a default order and we'll make use of the helper function we have inside our 0 mq connector object called generate default order dict and retrieve the basic structure of that order that we need and then modify it accordingly to suit our to suit this coin flip trading strategy that we have now inside this default order I need to specify a symbol then that symbol is specified as the key underscore symbol inside this dictionary and that will be the first component of our tupple which is index 0 and finally we also need lots and that is specified by the key lots and that will be the second component of our tupple index 1 so now we have the symbol and we have the lot size it we also want to set a stop loss for this we will just make it trivial so we'll say a default order have its stop loss set to the same as its take profit which is equal to 10 pips in our case so default our stop loss equals default order take profit both equal a hundred points which is 10pips in currencies the currencies that we are using and then finally we also want to set some way of identifying the trader and for this purpose for this example we'll use the default order each orders comment field has a way of identifying the trader behind that trait and we'll set this to something where we specify the symbol and something else tagged along with it for us to be able to pull that information later on when we want to check each one when each trader wants to check which of their respective trades has gone past five seconds so we've now constructed our order and our order contains this information the symbol the lot size the take profit and stop-loss in terms of points and the comment and as a means to identify the trader the next thing we want to do is while the market is open there's a variable inside this class called market open self dart market underscore open while that is set to true we want to do a few things we want to firstly acquire the lock we were talking about so the lock will be acquired as follows we say self dot underscore lock is what we defined it as and self door lock dot acquire so we've acquired a lock which means we now have this thread will now access the 0nq object solely without sort of coinciding with other threads trying to access it at the same time the first thing we want to do is set up the open trades scenario so the first thing we want to do is get all open trades and that's done by executing the get open trades instruction inside the reporting object that we created earlier so we'll call this underscore ot as an open trades equals self equals soft underscore reporting that underscore get open trades and inside that remember we're using the comment field of each instruction to serve as an ID in this example an ID to identify the individual trader so we're going to get open trades where only this value is found in the comment field of the trades so here we'll say get me all trades that have the comment symbol underscore trader and once you've done that I'd also like to specify a delay as I've specified here in my class and my delay it was 0.1 seconds and a W break which is essentially a multiplier a factor this doesn't necessarily have to be in this way you could just specify a weight in seconds I've done this for convenience as future tutorials will make use of it so I get open trades traded our format symbol self delay 10 so now we have open trades so we also want to do something here just as a precaution if the trades returned any response that wasn't valid we want to go back to the beginning of the loop again and make sure we come back and do this thing again because without open trades we are unsure if there is a trade in execution so in this case we'll say if self dot underscores @mq dot underscore valid response we'll pass it the variables item q strings item Q which sorry you know it said in Q underscore ot as we defined it earlier and if that response is false then we're going to go back to the beginning of this while loop and that's it now we have our open trades just write a little comment here to get all open trades so if we get past this point in our script we have now received a data frame inside the underscore OT object and we're ready to do something with it each data frame return could have one or more trades now we want to check for each trade in the index in execution whether each trade has been in execution for five seconds or more the reason I'm using a for loop here is because only for this example or are we choosing one as the maximum trade therefore by using a for loop here I'm adding the flexibility of using a larger number of trades later on in future examples now what I need to do here is the first thing is to close any trades that have been running for five seconds or more in order for me to do that I need to go into each trade and see if it's open time as returned my Metatrader and the current time differ by five seconds or more and to be able to do that I have to say if the absolute value of the time Delta between the between the current time which is to underscore date/time now plus the broker offset which I have to consider because my time is UTC in the brokers time is GMT plus three so I have to do the broker GMT calculation over here this time Delta here in hours - the open time returned to us by Metatrader so over here I'll just switch to a new line because that's getting quite a bit of stuff on one line so we'll say underscore ot dot at the index that my loop is currently at and the string key that Metatrader will have returned containing this information is underscore open underscore time dot total underscore seconds is greater than or equal to greater than sorry the closed Delta that we specified earlier and I want to get rid of this and there's a little mistake here I have to add the offset and that's pretty much it so we've got our absolute calculation done we've got absolute time Delta current time plus the time Delta to account for the brokers GMT offset we subtract the open time sent back to us by metatrader4 this ticket calculate the total seconds from this time delta and if it's greater the absolute value is greater than the closed Delta that we've specified then we need to close this trade so for us to do that we need to say underscore ret as a new variable we're going to create and this will hold the response from the execution we're now going to do which is to execute a closed trade the syntax of the execute function if you just go back to the execution script here it expects an execution dictionary object verbosity delay and a w break so what we're going to do is we're going to construct a little dictionary over here and inside this dictionary we're going to provide a few values we're going to perform the ACK provide the action that weather trader needs to perform which is close we're going to follow that up with the ticket number that meta trader needs to know in order for it to process the close and then a ticket number in our case corresponds to the index we are currently at the data frame is returned with indices corresponding indices built from ticket numbers and finally we're going to send the comment back to were also going to include the comment for us to identify this trader and that comment as we did before was the symbol name underscore trader and we're going to add in the symbol information over here which is underscore symbol the first component as you remember of our symbol being that and that's pretty much it so we're going to get rid of or up until here we don't need this over here finally we also need to supply the verbosity information that we want which in our case is self underscore verbose passed in earlier is false we also need to provide the delay that we passed a 0.1 and the W break value of ten and that's it so our close instruction is complete it's going to push all of this over there to keep things nice and neat and finally once we've done this once again as before we want to make sure that we have a response to deal with and if we don't we want to break out of this loop so we're going to ask zmq to tell us if this is a valid response in the past underscore read to it and if that value is false then we need to break out of this loop and finally if we've got past this point we have a valid response we just want to sleep for another 0.1 seconds before we proceed all right so we've now covered the opening trades but we've got the closing trades bit sorted out and now we need to do the final bit where if we've done all of this and the data frame above ot has trade less than or equal to our max trades which in our case right now because we've set max trades to one it has to have no trades in it so the next thing we need to do is check if there are any trades in the data frame at all nor if basically the shape of the data frame in terms of rows is less than our max trades and if it is we need to execute a new order so bearing that in mind let's go ahead and complete the last part of our worker function here which is that well we've got pass the point we've got the open trades we've checked that the open trades are valid we've then checked for any trades that have gone over our closed Delta which is 5 seconds and if we found any such trades we've issued a closed instruction for that ticket for that trader for that symbol and then gone ahead to see if we can open any more trades so in this section we'll just say open trades and over here we're going to basically first check if the shape of the data frame that we were passed from the theorem key functions above is less than our max trades which was specified earlier as 1 and if that is the case then we are now it is time to open a new trade because if our max trades has not been breached we are we are able to open a new trade so the first thing we need to do we've already defined the default order that we want to use we want the trader to trade his own symbol with a fixed lot size with a fixed take profit of 10 pips and of course the ID being the trades comment so the only thing for us to change really is the is to randomize the buy or sell decision and to do that we're going to make use of the random dot get round bits function we talked about earlier on in the tutorial and for us to change the type of the order we have to insert the string key underscore type and set that to 0 or 1 for metatrader to infer as opie underscore buy or OB underscore sell and what we'll do here is simply ask the random module get I get ran bit's function pass at a value of 1 which will return either a 0 or a 1 so once we have this this is our let's call that our decision buy or sell once we have that we need it's a simple case of going ahead and executing the trade now with all these parameters set so once again we'll go to our execution object will find the execute function and we'll pass it the default order that we've constructed so far and the rest of the variables arguments necessary in this case the verbosity that we'd like the delay that we'd like to use and the W break that will become more known in future tutorials as I use it more so that's it we've sent an instruction for a trade to be executed given the parameters we've put together and finally once again to stay on the safe side we want to make sure that we've received a valid response to our execution and for this to happen we need to ask the 0 mq connector to double-check our response and if our response returns of false then we have to break out of the loop and go back and do it again lastly we've been in a try-catch try except try finally scenario here so we need to complete that code and for us to do that we simply need to type in finally over here remember we acquired the lock at the very beginning for this thread to make use of the resources so we're now going to release that lock so other threads can resume using these resources and we simply say lockdown underscore release and that's it so we've now released the lock that will allow other traders to start using the lock and we're also going to sleep for another 0.1 seconds here before we go back to the beginning of the while loop and that will be specified with the delay so that's it so we have now completed the trader the trader is a worker function and it's now going to be a case of running this script and checking for any bugs and getting on with it so let's proceed I've maximized my screen and open up my buy Python console so we can run the script make sure there aren't any bugs that remain to be fixed of course I've tested all the initial components the preliminary code that's been inserted into this prior to mean writing the trader function here so we'll just run the script now and make sure everything's in order and it is a fairly basic code here in the trader script so remember we have a maximum number of trades and the maximum number of trades set to 1 each trader will trade his own unique symbol will trade a fixed lot size as specified at the beginning and there is an update that's going to give us a live view of what's passing through the serum q threads at the present time and that's pretty much it we're going to open for if we're going to check for open trades for this particular each trader will check for his own particular trades in this whole threading context if a trade and then we'll cycle through the trades and if one of the if any of the trades are have been an execution longer then closed Delta which was set to 5 seconds and remember that the choice of 5 seconds is purely for demonstration purposes a trade that lasts 5 seconds in FX is usually not going to not going to do very much for you so this is purely for demonstration so we can have some activity in meta trader once once we switch over to my terminal there to see how this is going we execute a closed trade when that closed Delta is breached and that closed trade contains the closed instruction to Metatrader by your mq the ticket number and the traders ID can then said we check for a valid response and then we sleep a little bit before we go to the next one finally if our data frame that was returned by a gate open trades has trades a number of trades less than max trades then we are able to generate more instructions and send more trades to minute rater until we meet our max trades ceiling and this in this example the max rate ceiling is 1 so we execute a trade here we send the default order for which we randomized the buy or sell decision using random target ran bits and we wait for a response from Metatrader and finally we release the lock and let other traders take over the resources and do their thing - and that's it so we run the script everything seems to be working fine now we have to initialize this trading strategy with our number of traders here and go to Metatrader to see how all of this is happening so let's switch over to Metatrader now where we need to go load up our 0 mq server EA on to Metatrader let's load that up let's make sure live trading is permitted DLL imports are permitted everything is permitted that's fine 0.01 maximum lot size is fine too because we've only set those as the fixed lot sizes for each of our traders with each of their unique symbols and that's it we click on ok just quickly have a little browse of the experts and journal tab to see everything's loaded up ok and that's pretty much it's an hour in a position to execute our universe of traders I'm going to pull this order terminal here a bit further up because we'll have a few trades going back and forth here and that's it so we're ready to we're ready to execute we just create a test now call it a test and the name of our strategy our class here is coin flip traders as you saw earlier we'll pass all the defaults we won't change anything because all the defaults I've set up are exactly as we need them to be and that's it so those two messages are from the zero meu connector object that's initialized in the background through the execution through the strategy base class and that's how we're ready to run this so we've overridden the underscore run function here with our multiple traders each trader having one symbol and one lot size combination and all we need to do here is to execute so we go one two three four five six seven eight nine we've got nine traders who've gone into the market our live updates are coming through from Metatrader responding with keys and all the other information it's been done as an example so it's not the neatest you could of course transform this into a real-time up eating pandas dataframe there would be a lot nicer let's go have a look at how this is happening in Metatrader so now all the traders that we had earlier on specified with the symbol underscore trader ID using the comment field of each trade are executing trades one at a time just sort this column so we see that each trade is being executed once at any given time each trade is lasting five seconds as we specified earlier so let's have a look at the account history the lot size is fixed at 0.01 and see how they or the trading decisions are all randomized here so we've got byes happening cells happening completely randomly and naturally because this is such a good trading strategy we're losing money hand over fist the point of this exercise was not to demonstrate profitability but more to demonstrate how you can create a trading strategy using 0 mq and that are NX 0 mq connector and some additional helper classes that I've created for you as we discussed earlier on today the strategy based class that that uses execution and reporting classes and those execution and reporting classes we're making use of the 0 mq connector and making some helping and helpful wrapper functions in here to allow us to execute open closed trades open and close trades and also get open trades of course you can extend this to getting closed trades historical data all sorts of things and that that is the idea behind creating these sort of base level classes for you so that in future tutorials we can extend these as we go along and put together more interesting strategies perhaps maybe even some that perform well compared to this one which is purely for demonstration say I can show you that it is indeed possible for an algorithmic trailer using an external programming language that is not MQL to bridge the gap between that external programming environment and darwin x liquidity by using the darwin x 0 mq connector bridge incorporating it into their code base it's all open source you can modify the features of the mq l server EA as well as the app the Python application the client application to suit your purposes you don't have to use the code as is your absolutely perfectly fine to incorporate that code into your existing strategies however you choose but this example should hopefully demonstrate that it is indeed fairly straightforward to put together a trading strategy using this connector and now in future tutorials what we will do is create more slightly more interesting trading strategies possibly in more complex using some using indicators using in some places interesting machine learning concepts applied to time series both short and long term strategies and stuff like that so we'll have a fair bit of fun doing it but the important thing is that we'll educate ourselves in in several things how to obviously develop algorithmic trading strategies most importantly how to use 0 mq to bridge the the Metatrader gap that many algorithmic traders experienced by not having MQL knowledge or not wanting to have MQL knowledge both of those things are perfectly fine and also of course to explore software engineering concepts involved in developing algorithmic trading strategies for instance we went through a few just now very basic ones in our in our basic strategy here where we were checking for responses and validating responses and choosing when to break out of a while loop and those sorts of basic but important software engineering principles come into play and now trading as well so hopefully this would be an education in a few concepts ranging from trading to software engineering in future tutorials if you've enjoyed this tutorial I know you have enjoyed the trading strategies performance because that was not the point but still it would be nice to see some grain and hopefully we might in future tutorials but if you've enjoyed this and you think this is helpful to you and helpful to other colleagues people in your network who you feel would find this useful please do share this tutorial video with them we're working very hard to make solutions to create further solutions to allow algorithmic traders to trade as comfortably as possible and leverage Darwin X all its features tools and liquidity to their advantage thank you very much for watching see you in the next tutorial
Info
Channel: Darwinex
Views: 19,272
Rating: undefined out of 5
Keywords: Forex Trading, FX trading, Investing, DARWINs, Darwinex, Investment, Risk, Hedge fund, algorithmic trading, python, zeromq, algo trading, CFD trading, metatrader 4, metatrader 5, metatrader, mt4, mt5
Id: VtOfF-nhhj8
Channel Id: undefined
Length: 25min 48sec (1548 seconds)
Published: Fri Mar 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.