Brokers¶
boatwright endeavors to generalize automated trading strategies, without limiting users, which includes the type of orders and how order are managed with a broker. However, brokers do differ in the available orders types, order managament, backend logic for order placement on exchanges, and of course in their API call format. This is to say that the backtest broker will not exactly replicate any live broker, inheriently introducing error to backtests. Further, it makes it difficult to generalize what a broker is and can do.
The compromise implemented here is that the parent broker class imposes the most common order types / managment and the BacktestBroker contains models for these. Users seeking to implement more complex order flows offered by their broker can write their own instances of ‘backtest’ and ‘live’ broker API interfaces.
Broker¶
- class src.boatwright.Brokers.Broker.Broker(key=None, secret_key=None, broker_name=None, quote_symbol='USD')¶
standardized class of broker interactions (place orders, check order, check accounts etc)
Note
supported broker functionality: * orders - market, limit, stop and trailing * time_in_force - good till cancelled “gtc”, good for day “gfd”, good till date (indicated by datetime object)
- Parameters:
key – API key (for live brokers), alternatively can provide broker_name
secret_key – API secret key (for live brokers), alternatively can provide broker_name
broker_name – (optional) providing the broker name will load the broker parameters from ~/boatwright/config.json
quote_symbol (str) – brokers reference currency, defaults to “USD”
- Variables:
orders – dictionary of orders, fills during a
Backtest
orLiveExecution
orders = { "OPEN": {order_id: open order, ...}, "PARTIALLY_FILLED": {order_id: partially_filled order, ...}, "FILLED": {order_id: filled order, ...}, "CANCELLED": {order_id: cancelled order, ...}, "FAILED": {order_id: failed order, ...} }
- calc_base_size(base_symbol, frac=1)¶
calculates the frac * available_base_amount truncated to base_increment where, if base_symbol=BTC-USD, base_symbol=BTC, quote_symbol=USD
- calc_quote_size(base_symbol, quote_symbol, frac=1)¶
calculates the frac * (available_quote_amount) worth of product truncated to quote_increment where, if base_symbol=BTC-USD, base_symbol=BTC, quote_symbol=USD
- abstract cancel_order(order_id)¶
cancels an open order returns “success” or “fail”
- abstract get_account_balance(base_symbol)¶
- Parameters:
base_symbol (str) – e.g. “BTC” or “AAPL”
- Returns:
account info for the specified base_symbol (e.g. how much BTC does user have)
account_info = { "available_balance": account_response["available_balance"]["value"], "currency": account_response["available_balance"]["currency"] }
- abstract get_order_info(order_id)¶
- Parameters:
order_id (str) – identifying string
- Returns:
dict
or order info
order_info = { "order_id": order_status["order_id"], "status": order_status["status"], "time_opened": , "time_closed": , "completion_percentage": order_status["completion_percentage"], "filled_size": order_status["filled_size"], "avg_price": order_status["average_filled_price"], "fee": order_status["total_fees"] }
- abstract get_product_info(base_symbol)¶
returns product info as:
product_info = { "base_symbol": response["base_symbol"], "price": response["price"], "volume": response["volume_24h"], "base": response["base_display_symbol"], "base_size_increment": response["base_increment"], "min_base_size": response["base_min_size"], "max_base_size": response["base_max_size"], "quote": response["quote_display_symbol"], "quote_size_increment": response["quote_increment"], "min_quote_size": response["quote_min_size"], "max_quote_size": response["quote_max_size"], }
- abstract limit_order(order)¶
place limit order
- Parameters:
order (LimitOrder)
- abstract market_order(order)¶
place market order
- Parameters:
order (MarketOrder)
- place_order(order)¶
handles the order according to its type, MARKET, LIMIT, STOP or TRAILING
- Parameters:
order (Order) – order to be placed
- step(data_row)¶
actions on each ‘step’ through a backtest, or live execution :return: None
- abstract trailing_order(order)¶
place trailing stop order
- Parameters:
order (TrailingOrder)
- abstract update_aum()¶
update values for cash, stock/crypto/forex etc assets, and total aum :return: None
- abstract update_open_orders()¶
checks open orders to see if order get triggered, or was filled etc