1. Homepage
  2. Programming
  3. [2022] COMP226 Computer-Based Trading in Financial Markets - Assignment 2: Strategy Development

[2022] COMP226 Computer-Based Trading in Financial Markets - Assignment 2: Strategy Development

Engage in a Conversation
University of LiverpoolCOMP226Computer-Based Trading in Financial MarketsR

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 CourseNana.COM

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). CourseNana.COM

  CourseNana.COM

Part 1: strategy implementation (70%) CourseNana.COM

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. CourseNana.COM

  CourseNana.COM

Two template files are provided to get you started: CourseNana.COM

• strategies/a2_strategy_template.R, which should become the file strategy.R that you eventually submit; CourseNana.COM

• a2_main_template.R, which uses DATA/A2 and strategies/a2_strategy_template.R. CourseNana.COM

  CourseNana.COM

If you source a2_main_template.R with no edits to these two files you will get an error: CourseNana.COM

Error in if (store$iter > params$lookbacks$long) { : CourseNana.COM

argument is of length zero CourseNana.COM

  CourseNana.COM

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 CourseNana.COM

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. CourseNana.COM

  CourseNana.COM

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. CourseNana.COM

  CourseNana.COM

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. CourseNana.COM

  CourseNana.COM

MA                               MA                               MA                               Position CourseNana.COM

short MA           <          medium MA       <          long MA                        short CourseNana.COM

short MA           >          medium MA       >          long MA                        long CourseNana.COM

  CourseNana.COM

Example output for checkE01 ... checkE06 and getTMA CourseNana.COM

The file a2_test_checks_and_getTMA.R is provided to give you guidance on how you can test the six functions, checkE01 ... checkE06. For each one, two tests are provided: for a correct implementation, one test should produce TRUE and the other FALSE. (You don't need to use these tests, as you can also just rely on the tests on CodeGrade.) CourseNana.COM

  CourseNana.COM

To use these tests, first source a2_test_checks_and_getTMA.R and also source the implementations that you would like to test. The tests that should return TRUE are test_checkE01() ... test_checkE06(); for tests that should return FALSE, there is single function, test_pass_all_checks, which takes the function to test as its only argument. CourseNana.COM

  CourseNana.COM

Here's an example of both types of test for E01 (where a correct implementation of checkE01 has been sourced): CourseNana.COM

  CourseNana.COM

> test_checkE01() CourseNana.COM

[1] TRUE CourseNana.COM

> test_pass_all_checks(checkE01) CourseNana.COM

[1] FALSE CourseNana.COM

  CourseNana.COM

The way these tests work is clear from the source code in a2_test_checks_and_getTMA.R: CourseNana.COM

############################################################################### CourseNana.COM

# Source the functions that you would like to test, e.g., with CourseNana.COM

# source('strategies/a2_strategy_template.R') or source('strategies/strategy.R') CourseNana.COM

############################################################################### CourseNana.COM

source('framework/data.R'); dataList <- getData(directory="A2") CourseNana.COM

prices <- dataList[[1]] CourseNana.COM

prices_19_rows <- dataList[[1]]$Close[1:19] CourseNana.COM

prices_20_rows <- dataList[[1]]$Close[1:20] CourseNana.COM

prices_20_rows_renamed <- prices_20_rows CourseNana.COM

colnames(prices_20_rows_renamed) <- 'Closed' CourseNana.COM

bad_prices <- c(1,2,3) CourseNana.COM

lookbacks_no_names <- list(5,10,25) # list elements not named CourseNana.COM

lookbacks_not_integer <- list(short=5,medium=as.integer(10),long=as.integer(20)) CourseNana.COM

lookbacks_wrong_order <- list(short=as.integer(15),medium=as.integer(10),long=as.integer(20)) CourseNana.COM

lookbacks <- list(short=as.integer(5),medium=as.integer(10),long=as.integer(20)) CourseNana.COM

test_checkE01 <- function() CourseNana.COM

  CourseNana.COM

Part 2: cross-validation (30%) CourseNana.COM

  CourseNana.COM

In this part of the assignment you are asked to do a cross-validated parameter optimization of profit, where you will use an in-sample and out-of-of-sample time period. Every student has their own in-sample and out-of-sample periods based on their MWS username (only the part before the @, e.g., for Rahul Savani, this username is rsjs, rather than the full email form rsjs@liverpool.ac.uk). By having different time periods for different sutdents, there is not one single correct results.yaml. CourseNana.COM

  CourseNana.COM

To get your in-sample and out-of-sample periods, use a2_periods.R as follows. Source it and run the function getPeriods with your MWS username as per the following example (where we use the fake username "x1xxx"). Use startIn, endIn, startOut, and endOut as the start and end of the in-sample and out-of-sample periods respectively. CourseNana.COM

  CourseNana.COM

> source('a2_periods.R') CourseNana.COM

> getPeriods('x1xxx') CourseNana.COM

$startIn CourseNana.COM

[1] 1 CourseNana.COM

$endIn CourseNana.COM

[1] 884 CourseNana.COM

$startOut CourseNana.COM

[1] 885 CourseNana.COM

$endOut CourseNana.COM

[1] 2000 CourseNana.COM

  CourseNana.COM

You will do two parameter sweeps. One on your in-sample period, and one on your out-of-sample period (normally one doesn't do a sweep on the out-of-sample period in practice; we do it here to allow detailed cross-period performance analysis). The sweep will be over the following parameters: the short, medium, and long lookbacks, and the subset of series that are traded on. CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
University of Liverpool代写,COMP226代写,Computer-Based Trading in Financial Markets代写,R代写,University of Liverpool代编,COMP226代编,Computer-Based Trading in Financial Markets代编,R代编,University of Liverpool代考,COMP226代考,Computer-Based Trading in Financial Markets代考,R代考,University of Liverpoolhelp,COMP226help,Computer-Based Trading in Financial Marketshelp,Rhelp,University of Liverpool作业代写,COMP226作业代写,Computer-Based Trading in Financial Markets作业代写,R作业代写,University of Liverpool编程代写,COMP226编程代写,Computer-Based Trading in Financial Markets编程代写,R编程代写,University of Liverpoolprogramming help,COMP226programming help,Computer-Based Trading in Financial Marketsprogramming help,Rprogramming help,University of Liverpoolassignment help,COMP226assignment help,Computer-Based Trading in Financial Marketsassignment help,Rassignment help,University of Liverpoolsolution,COMP226solution,Computer-Based Trading in Financial Marketssolution,Rsolution,