Portfolio

The functionality here can be pulled into the namespace by

using Trading.Portfolio

A portfolio is represented by a combination of components:

  • Cash: the real cash balance of the portfolio, updated as Orders get filled, see current_cash
  • Position: represents a held quantity of an asset. current_position can be used as an easy way to retrieve the position size.
  • PurchasePower: can be used to determine whether certain orders can be made, see current_purchasepower. At the start of every cycle this gets equalized with the current Cash, 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 the Seller system.
  • Sale: communicates that a sale order should be made. Will be executed by the Purchaser system.

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 0
Note

Shorting is allowed

For different options and order types see OrderType and TimeInForce

References

Trading.current_positionFunction
current_position(trader, asset::Asset)

Returns the current portfolio position for asset. Returns nothing if asset is not found in the portfolio.

source
Trading.CashType

Represents the actual cash balance. Currently there is no particular currency tied to this.

source
Trading.PurchasePowerType

Represents 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.

source
Trading.PurchaseType
Purchase(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.

source
Trading.SaleType
Sale(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.

source
Trading.OrderTypeModule

Enum representing the different kinds of orders that can be made.

  • Market
  • Limit
  • Stop
  • StopLimit
  • TrailinStop
source
Trading.TimeInForceModule

Enum 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.
source