Title: | Energy Trading and Risk Management |
---|---|
Description: | Provides a collection of functions to perform core tasks within Energy Trading and Risk Management (ETRM). Calculation of maximum smoothness forward price curves for electricity and natural gas contracts with flow delivery, as presented in F. E. Benth, S. Koekebakker, and F. Ollmar (2007) <doi:10.3905/jod.2007.694791> and F. E. Benth, J. S. Benth, and S. Koekebakker (2008) <doi:10.1142/6811>. Portfolio insurance trading strategies for price risk management in the forward market, see F. Black (1976) <doi:10.1016/0304-405X(76)90024-6>, T. Bjork (2009) <https://EconPapers.repec.org/RePEc:oxp:obooks:9780199574742>, F. Black and R. W. Jones (1987) <doi:10.3905/jpm.1987.409131> and H. E. Leland (1980) <http://www.jstor.org/stable/2327419>. |
Authors: | Anders D. Sleire |
Maintainer: | Anders D. Sleire <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.2 |
Built: | 2025-02-09 03:44:38 UTC |
Source: | https://github.com/sleire/etrm |
Tools for energy market risk management (forward curves and trading strategies)
Anders D. Sleire <[email protected]>
F. E. Benth, S. Koekkebakker, and F. Ollmar. Extracting and applying smooth forward curves from average-based commodity contracts with seasonal variation.The Journal of Derivatives, 15(1):52–66,2007b. https://doi.org/10.3905/jod.2007.694791
F. E. Benth, J. S. Benth, and S. Koekebakker. Stochastic modelling of electricity and related markets,volume 11. World Scientific, 2008. https://doi.org/10.1142/6811
F. Black. The pricing of commodity contracts.Journal of financial economics, 3(1):167–179, 1976. https://doi.org/10.1016/0304-405X(76)90024-6
T. Bjork. Arbitrage Theory in Continuous Time. Oxford University Press, 3 edition, 2009. https://EconPapers.repec.org/RePEc:oxp:obooks:9780199574742
F. Black and R. W. Jones. Simplifying portfolio insurance. The Journal of Portfolio Management, 14(1):48–51, 1987. https://doi.org/10.3905/jpm.1987.409131
H. E. Leland. Who should buy portfolio insurance? The Journal of Finance, 35(2):581–594, 1980. http://www.jstor.org/stable/2327419
Implements CPPI strategy for commodity price risk management
cppi(q, tdate, f, tper, rper, tcost = 0, int = TRUE)
cppi(q, tdate, f, tper, rper, tcost = 0, int = TRUE)
q |
numeric value for quantity to be hedged, either positive (net buyer) or negative (net seller) |
tdate |
date vector with trading days |
f |
numeric futures price vector |
tper |
numeric target price markup/down to the price on the first trading day |
rper |
numeric risk factor as a percentage of the price on the first trading day |
tcost |
numeric transaction costs pr unit |
int |
TRUE/FALSE integer restriction on tradable volume |
instance of the CPPI class
# CPPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") # implement cppi strategy for buyer cppi_b <- cppi(q = 10, tdate = tr_dates, f = f_gbm, tper = 0.1, rper = 0.1, tcost = 0, int = TRUE) # implement cppi strategy for seller cppi_s <- cppi(q = - 10, tdate = tr_dates, f = f_gbm, tper = - 0.1, rper = 0.1, tcost = 0, int = TRUE)
# CPPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") # implement cppi strategy for buyer cppi_b <- cppi(q = 10, tdate = tr_dates, f = f_gbm, tper = 0.1, rper = 0.1, tcost = 0, int = TRUE) # implement cppi strategy for seller cppi_s <- cppi(q = - 10, tdate = tr_dates, f = f_gbm, tper = - 0.1, rper = 0.1, tcost = 0, int = TRUE)
An S4 class for the CPPI hedging strategy
RiskFactor
The risk factor (cushion) used in the CPPI model
Implements DPPI strategy for commodity price risk management
dppi(q, tdate, f, tper, rper, tcost = 0, int = TRUE)
dppi(q, tdate, f, tper, rper, tcost = 0, int = TRUE)
q |
numeric value for quantity to be hedged, either positive (net buyer) or negative (net seller) |
tdate |
date vector with trading days |
f |
numeric futures price vector |
tper |
numeric target price factor, markup/down to the price on the first trading day |
rper |
numeric risk factor as a percentage of the price on the first trading day |
tcost |
numeric transaction costs pr unit |
int |
TRUE/FALSE integer restriction on tradable volume |
instance of the DPPI class
# DPPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") # implement dppi strategy for buyer dppi_b <- dppi(q = 10, tdate = tr_dates, f = f_gbm, tper = 0.1, rper = 0.1, tcost = 0, int = TRUE) # implement dppi strategy for seller dppi_s <- dppi(q = - 10, tdate = tr_dates, f = f_gbm, tper = - 0.1, rper = 0.1, tcost = 0, int = TRUE)
# DPPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") # implement dppi strategy for buyer dppi_b <- dppi(q = 10, tdate = tr_dates, f = f_gbm, tper = 0.1, rper = 0.1, tcost = 0, int = TRUE) # implement dppi strategy for seller dppi_s <- dppi(q = - 10, tdate = tr_dates, f = f_gbm, tper = - 0.1, rper = 0.1, tcost = 0, int = TRUE)
An S4 class for the DPPI hedging strategy
TargetPercent
A percentage of first trading day's market price used to set target price (cap or floor)
RiskFactor
The risk factor (cushion) used in the DPPI model
An S4 VIRTUAL parent class for the hedging strategy classes in etrm
Name
A string with the portfolio insurance strategy name
Volume
The quantity to be hedged
TargetPrice
The target price(s) for the portfolio (cap or floor)
TransCost
Transaction costs pr unit traded
TradeisInt
TUE/FALSE integer restriction on tradable volume, TRUE sets smallest transacted unit to 1
Results
Data frame with strategy results, daily values for market price, transactions, exposure, position, hedge and portfolio price
Creates a smooth forward curve from futures prices for a flow delivery
msfc(tdate, include, contract, sdate, edate, f, prior = 0)
msfc(tdate, include, contract, sdate, edate, f, prior = 0)
tdate |
trading date |
include |
logical vector to determine if contracts should be included in calculation |
contract |
vector with contract names |
sdate |
date vector with contract delivery start dates |
edate |
date vector with contract delivery end dates |
f |
numeric vector with futures contract prices |
prior |
numeric vector with prior forward price curve |
instance of the MSFC class
# calculate forward curve for synthetic futures contracts, without prior # date for curve calculation and contract information tdate <- as.Date("2021-06-17") include <- rep(TRUE, 10) contract <- c("JUL-21", "AUG-21", "SEP-21", "OCT-21", "NOV-21", "DEC-21", "Q1-22", "Q2-22", "Q3-22", "Q4-22") sdate <- as.Date(c("2021-07-01", "2021-08-01", "2021-09-01", "2021-10-01", "2021-11-01", "2021-12-01", "2022-01-01", "2022-04-01", "2022-07-01", "2022-10-01")) edate <- as.Date(c("2021-07-30", "2021-08-31", "2021-09-30", "2021-10-31", "2021-11-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31")) f <- c(32.55, 32.50, 32.50, 32.08, 36.88, 39.80, 39.40, 25.20, 21.15, 29.50) fwd_curve <- msfc(tdate = tdate, include = include, contract = contract, sdate = sdate, edate = edate, f = f)
# calculate forward curve for synthetic futures contracts, without prior # date for curve calculation and contract information tdate <- as.Date("2021-06-17") include <- rep(TRUE, 10) contract <- c("JUL-21", "AUG-21", "SEP-21", "OCT-21", "NOV-21", "DEC-21", "Q1-22", "Q2-22", "Q3-22", "Q4-22") sdate <- as.Date(c("2021-07-01", "2021-08-01", "2021-09-01", "2021-10-01", "2021-11-01", "2021-12-01", "2022-01-01", "2022-04-01", "2022-07-01", "2022-10-01")) edate <- as.Date(c("2021-07-30", "2021-08-31", "2021-09-30", "2021-10-31", "2021-11-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31")) f <- c(32.55, 32.50, 32.50, 32.08, 36.88, 39.80, 39.40, 25.20, 21.15, 29.50) fwd_curve <- msfc(tdate = tdate, include = include, contract = contract, sdate = sdate, edate = edate, f = f)
An S4 class for the Maximum Smoothness Forward Curve (MSFC) in etrm
Name
A string with the acronym for Maximum Smoothness Forward Curve, "MSFC"
TradeDate
The trading date
BenchSheet
A data frame with futures contracts selected for calculation with MSFC computed prices
Polynomials
The number of polynomials in the MSFC spline
PriorFunc
A numeric vector with the prior function values
Results
A data frame with daily values for the calculated MSFC and contracts in "BenchSheet"
SplineCoef
List with coefficients for the polynomials in the MSFC spline
KnotPoints
Vector with spline knot points
CalcDat
Data frame extending "Results" with daily values for time vectors and polynomial coefficients used in calculation
Implements OBPI strategy for commodity price risk management
obpi( q, tdate, f, k = f[1], vol, r = 0, tdays = 250, daysleft, tcost = 0, int = TRUE )
obpi( q, tdate, f, k = f[1], vol, r = 0, tdays = 250, daysleft, tcost = 0, int = TRUE )
q |
numeric value for quantity to be hedged, either positive (net buyer) or negative (net seller) |
tdate |
date vector with trading days |
f |
numeric futures price vector |
k |
numeric value for option strike price |
vol |
value for volatility |
r |
value for interest rate |
tdays |
integer assumed number of trading days per year |
daysleft |
integer with days left to option expiry |
tcost |
numeric transaction costs pr unit |
int |
TRUE/ FALSE integer restriction on tradable volume |
instance of the OBPI class
# OBPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") #implement obpi strategy for buyer obpi_b <- obpi(q = 10, tdate = tr_dates, f = f_gbm, k = f_gbm[1], vol = 0.2, r = 0, tdays = 250, daysleft = length(f_gbm), tcost = 0, int = TRUE) # implement obpi strategy for seller obpi_s <- obpi(q = - 10, tdate = tr_dates, f = f_gbm, k = f_gbm[1], vol = 0.2, r = 0, tdays = 250, daysleft = length(f_gbm), tcost = 0, int = TRUE)
# OBPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") #implement obpi strategy for buyer obpi_b <- obpi(q = 10, tdate = tr_dates, f = f_gbm, k = f_gbm[1], vol = 0.2, r = 0, tdays = 250, daysleft = length(f_gbm), tcost = 0, int = TRUE) # implement obpi strategy for seller obpi_s <- obpi(q = - 10, tdate = tr_dates, f = f_gbm, k = f_gbm[1], vol = 0.2, r = 0, tdays = 250, daysleft = length(f_gbm), tcost = 0, int = TRUE)
An S4 class for the OBPI hedging strategy
StrikePrice
Strike price for the synthetic option hedging
AnnVol
Annualized volatility for the contract to be traded
InterestRate
Risk-free rate of interest
TradingDays
The number of trading days per year
S4 method for the plot generic for portfolio insurance strategy classes
## S4 method for signature 'GenericStrat' plot( x, y = NULL, title = "Strategy plot", xlab = "", ylab.1 = "Price", ylab.2 = "Hedge %", pcols = c("#F8766D", "steelblue3", "gray60", "gray80"), legend = "bottom" )
## S4 method for signature 'GenericStrat' plot( x, y = NULL, title = "Strategy plot", xlab = "", ylab.1 = "Price", ylab.2 = "Hedge %", pcols = c("#F8766D", "steelblue3", "gray60", "gray80"), legend = "bottom" )
x |
instance of the strategy class created by the corresponding strategy function |
y |
NULL |
title |
plot title |
xlab |
label for x-axis |
ylab.1 |
label for y-axis on price plot in top panel |
ylab.2 |
label for y-axis on hedge plot in bottom panel |
pcols |
vector with four color codes for plot |
legend |
legend position in c("top", "bottom") |
a two-panel chart with daily values for (top panel) target price, market price and portfolio price and (bottom) portfolio hedge rate
S4 method for the plot generic for class "MSFC"
## S4 method for signature 'MSFC' plot( x, y = NULL, plot.prior = FALSE, title = "", xlab = "", ylab = "Price", legend = "right" )
## S4 method for signature 'MSFC' plot( x, y = NULL, plot.prior = FALSE, title = "", xlab = "", ylab = "Price", legend = "right" )
x |
instance of the MSFC class created by the msfc function |
y |
NULL |
plot.prior |
TRUE/FALSE for incuding prior function in plot |
title |
plot title |
xlab |
x-axis title |
ylab |
y-axis title |
legend |
position of legend, as implemented in ggplot2 |
a chart with daily values for the forward curve and contracts used in calculation
A synthetic dataset containing the closing prices and other attributes of 11 power futures contracts for calendar year delivery for 2006 - 2016.
powcal
powcal
A data frame with 3253 rows and 12 columns:
the trading date
the closing price for the 2006 futures contract
the closing price for the 2007 futures contract
the closing price for the 2008 futures contract
the closing price for the 2009 futures contract
the closing price for the 2010 futures contract
the closing price for the 2011 futures contract
the closing price for the 2012 futures contract
the closing price for the 2013 futures contract
the closing price for the 2014 futures contract
the closing price for the 2015 futures contract
the closing price for the 2016 futures contract
A synthetic dataset containing the closing prices and other attributes of 38 power futures contracts.
powfutures130513
powfutures130513
A data frame with 38 rows and 5 columns:
boolean variable to determine if contract should be included in forward curve calculation
the name of the futures contract
delivery start date for the futures contract
delivery start date for the futures contract
the futures contract closing price
An example of two simple priors for forward market price to be used with powfutures130513
powpriors130513
powpriors130513
A data frame with 3885 rows and 3 columns:
vector of dates ranging from 2013-05-13 to final end date of contracts in powfutures130513
a simple smooth trigonometric prior describing power price seasonality
a trigonometric prior adjusted for typical calendar effects
S4 method for the show generic for portfolio insurance strategy classes
## S4 method for signature 'GenericStrat' show(object)
## S4 method for signature 'GenericStrat' show(object)
object |
instance of a strategy class |
a data frame with daily observations for market price, transactions, exposed volume, forward positions, hedge rate, target price and portfolio price
S4 method for the show generic for class "MSFC"
## S4 method for signature 'MSFC' show(object)
## S4 method for signature 'MSFC' show(object)
object |
instance of the MSFC class |
data frame with daily values for forward curve and forward contracts used in calculation
Implements SHPI strategy for commodity price risk management
shpi(q, tdate, f, daysleft, tper, tcost = 0, int = TRUE)
shpi(q, tdate, f, daysleft, tper, tcost = 0, int = TRUE)
q |
numeric value for quantity to be hedged, either positive (net buyer) or negative (net seller) |
tdate |
date vector with trading days |
f |
numeric futures price vector |
daysleft |
integer with days left to contract expiry |
tper |
numeric target price markup/down to the price on the first trading day |
tcost |
numeric transaction costs pr unit |
int |
TRUE/FALSE integer restriction on tradable volume |
instance of the SHPI class
# SHPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") # implement step-hedge strategy for buyer shpi_b <- shpi(q = 10, tdate = tr_dates, f = f_gbm, daysleft = length(tr_dates), tper = 0.1, tcost = 0, int = TRUE) # implement step-hedge strategy for seller shpi_s <- shpi(q = - 10, tdate = tr_dates, f = f_gbm, daysleft = length(tr_dates), tper = - 0.1, tcost = 0, int = TRUE)
# SHPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") # implement step-hedge strategy for buyer shpi_b <- shpi(q = 10, tdate = tr_dates, f = f_gbm, daysleft = length(tr_dates), tper = 0.1, tcost = 0, int = TRUE) # implement step-hedge strategy for seller shpi_s <- shpi(q = - 10, tdate = tr_dates, f = f_gbm, daysleft = length(tr_dates), tper = - 0.1, tcost = 0, int = TRUE)
Implements SLPI strategy for commodity price risk management
slpi(q, tdate, f, tper, tcost = 0, int = TRUE)
slpi(q, tdate, f, tper, tcost = 0, int = TRUE)
q |
numeric value for quantity to be hedged, either positive (net buyer) or negative (net seller) |
tdate |
date vector with trading days |
f |
numeric futures price vector |
tper |
numeric target price markup/down to the price on the first trading day |
tcost |
numeric transaction costs pr unit |
int |
TRUE/FALSE integer restriction on tradable volume |
instance of the SLPI class
# SLPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") # implement stop-loss strategy for buyer slpi_b <- slpi(q = 10, tdate = tr_dates, f = f_gbm, tper = 0.1, tcost = 0, int = TRUE) # implement stop-loss strategy for seller slpi_s <- slpi(q = - 10, tdate = tr_dates, f = f_gbm, tper = - 0.1, tcost = 0, int = TRUE)
# SLPI for a buyer (seller), where stop loss is set 10% above (below) initial market price. set.seed(5) # GBM price process parameters mu <- 0.2 sigma <- 0.1 S0 <- 100 # time Y <- 2 N <- 500 delta <- Y/N t <- seq (0, 1, length = N + 1) # price process and date vector W <- c(0, cumsum ( sqrt(delta) * rnorm (N))) f_gbm <- S0 * exp(mu * t + sigma * W) tr_dates <- seq(Sys.Date(), Sys.Date()+500, by = "day") # implement stop-loss strategy for buyer slpi_b <- slpi(q = 10, tdate = tr_dates, f = f_gbm, tper = 0.1, tcost = 0, int = TRUE) # implement stop-loss strategy for seller slpi_s <- slpi(q = - 10, tdate = tr_dates, f = f_gbm, tper = - 0.1, tcost = 0, int = TRUE)
S4 method for the summary generic for portfolio insurance strategy classes
## S4 method for signature 'GenericStrat' summary(object)
## S4 method for signature 'GenericStrat' summary(object)
object |
instance of a strategy class |
a list with five elements. 1) A string describing the type of portfolio insurance trading strategy and number of observations, 2) volume to be hedged, calculated churn rate (numer of times volume to be hedged has been traded) and 5) a data frame with summary statistics for achieved results
S4 method for the summary generic for class "MSFC"
## S4 method for signature 'MSFC' summary(object)
## S4 method for signature 'MSFC' summary(object)
object |
instance of the MSFC class |
a list with three elements. 1) A string describing length of forward curve, number of polynomials used in spline and trading date, 2) a vector with a sample of the prior used via head(prior) and 3) a data frame with all forward contracts used in the calculation along with computed forward curve prices