Commodities Trading with MATLAB

hello and welcome to the commodities trading with MATLAB webinar I'm unshaven Mishra and I'm a product marketing manager here at mat works working with our computational finance products today I'm going to show you how to quickly develop and test commodities trading strategies in MATLAB we will cover importing data into MATLAB performing exploratory data analysis developing training strategies analyzing and testing strategy performance and finally connecting to an automated trading platform and sending orders here's a typical commodities trading workflow now commodities analysts tend to have a basic workflow they need to get price data from a variety of sources such as spreadsheets custom databases of professional data feeds once they obtain the data they will need to analyze and study its properties model and isolate some of its behavior and finally develop a complete algorithm or strategy having completed the analysis or algorithm they might need to share it in the form of reports standalone applications or even deploy the algorithm in production by connecting to a trading platform for instance and sending trade orders through the course of this webinar we will bring to your attention how MATLAB and its associated products can be used to address the challenges faced in maintaining this entire workflow so what are some of the challenges that come out these analysts and traders face one of the biggest challenges is data management financial data can be maintained and accessed in a variety of forms and sources it's a tough task to be able to access this data uniformly and to make sure it's aligned to the same date and time as well as cleaned up the next roadblock is the ability to explore data easily in order to come up with new strategies at this stage interactive data exploration is crucial to come up with new ideas in the field of strategy back-testing analysts often face challenges with their ability to extend modify or create custom and ethics and metrics that are more appropriate for the task finally once the strategy has been back tested one needs to be able to integrate with trading platforms in order for the strategy to be useful with that in mind let's dig deeper into the data management challenge so what are some of the challenges that analysts face in managing data the biggest issue is managing multiple sources of data data can be obtained from spreadsheets databases or commercial data feeds such as thorns and Reuters Bloomberg or Yahoo Finance the second issue is cleaning up the data in case some of it is bad and aligning data from various sources together this is often a manual process that is managed by the analyst what we're going to do now is take a brief look at how MATLAB can help you combat these challenges let's switch to MATLAB now I have stored most of my commodity data in the form of spreadsheets in this folder let's try and import one of these into MATLAB reading this data into MATLAB is quite easy to do you can select the data you wish to import choose the type of variable that will hold the data and hit the import selection button MATLAB can generate code for you so it's easy to use this work later you can generate scripts and functions that you can call from within your own code now you can see here the data that we imported from the CSV file let's close this you can also import data from databases using the database Explorer app here I am going to import data from a commodity database that I have locally stored you can interact with the data create your own queries choose the kind of variable you want to store the result in and import your data to the MATLAB workspace as before MATLAB can generate sequel or MATLAB code for you in order to automate this process now that we have some commodity data in MATLAB let's try and open it up and see if we can enhance it with some custom metrics what I'm going to do now is compute the return for every day you can see here that matlab's vectorize math enabled us to compute the returns for every day in the data set let's close this and such a word back to the slide getting back to the data management challenges faced by analysts we can see the MATLAB enables you to manage each of these challenges efficiently and easily you can import from databases spreadsheets commercial data feeds you can align and clean your data using MATLAB vectorize math you can combine your own code with math labs auto-generated scripts and functions in order to automate this process as far as possible moving on what are some of the challenges that analysts face in analyzing the data that they have obtained they want to gain insight into the data they want to create new trading ideas and hypotheses they want to test the idea on the data that they have they want to tweak the analysis to make sure they have an optimal trading strategy once we're satisfied with the outcome they want to summarize report and otherwise share the results of their analysis with others now throughout the rest of this webinar I'm going to talk about how MATLAB enables the analyst to successfully face all of these challenges now I have created a very simple trading strategy that I'm going to talk about for the rest of the webinar here are some assumptions that I have made for instance this trading strategy only uses data for actual commodities not interest rates not equities or currencies but energy metals cranes livestock and soft commodities such as coffee cotton or sugar I have restricted my set of commodities to American exchange traded products this is just so I won't have to bother with timezone considerations in processing the data next I assume in this webinar that every position is fully collateralized in other words the margin that is put up is equivalent to the contract value and there is no leverage that is due to smaller margins I've also assumed that trading frictions are constant since positions are collateralized as profits increase and positions get larger this assumption becomes a stretch finally I have assumed that I get perfect fills at market open and market closed again as positions get larger this will become a problem that we can choose to ignore for now here are the commodities that I have chosen as you can see here I have tried to create a representative set of commodities that is fairly liquid I have five energy commodities three metals four grains four soft commodities livestock and the S&P 500 index now these commodities come from a variety of different data sources some of them are going to be read from spreadsheets some of them from databases and some of them from Yahoo Finance we keep track of all of these commodities in the form of a repository in this specific instance we use a simple excel sheet to store product specific metadata we need this into MATLAB so our program knows where to get the data from for each of these commodities again I have continuous contract data for the front once the next month and the fourth month this data is in the form of daily open high low and closed prices now before I get started I'd like to take a few minutes to talk about the different kinds of trading strategies that commodities analysts and traders use commodities markets can either be trending or sideways moving mean reversion strategies are often used to create sideways moving markets trend following strategies are often used to trade trending markets now trend following is an example of an absolute momentum strategy it's absolute because the momentum of each commodity is measured by its latest closing price with relation to its own historical prices without referring to any other commodity in contrast across sectional momentum strategy measures and ranks momentum across the basket of commodities we can then choose to buy or sell the strongest or weakest commodities depending upon how we design our strategy another popular form of trading strategy is a term structure trade in which traders can go long or short the nearest month and take the opposite position in a further month contract depending upon whether the commodity is in contango a backwardation with that in mind let's move on to MATLAB I have written a simple script here that imports a commodity metadata into MATLAB and then goes on to read each of the data sets from each of the different sources that I have aligns them to the same starting and ending dates and then divides them into training and testing data sets let's run the script let's take a closer look at what the script does in this section of the script MATLAB reads data from a variety of different sources such as CSV files databases or Yahoo Finance next it aligns the data to the same starting and ending dates now Mattala makes it really easy to write filtering functions now that I have daily open high low close data I need to convert this to weekly and monthly data for my analysis needs I run a custom function here that rolls up this data for all commodities in this given container finally I use custom filtering functions to split my original data set into a training set and a test set this training set extends from January 1999 to December 2005 the test set is from January 2006 to May 2013 we will use this test set later the tests of chosen trading strategy let's just verify that we have all the data within MATLAB as you can see here you can scroll through each of the different commodities and browse the data now that we have our data in MATLAB let's get our hands dirty let's view all the commodities together and see if we can make sense of the data we are now looking at all the commodities that we have data for so it's difficult to make much sense of this we can explore this figure a little bit let's zoom in into the top right corner here we observed that there is a tendency for certain commodities to move together let's see what each of these tri-series is this is brand crude oil this is heating oil this is gasoline and this is WTI crude oil so here we have Brent crude heating oil gasoline and WTI crude demonstrating high returns over historical time period let's look at some other groups let's see this group here similarly we have silver cold and copper moving together this should give us an idea let's filter the data into different sector wise groups and examine them I'm going to close this so here I'm going to slice our data by the type of commodity and now let's look at just the energy commodities it's immediately obvious that natural gas has behaved differently from the rest of the commodities over this period let's now see if we can gain insights into other commodity groups as well in the soft commodity group we notice that sugar has behaved differently from the others similarly among the metals copper seems to have reacted differently to the financial crisis and it's after-effects standard gold among the grains we can see wheat peaking earlier than the other grains so across the different groups you can see that each group reacted differently to the financial crisis some peaking before and some peaking after now commodity traders often use momentum based strategies such as trend following in order to trade the markets next we are now going to look at a basic example of this technique what I'm going to do here is implement a very simple trend following strategy this strategy will go long at the next week's open if this week's closed is higher than the highest high of the last few weeks of a look-back period we are going to measure how the strategy performs over different look-back periods across different commodities now this strategy might signal multiple trades for the period in question what we are going to do is average out the trade performance for each product in this space let's run the script we're going to load all the data into MATLAB and we are only going to look at the training set weekly data and only front one contracts now matlab's vectorize form of programming makes it quite easy and quick to analyze large amounts of historical data this is the main part of the script that manages the trend following strategy we set up the look-back window and the holding period window and the signal is computed as before as being true if this week's close is higher than the highest high over the look-back window in this figure we have plotted the system's performance over a hundred week holding period for all the commodities over different look-back windows we notice as the look-back window size increases that there is greater variation in performance now we can dig deeper by looking at how different commodity groups did overall let's look at energy commodities we see that the energy group as expected has performed really well now let's try this with some other groups such as metals metals have also performed decently but what about other groups such as greens are soft commodities let's look at soft commodities next you can see that soft commodities have not had a great performance with this trading strategy the next strategy that we're going to try will be a cross-sectional momentum strategy let's close this now there are many different ways to measure momentum we can measure momentum using the price change over a period of time we can measure the difference between a fast moving average and a slower moving average we can also measure how fast the moving average is changing in other words the instantaneous slope of the moving average now I have created three different momentum indicators rate of change moving average difference and moving average slope that can be used to capture each of these different facets of momentum now this is a simple strategy I developed that I call a momentum catch-up strategy what this does is for a particular group of commodities it measures momentum using any of the indicators I just mentioned over a look back window of a few months and then picks the one with the lowest momentum then it rates two months before going long on this commodity at the open for that month and holding it for one month at the end of the month the position is closed and the process is repeated and the basic idea behind this strategy comes from the fact that as we noticed within different commodity groups such as energy metals or grains some commodities take longer to participate in a particular trend in comparison to others that are quicker here we will first test how this strategy performs over all the commodities lumped together we are going to choose the SMA slope indicator and a look back window of six months first we load all the data into the MATLAB workspace now we are going to compute the momentum signal for every commodity in the training set of data we are going to use the SMA slope indicator with a look-back window size of six months next we extract the corresponding returns for the ketchup commodity now we compute the equal weighted returns for the entire group of commodities as well as for our strategy we also compute a normalized index for both so we can compare the relative performance what is equal weighting the idea is that at the end of every period you will close out every position collect all your capital and then allocate it in equal amounts across each trade for the next period now let's run this and see how our strategy has performed disappointed strategy seems to have generated the same returns as a buy and hold equally weighted strategy but with far worse drawdowns worse Sharpe ratios and worse sautee no ratios why is this now one possible reason could be that our strategy might be trying to catch up with trends that move all the commodities in our universe obviously such trends are few in number we can test this hypothesis by studying our strategy performs for more focus groups such as energy or grains let's do that right now now we are going to perform back tests on a focus group such as energy we can see here that our strategy outperforms buy-and-hold in terms of returns in terms of decreased drawdowns increase Sharpe ratios and increase Sortino ratios let's try something else now let's try grains again we can see that the strategy seems to be doing a good job of capturing the trends in this group now if you remember one of the earlier scripts we had mentioned the lack of a clear trend in soft commodities this makes sense because this group consists of commodities that are fairly unrelated to each other such as coffee and cotton now if we change the filter to soft it is obvious that the lack of a clear common trend makes our strategy failure notice that we have been trying this out with the training set of data and we haven't touched the test set yet now I have a question for you if energy and grains are the best performing group shouldn't we simply trade these two and ignore the others not really for a couple of reasons first these groups were the best performing for the training period that's 1999 to 2005 there is no reason to assume that they'll continue to do so later secondly other groups that have demonstrated a lack of common trends might start to do so for instance the introduction of a popular soft commodities ETF could lead to increased NGO movements between products that have been so far unrelated in other words we should maintain positions across each commodity on an equally weighted basis since we can't protect which commodity group will be trending or not now that we've seen how our strategy manages to capture trends within commodity groups we can allocate equal money to each group and every month we select the top candidate for each group the group champion which we trade on a monthly basis in other words we will hold positions in all five groups though the particular commodities might change from months to months now here is a script that I've created that tests the commodity momentum catch-up strategy let's load all the data into MATLAB now I have created a compute cross-sectional momentum function that computes strategy returns and buy-and-hold returns for a given parameter set most importantly for a given momentum indicator and a look back window here my momentum indicator is the SMA difference indicator and the look back window is of size six months now we are going to loop through our different commodity groups and filter our training set data accordingly we compute the strategy returns and also some interesting statistics such as the compounded annual growth rate the maximum drawdown the Sharpe ratio and the Sortino ratio for each group now we are going to compute the equally weighted indices for buy and hold as well as for our strategy and plot them together to see the relative performance of the two as you can see here our strategy has been an odd performer it has a higher compounded annual growth rate it has almost the same drawdown it has a higher Sharpe ratio and a highest Sortino ratio next we generate a relative performance report over the statistics that we had computed previously we can see here that the lack of trends clearly shows up in the numbers for metals soft and livestock however training in an equal weighted fashion lets us increase strategy returns and Sortino ratios as well as decreasing draw downs and all of these are very good things now we've been using one momentum indicator throughout as well as one log back window we should try and see how stable this performance is for a choice of momentum indicator and look back window in this script we are going to test the cross-sectional momentum strategy for various indicators and plot the indices versus buy-and-hold or indicator now we have categorized or plots per indicator and captured strategy performance while varying the look-back window notice that for the SMA difference indicator a look-back window of size six and nine beat by and hold for the SMA slope indicator look back windows of size 9 and 12 feet by and hold similarly for the ROC indicator look back windows of size 9 and 12 feet by and hold now looking at the stability of a look back window of size 9 across each of these different momentum indicators we choose to go with that when testing with the test set we are also going to use the SMA difference indicator for the test set as it seems to have a uniform increase in outcomes across the look-back windows this is unlike the SMA slope or the ROC indicators in which a look back window of size 9 seems to be an outlier now we are going to close this and we are going to use these parameters to measure performance of our strategy across the test set as well as for the overall data now we are going to choose the SMA difference indicator and we are going to use a look back window of size 9 on the test set the test set consists of monthly data from January 2006 to May 2013 so we're going to compute the catch-up strategy performance and then plot it you can see here that our strategy has outperformed the buy-and-hold equally weighted strategy by nearly 1300 basis points on a per annum basis it has increased compounded annual growth rate it has a decrease drawdown and increase in the Sharpe ratio and an increase in the Sortino ratio now let's see if we can dig deeper and see how each of the different commodity groups performed for our strategy known as that our strategy of trading equally across all commodity groups paid off as there were clear trends in energy metals grains soft commodities and livestock finally and though this is really bad data mining let us see overall how our strategy would have performed for the entire period in question we're going to change the data container to the entire period we see that our strategy beats by an old by 10 percentage points on a per annum basis it has an increased compounded annual growth rate it has decreased draw downs increased Sharpe ratios and increase certain ratios on a commodity group basis we see that our strategy has managed to catch up to trends in all commodity groups with the exception of soft commodities this sounds correct due to the reasons mentioned earlier now let's close this another thing that we can try out is to modify the look-back window size and analyze its effect on the strategy performance so let's go ahead and do that right now now Martha makes it really easy for you to modify parameters and measure their effects in this case we are going to increment or decrement the value of the loop back window and evaluate this section we can see here that with a look back window of size 10 our strategy still outperforms the buy-and-hold strategy now let's take the look back window all the way down to say three months and see how the strategy performs you can see as we increment or decrement the parameter the figure on the right changes to reflect the updated variables you can see that as the back window shrinks in size our strategy starts to deteriorate and performance similarly after a certain period of time increasing the look-back window size seems to have very little effects as you can see here Mottola makes it really easy for you to change parameters and rapidly measure the impact on your analysis let's close this to conclude a strategy has managed to outperform an equally weighted by an old strategy across most major metrics such as compounded annual growth rate certain ratios decreased draw downs and increased Sharpe ratios now once your trading strategy is stable and generating signals you want to hook it up to a trading platform for live trading trading toolbox provides connectivity to multiple trading platforms and brokers enabling you to obtain real-time market information and manage trade orders directly from within MATLAB now we are going to try to subscribe to an instrument and get price updates but first we are going to create an X radio connection now you see the trading technologies login screen here we're going to login in simulation mode now that the platform is up and running we are going to first create a notifier then create an instrument structure that we plan to subscribe to and the events that we are interested in the instrument that we are going to look at is crude oil the September 2013 contract and we are going to make sure that we get notified if the instrument has been found or if it has not been found or if there is a price update and we are only going to monitor these particular fields the last price the last quantity and changes now we are going to start listening to this instrument now that we're listening we can see price updates coming into MATLAB from the exchange we will listen to this for 10 seconds and then shut down the connection now let's try and create a trade order through extruder and then submit it we'll open up a script that does just that again we are going to open up a next radio connection we are going to create an instrument again it's crude oil September 2013 we are just an event handler for this order server next we create an order set that specifies the detail levels of the order status events as well as switching off self-imposed position limit checks now in a real-life trading situation this check should always be on again we wish to be notified when events such as order filled rejected submitted or deleted happen and we register the events accordingly finally we enable the sending of orders here in this section we are going to build an order profile and specify the quantity we want to buy and sell as well as the type of order next we check if the order server is up before attempting to submit the order if it's up we submit the order our order has been filled we can also try and get the last order and programmatically try to delete it finally let's shut down the extruder connection with that let's go back and revisit a commodity trading workflow what have we learned so far we've seen how easy it is to interactively explore data in MATLAB and gather insights in this case we noted that commodity groups tend to move together and some commodities react slower than others to trends that move the entire group we find that it's also easy to create new hypotheses and trading ideas in MATLAB here we created a catch-up strategy that identifies groups of commodities such as energy metals soft commodities grains and livestock at the close of every month this strategy ranks each commodity in each group on a relative momentum basis picks the commodity that has the lowest momentum as measured by one of three different indicators it weights two months and then allocates equal capital to each commodity one per group at the beginning of the third month it buys the commodities that it has selected at the market open and holds it until the market closed of the month now this is done on a monthly basis repeated and rebalanced on an equally weighted basis now we find that MATLAB makes it really easy for us to create our own custom analytics our own charts and plots in this case we measure back-testing performance on our training set of data and we see it's really easy to create custom plots such as power plots line plots and create combinations of the same in order to delve deeper into our strategy we also see how easy it is in MATLAB to tweak our ideas and go back and change things if they're not working or if you think we can do some better in our test case we used the idea of tweaking our strategy by a parameter selection process we selected the parameter that we thought would work best going forward and then tested the idea on a walk forward basis now these are the results of the trading strategy test we did a back rest and a walk forward test on the complete set of data we use the SMA difference indicator as chosen by the parameter selection process and a loop back window of nine months we've seen overall how the strategy has outperformed a buy and hold strategy of course there are caveats we have not taken into account interim ons highs and lows while computing the maximum draw downs in such a case we might just be under estimating the draw downs that we would have to face on a day to day basis while shading this strategy so let's revisit the challenges that commodities analysts and traders face when trying to analyze data and coming up with new strategies gaining insight into data now math labs interactive visualization tools help you get exactly the kind of insights that you want when you're trying to create a new trading strategy you can rapidly develop code within MATLAB and because of matlab's advanced vectorization math you can test the idea fast and efficiently you can change code and get the results immediately and finally you can share the code and the results with others let's revisit the commodities trading workflow that we had mentioned at the beginning of the webinar MATLAB and its associated database data feed trading toolbox and spreadsheet link toolbox help you access data from a variety of sources as well as interact with different trading platforms financial toolbox provides you with a core group of capabilities in order to perform standardized financial analytics tasks you can use the financial instruments toolbox to model exotic derivatives and the econometrics toolbox in order to perform advanced econometrics and alysus such as GARCH modeling or vector autoregressive modeling statistics and optimization toolboxes allow you to integrate advanced analytics into your workflow finally once you are done with your analysis when you want to share the results you can use the MATLAB report generator to create professional quality reports you can use the MATLAB compiler in order to create standalone executables that you can deploy and that you can share royalty-free with other users you can integrate your MATLAB code with dotnet Java or Excel using the MATLAB builder products and you can also deploy your MATLAB code in a production environment using the MATLAB production server finally you can improve performance of your MATLAB code using the parallel computing toolbox in order to take advantage of multiple cores within your desktop you can also use the MATLAB distributed computing server in order to access computing power across a grid that brings us to the end of the webinar here are some references for the trading strategy that I developed for the purposes of this webinar thank you

One thought on “Commodities Trading with MATLAB

Leave a Reply

Your email address will not be published. Required fields are marked *