Type: | Package |
Title: | Energy Trading and Risk Management |
Version: | 1.0.1 |
Date: | 2021-06-22 |
Author: | Anders D. Sleire |
Maintainer: | Anders D. Sleire <sleire@gmail.com> |
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. |
License: | MIT + file LICENSE |
Encoding: | UTF-8 |
LazyData: | TRUE |
RoxygenNote: | 7.1.1 |
Imports: | ggplot2, reshape2, methods |
Suggests: | testthat, knitr, rmarkdown, markdown |
VignetteBuilder: | knitr |
Depends: | R (≥ 3.5.0) |
NeedsCompilation: | no |
Packaged: | 2021-06-22 13:35:24 UTC; anders |
Repository: | CRAN |
Date/Publication: | 2021-06-23 07:30:05 UTC |
etrm: Energy Trading and Risk Management
Description
Tools for energy market risk management (forward curves and trading strategies)
Author(s)
Anders D. Sleire <sleire@gmail.com>
References
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
An S4 class for the CPPI hedging strategy
Description
An S4 class for the CPPI hedging strategy
Slots
RiskFactor
The risk factor (cushion) used in the CPPI model
An S4 class for the DPPI hedging strategy
Description
An S4 class for the DPPI hedging strategy
Slots
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
Description
An S4 VIRTUAL parent class for the hedging strategy classes in etrm
Slots
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
An S4 class for the Maximum Smoothness Forward Curve (MSFC) in etrm
Description
An S4 class for the Maximum Smoothness Forward Curve (MSFC) in etrm
Slots
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
An S4 class for the OBPI hedging strategy
Description
An S4 class for the OBPI hedging strategy
Slots
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
An S4 class for the SHPI hedging strategy
Description
An S4 class for the SHPI hedging strategy
An S4 class for the SLPI hedging strategy
Description
An S4 class for the SLPI hedging strategy
Constant Proportion Portfolio Insurance (CPPI)
Description
Implements CPPI strategy for commodity price risk management
Usage
cppi(q, tdate, f, tper, rper, tcost = 0, int = TRUE)
Arguments
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 |
Value
instance of the CPPI class
Examples
# 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)
Dynamic Proportion Portfolio Insurance (DPPI)
Description
Implements DPPI strategy for commodity price risk management
Usage
dppi(q, tdate, f, tper, rper, tcost = 0, int = TRUE)
Arguments
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 |
Value
instance of the DPPI class
Examples
# 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)
Maximum Smoothness Forward Curve (MSFC)
Description
Creates a smooth forward curve from futures prices for a flow delivery
Usage
msfc(tdate, include, contract, sdate, edate, f, prior = 0)
Arguments
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 |
Value
instance of the MSFC class
Examples
# 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)
Option Based Portfolio Insurance (OBPI)
Description
Implements OBPI strategy for commodity price risk management
Usage
obpi(
q,
tdate,
f,
k = f[1],
vol,
r = 0,
tdays = 250,
daysleft,
tcost = 0,
int = TRUE
)
Arguments
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 |
Value
instance of the OBPI class
Examples
# 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)
S4 method for the plot generic for portfolio insurance strategy classes
Description
S4 method for the plot generic for portfolio insurance strategy classes
Usage
## 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"
)
Arguments
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") |
Value
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"
Description
S4 method for the plot generic for class "MSFC"
Usage
## S4 method for signature 'MSFC'
plot(
x,
y = NULL,
plot.prior = FALSE,
title = "",
xlab = "",
ylab = "Price",
legend = "right"
)
Arguments
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 |
Value
a chart with daily values for the forward curve and contracts used in calculation
Historical daily closing prices for 11 calendar year power futures contracts
Description
A synthetic dataset containing the closing prices and other attributes of 11 power futures contracts for calendar year delivery for 2006 - 2016.
Usage
powcal
Format
A data frame with 3253 rows and 12 columns:
- Date
the trading date
- CAL-06
the closing price for the 2006 futures contract
- CAL-07
the closing price for the 2007 futures contract
- CAL-08
the closing price for the 2008 futures contract
- CAL-09
the closing price for the 2009 futures contract
- CAL-10
the closing price for the 2010 futures contract
- CAL-11
the closing price for the 2011 futures contract
- CAL-12
the closing price for the 2012 futures contract
- CAL-13
the closing price for the 2013 futures contract
- CAL-14
the closing price for the 2014 futures contract
- CAL-15
the closing price for the 2015 futures contract
- CAL-16
the closing price for the 2016 futures contract
Closing prices for power futures contracts at trading date 2013-05-13
Description
A synthetic dataset containing the closing prices and other attributes of 38 power futures contracts.
Usage
powfutures130513
Format
A data frame with 38 rows and 5 columns:
- Include
boolean variable to determine if contract should be included in forward curve calculation
- Contract
the name of the futures contract
- Start
delivery start date for the futures contract
- End
delivery start date for the futures contract
- Closing
the futures contract closing price
Example priors at trading date 2015-05-13
Description
An example of two simple priors for forward market price to be used with powfutures130513
Usage
powpriors130513
Format
A data frame with 3885 rows and 3 columns:
- Date
vector of dates ranging from 2013-05-13 to final end date of contracts in powfutures130513
- trig.prior
a simple smooth trigonometric prior describing power price seasonality
- mod.prior
a trigonometric prior adjusted for typical calendar effects
S4 method for the show generic for portfolio insurance strategy classes
Description
S4 method for the show generic for portfolio insurance strategy classes
Usage
## S4 method for signature 'GenericStrat'
show(object)
Arguments
object |
instance of a strategy class |
Value
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"
Description
S4 method for the show generic for class "MSFC"
Usage
## S4 method for signature 'MSFC'
show(object)
Arguments
object |
instance of the MSFC class |
Value
data frame with daily values for forward curve and forward contracts used in calculation
Step Hedge Portfolio Insurance (SHPI)
Description
Implements SHPI strategy for commodity price risk management
Usage
shpi(q, tdate, f, daysleft, tper, tcost = 0, int = TRUE)
Arguments
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 |
Value
instance of the SHPI class
Examples
# 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)
Stop Loss Portfolio Insurance (SLPI)
Description
Implements SLPI strategy for commodity price risk management
Usage
slpi(q, tdate, f, tper, tcost = 0, int = TRUE)
Arguments
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 |
Value
instance of the SLPI class
Examples
# 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
Description
S4 method for the summary generic for portfolio insurance strategy classes
Usage
## S4 method for signature 'GenericStrat'
summary(object)
Arguments
object |
instance of a strategy class |
Value
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"
Description
S4 method for the summary generic for class "MSFC"
Usage
## S4 method for signature 'MSFC'
summary(object)
Arguments
object |
instance of the MSFC class |
Value
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