MATLAB Examples

Trading with Rotman Intractive Trader

This example shows how to use the rotmanTrader functions to connect to and trade through Rotman Interactive Trader (RIT). RIT must be installed on your computer along with the Excel® RTD Links. For more information visit http://rit.rotman.utoronto.ca/software.asp. This example also assumes that RIT is running Liability Trading 3 Case.

Contents

Create a Connection

First create a connection to Rotman Interactive Trader and list the functions (methods) available.

rit = rotmanTrader;
methods(rit)
Methods for class rotmanTrader:

addOrder           cancelQueuedOrder  isOrderQueued      subscribe          
addUpdateFcn       clearQueuedOrders  limitOrder         unsubscribe        
addprop            delete             removeUpdateFcn    update             
blotterOrder       getOrderInfo       restartTimer       
buy                getOrders          rotmanTrader       
cancelOrder        getSubscriptions   sell               
cancelOrderExpr    getTickerInfo      stopUpdates        

Call "methods('handle')" for methods of rotmanTrader inherited from handle.

To get more information on the functions, type help or doc and the name of the function. For example:

help rotmanTrader
help rotmanTrader/buy
help sell % same as help rotmanTrader/sell
 ROTMANTRADER connects MATLAB(R) to Rotman Interactive Trader
 
    RIT = ROTMANTRADER will create a connection to the Rotman
    Interactive Trader API using the path specified in the Windows
    System registry.
 
    RIT = ROTMANTRADER(DLLPATH) connects to Rotman Interactive Trader
    through the DLLPATH specified.  This allows connection to different
    dll locations.
 
    A connection is required to trade from MATLAB through Rotman
    Interactive Trader.
 
    Example:
    rit = rotmanTrader
 
    See also: buy, sell, limitOrder, blotterOrder, addOrder

    Published output in the Help browser
       showdemo rotmanTrader

 BUY submits a market buy order to Rotman Interactive Trader.
 
    ID = buy(RIT,TICKER,SIZE) returns queued order ID if market
    order was successfully submitted.  RIT is the
    connection to Rotman interactive Trader.  TICKER is the
    symbol(s) as a string or cell array of strings for the
    tickers to trade.  SIZE is the quantity to buy at market.
 
    Example:
    rit = rotmanTrader;
    buyID = buy(rit,'CRZY',100)
    sellID = buy(rit,'TAME',-100) % negative is sell
 
    See also sell, limitOrder, addOrder, blotterOrder

    Published output in the Help browser
       showdemo rotmanTrader

 --- help for rotmanTrader/sell ---

 SELL submits a market sell order to Rotman Interactive Trader.
 
    ID = SELL(RIT,TICKER,SIZE) returns queued order id if
    market order was successfully submitted.  RIT is the
    connection to Rotman interactive Trader.  TICKER is the
    symbol(s) as a string or cell array of strings for the
    tickers to trade.  SIZE is the quantity to buy at market.
 
    Example:
    rit = rotmanTrader;
    sellID = sell(rit,'TAME',200)
    buyID = sell(rit,'CRZY',-200)
 
    See also buy, limitOrder, addOrder, blotterOrder

    Published output in the Help browser
       showdemo rotmanTrader

Submitting Market Orders

Buy and sell market order for a single security. For both buy and sell functions, the returned value is the orderID.

buyID = buy(rit,'CRZY',10)
sellID = sell(rit,'TAME',15)
buyID =

    85


sellID =

    86

Buy and sell market order for multiple securities at same quantity

tickers = {'CRZY','TAME'};
qty = 20;
buyID = buy(rit,tickers,qty)
sellID = sell(rit,tickers,qty)
buyID =

    87    88


sellID =

    89    90

Buy and sell market orders for multiple securities at diferent quantities

buyID = buy(rit,tickers,[30 40])
buyID = sell(rit,tickers, [50 60])
buyID =

    91    92


buyID =

    93    94

Type of order can also be changed based upon the qty sign. If submitting a buy order with quantity of -70 it becomes a sell order.

buyID = buy(rit,'CRZY',-70)  % becomes sell order
sellID = sell(rit,'TAME',-80) % becomes buy order
buyID =

    95


sellID =

    96

Submitting Limit Orders

Limit orders can be submitted using the limitOrder function.

help limitOrder
 --- help for rotmanTrader/limitOrder ---

 LIMITORDER submits a limit order to Rotman Interactive Trader.
 
    ID = LIMITORDER(RIT,TICKER,QTY,PRICE) submits a market buy
    or sell order depending upon the sign of QTY and returns
    queued ID when order is accepted.  False (0) if not
    accepted, and -1 if the case is not running.    RIT is the
    connection to Rotman interactive Trader.  TICKER is the
    symbol(s) as a string or cell array of strings for the
    securities to trade.  QTY is the quantity to submit for bid
    (buy) or ask (sell).  Price is the bid/ask price to offer.
 
    QTY defines the limit order as a buy limit order if
    positive.  If QTY is negative, submits a sell limit order.
 
    Example:
    rit = rotmanTrader;
    limitBuyID = limitOrder(rit,'CRZY',90,20.00)
    limitSellID = limitOrder(rit,'TAME',-100,15.00)
    
    See also buy, sell, addOrder, blotterOrder

    Published output in the Help browser
       showdemo rotmanTrader

Submit a buy limit order, a bit for CRZY at price of 20.00 and quantity 90

buyID = limitOrder(rit,'CRZY',90,20.00)
buyID =

    97

Submit a sell limit order, an ask for TAME at price of 15.00 and quantity 100. Note the quantity is (-) negative, to denote a sell limit order.

limitID = limitOrder(rit,'TAME',-100,15.00)
limitID =

    98

Submit Orders Using a Blotter

Create an order blotter, a table with order information

help blotterOrder
 --- help for rotmanTrader/blotterOrder ---

 BLOTTERORDER submits orders using an order blotter to Rotman
 Interactive Trader.
 
    ID = BLOTTERORDER(RIT,BLOTTER) submits orders to Rotman
    Interactive Trader, through connection RIT.  BLOTTER is the
    order table specifying orders to place.
 
    For market orders, BLOTTER must contain the variables names
    TICKER, QUANTITY, and ACTION (with Buy/Sell values).
 
    For limit orders, BLOTTER must contain the variable names
    TICKER, QUANTITY, ACTION (with Buy/Sell values) and PRICE
    of the limit order.  To submit a market order with limit
    orders, PRICE must be set to 0 or NaN for the market
    orders.  Otherwise the will be submitted as market orders.
 
    Example:
    rit = rotmanTrader;
    % Create a blotter of buy and sells at market (no need for Price).
    ticker  = {'CRZY'; 'TAME'}; % tables need column vectors (use ; instead of ,)
    action   = {'Buy'; 'Sell'};
    quantity = [110; 120];
    blotter = table(ticker,action,quantity)
    % submit the blotter order
    blotterID = blotterOrder(rit,blotter)
    % Add some limit orders into the mix.  Prices must be present to define a
    % limit order.  Use 0 or nans for market orders in prices.
    ticker  = {'CRZY'; 'TAME'; 'CRZY'; 'CRZY'; 'TAME'; 'TAME'};
    action   = {'Buy'; 'Sell'; 'Buy'; 'Sell'; 'Buy';'Sell'};
    quantity = [130; 140; 150; 160; 170; 180];
    price   = [nan; nan; 9.50; 10.5; 24.5; 26.5];
    blotter = table(ticker,action,quantity, price)
    % submit the blotter order
    blotterID = blotterOrder(rit,blotter)
 
    See also: buy, sell, limitOrder, addOrder

    Published output in the Help browser
       showdemo rotmanTrader

Create a blotter of buy and sells at market (no need for Price).

ticker  = {'CRZY'; 'TAME'}; % tables need column vectors (use ; instead of ,)
action   = {'Buy'; 'Sell'};
quantity = [110; 120];
blotter = table(ticker,action,quantity)
blotter = 

    ticker    action    quantity
    ______    ______    ________

    'CRZY'    'Buy'     110     
    'TAME'    'Sell'    120     

submit the blotter order

blotterID = blotterOrder(rit,blotter)
blotterID =

    99
   100

Add some limit orders into the mix. Prices must be present to define a limit order. Use 0 or nans for market orders in prices.

ticker  = {'CRZY'; 'TAME'; 'CRZY'; 'CRZY'; 'TAME'; 'TAME'};
action   = {'Buy'; 'Sell'; 'Buy'; 'Sell'; 'Buy';'Sell'};
quantity = [130; 140; 150; 160; 170; 180];
price   = [nan; nan; 9.50; 10.5; 24.5; 26.5];
blotter = table(ticker,action,quantity, price)
blotter = 

    ticker    action    quantity    price
    ______    ______    ________    _____

    'CRZY'    'Buy'     130          NaN 
    'TAME'    'Sell'    140          NaN 
    'CRZY'    'Buy'     150          9.5 
    'CRZY'    'Sell'    160         10.5 
    'TAME'    'Buy'     170         24.5 
    'TAME'    'Sell'    180         26.5 

submit the blotter order

tf = blotterOrder(rit,blotter)
pause(10)
tf =

   101
   102
   103
   104
   105
   106

id = getOrders(rit)
id =

        1172        1168        1167         813         808         799

orderBlotter = getOrderInfo(rit,id)
orderBlotter = 

    OrderID    Ticker     Type     OrderType    Quantity    Price    Status    Quantity2
    _______    ______    ______    _________    ________    _____    ______    _________

    1172       'TAME'    'SELL'    'LIMIT'      -180        26.5     'LIVE'    180      
    1168       'CRZY'    'SELL'    'LIMIT'      -160        10.5     'LIVE'    160      
    1167       'CRZY'    'BUY'     'LIMIT'       150         9.5     'LIVE'    150      
     813       'TAME'    'SELL'    'LIMIT'      -180        26.5     'LIVE'    180      
     808       'CRZY'    'BUY'     'LIMIT'       150         9.5     'LIVE'    150      
     799       'TAME'    'SELL'    'LIMIT'      -300          26     'LIVE'    300      

Canceling Orders

Cancel an order by order ID

cancelID = id(1);
cancelOrder(rit,cancelID)
pause(3)
orderBlotter = getOrderInfo(rit,id)
orderBlotter = 

    OrderID    Ticker     Type     OrderType    Quantity    Price    Status    Quantity2
    _______    ______    ______    _________    ________    _____    ______    _________

    1168       'CRZY'    'SELL'    'LIMIT'      -160        10.5     'LIVE'    160      
    1167       'CRZY'    'BUY'     'LIMIT'       150         9.5     'LIVE'    150      
     813       'TAME'    'SELL'    'LIMIT'      -180        26.5     'LIVE'    180      
     808       'CRZY'    'BUY'     'LIMIT'       150         9.5     'LIVE'    150      
     799       'TAME'    'SELL'    'LIMIT'      -300          26     'LIVE'    300      

Cancel orders by expression

expr = 'Price <= 15.00 AND ticker = ''CRZY''';
cancelOrderExpr(rit,expr)
pause(3)
orderBlotter = getOrderInfo(rit,id)
orderBlotter = 

    OrderID    Ticker     Type     OrderType    Quantity    Price    Status    Quantity2
    _______    ______    ______    _________    ________    _____    ______    _________

    813        'TAME'    'SELL'    'LIMIT'      -180        26.5     'LIVE'    180      
    799        'TAME'    'SELL'    'LIMIT'      -300          26     'LIVE'    300      

Cancel Queued Orders

Orders are submitted to Rotaman Interactive Trader and may be queued if the orders are submitted faster than the case allows. The queued orders can be queried and even deleted. Resubmite the blotter order from above, query which ones are still queued, and then cancel them.

blotter
queuedID = blotterOrder(rit,blotter)
inQueue = isOrderQueued(rit,queuedID)
cancelQueuedOrder(rit,queuedID(inQueue))
isOrderQueued(rit,queuedID)
id = getOrders(rit)
orderBlotter = getOrderInfo(rit,id)
blotter = 

    ticker    action    quantity    price
    ______    ______    ________    _____

    'CRZY'    'Buy'     130          NaN 
    'TAME'    'Sell'    140          NaN 
    'CRZY'    'Buy'     150          9.5 
    'CRZY'    'Sell'    160         10.5 
    'TAME'    'Buy'     170         24.5 
    'TAME'    'Sell'    180         26.5 


queuedID =

   107
   108
   109
   110
   111
   112


inQueue =

     0
     1
     1
     1
     1
     1


ans =

     0
     0
     0
     0
     0
     0


id =

   813   799


orderBlotter = 

    OrderID    Ticker     Type     OrderType    Quantity    Price    Status    Quantity2
    _______    ______    ______    _________    ________    _____    ______    _________

    813        'TAME'    'SELL'    'LIMIT'      -180        26.5     'LIVE'    180      
    799        'TAME'    'SELL'    'LIMIT'      -300          26     'LIVE'    300      

Alternatively, you can submit orders faster than RIT can accept them. This allows canceling queued orders that have not been accepted yet (9 of 10 orders will be canceled).

queuedID = zeros(10,1);
for order = 1:10
    queuedID(order) = buy(rit,'CRZY',10*order);
end
cancelQueuedOrder(rit,queuedID)

Can also clear all queued orders using clearQueuedOrders

blotter
queuedID = blotterOrder(rit,blotter)
inQueue = isOrderQueued(rit,queuedID)
clearQueuedOrders(rit)
isOrderQueued(rit,queuedID)
id = getOrders(rit)
orderBlotter = getOrderInfo(rit,id)
blotter = 

    ticker    action    quantity    price
    ______    ______    ________    _____

    'CRZY'    'Buy'     130          NaN 
    'TAME'    'Sell'    140          NaN 
    'CRZY'    'Buy'     150          9.5 
    'CRZY'    'Sell'    160         10.5 
    'TAME'    'Buy'     170         24.5 
    'TAME'    'Sell'    180         26.5 


queuedID =

   123
   124
   125
   126
   127
   128


inQueue =

     0
     1
     1
     1
     1
     1


ans =

     0
     0
     0
     0
     0
     0


id =

   813   799


orderBlotter = 

    OrderID    Ticker     Type     OrderType    Quantity    Price    Status    Quantity2
    _______    ______    ______    _________    ________    _____    ______    _________

    813        'TAME'    'SELL'    'LIMIT'      -180        26.5     'LIVE'    180      
    799        'TAME'    'SELL'    'LIMIT'      -300          26     'LIVE'    300      

Cleaning Up

To properly clean up, you first need to delete the rotmanTrader connection before clearing it from the workspace. This stops the updates and disconnect from Rotman Interactive Trader.

delete(rit)
clear rit

We now no longer have a connection.

If you cleared the rit variable before issuing delete, the update timer is still running in the background, and you may see errors/warnings. To stop it, issue the following command:

delete(timerfind('Name','RotmanTrader'))