-
Notifications
You must be signed in to change notification settings - Fork 28
Trader
- connect(cfg_file, password)
- disconnect()
- is_connected()
- submit_order(order)
- submit_cancellation(order)
- get_portfolio_summary()
- get_portfolio_items()
- get_portfolio_item(symbol)
- get_unrealized_pl(symbol="")
- get_submitted_orders_size()
- get_submitted_orders()
- get_order(order_id)
- get_executed_orders(order_id)
- get_waiting_list_size()
- get_waiting_list()
- cancel_all_pending_orders(timeout=10)
- get_close_price(symbol, buy, size)
- get_close_price(symbol)
- get_last_price(symbol)
- get_last_size(symbol)
- get_last_trade_time()
- get_best_price(symbol)
- get_order_book(symbol, type, max_level=99)
- get_order_book_with_destination(symbol, type)
- get_stock_list()
- request_company_names()
- get_company_names()
- get_company_name(symbol)
- request_sample_prices(symbols, sampling_frequency=1.0, sampling_window=31)
- cancel_sample_prices_request(symbols)
- cancel_all_sample_prices_requests()
- get_sample_prices_size(symbol)
- get_sample_prices(symbol, mid_prices=False)
- get_log_returns_size(symbol)
- get_log_returns(symbol, mid_prices=False)
- sub_order_book(symbol)
- unsub_order_book(symbol)
- sub_all_order_book()
- unsub_all_order_book()
- get_subscribed_order_book_list()
-
username : str
The username used to log in to the server.
None
Usage of the constructor Trader(username) with username
"foo_bar"
:import shift trader = shift.Trader("foo_bar")
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 is_connected() method:
trader.is_connected()
This method submits an order to the server.
shift.Order
: shift.Order
NA
The following example shows the usage of the submit_order(order) method:
limit_buy = shift.Order(shift.Order.Type.LIMIT_BUY, "AAPL", 1, 10.00) trader.submit_order(limit_buy)
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 submit_cancellation(order) method:
for order in trader.get_waiting_list(): trader.submit_cancellation(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 get_portfolio_summary() method:
print("Buying Power\tTotal Shares\tTotal P&L\tTimestamp") print( "%12.2f\t%12d\t%9.2f\t%26s" % ( trader.get_portfolio_summary().get_total_bp(), trader.get_portfolio_summary().get_total_shares(), trader.get_portfolio_summary().get_total_realized_pl(), trader.get_portfolio_summary().get_timestamp(), ) ) Buying Power Total Shares Total P&L Timestamp 975703.00 600 -3.00 2019-04-18 20:59:59.837795
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 get_portfolio_items() method:
print("Symbol\t\tShares\t\tPrice\t\tP&L\t\tTimestamp") for item in trader.get_portfolio_items().values(): print( "%6s\t\t%6d\t%9.2f\t%7.2f\t\t%26s" % ( item.get_symbol(), item.get_shares(), item.get_price(), item.get_realized_pl(), item.get_timestamp(), ) ) Symbol Shares Price P&L Timestamp AAPL -100 167.69 -2.00 2019-04-18 20:59:59.760297 XOM -100 75.25 -1.00 2019-04-18 20:59:59.837834
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 get_portfolio_item(symbol) method:
item = trader.get_portfolio_item("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.get_symbol(), item.get_shares(), item.get_price(), item.get_realized_pl(), item.get_timestamp(), ) ) Symbol Shares Price P&L Timestamp AAPL -100 167.69 -2.00 2019-04-18 20:59:59.760297
This method returns the estimated unrealized P&L of the current position for a given symbol. If the symbol parameter is missing, it returns the estimated unrealized P&L of the whole current porfolio.
-
symbol: str
The symbol of the requested estimated unrealized P&L (default = "").
float:
The estimated unrealized P&L for the requested symbol.
The following example shows the usage of the get_unrealized_pl(symbol) method:
trader.get_unrealized_pl("AAPL") 3.00
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 get_submitted_orders_size() method:
print("Submitted orders size: %d" % trader.get_submitted_orders_size()) 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 get_submitted_orders() method:
print( "Symbol\t\t\t\tType\t Price\t\tSize\tExecuted\tID\t\t\t\t\t\t\t\t\t\t\t\t\t\t Status\t\tTimestamp" ) for order in trader.get_submitted_orders(): if order.status == shift.Order.Status.FILLED: price = order.executed_price else: price = order.price print( "%6s\t%16s\t%7.2f\t\t%4d\t\t%4d\t%36s\t%23s\t\t%26s" % ( order.symbol, order.type, price, order.size, order.executed_size, order.id, order.status, order.timestamp, ) ) Symbol Type Price Size Executed ID Status Timestamp AAPL Type.LIMIT_BUY 10.00 1 1 c544caf9-78d2-4c33-8675-f2d377c96f44 Status.CANCELED 2019-04-18 20:48:58.768379 AAPL Type.LIMIT_BUY 10.00 10 10 a0228bef-7905-41a4-a06a-56f028a60a22 Status.CANCELED 2019-04-18 20:49:00.772277 XOM Type.LIMIT_BUY 10.00 10 10 3d17ccea-c922-4cac-bde7-b388b91ec681 Status.CANCELED 2019-04-18 20:49:00.772642 AAPL Type.MARKET_BUY 167.71 1 1 b3a60493-ad7b-4096-8271-e058fd05be7d Status.FILLED 2019-04-18 20:59:55.758615 XOM Type.MARKET_BUY 75.26 1 1 3ba983f3-e705-4016-b144-76970712aa11 Status.FILLED 2019-04-18 20:59:55.758906 AAPL Type.MARKET_SELL 167.69 1 1 51192791-26b0-453f-8c0e-0e106aa7b076 Status.FILLED 2019-04-18 20:59:58.425309 XOM Type.MARKET_SELL 75.25 1 1 e41c61c7-0d2f-4ef6-baa9-95cea3125d11 Status.FILLED 2019-04-18 20:59:58.425692 AAPL Type.MARKET_SELL 167.69 1 1 1477caf2-7376-4645-93f0-b0978ba71d03 Status.FILLED 2019-04-18 20:59:59.598876 XOM Type.MARKET_SELL 75.25 1 1 118c1315-fdcd-46b8-9aac-cb7292d12a44 Status.FILLED 2019-04-18 20:59:59.599157
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 get_order(order_id) method:
order = trader.get_order("b3a60493-ad7b-4096-8271-e058fd05be7d") print( "%6s\t%16s\t%7.2f\t\t%4d\t\t%4d\t%36s\t%23s\t\t%26s" % ( order.symbol, order.type, order.executed_price, order.size, order.executed_size, order.id, order.status, order.timestamp, ) ) Symbol Type Price Size Executed ID Status Timestamp AAPL Type.MARKET_BUY 167.71 1 1 b3a60493-ad7b-4096-8271-e058fd05be7d Status.FILLED 2019-04-18 20:59:55.758615
This method returns a list of executed orders, given a user provided order ID. One order in the submitted orders list may correspond to several executed orders, but all will share the same order ID. Executed price and size information in these orders correspond to their individual executions. Status information corresponds to the status of the order at the moment of the particular execution.
-
order_id: str
The id of a previously submitted order.
List[shift.Order]
: List of executed orders corresponding to the provided order ID, see also shift.Order.
The following example shows the usage of the get_executed_orders(order_id) method:
limit_buy = shift.Order(shift.Order.Type.LIMIT_BUY, "AAPL", 10, 168.50) print( "Symbol\t\t\t\tType\t Price\t\tSize\tExecuted\tID\t\t\t\t\t\t\t\t\t\t\t\t\t\t Status\t\tTimestamp" ) for order in trader.get_executed_orders(limit_buy.id): print( "%6s\t%16s\t%7.2f\t\t%4d\t\t%4d\t%36s\t%23s\t\t%26s" % ( order.symbol, order.type, order.executed_price, order.size, order.executed_size, order.id, order.status, order.timestamp, ) ) Symbol Type Price Size Executed ID Status Timestamp AAPL Type.MARKET_BUY 168.15 10 6 d5c5f720-b774-49f8-a0b8-eec699857d69 Status.PARTIALLY_FILLED 2019-07-25 16:54:36.337757 AAPL Type.MARKET_BUY 168.15 10 4 d5c5f720-b774-49f8-a0b8-eec699857d69 Status.FILLED 2019-07-25 16:54:36.337757
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 get_waiting_list_size() method:
print("Waiting list size: %d" % trader.get_waiting_list_size()) 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 get_waiting_list() method:
print( "Symbol\t\t\t\tType\t Price\t\tSize\tExecuted\tID\t\t\t\t\t\t\t\t\t\t\t\t\t\t Status\t\tTimestamp" ) for order in trader.get_waiting_list(): print( "%6s\t%16s\t%7.2f\t\t%4d\t\t%4d\t%36s\t%23s\t\t%26s" % ( order.symbol, order.type, order.price, order.size, order.executed_size, order.id, order.status, order.timestamp, ) ) Symbol Type Price Size Executed ID Status Timestamp XOM Type.LIMIT_BUY 10.00 10 0 3d17ccea-c922-4cac-bde7-b388b91ec681 Status.NEW 2019-04-18 20:49:00.790479 AAPL Type.LIMIT_BUY 10.00 10 0 a0228bef-7905-41a4-a06a-56f028a60a22 Status.NEW 2019-04-18 20:49:00.790481 AAPL Type.LIMIT_BUY 10.00 1 0 c544caf9-78d2-4c33-8675-f2d377c96f44 Status.NEW 2019-04-18 20:49:00.790482
This method cancels all pending orders (i.e. the current orders in the waiting list).
-
timeout: int
Maximum amount of time (in seconds) to wait until cancellations go through (default = 10).
NA
The following example shows the usage of the cancel_all_pending_orders() method:
trader.cancel_all_pending_orders()
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 get_close_price(symbol, buy, size) method:
trader.get_close_price("AAPL", True, 5) 166.89
This method returns what would be the closing price of the current position for a given symbol.
-
symbol: str
The symbol of the requested close price.
float:
The close price for the requested symbol.
The following example shows the usage of the get_close_price(symbol) method:
trader.get_close_price("AAPL") 166.89
This method returns the last traded price for a given symbol. Synchronization with get_last_size(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 get_last_price(symbol) method:
trader.get_last_price("AAPL") 166.75
This method returns the last traded size for a given symbol. Synchronization with get_last_price(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 get_last_size(symbol) method:
trader.get_last_size("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 get_last_trade_time() method:
trader.get_last_trade_time() 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 get_best_price(symbol) method:
bp = trader.get_best_price("AAPL") bp.get_bid_price() 166.64 bp.get_bid_size() 28 bp.get_ask_price() 178.68 bp.get_ask_size() 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 get_order_book(symbol, type, max_level) method:
print(" Price\t\tSize\t Dest\t\tTime") for order in trader.get_order_book("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 get_order_book_with_destination(symbol, type) method:
print(" Price\t\tSize\t Dest\t\tTime") for order in trader.get_order_book_with_destination( "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 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 ... ... ... ...
This method returns a list of all current tradable symbols.
NA
List[str]
: List of symbols.
The following example shows the usage of the get_stock_list() method:
trader.get_stock_list() ['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 request_company_names() method
trader.request_company_names()
This method returns the company names for all the current tradable symbols. A call to request_company_names() 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 get_company_names() method:
trader.get_company_names() {'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 request_company_names() 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 get_company_name(symbol) method
trader.get_company_name("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.0, minimum = 0.000001).
-
sampling_window: int
The number of sample prices that will be kept in memory (default = 31, minimum = 2).
bool
: True
if request was accepted, False
otherwise (it will be refused if all the requested symbols are already being processed by another thread or with invalid parameter values are used).
The following example shows the usage of the request_sample_prices(symbols, sampling_frequency, sampling_window) method:
trader.request_sample_prices(["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 cancel_sample_prices_request(symbols) method:
trader.cancel_sample_prices_request(["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 cancel_all_sample_prices_requests() method:
trader.cancel_all_sample_prices_requests()
This method returns the number of prices currently sampled for the requested symbol. A call to request_sample_prices(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 get_sample_prices_size(symbol) method:
trader.get_sample_prices_size("AAPL") 31
This method returns all current sampled prices for the requested symbol. A call to request_sample_prices(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 get_sample_prices(symbol, mid_prices) method:
trader.get_sample_prices("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 request_sample_prices(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 get_log_returns_size(symbol) method
trader.get_log_returns_size("AAPL") 30
This method returns all current sampled log returns for the requested symbol. A call to request_sample_prices(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 get_log_returns(symbol, mid_prices) method:
trader.get_log_returns("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 sub_order_book(symbol) method:
trader.sub_order_book("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 unsub_order_book(symbol) method:
trader.unsub_order_book("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 sub_all_order_book() method:
trader.sub_all_order_book()
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 unsub_all_order_book() method:
trader.unsub_all_order_book()
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 get_subscribed_order_book_list() method:
trader.get_subscribed_order_book_list()
Overview
Classes