Portfolio
The functionality here can be pulled into the namespace by
using Trading.PortfolioA portfolio is represented by a combination of components:
Cash: the real cash balance of the portfolio, updated asOrdersget filled, seecurrent_cashPosition: represents a held quantity of an asset.current_positioncan be used as an easy way to retrieve the position size.PurchasePower: can be used to determine whether certain orders can be made, seecurrent_purchasepower. At the start of every cycle this gets equalized with the currentCash, can be used as an estimation of "future" cash if certain orders would get executed.PortfolioSnapshot: a periodical snapshot of the portfolio
Orders
The state of the portfolio can be changed by two types of order:
Purchase: communicates to the system that a purchase order should be made. Will be executed by theSellersystem.Sale: communicates that a sale order should be made. Will be executed by thePurchasersystem.
Each order can have an OrderType which defaults to OrderType.Market, and a TimeInForce which defaults to TimeInForce.GTC (good till canceled). A price can be specified for orders that are not a Market order.
Example
We first construct a Trader which we start without any strategies.
broker = AlpacaBroker("<key_id>", "<secret>")
trader = Trader(broker)
start(trader)Now we can interact with it and do some basic trades. First we ask for a Market order on AAPL
e = Entity(trader, Purchase("AAPL", 1))After a while e will have a Filled component, signalling that the order was executed succesfully, and
current_position(trader, "AAPL")will return 1.0.
We can do the exact same to make a Sale.
e = Entity(trader, Sale("AAPL", 1))
current_position(trader, "AAPL") # now 0Shorting is allowed
For different options and order types see OrderType and TimeInForce
References
Trading.current_position — Functioncurrent_position(trader, asset::Asset)Returns the current portfolio position for asset. Returns nothing if asset is not found in the portfolio.
Trading.current_cash — Functioncurrent_cash(trader)Returns the current cash balance of the trader.
Trading.current_purchasepower — Functioncurrent_purchasepower(trader)Returns the current PurchasePower.
Trading.Cash — TypeRepresents the actual cash balance. Currently there is no particular currency tied to this.
Trading.PurchasePower — TypeRepresents the current purchasing power. This is updated at the start of each update cycle to the current value of the Cash singleton. It can thus be used to determine how many purchases/trades can be made during one cycle.
Trading.Position — TypeRepresents a position held in an equity represented by asset.
Trading.Purchase — TypePurchase(asset, quantity;
type = OrderType.Market,
time_in_force = TimeInForce.GTC,
price = 0.0,
trail_percent = 0.0)The local representation of a purchase order. This will be turned into an Order by the Purchaser System as soon as it's communicated to the broker. See OrderType and TimeInForce for more information on those kwargs.
Trading.Sale — TypeSale(asset, quantity;
type = OrderType.Market,
time_in_force = TimeInForce.GTC,
price = 0.0,
trail_percent = 0.0)The local representation of a sell order. This will be turned into an Order by the Seller System as soon as it's communicated to the broker. See OrderType and TimeInForce for more information on those kwargs.
Trading.Order — TypeRepresentation of a Purchase or Sale order that has been communicated to the broker. Once the status goes to "filled" the filling information will be taken by the Filler System to create a Filled component.
Trading.Filled — TypeRepresents the filled avg_price and quantity of an Order.
Trading.PortfolioSnapshot — TypeTrading.OrderType — ModuleEnum representing the different kinds of orders that can be made.
- Market
- Limit
- Stop
- StopLimit
- TrailinStop
Trading.TimeInForce — ModuleEnum representing the lifetime of an order.
- Day: till the end of the trading day
- GTC: good till canceled
- OPG: executed on market open
- CLS: executed at market close
- IOC: immediate or canceled, any unfilled part of the order will be canceled
- FOK: executed only when the full quantity can be filled, otherwise canceled.