# Portfolio

## Key Concepts

### Introduction

The Portfolio object provides information about the whole portfolio state.

### Properties

The Portfolio object has the following properties:

var invested = Portfolio.Invested;
var value = Portfolio.TotalPortfolioValue;
invested = self.Portfolio.Invested
value = self.Portfolio.TotalPortfolioValue

### Cost Averaging Accounting

LEAN uses the cost averaging accounting method, which determines the cost of your holdings by taking a weighted average of all your purchase prices. For example, say you place the following buy orders:

1. Buy 10 ABC @ $10 2. Buy 5 ABC @$11
3. Buy 20 ABC @ $14 4. Buy 3 ABC @$9

In the preceding example, the average cost of your ABC position is (10*10 + 5*11 + 20*14 + 3*9) / (10 + 5 + 20 + 3) = 12.1579/share. In contrast, if you use the first-in, first-out (FIFO) accounting method, the cost of the first 10 shares is 10/share, not 12.1579/share.

To get the cost of your security holdings, use the HoldingsCost property of the SecurityHolding object. If you fill buy and sell orders, the holdings cost is the product of the holding quantity and the average price. For example, the following table shows how the average price and holdings cost changes with each buy and sell order order in a long position:

Order QuantityFill Price ($)Holding QuantityAverage Price ($)Holdings Cost ($) 2102(2 * 10) / 2 = 102 * 10 = 20 -1111(1 * 10) / 1 = 101 * 10 = 10 1122(1 * 10 + 1 * 12) / 2 = 11 2 * 11 = 22 -21300 0 * 0 = 0 The following table shows how the average price and holdings cost changes with each buy and sell order order in a short position: Order QuantityFill Price ($)Holding QuantityAverage Price ($)Holdings Cost ($)
-210-2(-2 * 10) / -2 = 10-2 * 10 = -20
111-1(-1 * 10) / -1 = 10-1 * 10 = -10
-112-2(-1 * 10 + (-1) * 12) / -2 = 11
-2 * 11 = -22
213000 * 0 = 0

Note that when you decrease the position size without closing the trade, the average price doesn't change because the denominator and the coefficients in the numerator of its formula are scaled by the quotient of the current holding quantity and the previous holding quantity. For instance, if the last row in the preceding table had an order quantity 1, the holding quantity would be -1 and the average price would be

$$\frac{(-1 * q) * 10 + (-1 * q) * 12}{-2 * q}$$ $$=\frac{(-1 * \frac{-1}{-2}) * 10 + (-1 * \frac{-1}{-2}) * 12}{-2 * \frac{-1}{-2}}$$ $$=\frac{\frac{-1}{2} * 10 + \frac{-1}{2} * 12}{-1 }$$ $$=\frac{-5 - 6}{-1} = 11$$

### Long and Short Holdings

You can identify a position as long- or short-biased based on the sign of the holding quantity. Long positions have a positive quantity and short positions have a negative quantity.

To get the maximum buying power in your account currency you can use for a given Symbol and order direction, call the GetBuyingPower method.
var availableBuyingPower = Portfolio.GetBuyingPower(_symbol, OrderDirection.Buy);
available_buying_power = self.Portfolio.GetBuyingPower(self.symbol, OrderDirection.Buy)