You are on page 1of 12

RIT VBA Tutorial

Tutorial

Note: RIT’s Application Programming Interface (API) is implemented in RIT versions 1.4 and
higher. The following instructions will not work for versions prior to RIT Client 1.4.

Getting Started
Rotman Interactive Trader allows users to program trading instructions in Microsoft Excel’s
Visual Basic for Applications (VBA) modules. The purpose of this is to allow for program or
“algorithmic” trading, where the computer executes trades based on a pre-described set of
instructions or parameters.

This help file assumes that the user has no previous knowledge of VBA, and begins by
discussing the concepts of programming before in-depth trading algorithms are introduced.
Those who are already familiar with VBA should skip to the section entitled “API commands
for RIT”.

This document also does not discuss the strategies behind algorithmic trading. Rather, it
introduces the user to the tools that are available through the RIT API. Users are encouraged
to explore possible strategies and techniques and use the building blocks here to implement
them.

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 1 of 12
V 1.1
RIT VBA Tutorial

Tutorial
Introduction to Excel VBA (Developer)
To access the VBA editor, first ensure that it is turned on by clicking on the Microsoft Office
Button in the top-left hand corner of Excel, and go to “Excel Options”. Ensure that “Show
Developer tab in the Ribbon” is checked. Once this is turned on, the Developer Tab will
appear in the original list of Excel tabs.

You can access the VBA editor by clicking on the “Visual Basic” icon within the Developer tab.
Hint: You can access this at anytime with the shortcut Alt+F11

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 2 of 12
V 1.1
RIT VBA Tutorial

Tutorial
The VBA editor will display all of the loaded Excel projects and add-ins. What is relevant is
the VBAProject (Book1) that you are currently working on. Note: Book1 refers to the name of
your excel spreadsheet file and will change as you change your filename.

We will begin by writing some basic procedures in your Book1.xls. In order to do this, create
a module in your book by going to Insert -> Module.

Module1 will be added to your Book1 project and a code window will open on the right hand
side allowing you to input your programming code.

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 3 of 12
V 1.1
RIT VBA Tutorial

Tutorial

The first step is to write a very simple procedure. A procedure is a set of programming lines
that are run by the computer whenever instructed to do so. Procedures are defined with the
lines “sub <procedure>” and “end sub” enclosing them. We will define a procedure named
“message” by inputting “Sub message” into the code window. As soon as you type “Sub
message” (without quotes) and press enter, VBA will automatically format the text by adding
brackets after message and add “End Sub” to the next line.

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 4 of 12
V 1.1
RIT VBA Tutorial

Tutorial
We have just created a procedure called “message”. When this procedure is run, it will
execute the code. In this case, it will do nothing since we have not written any code between
the beginning of the procedure (sub) and end of the procedure (end sub).

We will start with a basic set of code that references the built-in VBA function “MsgBox”. To
do this, type “MsgBox (“Hello World”)” into the code window between your (Sub) and (end
sub). The ”MsgBox” command will cause a pop-up message box to show up in Excel when
the code is executed. After you have typed the code into the window, click on the “Play”
button in the VBA editor, your code will execute and a pop-up message in Excel should
appear.

You have just completed writing and running a procedure in VBA. Obviously running the
procedure from the VBA editor is rather cumbersome, so the next step involves linking the
macro to an Excel button so that it is easier to run the procedure.

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 5 of 12
V 1.1
RIT VBA Tutorial

Tutorial
To create the Macro button, go back to the Developer tab in Excel and click on Insert, and
then select the first option “Button”.

When you move your mouse over the spreadsheet, the mouse cursor will become a crosshair
instead of an arrow. Click and drag anywhere on the spreadsheet to draw the button. Once
you finish drawing the button, the “Assign Macro” form will appear, select “message” (the
name of your macro you just written) then click OK. Now that you have assigned the
procedure “message” to the button, the procedure will be executed each time you click the
button. Note: If you change the name of your procedure, do not forget to re-assign your Macro

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 6 of 12
V 1.1
RIT VBA Tutorial

Tutorial
Once that is complete, left-click on the button and your “Hello World” message box should
appear. If you ever want to edit this object (resize, redirect, etc.) right click on it and a context
menu will appear allowing you adjust the box.

To understand a little bit more behind the programming, we will revisit the code and modify it
to be slightly more complex. In the Visual Basic Editor, we are going to modify the code to
read “MsgBox Cells(1,1)” instead of “MsgBox (“Hello World”)”.

Much like Microsoft Excel, VBA assumes that any text wrapped in “quotes” is plain text,
whereas anything not wrapped in “quotes” is a function, procedure, or operation. Since there
are no quotes around “Cells(1,1)”, it will not say “Hello Cells(1,1)”, instead, it will follow the
command of Cells(1,1).

The Cells(x,y) command is a function in Excel that instructs VBA to replace itself with the data
from the spreadsheet row x, column y. Essentially the way VBA interprets this set of code is:

MsgBox(“x”) “Create a message box with the text x”

Replace (“x”) with Cells(1,1)

Will now use the data from the cell located in row 1, column 1”.

MsgBox Cells(1,1) “Create a message box with the data from row 1, column 1”

Now go to the Cell A1 in the current Excel Sheet1 and type in “Bob”. Click on your Macro
button, the result should be a message box that says “Hello Bob”. Hint: If you want to
reference cells from other sheets, you can do this by typing Sheet3.Cells(1,1) This will now
use the data from cell A1 on Sheet3

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 7 of 12
V 1.1
RIT VBA Tutorial

Tutorial
We can make this more complex by adding an equation into the procedure. Go back to the
VBA editor and change your code to the following:

Go to your Excel Sheet and type “Sally” into Cell A2, and click your macro button. The result
should be:

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 8 of 12
V 1.1
RIT VBA Tutorial

Tutorial
To clean this up a little bit, we will make another adjustment to the code by adding the word
“and” between the two references. This is accomplished as follows:

Notice the quotes around the word “and”, as well as the space between the quotes and the
word “ and ”. Without the spaces, the message box would simply say “BobandSally”.
Alternatively without the “quotes” around <and>, VBA would think “and” is a command
instead of using it as “text”.

The last code adjustment that we will make is to add a mathematical equation to our message
box. This is accomplished as follows:

Type the values “3” and “5” into cells A3 and A4 and run your procedure by clicking the
button. The result should be “Bob and Sally15”. Since we used the asterisk “*” between
Cells(3,1) and Cells(4,1), VBA is instructed to multiply the values from these two cells, and
then append them as text to the rest of the text.

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 9 of 12
V 1.1
RIT VBA Tutorial

Tutorial
This concludes the basic VBA training that you will need in order to access the RIT API. You
are now able to write a simple set of instructions (a procedure) in VBA using a predesigned
function (MsgBox) and execute it via the Button that was created. In the next section, you will
use the skills that you have learned, and apply them to trading!

API Commands for RIT


To begin, start with a NEW spreadsheet and access VBA. In order to access RIT’s built-in
VBA commands, you will need to add it as a reference to your VBA project by going to: Tools
-> References

When the Reference window appears, scroll down and check the item “Rotman”. This loads
the Rotman commands and functions into VBA so that you can reference them.

Create a module in your file by going to Insert -> Module.

Launch the RIT Client (Version 1.4 or later) and then connect to the RIT Server. While you
can program your algorithms without running RIT, you won’t be able to test any of your
commands. Connect to a server running a API-enabled case (most commonly ALGO1).

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 10 of 12
V 1.1
RIT VBA Tutorial

Tutorial
Submitting an Order

In the module that you have created, type the following text into the module

Sub submitorder()
Dim api As Rotman.RITAPI
Set api = New Rotman.RITAPI
api.AddOrder "CRZY_M", 500, 5, api.BUY, api.LMT
End Sub

The first two lines simply initialize the coding structure that we will be using. The third is the
code that instructs the API to submit an order. You will notice that as you type the beginning
of the command “api.” a dropdown will appear showing all of the different API commands that
you can access.

The line given in the example is setup assuming you are trading a case with the stock
“CRZY_M”. If you are trading a different case, you will need to change the ticker
otherwise the command will not work since the security “CRZY_M” does not exist.

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 11 of 12
V 1.1
RIT VBA Tutorial

Tutorial

You will notice that as you type in the api.AddOrder command, a tooltip will show you the
different command line parameters that are required for the api.AddOrder command. Note:
You should not use brackets around the parameters in the api.AddOrder command; however
you may use brackets if you are referencing another function such as cells(x,y) in the
command.

Once you have completed the code, try to create a button and link the button to the
procedure. Click the button a few times and visit your RIT Client, you should see limit orders
placed at $5.00 to buy shares of CRZY_M. Alternatively simply press the play button in the
VBA editor to run your procedure.

This concludes the basic VBA tutorial designed to show you how to run VBA, create a basic
procedure, and use the RIT API to submit an order to the market. These tutorials are
expanded on significantly in the ALGO1 and ALGO2 cases/tutorials.

© Rotman School of Management


http://rit.rotman.utoronto.ca
Page 12 of 12
V 1.1

You might also like