R代写-RMSC5001
时间:2022-05-03
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
Thu 31 March 2022 (week 9)
Deadline 17:00 Fri 13th May 2022 (week 12)
Submission
Mode
CodeGrade (via Canvas)
Two handouts / sets of slides
For this assignment, there are two handouts and two
corresponding sets of slides with videos:
1. backtester.pdf and a2_slides_01.pdf (these slides) provide
an an introduction to the backtester_2022 framework
2. a2.pdf and a2_slides_02.pdf describes the assignment,
which asks you to implement and optimize a trading strategy
within the backtester_2022 framework
Please read the handouts carefully and in full
Getting started -- main.R
• Download backtester_2022.zip and unzip it
• Go to resulting backtester_2022 directory, open R, and set
this directory as the working directoty (e.g. with setwd)
• Now you can run example strategies via main.R:
source('main.R')
1. It sources the framework and example strategy helper
2. Loads data and subsets it; loads an example strategy
3. Running a backtest, and plots the results
Sourcing
source('framework/data.R')
source('framework/backtester.R')
source('framework/processResults.R')
source('example_strategies.R')
Loading and subsetting 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=1)
Open High Low Close Volume
1970-01-02 1903.75 1917.75 1902.75 1915 39101
# just use first 200 days
dataList <- lapply(dataList, function(x) x[1:200])
Example strategies
example_strategies.R contains helper functions for:
example_strategies <- c("fixed",
"copycat",
"rsi_contrarian",
"bbands_trend_following",
"bbands_contrarian",
"bbands_holding_period")
The helper functions are used in main.R as follows:
# 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 via 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: list of parameters (facilitates parameter optimization)
Running a backtest
# Do backtest
results <- backtest(dataList,getOrders,params,sMult)
plotResults(dataList,results)
cat("Profit:", results$aggProfit, '\n')
Market and limit orders
• In the framework, trading decisions are made after the close
of day k, and trades are executed on day k+1.
• Each day, the framework allows one market order for each
series, and two limit orders for each series; these are
returned from getOrders as follows:
return(list(store=store,marketOrders=marketOrders,
limitOrders1=limitOrders1,
limitPrices1=limitPrices1,
limitOrders2=limitOrders2,
limitPrices2=limitPrices2))
• We will not use limit orders for assignment 2 (leave
limitOrders1, limitPrices1, limitOrders2, limitPrices2
as zero vectors)
Market orders - example
• Market orders will be executed at the open on day k+1
(incurring slippage as described in slides 4.5)
• Their size (number of units to trade) and direction (buy/sell)
are specified in the vector marketOrders of signed numbers,
which is returned by getOrders. E.g.
c(0,-5,0,1,0)
represents a market order for 5 units short in series 2, and 1
unit long in series 4
Exercise
Run the example strategies and play with their parameters
The second set of slides describes the assignment, where you will
implement and optimise the Triple Moving Average (TMA)
momentum strategy from slides 4.7
essay、essay代写