Trading.Backtest.Portfolio (trading v0.1.0)

Portfolio management for backtesting.

Summary

Functions

Gets available capital for trading.

Gets the current position for a symbol.

Gets all trades.

Gets initial capital.

Creates a new portfolio.

Records equity curve point.

Calculates total portfolio value.

Updates market values of positions.

Types

position()

@type position() :: %{
  symbol: String.t(),
  quantity: integer(),
  avg_price: float(),
  market_value: float(),
  unrealized_pnl: float()
}

t()

@type t() :: %Trading.Backtest.Portfolio{
  cash: float(),
  equity_curve: [map()],
  initial_capital: float(),
  positions: %{required(String.t()) => position()},
  timestamp: integer(),
  trades: [trade()]
}

trade()

@type trade() :: %{
  id: String.t(),
  symbol: String.t(),
  side: :buy | :sell,
  quantity: integer(),
  price: float(),
  commission: float(),
  timestamp: integer(),
  pnl: float() | nil
}

Functions

available_capital(portfolio)

@spec available_capital(t()) :: float()

Gets available capital for trading.

get_position(portfolio, symbol)

@spec get_position(t(), String.t()) :: position()

Gets the current position for a symbol.

get_trades(portfolio)

@spec get_trades(t()) :: [trade()]

Gets all trades.

initial_capital(portfolio)

@spec initial_capital(t()) :: float()

Gets initial capital.

new(initial_capital)

@spec new(float()) :: t()

Creates a new portfolio.

record_equity(portfolio, timestamp)

@spec record_equity(t(), integer()) :: t()

Records equity curve point.

total_value(portfolio)

@spec total_value(t()) :: float()

Calculates total portfolio value.

update_market_values(portfolio, prices)

@spec update_market_values(t(), %{required(String.t()) => float()}) :: t()

Updates market values of positions.

update_position(portfolio, symbol, quantity, price, commission)

@spec update_position(t(), String.t(), integer(), float(), float()) :: t()

Updates a position based on a fill.