Skip to content

Latest commit

 

History

History
74 lines (61 loc) · 2.43 KB

portfolio.md

File metadata and controls

74 lines (61 loc) · 2.43 KB

Portfolio

CurrentModule=Trading

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](@ref Order) 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_position
Trading.current_cash
Trading.current_purchasepower
Trading.Cash
Trading.PurchasePower
Trading.Position
Trading.Purchase
Trading.Sale
Trading.Order
Trading.Filled
Trading.PortfolioSnapshot
Trading.OrderType
Trading.TimeInForce