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, seecurrent_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, 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
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 theSeller
system.Sale
: communicates that a sale order should be made. Will be executed by thePurchaser
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.
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
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