Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMP226 Assignment 2: Strategy Development
Continuous
Assessment
Number
2 (of 2)
Weighting 10%
Assignment
Circulated
Monday 15 April 2024 (week 9)
Deadline Monday 13 May 2024 (week 13)
Submission
Mode
Submit up to two files to the CodeGrade assignment on Canvas: strategy.R
(required to get marks) and results.yaml (optional).
Learning
Outcomes
Assessed
This assignment addresses the following learning outcomes:
• Understand the spectrum of computer-based trading applications and
techniques, from profit-seeking trading strategies to execution algorithms.
• Be able to design trading strategies and evaluate critically their historical
performance and robustness.
• Understand the common pitfalls in developing trading strategies with historical
data.
Summary of
Assessment • The goal is to implement and optimize a well-defined trading strategy within the
backtester_2024 framework.
• Marks are available for the correct implementation of 10 functions in
strategy.R (70%).
• Further marks (that require a correct implementation in strategy.R) are
available for the results of a cross-validated optimisation that you can include in
results.yaml (30%).
• CodeGrade pre-deadline tests and offline example outputs are available to help
you check the correctness of your work.
Submission
necessary to
pass module
No
Late Submission
Penalty
Standard UoL policy; resubmissions after the deadline may not be considered.
Expected time
taken
Roughly 8-12 hours
Before you move on and read more about the assignment and start working on it, please make sure you
have worked through "backtester.pdf" (and the corresponding lectures if you want), which is an intro to the
backtester_2024 framework. Only return to this document when you already have the framework up and
running.
First, let's recall the contents of the backtester_2024.zip:
backtester_2024
nnn DATA
n nnn A2
n n nnn 01.csv
n n nnn 02.csv
n n nnn 03.csv
n n nnn 04.csv
n nnn EXAMPLE
n nnn 01.csv
n nnn 02.csv
n nnn 03.csv
n nnn 04.csv
n nnn 05.csv
nnn a2_main_template.R
nnn a2_periods.R
nnn a2_test_checks_and_getTMA.R
nnn a2_yamls
n nnn rsjs
n n nnn results.yaml
n nnn x1xxx
n n nnn results.yaml
n nnn x1yyy
n n nnn results.yaml
n nnn x1zzz
n nnn results.yaml
nnn example_strategies.R
nnn framework
n nnn backtester.R
n nnn data.R
n nnn processResults.R
nnn main.R
nnn strategies
nnn a2_strategy_template.R
nnn bbands_contrarian.R
nnn bbands_holding_period.R
nnn bbands_trend_following.R
nnn copycat.R
nnn fixed.R
nnn rsi_contrarian.R
10 directories, 28 files
In the above listing, the following files/directories are specifically there for assignment 2:
• a2_main_template.R
• a2_periods.R
• a2_test_checks_and_getTMA.R
• strategies/a2_strategy_template.R
• a2_example_yamls
• DATA/A2
The relevance of these files and directories will be explained below. The rest of the document is split into
three parts:
• Part 1 describes the 10 functions that should be implemented to fully complete strategy.R; you
should start from a2_strategy_template.R;
• Part 2 describes how to create (the optional) results.yaml;
• Part 3 describes submission via CodeGrade and the available pre-deadline tests.
In addition to pre-deadline tests on CodeGrade, example outputs are provided (in this document and as
files) so that you can test whether you have implemented things correctly.
As for assignment 1, the pre-deadline tests will determine your mark for the first part, corresponding to 70%
of the overall marks that are available. Assuming that you have achieved full marks on the first part, the
pre-deadline tests will check that the form of results.yaml is correct, and that it uses the expected student
username (i.e., your one) and corresponding time periods; the pre-deadline tests do not check the
correctness of the other fields in results.yaml, which will be checked post deadline only if you pass the
pre-deadline test for results.yaml. For those other fields, you should use the examples provided (which
are in the subdirectory a2_example_yamls).
Part 1: strategy implementation (70%)
The trading strategy that you should implement is a triple moving average (TMA) momentum strategy, which
is described in slides 4.7. The specification of the strategy and the functions that it should comprise are given
in full detail, so the correctness of your code can and will be checked automatically.
Two template files are provided to get you started:
• strategies/a2_strategy_template.R, which should become the file strategy.R that you
eventually submit;
• a2_main_template.R, which uses DATA/A2 and strategies/a2_strategy_template.R.
If you source a2_main_template.R with no edits to these two files you will get an error:
Error in if (store$iter > params$lookbacks$long) { :
argument is of length zero
This is because the strategy requires a parameter called lookbacks that you will need to pass in from
a2_main_template.R. Read on to see what form this parameter should take, and, more generally, how
you should be editing these two files.
a2_strategy_template.R contains 10 incomplete functions that you need to complete. The first 6
functions (checkE01,..., checkE06) are error checks for the inputs to getTMA. These error checks are all
one-liners, worth 3% each. They are intentionally meant to be straightforward to implement. The next three
functions compute the moving averages (getTMA), use them to compute the position sign
(getPosSignFromTMA), and compute the position size (getPosSize). The final, tenth function,
getOrders combines the last three to implement that actual trading strategy. Recall that every strategy in
the backtester framework has a getOrders function.
The TMA momentum strategy that you should implement uses three moving averages with different
lookbacks (window lengths). The short lookback should be smaller than the medium one, which in turn
should be smaller than the long lookback. In every trading period, the strategy will compute the value of these
three moving averages (for the series that it trades on, which will be determined by params$series). You
will achieve this by completing the implementation of the function getTMA.
The following table indicates the position that the strategy will take depending on the relative values of the
three moving averages (MAs). You will compute this position (sign, but not size) by completing the function
getPosSignFromTMA. The system is out of the market (i.e., flat) when the relationship between the short
MA and the medium MA does not match the relationship between the medium MA and the long MA.
MA MA MA Position
short MA < medium MA < long MA short
short MA > medium MA > long MA long
The function getPosSignFromTMA takes the output of getTMA as input. The position size, i.e., the number
of units to be long or short, is determined by getPosSize. As for all strategies in the backtester framework,
the positions are given to the backtester by getOrders. Here are the detailed specification and marks
available for these 10 functions.