|
Ordering Methods |
|
Subject: Ordering Methods
Author: WebSpider
In response to: Object Data
Posted on: 05/02/2019 11:24:39 PM
order_optimal_portfolio(objective, constraints) This method places one or more orders by calculating a new optimal portfolio based on the objective and constraints.
order(asset, amount, style=OrderType) This methods places an order for the specified asset and the specified amount of shares (equities) or contracts (futures).
Parameter OrderType:
style=MarketOrder(exchange) style=StopOrder(stop_price, exchange) style=LimitOrder(limit_price, exchange) style=StopLimitOrder(limit_price=price1, stop_price=price2, exchange)
order_value(asset, amount, style=OrderType) Place an order by desired value rather than desired number of shares or contracts.
order_percent(asset, amount, style=OrderType) Places an order in the specified asset corresponding to the given percent of the current portfolio value, which is the sum of the positions value and ending cash balance. Placing a negative percent order will result in selling the given percent of the current portfolio value. Orders are always truncated to whole shares or contracts. Percent must be expressed as a decimal (0.50 means 50%).
Example order_percent(symbol('AAPL'), .5) will order AAPL shares worth 50% of current portfolio value. If AAPL is $100/share and the portfolio value is $2000, this buys 10 shares (discarding slippage and transaction cost).
order_target(asset, amount, style=OrderType) Places an order to maintain the target number of shares in portfolio. Placing a negative target order will result in a short position equal to the negative number specified.
Example If the current portfolio has 5 shares of AAPL and the target is 20 shares, order_target(symbol('AAPL'), 20) orders 15 more shares of AAPL.
order_target_value(asset, amount, style=OrderType) Places an order to adjust a position to a target value. Placing a negative target order will result in a short position equal to the negative target value.
Example If the current portfolio holds $500 worth of AAPL and the target is $2000, order_target_value(symbol('AAPL'), 2000) orders $1500 worth of AAPL (rounded down to the nearest share).
cancel_order(order) Attempts to cancel the specified order.
Parameters order: Can be the order_id as a string or the order object.
get_open_orders(sid) If asset is None or not specified, returns all open orders. If asset is specified, returns open orders for that asset
Parameters sid: An Equity object or a Future object. Can also be None.
Returns If asset is unspecified or None, returns a dictionary keyed by asset ID. The dictionary contains a list of orders for each ID, oldest first. If an asset is specified, returns a list of open orders for that asset, oldest first.
get_order(order) Returns the specified order. The order object is discarded at the end of handle_data.
Parameters order: Can be the order_id as a string or the order object.
Returns An order object that is read/writeable but is discarded at the end of handle_data.
>
> On 05/02/2019 10:24:53 PM WebSpider wrote:
data.current(assets, fields)
This method returns the current value of the given assets for the given fields at the current algorithm time.
Parameter fields: 'price' -- returns the last known close price, NaN if not founded 'last_traded' -- returns the date of the last trade event 'open' -- return the relevant information for the current trade bar 'high' -- return the relevant information for the current trade bar 'low' -- return the relevant information for the current trade bar 'close' -- return the relevant information for the current trade bar 'volume' -- returns the trade volume, 0 if there is no trade this minute 'contract' -- returns the current active contract
Example:
price = data.current(symbol('AAPL'), 'price');
data.history(assets, fields, bar_count, frequency) This method returns a window of data for the given assets and fields.
Parameter fields: 'price' -- returns the last known close price, NaN if not founded 'open' -- return the relevant information for the current trade bar 'high' -- return the relevant information for the current trade bar 'low' -- return the relevant information for the current trade bar 'close' -- return the relevant information for the current trade bar 'volume' -- returns the trade volume, 0 if there is no trade this minute
Parameter frequency: '1m' -- for minutely data '1d' -- for daily data
Examples (DAILY):
# returns the current price
price_history = data.history(assets, "price", 1, "1d");
# returns yesterday's close price and the current price
price_history = data.history(assets, "price", 2, "1d");
# returns the prices for the previous 5 days and the current price
price_history = data.history(assets, "price", 6, "1d");
# returns the volume since the current day's open, even if it is partial
price_history = data.history(assets, "volume", 1, "1d");
Examples (MINUTELY):
# returns the current price
price_history = data.history(assets, "price", 1, "1m");
# returns the previous minute's close price and the current price
price_history = data.history(assets, "price", 2, "1m");
# returns the prices for the previous 5 minutes and the current price
price_history = data.history(assets, "price", 6, "1m");
# returns the volume for the previous 60 minutes
price = data.history(assets, "volume", 60, "1m");
Example -- Up or down compared to previouse day's close price?
def initialize(context):
# AAPL, MSFT, and SPY
context.securities = [sid(24), sid(5061), sid(8554)]
def handle_data(context, data):
price_history = data.history(context.securities, fields="price", bar_count=2, frequency="1d")
for s in context.securities:
prev_close = price_history[s][-2];
curr_price = price_history[s][-1];
if curr_price > prev_close:
print(s, prev_close, curr_price, ' --> UP');
else:
print(s, prev_close, curr_price, ' --> DOWN');
Example -- VWAP (Volume Weighted Average Price)
def initialize(context):
# AAPL, MSFT, and SPY
context.securities = [sid(24), sid(5061), sid(8554)]
def vwap(prices, volumes):
return (prices * volumes).sum() / volumes.sum()
def handle_data(context, data):
hist = data.history(context.securities, fields=["price", "volume"], bar_count=30, frequency="1d")
vwap_10 = vwap(hist["price"][-10:], hist["volume"][-10:])
vwap_30 = vwap(hist["price"], hist["volume"])
for s in context.securities:
if vwap_10[s] > vwap_30[s] :
order(s, 100)
elif vwap_10[s] < vwap_30[s] :
order(s, -10)
Other data's methods: data.can_trade(assets) -- For the given asset or iterable of assets, returns true if the security has a known last price data.is_stale(assets) -- For the given asset or iterable of assets, returns true if the asset has ever traded and there is no trade data for the current simulation time
References:
|
|
|
|