Skip to content
alienbrett edited this page Mar 24, 2020 · 1 revision

Orders

An order object (internally represented as a dict) should be created by passing in a few parameters, specified as ally.order.xxx objects.

Every order needs:

  • An instrument (equity, option, etc)
  • Price information (market, limit, etc)
  • Side (buy, sell, short, or buy-to-cover)
  • Timespan (day, or gtc)

Order( timespan, type, price, instrument, quantity)

market_buy = ally.order.Order(
    
    # Good for day order
    timespan   = ally.order.Timespan('day'),
    
    # Buy order (to_open is True by defaul)
    type       = ally.order.Buy(),
    
    # Market order
    price      = ally.order.Market(),
    
    # Stock, symbol F
    instrument = ally.instrument.Equity('f'),
    
    # 1 share
    quantity   = ally.order.Quantity(1)
)

Instruments

Equity

Equity and Instrument are identical, just pass in the ticker as case-insensitive string

Equity("SPY")     # Perfectly equivalent statements
Instrument('spy') # Perfectly equivalent statements

Option

Options have a few extra parameters

  • Underlying instrument (same as equity)
  • Maturity date (specify 'YYYY-MM-DD'
  • Strike price (int)
Call (
    instrument    = Equity("spy"), # Underlying
    maturity_date = "2019-09-30",  # Expiration date
    strike        = 290            # Strike
)

Put (
    instrument    = Instrument("ALLY"), # Underlying
    maturity_date = "2019-10-18",       # Expiration date
    strike        = 300                 # Strike
)

TimeInForce (Timespans)

Only really 3 types. Day orders will auto-cancel if not filled by the end of the day. GTC will remain outstanding until filled. Market-on-close (allegedly, I've never used one) will only execute at the end of the day

#### Timespans
Timespan('day')
Timespan('gtc')
Timespan('marketonclose')

Types

In order to submit a buy or sell order, you need to specify whether the security already exists in the specified account. So Ally's API won't turn a buy order into a buy-to-cover order if the security is already short in your account. By default, Buy() and Sell() will open a new position (buy => long, sell => short). Specifying to_open=False will close an existing position. So to buy a security before exiting the position, use orders with

submit order ... type=Buy() ... submit order ... type=Sell(to_open=False) ...

Buy()              # Buy to open (default)
Buy(to_open=False) # Buy to cover

Sell()              # Sell short (default)
Sell(to_open=False) # Sell to close

Pricing

PyAlly supports the following price types:

  • Market
  • Limit (side is auto-detected)
  • Stop
  • Stoplimit
  • Stoploss

Note that StopLimit is created by passing in a stop and a limit separately

Market ()         # Give me anything
Limit (69)        # Execute at least as favorably as $69.00
Stop (4.20)       # Stop order at $4.20
StopLimit (
    Stop  (10),   # Stop at $10.00
    Limit (9.50)  # No worse than $9.50
)
StopLoss (
    isBuy=False,  # Interpret stop as less than current price
    pct=True,     # Treat 'stop' as pct
    stop=5.0      # Stop at 5% from highest achieved after order placed
)
StopLoss (
    isBuy=True,   # Interpret stop as less than current price
    pct=False,    # Treat 'stop' as pct
    stop=5.0      # Stop at $5.00 above lowest price
)

Quantity

Quantity is pretty easy For options, this specifies the number of contracts, not the shares of the underlying

Quantity ( 15 )  # In shares or lots, for an option