英语代写-COMP226-Assignment 2
时间:2021-04-21
COMP226: Assignment 2
Slides 1 of 2: The backtester framework
Rahul Savani
rahul.savani@liverpool.ac.uk
Assignment 2
Continuous
Assessment
Number
2 (of 2)
Weighting 15%
Assignment
Circulated
Monday 19 April 2021 (week 8)
Deadline 17:00 Friday 7th May 2021 (end of week 10)
Submission
Mode
CodeGrade (via Canvas)
Overview of these slides
• There are two sets of slides for this assignment
• This set is the first of these two set
• These slides: an introduction to the backtester_v5.5
framework
• The next set of slides introduces the actual assignment which
asks you to implement and optimize a well-defined trading
strategy within the backtester_v5.5 framework.
Getting started
• Download backtester_v5.5.zip and unzip it
• Go to resulting backtester_v5.5 directory, open R, and set
this directory as the working directoty (e.g. with setwd)
• Now you can run main.R, which allows you to run a number of
example strategies
source('main.R')
main.R
We go through main.R in order and see what each part does:
1. Sourcing the framework and example strategies
2. Loading data
3. Subsetting the data
4. Loading an example strategy
5. Running a backtest
6. Plotting the results
Sourcing
source('framework/data.R')
source('framework/backtester.R')
source('framework/processResults.R')
source('example_strategies.R')
Loading data
# load data
dataList <- getData(directory="EXAMPLE")
> for (x in dataList) print(paste(class(x)[1],start(x),end(x)))
[1] "xts 1970-01-02 1970-07-20"
[1] "xts 1970-01-02 1970-07-20"
[1] "xts 1970-01-02 1970-07-20"
[1] "xts 1970-01-02 1970-07-20"
[1] "xts 1970-01-02 1970-07-20"
> head(dataList[[1]],n=2)
Open High Low Close Volume
1970-01-02 0.7676 0.7698 0.7667 0.7691 3171
1970-01-03 0.7689 0.7737 0.7683 0.7729 6311
Subsetting the data
For assignment 2 you will need to subset the data
There is an example in main.R:
# just use first 200 days
dataList <- lapply(dataList, function(x) x[1:200])
Example strategies
example_strategies.R:
example_strategies <- c("fixed",
"copycat",
"rsi_contrarian",
"bbands_trend_following",
"bbands_contrarian",
"bbands_holding_period")
Loading an example strategy
load_strategy <- function(strategy) {
strategyFile <- file.path('strategies', paste0(strategy,'.R'))
cat("Sourcing",strategyFile,"\n")
source(strategyFile) # load in getOrders
params <<- example_params[[strategy]] # set params via global assignment
print("Parameters:")
print(params)
}
# choose strategy from example_strategies
strategy <- "fixed"
# check that the choice is valid
stopifnot(is_valid_example_strategy(strategy))
# load in strategy and params
load_strategy(strategy) # function from example_strategies.R
fixed.R
getOrders <- function(store, newRowList, currentPos, info, params) {
allzero <- rep(0,length(newRowList))
marketOrders <- allzero
if (is.null(store)) {
marketOrders <- params$sizes
store <- 1 # not null
}
return(list(store=store,marketOrders=marketOrders,
limitOrders1=allzero,
limitPrices1=allzero,
limitOrders2=allzero,
limitPrices2=allzero))
}
getOrders
All strategies are implemented in a function called
getOrders, which always uses these arguments:
getOrders <- function(store,newRowList,currentPos,info,params)
• store: contains all data you save from one period to the next
• newRowList: new day's data (a list of rows from the series)
• currentPos: the vector of current positions in each series
• info: not needed for this assignment
• params: a list of parameters (needed for parameter
optimization)
Running a backtest
# Do backtest
results <- backtest(dataList,getOrders,params,sMult=0.2)
pfolioPnL <- plotResults(dataList,results)
The Profit Drawdown (PD) Ratio
In assignment 2 you will need to optimize the PD ratio.
The PD ratio is stored in pfolioPnL$fitAgg, e.g., for the example
strategy "fixed.R":
> print(pfolioPnL$fitAgg)
[1] -153.44
Market orders
• Recall that market orders specify volume and direction
• In the backtester framework, trading decisions are made after
the close of day k, and trades are executed on day k+1.
• For each day, the framework supports one market order for
each series, and two limit orders for each series.
• These orders are returned from getOrders as follows:
return(list(store=store,marketOrders=marketOrders,
limitOrders1=limitOrders1,
limitPrices1=limitPrices1,
limitOrders2=limitOrders2,
limitPrices2=limitPrices2))
Market orders - example
Market orders will be executed at the open on day k+1. They
incur slippage (20% of the overnight gap for assignment 2).
Market orders are specified by
• size (number of units to trade)
• direction (buy/sell)
The sizes and directions of market orders are encoded in the
vector marketOrders of the return list of getOrders. E.g.
c(0,-5,0,1,0)
means place a market order for 5 units short in series 2, and 1
unit long in series 4.
Limit orders
• We will not use limit orders for assignment 2
• You can leave limitOrders1, limitPrices1, limitOrders2,
limitPrices2 as zero vectors when you do assignment 2
• (We will introduce the limit order functionality in COMP396)
Exercise
Run all the example strategies and play with their
parameters
Next time
The next set of slides will describe the actual assignment, whre
you are asked to:
1. implement a Triple Moving Average (TMA) momentum
strategy in the backtester_v5.5 framework
2. optimise and cross-validate the strategy





























































































































































学霸联盟


essay、essay代写
essay、essay代写