-
Notifications
You must be signed in to change notification settings - Fork 28
Trader
- connect(cfg_file, password)
- disconnect()
- isConnected()
- submitOrder(order)
- submitCancellation(order)
- getPortfolioSummary()
- getPortfolioItems()
- getPortfolioItem(symbol)
- getSubmittedOrdersSize()
- getSubmittedOrders()
- getOrder(order_id)
- getWaitingListSize()
- getWaitingList()
- cancelAllPendingOrders()
- getClosePrice(symbol, buy, size)
- getLastPrice(symbol)
- getLastSize(symbol)
- getLastTradeTime()
- getBestPrice(symbol)
- getOrderBook(symbol, type, max_level=99)
- getOrderBookWithDestination(symbol, type)
- getStockList()
- requestCompanyNames()
- getCompanyNames()
- getCompanyName(symbol)
- requestSamplePrices(symbols, sampling_frequency=1, sampling_window=31)
- cancelSamplePricesRequest(symbols)
- cancelAllSamplePricesRequests()
- getSamplePricesSize(symbol)
- getSamplePrices(symbol, mid_prices=False)
- getLogReturnsSize(symbol)
- getLogReturns(symbol, mid_prices=False)
- subOrderBook(symbol)
- unsubOrderBook(symbol)
- subAllOrderBook()
- unsubAllOrderBook()
- getSubscribedOrderBookList()
-
username : str
The username used to log in to the server.
None
Usage of the constructor Trader(userName) with username
"FooBar"
:import shift trader = shift.Trader("FooBar")
This method connects to the server.
-
cfg_file : str
The path to the configuration file, usually named
initiator.cfg
. -
password : str
The password required for login.
bool
: True
if successfully connected, False
otherwise.
The following example shows the usage of the connect(cfg_file, password) method:
trader.connect("initiator.cfg", "password")
This method disconnects from the server.
NA
bool
: True
if successfully disconnected, False
otherwise.
The following example shows the usage of the disconnect() method:
trader.disconnect()
This method returns the status of the connection to the server.
NA
bool
: True
if there is a stablished connection with the server, False
otherwise.
The following example shows the usage of the isConnected() method:
trader.isConnected()
This method submits an order to the server.
shift.Order
: shift.Order
NA
The following example shows the usage of the submitOrder(order) method:
limitBuy = shift.Order(shift.Order.LIMIT_BUY, "AAPL", 1, 10.00) trader.submitOrder(limitBuy)
This method converts a limit order into a cancel order, and submits it to the server. Order ID and price are kept the same, and the size of the order is subtracted by its executed size.
shift.Order
: shift.Order
NA
The following example shows the usage of the submitCancellation(order) method:
for order in trader.getWaitingList(): trader.submitCancellation(order)
This method returns the current summary of the portfolio, which consists of the total buying power, total amount of traded shares, and total realized P&L.
NA
shift.PortfolioSummary
: The current portfolio summary, see also shift.PortfolioSummary.
The following example shows the usage of the getPortfolioSummary() method:
print("Buying Power\tTotal Shares\tTotal P&L\tTimestamp") print("%12.2f\t%12d\t%9.2f\t%26s" % (trader.getPortfolioSummary().getTotalBP(), trader.getPortfolioSummary().getTotalShares(), trader.getPortfolioSummary().getTotalRealizedPL(), trader.getPortfolioSummary().getTimestamp())) Buying Power Total Shares Total P&L Timestamp 975810.00 600 3.00 2019-02-06 14:12:21.069935
This method returns the items in the portfolio, the items are in the form of a dictionary with keys as symbol and corresponding traded price, amount of traded shares, and realized P&L.
NA
Dict[str, shift.PortfolioItem]
: The current portfolio items, with tickers as keys to the dictionary, see also shift.PortfolioItem.
The following example shows the usage of the getPortfolioItems() method:
print("Symbol\t\tShares\t\tPrice\t\tP&L\t\tTimestamp") for item in trader.getPortfolioItems().values(): print("%6s\t\t%6d\t%9.2f\t%7.2f\t\t%26s" % (item.getSymbol(), item.getShares(), item.getPrice(), item.getRealizedPL(), item.getTimestamp())) Symbol Shares Price P&L Timestamp AAPL -100 166.92 0.00 2019-02-06 14:12:21.069960 XOM -100 75.01 3.00 2019-02-06 14:12:21.068282
This method returns an item in the portfolio, given a user provided symbol. The item contains information such as traded price, amount of traded shares, and realized P&L.
-
symbol: str
The symbol of the requested portfolio item.
shift.PortfolioItem
: The portfolio item for the requested symbol, see also shift.PortfolioItem.
The following example shows the usage of the getPortfolioItem(symbol) method:
item = trader.getPortfolioItem("AAPL") print("Symbol\t\tShares\t\tPrice\t\tP&L\t\tTimestamp") print("%6s\t\t%6d\t%9.2f\t%7.2f\t\t%26s" % (item.getSymbol(), item.getShares(), item.getPrice(), item.getRealizedPL(), item.getTimestamp())) Symbol Shares Price P&L Timestamp AAPL -100 166.92 0.00 2019-02-06 14:12:21.069960
This method returns the number of orders in the submitted orders list.
NA
int
: Number of orders in the submitted orders list.
The following example shows the usage of the getSubmittedOrdersSize() method:
print("Submitted orders size: %d" % trader.getSubmittedOrdersSize()) Submitted orders size: 9
This method returns the submitted orders and their information such as symbol, price, size, type, and id. This list contains all previous submitted orders, both executed or not, but does not include cancellation requests.
NA
List[shift.Order]
: List of submitted orders, see also shift.Order.
The following example shows the usage of the getSubmittedOrders() method:
print("Symbol\t\t\t\t\t Type\t Price\t\tSize\tID\t\t\t\t\t\t\t\t\t\tTimestamp") for order in trader.getSubmittedOrders(): print("%6s\t%21s\t%7.2f\t\t%4d\t%36s\t%26s" % (order.symbol, order.type, order.price, order.size, order.id, order.timestamp)) Symbol Type Price Size ID Timestamp AAPL OrderType.LIMIT_BUY 10.00 1 7a4c180d-eb7d-4b5c-ad8a-811dace2bb80 2019-02-06 14:08:36.910916 AAPL OrderType.LIMIT_BUY 10.00 10 fdc63be9-14db-498d-a9aa-2747ac944eb8 2019-02-06 14:08:39.024619 XOM OrderType.LIMIT_BUY 10.00 10 0ac9874c-62dd-439d-afc0-c859fb91fb90 2019-02-06 14:08:39.024905 AAPL OrderType.MARKET_BUY 0.00 1 d7608d65-1e88-4bee-b554-67369c516db0 2019-02-06 14:10:41.599557 XOM OrderType.MARKET_BUY 0.00 1 3552989b-e547-4fa3-88e7-ee6dff3021a2 2019-02-06 14:10:41.599861 AAPL OrderType.MARKET_SELL 0.00 1 9b8282c0-be5c-4045-b8b2-dcfa02e68488 2019-02-06 14:11:59.011548 XOM OrderType.MARKET_SELL 0.00 1 52779f1e-35b0-418c-86de-0ead3c87f348 2019-02-06 14:11:59.011790 AAPL OrderType.MARKET_SELL 0.00 1 854832a7-bad3-49e4-9e86-e8d574ac471e 2019-02-06 14:12:21.016001 XOM OrderType.MARKET_SELL 0.00 1 1a34cafd-6652-48b7-8808-9e5629e505bf 2019-02-06 14:12:21.017010
This method returns a previously submitted order, given a user provided order ID.
-
order_id: str
The id of a previously submitted order.
shift.Order
: The order corresponding to the provided order ID, see also shift.Order.
The following example shows the usage of the getOrder(order_id) method:
order = trader.getOrder("7a4c180d-eb7d-4b5c-ad8a-811dace2bb80") print("%6s\t%21s\t%7.2f\t\t%4d\t%36s\t%26s" % (order.symbol, order.type, order.price, order.size, order.id, order.timestamp)) Symbol Type Price Size ID Timestamp AAPL OrderType.LIMIT_BUY 10.00 1 7a4c180d-eb7d-4b5c-ad8a-811dace2bb80 2019-02-06 14:08:36.910916
This method returns the number of orders in the waiting list, in other words the orders which are not being executed yet.
NA
int
: Number of orders in the waiting list.
The following example shows the usage of the getWaitingListSize() method:
print("Waiting list size: %d" % trader.getWaitingListSize()) Waiting list size: 3
This method returns orders in the waiting list and their information such as symbol, price, size, type, and id. The list contains all previous submitted orders that are yet to be executed.
NA
List[shift.Order]
: List of orders in the waiting list, see also shift.Order.
The following example shows the usage of the getWaitingList() method:
print("Symbol\t\t\t\t\t Type\t Price\t\tSize\tID\t\t\t\t\t\t\t\t\t\tTimestamp") for order in trader.getWaitingList(): print("%6s\t%21s\t%7.2f\t\t%4d\t%36s\t%26s" % (order.symbol, order.type, order.price, order.size, order.id, order.timestamp)) Symbol Type Price Size ID Timestamp XOM OrderType.LIMIT_BUY 10.00 10 0ac9874c-62dd-439d-afc0-c859fb91fb90 2019-02-06 14:08:39.106050 AAPL OrderType.LIMIT_BUY 10.00 1 7a4c180d-eb7d-4b5c-ad8a-811dace2bb80 2019-02-06 14:08:39.106052 AAPL OrderType.LIMIT_BUY 10.00 10 fdc63be9-14db-498d-a9aa-2747ac944eb8 2019-02-06 14:08:39.106053
This method cancels all pending orders (i.e. the current orders in the waiting list).
NA
NA
The following example shows the usage of the cancelAllPendingOrders() method:
trader.cancelAllPendingOrders()
This method returns what would be the closing price of a position if you were to buy/sell (a boolean parameter) a given amount of stocks (size) at the moment of the function call for a given symbol.
-
symbol: str
The symbol of the requested close price.
-
buy: bool
True
if user wants to buy,False
if user wants to sell. -
size: int
The size of the position to close (1 size = 100 shares).
float:
The close price for the requested symbol, position side, and size.
The following example shows the usage of the getClosePrice(symbol, buy, size) method:
trader.getClosePrice("AAPL", True, 5) 166.89
This method returns the last traded price for a given symbol. Synchronization with getLastSize(symbol) is not guaranteed.
-
symbol: str
The symbol of the requested last price.
float
: The last traded price for the requested symbol.
The following example shows the usage of the getLastPrice(symbol) method:
trader.getLastPrice("AAPL") 166.75
This method returns the last traded size for a given symbol. Synchronization with getLastPrice(symbol) is not guaranteed.
-
symbol: str
The symbol of the requested last price.
int
: The last traded size for the requested symbol (1 size = 100 shares).
The following example shows the usage of the getLastSize(symbol) method:
trader.getLastSize("AAPL") 2
This method returns the time of the last trade in the simulation.
NA
datetime.datetime
: The time of the last trade in the simulation.
The following example shows the usage of the getLastTradeTime() method:
trader.getLastTradeTime() datetime.datetime(2018, 12, 17, 9, 56, 30)
This method returns a shift.BestPrice object for a given symbol. In other words, it returns the best bid and ask prices and their corresponding volume information.
-
symbol: str
The symbol of the requested best price object.
shift.BestPrice
: The best price object for the requested symbol, see also shift.BestPrice.
The following example shows the usage of the getBestPrice(symbol) method:
bp = trader.getBestPrice("AAPL") bp.getBidPrice() 166.64 bp.getBidSize() 28 bp.getAskPrice() 178.68 bp.getAskSize() 12
This method is used to get the order book for a corresponding symbol and type.
-
symbol: str
The symbol of the requested order book.
-
type: shift::OrderBook::Type
The type of the requested order book. The possible values are:
- shift.OrderBookType.GLOBAL_BID
- shift.OrderBookType.GLOBAL_ASK
- shift.OrderBookType.LOCAL_BID
- shift.OrderBookType.LOCAL_ASK
-
max_level: int
The display length, i.e. the number of top prices that will be returned (default = 99).
List[shift.OrderBookEntry]
: List of order book entries, see also shift.OrderBookEntry.
The following example shows the usage of the getOrderBook(symbol, type, max_level) method:
print(" Price\t\tSize\t Dest\t\tTime") for order in trader.getOrderBook("AAPL", shift.OrderBookType.GLOBAL_BID, 5): print("%7.2f\t\t%4d\t%6s\t\t%19s" % (order.price, order.size, order.destination, order.time)) Price Size Dest Time 168.06 22 Market 2018-12-17 11:23:06 168.05 22 Market 2018-12-17 11:21:32 168.04 18 Market 2018-12-17 11:21:31 168.03 23 Market 2018-12-17 11:21:31 168.02 16 Market 2018-12-17 11:21:28
This method is used to get the order book for a corresponding symbol and type, with routing (destination) information.
-
symbol: str
The symbol of the requested order book.
-
type: shift::OrderBook::Type
The type of the requested order book. The possible values are:
- shift.OrderBookType.GLOBAL_BID
- shift.OrderBookType.GLOBAL_ASK
- shift.OrderBookType.LOCAL_BID
- shift.OrderBookType.LOCAL_ASK
List[shift.OrderBookEntry]
: List of order book entries, see also shift.OrderBookEntry.
The following example shows the usage of the getOrderBookWithDestination(symbol, type) method:
print(" Price\t\tSize\t Dest\t\tTime") for order in trader.getOrderBookWithDestination("AAPL", shift.OrderBookType.GLOBAL_BID): print("%7.2f\t\t%4d\t%6s\t\t%19s" % (order.price, order.size, order.destination, order.time)) Price Size Dest Time 168.04 1 NAS 2018-12-17 11:24:30 168.03 1 BAT 2018-12-17 11:24:30 168.03 1 PSE 2018-12-17 11:24:30 168.03 3 NAS 2018-12-17 11:24:30 168.03 4 NAS 2018-12-17 11:24:30 168.03 2 PSE 2018-12-17 11:24:30 168.03 1 BAT 2018-12-17 11:24:30 168.02 9 NYS 2018-12-17 11:24:30 168.02 3 NAS 2018-12-17 11:24:30 168.02 2 BAT 2018-12-17 11:24:30 168.02 1 DEA 2018-12-17 11:24:30 168.02 2 DEA 2018-12-17 11:24:30 ...
This method returns a list of all current tradable symbols.
NA
List[str]
: List of symbols.
The following example shows the usage of the getStockList() method:
trader.getStockList() ['AAPL','AXP','BA','CAT','CSCO','CVX','DIS','DWDP','GS','HD','IBM','INTC','JNJ','JPM','KO','MCD', 'MMM','MRK','MSFT','NKE','PFE','PG','TRV','UNH','UTX','V','VZ','WBA','WMT','XOM']
This method requests the download of company names for the current tradable symbols.
NA
NA
The following example shows the usage of the requestCompanyNames() method
trader.requestCompanyNames()
This method returns the company names for all the current tradable symbols. A call to requestCompanyNames() is required before using this function.
NA
Dict[str, str]
: Dictionary of symbols as keys and company names as values.
The following example shows the usage of getCompanyNames() method:
trader.getCompanyNames() {'AAPL': 'Apple Inc.', 'AXP': 'American Express Company', 'BA': 'The Boeing Company', 'CAT': 'Caterpillar Inc.', 'CSCO': 'Cisco Systems, Inc.', 'CVX': 'Chevron Corporation', 'DIS': 'The Walt Disney Company', 'DWDP': 'DowDuPont Inc.', 'GS': 'The Goldman Sachs Group, Inc.', 'HD': 'The Home Depot, Inc.', 'IBM': 'International Business Machines Corporation', 'INTC': 'Intel Corporation', 'JNJ': 'Johnson & Johnson', 'JPM': 'JPMorgan Chase & Co.', 'KO': 'The Coca-Cola Company', 'MCD': "McDonald's Corporation", 'MMM': '3M Company', 'MRK': 'Merck & Co., Inc.', 'MSFT': 'Microsoft Corporation', 'NKE': 'NIKE, Inc.', 'PFE': 'Pfizer Inc.', 'PG': 'The Procter & Gamble Company', 'TRV': 'The Travelers Companies, Inc.', 'UNH': 'UnitedHealth Group Incorporated', 'UTX': 'United Technologies Corporation', 'V': 'Visa Inc.', 'VZ': 'Verizon Communications Inc.', 'WBA': 'Walgreens Boots Alliance, Inc.', 'WMT': 'Walmart Inc.', 'XOM': 'Exxon Mobil Corporation'}
This method returns the company name for the requested symbol. A call to requestCompanyNames() is required before using this function.
-
symbol: str
The symbol of the requested company name.
str:
The company name of the requested symbol.
The following example shows the usage of the getCompanyName(symbol) method
trader.getCompanyName("AAPL") 'Apple Inc.'
This method creates a separate thread that sample prices for a given list of symbols. These sample prices can then be used to compute statistics.
-
symbols: List[str]
List of requested symbols.
-
sampling_frequency: float
The sampling frequency, in seconds (default = 1).
-
sampling_window: int
The sampling window size, i.e. the number of sample prices that will be kept in memory (default = 31).
bool
: True
if request was accepted, False
otherwise (it will be refused if all the requested symbols are already being processed by another thread).
The following example shows the usage of the requestSamplePrices(symbols, sampling_frequency, sampling_window) method
trader.requestSamplePrices(["AAPL", "CSCO", "INTC", "MSFT"])
This method cancels all current price sampling processes for the specified symbols.
-
symbols: List[str]
List of symbols for which the user wishes to cancel their price sampling processes.
bool
: True
if successfully cancelled, False
otherwise (e.g. there were no processes to cancel).
The following example shows the usage of the cancelSamplePricesRequest(symbols) method:
trader.cancelSamplePricesRequest(["AAPL", "CSCO", "INTC", "MSFT"])
This method cancels all current price sampling processes.
NA
bool
: True
if successfully cancelled, False
otherwise (e.g. there were no processes to cancel).
The following example shows the usage of the cancelAllSamplePricesRequests() method:
trader.cancelAllSamplePricesRequests()
This method returns the number of prices currently sampled for the requested symbol. A call to requestSamplePrices(symbols, samplingFrequency, samplingWindow) is required before using this function. The result will not exceed the samplingWindow
size of the sample prices request.
-
symbol: str
The symbol of the requested sample prices size.
int
: Number of prices currently sampled for the requested symbol. It will not exceed the samplingWindow
size of the sample prices request.
The following example shows the usage of the getSamplePricesSize(symbol) method:
trader.getSamplePricesSize("AAPL") 31
This method returns all current sampled prices for the requested symbol. A call to requestSamplePrices(symbols, samplingFrequency, samplingWindow) is required before using this function.
-
symbol: str
The symbol of the requested sample prices.
-
mid_prices: bool
True
for mid prices,False
for last prices (default =False
).
List[float]
: List of sample prices.
The following example shows the usage of the getSamplePrices(symbol, mid_prices) method:
trader.getSamplePrices("AAPL") [166.19, 166.19, 166.19, 166.2, 166.19, 166.18, 166.15, 166.15, 166.12, 166.13, 166.08, 166.08, 166.06, 166.06..
This method returns the number of log returns currently sampled for the requested symbol. A call to requestSamplePrices(symbols, samplingFrequency, samplingWindow) is required before using this function. The result will not exceed the samplingWindow
size of the sample prices request, minus 1.
-
symbol: str
The symbol of the requested sample log returns size.
int
: Number of log returns currently sampled for the requested symbol. It will not exceed the samplingWindow
size of the sample prices request, minus 1.
The following example shows the usage of the getLogReturnsSize(symbol) method
trader.getLogReturnsSize("AAPL") 30
This method returns all current sampled log returns for the requested symbol. A call to requestSamplePrices(symbols, samplingFrequency, samplingWindow) is required before using this function.
-
symbol: str
The symbol of the requested sample log returns.
-
mid_prices: bool
True
for using mid prices to compute log returns,False
for using last prices (default =False
).
List[float]
: List of sample log returns.
The following example shows the usage of the getLogReturns(symbol, mid_prices) method:
trader.getLogReturns("AAPL") [0.0001205182285284323, 0.0, -0.00030132280940353695, 0.00012054001943262449, 0.0, -0.000241094570513134..
This method subscribes to the order book data stream of the requested symbol.
-
symbol: str
The symbol of the order book subscribe request.
bool
: True
if subscription request was accepted, False
otherwise (it will be refused if the symbol is already subscribed).
The following example shows the usage of the subOrderBook(symbol) method:
trader.subOrderBook("AAPL")
This method unsubscribes to the order book data stream of the requested symbol.
-
symbol: str
The symbol of the order book unsubscribe request.
bool
: True
if unsubscription request was accepted, False
otherwise (it will be refused if the symbol is already unsubscribed).
The following example shows the usage of the unsubOrderBook(symbol) method:
trader.unsubOrderBook("AAPL")
This method subscribes to all order book data streams.
NA
bool
: True
if subscription request was accepted, False
otherwise (it will be refused if tall requested symbols are already subscribed).
The following example shows the usage of the subAllOrderBook() method:
trader.subAllOrderBook()
This method unsubscribes from all order book data streams.
NA
bool
: True
if unsubscription request was accepted, False
otherwise (it will be refused if all requested symbols are already unsubscribed).
The following example shows the usage of the unsubAllOrderBook() method:
trader.unsubAllOrderBook()
This method returns the list of all symbols for which an order book data stream subscription is active.
NA
List[str]
: List of all symbols for which an order book data stream subscription is active.
The following example shows the usage of the getSubscribedOrderBookList() method:
trader.getSubscribedOrderBookList()
Overview
Classes