weighted_value: Calculate weighted stock values using FIFO/LIFO

weighted_valueR Documentation

Calculate weighted stock values using FIFO/LIFO

Description

Calculate weighted stock values using FIFO/LIFO

Usage

weighted_value(
  value,
  n = rep(1, length(value)),
  technique = NULL,
  n_stocks = NULL,
  buy_only = TRUE,
  type = 1,
  ...
)

Arguments

value

Numeric vector. Representing the values of the stock.

n

Numeric vector. Representing the volume of the operation. Positive for 'Buy' and negative for 'Sale'.

technique

Character. Pick any of FIFO or LIFO, or NULL to skip.

n_stocks

Integer. Specify the number of stocks to consider. By default will sum positive values of n.

buy_only

Boolean. Consider only buy (positive) values?

type

Integer. 1 for returning the value, 2 for returning the data.frame with the details ("df" attribute)

...

Additional parameters.

Value

The calculated weighted mean value.

Examples

values <- c(10, 20, 30, 40, 50)
weights <- c(2, 3, -4, 5, 6)
mean(values)
weighted_value(values)
weighted.mean(values, weights)
weighted_value(values, weights, buy_only = FALSE)
# Using FIFO and LIFO
weighted_value(values, weights, "FIFO")
weighted_value(values, weights, "LIFO", n_stocks = 8)

lares documentation built on Sept. 13, 2024, 1:08 a.m.