ALGO2 Algorithm2 Tutorial
ALGO2 Algorithm2 Tutorial
Case Tutorial
Introduction
This tutorial will help traders program a complete high-frequency trading market making algorithm for the ALGO2 case.
Tutorial
Begin by linking your spreadsheet to basic RIT datafields via the RTD link function. You should be familiar with the function of the RTD links from previous spreadsheets. It is helpful to have fields of data set up such as the following: Broker, Period, Cash, Buying Power, Net Liquidation Value, Risk Free Rate, Time Remaining, Time in a Year, and Profit. We have set them up in the following spreadsheet. (Please note that the column B is live linked Data, and Column C describes the equations used in column B to link the data. i.e., your Column C should be completely blank.
If a case is actively running, you should see values in the fields. If no case is running, it should show [error]. If it shows #N/A then you have either typed the equation incorrectly, or do not have the RTD data patch (provided by Microsoft) installed properly.
Algorithm 2
Algorithmic Market Making
Case Tutorial
Next, add the following hard-coded parameters to your spreadsheet: The Column B section of the parameters (rows 13-16) are all numbers input by the user.
These parameters will govern how the algorithm will act, they define: When the algorithm starts to trade, when the algorithm stops trading, how much of a spread to submit when submitting buy/sell orders, and how many shares to buy/sell when submitting an order. Lastly, we need to link the market data:
Now that we have all of that data setup in Excel, we can start to program our algorithm.
Algorithm 2
Algorithmic Market Making
Case Tutorial
The Algorithm
We begin our algorithm by going into the VBA editor and adding a new module. In the module, we declare a function called marketmake with parameters time, triggerstart, and triggerstop. The function initializes the API and then compares the current time to the start/end times and runs specific code if the time is between the start/stop times.
Next, we need to instruct the function as to what to do when it reaches the RUN THIS CODE portion. We want it to submit two orders, a bid at a price of <Midmarket> <spread> for <volume>, and an offer at a price of <midmarket> + <spread> for <volume>. <sections> denote links to the spreadsheet variables. This can be accomplished as follows:
As you can see, the spreadsheet refers to ranges instead of cell locations, for example instead of saying Cells(16,2) for volume, it says Ranges(Shares). This is because in the Excel table we have labelled the different cells with range names. This can be accomplished by going back to the spreadsheet, clicking on each of the specific cells, and filling out the range name in the top left corner. The following is the example for labelling the volume. We suggest you label the Midmarket price, as well as the spread.
Algorithm 2
Algorithmic Market Making
Case Tutorial
Note the upper-left yellow section. It used to say B16, and it has been relabelled to Shares. We can now refer to that cell in VBA as Shares anytime we want. Reminder: Re-label the other sections Midmarket and Spread. The algorithm currently checks to see if the time is appropriate, if it is, it automatically submits a limit buy order and a limit sell order. There are a couple of problems with this algorithm. First of all, it will submit an infinite number of orders (keep submitting bids/offers over and over again) as long as the time is between start/stop time. Second, RIT only accepts orders at a certain speed (once every 0.25 seconds), and this algorithm is submitting them nearly instantaneously. To fix that, we alter the code as follows:
Algorithm 2
Algorithmic Market Making
Case Tutorial
The RIT software returns a string called TRUE or FALSE when you submit a trade request. If the order is successful, it returns TRUE, otherwise it returns FALSE. By setting the Status variable as false, and then running the addorder command, you can track when it properly submits the order. The program doesnt exit the loop (and keeps trying to submit the order) until it is successful. *Note: this could potentially crash your program if its never able to submit the command! (It gets stuck in the loop forever- consider ways to fix this potential issue. We need to add a pause to our application to make sure that it isnt running too quickly and getting confused. We can do that by adding the following sections of code to our program:
*Note: there are better ways to handle this, but this is the easiest way for illustrative purposes. Our code still submits orders constantly, but realistically, we only want it to submit orders when we have no open orders in the book. To fix this issue, we add another sheet to our Excel book labelled Open Orders and we add an RTD link to it:
This allows us to track when we have orders in the book. It shows our pending orders are displayed in this box with commas separating the fields, and semicolons separating each order. [Order1;Order2;Order3]. We can use the semicolon to count the number of orders in the book, if
Algorithm 2
Algorithmic Market Making
Case Tutorial
theres nothing in this box, we know theres no orders, if theres more than 1 semicolon in this box, we know theres more than 2 orders. Once again we alter our trading code, so that orders are only submitted when no orders are in the book, and we also add code to cancel our existing orders whenever theres only 1 order remaining. Our resulting program submits two orders when we have no orders, it cancels all orders if we only have 1 order, and otherwise it does nothing. This means we always have 2 orders in the book (a bid and an ask).
Our final code is presented above. In order to run the code, we need to call the function from the spreadsheet. This is accomplished by inputting into one of the cells =marketmake(a,b,c) and referencing the cells to the appropriate fields. An example is provided below (note: the function can be called from any cell, but the variables must be linked correctly to the time, triggerstart, and triggerstop).
Algorithm 2
Algorithmic Market Making
Case Tutorial
Your algorithm will now trade whenever the case is running (and the time is between 290 and 35!)