Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP226 Assignment 2: Strategy Development

First, let's recall the contents of the backtester_2022.zip:


backtester_2022

├── DATA

├── A2

├── 01.csv

├── 02.csv

├── 03.csv

└── 04.csv

└── EXAMPLE

├── 01.csv

├── 02.csv

├── 03.csv

├── 04.csv

└── 05.csv

├── a2_main_template.R

├── a2_periods.R

├── a2_test_checks_and_getTMA.R

├── a2_yamls

├── rsjs

└── results.yaml

├── x1xxx

└── results.yaml

├── x1yyy

└── results.yaml

└── x1zzz

│        └── results.yaml

├── example_strategies.R

├── framework

├── backtester.R

├── data.R

└── processResults.R

├── main.R

└── strategies

├── a2_strategy_template.R

├── bbands_contrarian.R

├── bbands_holding_period.R

├── bbands_trend_following.R

├── copycat.R

├── fixed.R

└── 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 shoud 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.


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.