Build Algorithmic Trading Strategies with Python & ZeroMQ: Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
right so up until this point in time if you've been following the content as regards Python 0 mq and metatrader4 then we've arrived at the point where we are going to develop a trading strategy using all the code we have so in previous tutorials blog posts etc we've developed Python client application necessary for doing the executions we've got a metatrader4 server EA that is responsible for receiving instructions from client applications in our case the Python application processing them parsing them making sure they are valid executing them and sending the response from that execution back to Python really simple so we've got we've established the necessary code and framework to perform this activity work we're at that point now so now we have to take all of this and make something that performs a function that performs trading actions so enough with the theory enough with all the hey we need to do this hey we need to do that let's get to the meat of it put all of this together into a trading strategy first let's discuss what we need in order to do that we're going to come up with the trading strategy that's rather simple the famous the infamous coin flip example that everybody loves to do we're going to follow the cliche and do it ourselves so here we're going to design a coin flip trading strategy we'll write the code for this shortly as well using all the other core parts that we've written so far Python and MQL so in this coin flip trading strategy we want to achieve a few things we want to have a few traders with the ability to trade and we want let's say a few traders and each trader will be assigned one symbol gets one symbol to trade ok and we're not going to do any dynamic lot sizing in this example that will be in future future tutorials or will develop as we go along from this introduction onwards we're going to develop increasingly complex strategies with various different types of rationale in them but for this first example to demonstrate how you actually put a strategy together in code we're going to do a very simple example to get the fundamentals right so coin flip trading is going to involve a trader who gets one symbol to trade we're going to fix the lot size for every trader to 0.01 they're going to send an order send it trade instruction with lot size set to 0.01 there's also another restriction that the max number of trades that each trader can trade is equal to one so no trader can trade more than one trade at any given time and since we want to make it slightly interactive we want to get things going really fast and see the activity on Metatrader instead of waiting for the next trade signal to come through for executions to happen the coin flip example is quite quite appropriate for that sort of interactive fun setting mind you this this coin flip trading example will result in a losing strategy so the point of this tutorial is not tutorial is not generate a winning strategy is to demonstrate how to create a strategy in the first place using the Python 0 mq connector and the corresponding mqr bridge so max number of trades set to 1 lot size set to 0.01 and that's pretty much it and the choice buy or sell will literally be a coin flip and in Python we're going to achieve this by you by importing the random module and inside random we have a function called get Rand bits and we're going to pass it the number one which will send us an output of either zero or one and this zero one we will use as our buy or so and that's a really simple coin flip trading example where a trader gets to have one symbol to trade with one trade at any given time and because we want to make it an interactive and and sort of things flying around on the screen a little bit as I mentioned earlier we're going to set a little deadline and expiry for each trade so we're going to say that not only should the trader trade only one trade at any given time the trader needs to close the trade after five seconds in execution of the trade after five seconds so that gives us quite a few things flying around the screen so you'll have trades going into military their trades coming out of Metatrader open-close open-close and it'll give us a good sort of interactive example of how this is working it also give us an idea of the latency involved between instructions from an external application like a Python trading strategy the Metatrader vias your mq so there's a reasoning there's there's a reason behind my choice for for these sorts of variables here using one trade per person and setting things to five seconds and no longer etc etc etc so for all of this to happen we need to we need to be able to write a few sort of helper scripts I've already written those for you and those helper scripts will basically take care of some of the nitty-gritty involved so when you're looking for a response you want to parse the response you want to wait for meta trader meta trader to have sent you the response and all those sorts of things I've already taken care of that code for you but in a nutshell the things that we're going to study as a result of writing this trading strategy are we're going to be able to use the get open trades functionality provided in the 0 mq connector and we'll be able to send instructions so send trades to Metatrader and receive responses from Metatrader so it'll allow us to study all of this laser the barebone is the meat of the entire framework so what better way to learn how to build one than this everything gets used that's of importance and the at a fundamental level everything's fast we've got trades closing after five seconds you got random by ourselves going on max number of trades to one a lot so I set to 0.01 and a straight of getting one symbol now when I say each trader we're going to select eight or nine symbols therefore we will have eight or nine traders accessing Metatrader through our trading strategy that should be fun so that should with a trade closing at five seconds and eight or nine trader sending eight or nine unique symbols to meta trader using this fun trading strategy we should get quite a bit of information as to well what kind of load can we send what does it all look like in practice so now that we've discussed all of this let's go straight to the code and then we'll start writing our strategy so the first thing we want to take a look at is where 0m cues output where where meta traders output back to Python will be stored once it is received and that is inside a variable called thread data output now this internal variable internal class variable holds any information that is received from meta trader by python and this storage happens because there is a thread running with our worker function DW x z mq pole data for which you can read the code in this script it's available on github and this pole data essentially performs two functions is continuously pulls the appropriate socket for information be it in response to trading instructions or in response to market subscriptions so because in in this example we're going to be trading only we're going to be looking at the thread data output polling for information from variable inside our trading strategy so when we send an instruction to Metatrader that instruction will be in the form of a JSON object that JSON object will get parsed by Metatrader a meta trader will then send a response back and the Python application the client will store this information in this variable so the first thing as a result is for us to write a few functions based on what we discussed earlier so we're going to be sending trades we're going to be receiving responses and we're going to be checking for how many trades are open because one of the stipulations in our example coin flip trading strategy here is to be able to hold one trade at a time and also close the trades after five seconds naturally for those two things for for us to be able to do those two things we need to know how many trades are currently open and we also need to know they're open times but those in those pieces of information will be sent to us by meta trader in response to an execution so we need a script to be able to read them the zmq execution script is the first one that houses one function at the present time to help you expand on this in future and that a function is called execute in this function what I've currently done is at the time of recording this tutorial on the 15th of March 2019 I've written one function that allows you to open and close a trade and you can expand on this you can completely change this you can add more functions to this class I will share this on github as soon as this tutorial is complete now in this function execute we when we we can send a new trade instruction and we can close a trade by a ticket so for us to be able to close a trade buy a ticket number we're going to need the ticket number and and the only way to retrieve that ticket number is by sending a get open trades instruction to Metatrader via CRM q this therefore presents the need for us to have that functionality so in the second class I've prepared for you which is also in its initial stages and is completely extensible we can do absolutely whatever you want with it also to be shared on github after this tutorial is dwx zmq reporting and inside this I've implemented a wrapper function for the get open trades dwx MTX get all open trades function available in the 0 mq connector python application that you already have access to and in this function what I'm essentially doing is I am sending an instruction to get all open trades and then there's a few lines on waiting for the response and if there is a certain timeout that's exceeded then you have to break out of waiting for the response etcetera I remember this is trading so we have to be careful how we design things if we are going to take a strategy live these sorts of checks and balances are important to make sure that you never get yourself into a situation you cannot get out of especially if it's a trade you're sending imagine looping through a trade over and over and over again if you're constantly stuck in a loop like this that would be a very bad move so these sorts of checks allow us to get out of those situations and finally if we receive a valid response then we check if there are any trades in that response and if there are trades in that response we construct a data frame and we send that pandas dataframe back to the calling application the calling application is our trading strategy that we haven't yet written so we're going to write that now so we have get open trades inside z w DW x z mq reporting that's the first class and we have the execute function which is inside DW x z mq execution the second class the two modules that i've prepared that serve as wrappers over the 0 mq connector python application which will communicate with the DW x 0 mq server Metatrader EA slash application to put all of this together i've also prepared a wrapper for our wrappers which is a base class didim DW x z mq strategy and this class is extensible you can you can expand on this and pull this into any trading strategy where you can use the basic initialized variables to your advantage this is purely an example you can modify this as you like but for our purposes I've created a base class where this class has a the strategy has a name it has a list of tuples and each tuple contains a symbol and the corresponding lot size this is what is used for this particular introductory example this isn't at all an efficient way to hold information regarding symbols and lot sizes that should that in future tutorials we will be doing completely different ways this in a completely different way but for now to do our coin flip trading example we'll pass in a list of tuples containing a currency pair and the associated fixed lot size that we would like to use with that and we'll also pass in the brokerage gmt offset this is important because if for instance my environment is in UTC and when I send a trading instruction if I were to save my time then that time will not correspond to the opening time of the instruction on Metatrader because Metatrader is open times correspond to the gmt offset on the broker and in this case my darwin x gmt offset darwin X is gmt offset is 3 at the present time 15 the March 2019 from UTC so I have to enter 3 so that anytime I check for that 5 second time out I need to make sure that I'm adding these 3 hours to my current UTC time in order to check correctly because the x returned by Metatrader will have the GMT plus 3 offset and I've added a flag from Robo City in case you want to see all the output coming through 0 mq which I assure you will be a lot adjacent on your screen text JSON text on your screen for me the default option is false next we have the assignment of the name symbols and brokerage empty to internal class variables and we will have 1 0 mq connector shared by all our traders quote-unquote inside the coin flip trading strategy we're going to write here shortly this one 0 mq connector will be passing information to and from Metatrader then we have the two modules zmq execution and reporting and we're going to pass our singular 0 mq connector object to both of these so that they can send through their respective functions the end receive outputs from Metatrader and pass it back to our strategy object as and when we need lastly we have a run function and this run function is where we're going to enter our strategy logic I'm going to create a new strategy class and that class will essentially inherit all of these variables from the WX cmq strategy so I will override this function in my in my coin flip trading strategy class that I'm about to write next right so I've done the initial work here that can be replicated by ourselves in working on your own strategies using this formulation in the twx is that mq strategy base class or you can modify it to anything else you like but for the purposes of this tutorial I've written this basic initializer code that you can copy and use as you like and what we're essentially doing is passing in the symbols and the corresponding fixed plot sizes that we would like to trade we've passed a name called coin flip traders the delay represents the delay is a variable in seconds and represents the delay between each message we send to Metatrader which at 0.1 seconds the broker GMT is 3 and reverse' T is false because for me that would be too much text in one screen to talk about the code and have that text flying around on my screen at the same time so that's set to false the maximum trades are 1 and the closed Delta which I'll show you later on why I've put this here I'll be using this later in the in deciding the close is going to is is a number in seconds and then represents how many seconds to wait before closing the trade and that's it we pass in our variables we set up some initializers and this is as we're going to be executing five traders concurrently we're going to create a lock object here that is going to be acquired by the trader currently in execution when they want to use the 0 mq object that is universal to all traders that's the same zmq object that everybody will be accessing and that's pretty much it inside the run function for each symbol we find in symbols we create a thread whosever worker function has yet to be written of called itself type underscore trader this is the function where I'm going to be writing the logic that the trainer will follow and we're going to pass the trader to things we're going to pass the trader a symbol and the number of the maximum number of trades that that trader can execute at any given time we're also going to demonize the trader which means that the trader can run independently in the background and start that trader and that's pretty much it so that completes the traders bit and then finally we will have one more thread running and that thread will have its worker function as the updater and this updater function will also acquire the lock at some point in time I'm alongside the traders and get the current response from zero and Q to just give us a visual on what's currently going on what is being transacted except right this is by no means necessary it's just a trivial thing that we have here for the moment just to see what's actually crossing through in terms of JSON commands being send responses being received etc etc and we have a stop function and when you that's when you execute that function will set mark it open to false each thread as we will write here shortly in the trader or worker function will be doing it doing anything while Market open this set to true if we set it to false all threads will deactivate and once they've deactivated we will issue a join call to their respective threads to wait for them to finish and we'll also issue a joint call to the updater thread so that everybody goes home quote/unquote and finally we'll just execute a mask close all trades call through to 0 mq only for the purposes of this strategy you don't have to issue a close of course in any other strategy just because and that's it so now let's write the function that will do all the traders work
Info
Channel: Darwinex
Views: 70,078
Rating: undefined out of 5
Keywords: Forex Trading, FX trading, Investing, DARWINs, Darwinex, Investment, Risk, Hedge fund, algorithmic trading, python, zeromq, metatrader 4, metatrader 5, metatrader, mt4, mt5, algo trading
Id: 4MxjFTQHTfw
Channel Id: undefined
Length: 19min 31sec (1171 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.