Rstudio代写-COMP396
时间:2021-11-08
2021/10/31 Downloads — COMP396 Automated Trading Project
https://student.csc.liv.ac.uk/internal/modules/comp396/download.html 1/1
Downloads
The data
10 anonymized time-aligned daily financial time series
The data is split into three parts chronologically
You get the first part to work with right away
Part 2 of the data is available straight after the design assessment
Part 3 is available as soon as the final strategies have been submitted for assessment 2
Part Number of days When is it distributed
01 1000 Now, in backtester_v5.6.zip
02 1000 After Assessment 1
03 1000 After Assessment 2
Backtester and part 1 data
Here is a zip file called backtester_v5.6.zip
It contains
R code for the backtester
Example strategies
Part 1 of the data
The contents of the zip file are given below.
Detailed comments are given within the .R files. Start by looking at and running main.R (e.g.
with source('main.R'); obviously you need to set the working directory first, e.g. with
setwd("X"), where X should be replaced with the path to the directory created by unzipping
backtester_v5.6.zip, which contains main.R).
You will need the following R packages (which are installed on all lab machines in the CS
department):
quantmod
ggplot2
gridExtra
TABLE OF CONTENTS
Home
Teams and Supervisors
Timetable
Backtester and data
Downloads
The data
Backtester and part 1 data
Budget Limit
Order types
Profit Drawdown (PD) Ratio
Lectures and slides
Exercises
Programming help sessions
Minutes
Assessments
Suggested reading
R resources
Plagiarism and Collusion
SEARCH
Go
Enter search terms.
SHOW SOURCE
Last updated on Sep 24, 2021. Created using Sphinx 1
COMP396 Automated Trading Project
INDEX | NEXT | PREVIOUS
2021/10/31 Budget Limit — COMP396 Automated Trading Project
https://student.csc.liv.ac.uk/internal/modules/comp396/budget.html 1/1
Budget Limit
At the start of trading you have £1,000,000
When you buy units of a series, you will spend some of this money. When you sell units,
you will get some money back.
The current amount of money that you have left is told to you via the info$balance
parameter to your strategy.
If you attempt to spend more money than you have, then the backtester will cancel all
orders for that particular day. It will be as if you never placed any orders at all. A
warning message will be printed if this happens.
Selling happens before buying, so you can sell units and use that money to buy other
units on the same day.
Since all trades happen at tomorrow’s prices, you do not know exactly how much
money will be needed to execute your orders. So even the best strategies may
accidentally try to spend more than they have. Therefore, there will be no penalty for
attempting to spend more than you have.
You may want to include code to estimate how much your trades will cost. The Big
Spender example strategy shows one way of doing this.
Bankruptcy
Your net worth is the sum of your current available cash, and the value of your
holdings.
Your current net worth is given to you in the info$netWorth parameter to your strategy.
The net worth of your strategy is shown in the big graph when you run the back tester.
The smaller graphs show the profit/loss of each individual series.
If you only ever buy (hold positive positions), then your networth will always be positive.
If you short series (hold negative positions), then it is possible for your networth to be
negative.
Shorting is allowed in COMP396.
However, if your net worth falls below 0, then you will be declared bankrupt. Your
strategy will stop trading, it’s assets will be sold, and your (negative) net worth will be
your final net worth.
Check the Bankrupt example strategy, which deliberately tries to go bankrupt by taking
a very large short position, and then buying a very large long position. Try changing the
leverage parameter to see how it affects how quickly the strategy goes bankrupt.
In summary: shorting can boost returns, but you must be careful not to go bankrupt!
TABLE OF CONTENTS
Home
Teams and Supervisors
Timetable
Backtester and data
Downloads
Budget Limit
Bankruptcy
Order types
Profit Drawdown (PD) Ratio
Lectures and slides
Exercises
Programming help sessions
Minutes
Assessments
Suggested reading
R resources
Plagiarism and Collusion
SEARCH
Go
Enter search terms.
SHOW SOURCE
Last updated on Sep 24, 2021. Created using Sphinx 1
COMP396 Automated Trading Project
INDEX | NEXT | PREVIOUS
2021/10/31 Order types — COMP396 Automated Trading Project
https://student.csc.liv.ac.uk/internal/modules/comp396/ordertypes.html 1/2
Order types
Trading decisions are made by close on day k.
You can submit one market order for each series.
In addition you can submit 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))
which is explained in more detail below.
For details of other trading assumptions, see here.
Market orders
The sizes and directions of market orders are encoded in marketOrders, where positive
numbers indicate long, and negative numbers indicate short.
When the trading decision is made by close on day k, the market order is executed at
the open on day k+1 with slippage at 20% of the overnight gap.
Limit orders
You can submit two limit orders for each series.
Limit orders are valid on day k+1 only.
The sizes and directions of the first limit order are encoded in limitOrders1 which has an
identical interpretation as marketOrders (positive numbers indicate long, and negative
numbers indicate short). The corresponding prices are encoded in limitPrices1. For
example, if we have
limitOrders1= c(1,0,0,0,0,0,0,0,0,0)
limitPrices1=c(101.5,0,0,0,0,0,0,0,0,0)
this corresponds to placing a buy limit order for one unit with limit price 101.5.
The sizes and directions of the second limit order are encoded in limitOrders2 and the
corresponding prices in limitPrices2.
A buy limit order is executed if the next day’s low is strictly less than the limit price.
A sell limit order is executed if the next day’s high is strictly greater than the limit price.
A buy limit order that is executed is executed at the minimum of the limit price and the
high of the day.
A sell limit order that is executed is executed at the maximum of the limit price and the
low of the day.
Note that whether a limit order is executed will depend only on the price and not the
order’s size (number of units to trade). This is unrealistic, but simple to implement.
Limit orders are automatically cancelled at the end of the day.
If on the same day, you place a buy limit order and a sell limit order with the price of the
buy limit order below the price of the sell limit order and both are executed, then that
will result in a profit. If the two orders have the same size (number of units to trade)
then no position will result from these orders. If only one of such a pair of orders is
executed then a position will result.
TABLE OF CONTENTS
Home
Teams and Supervisors
Timetable
Backtester and data
Downloads
Budget Limit
Order types
Market orders
Limit orders
Profit Drawdown (PD) Ratio
Lectures and slides
Exercises
Programming help sessions
Minutes
Assessments
Suggested reading
R resources
Plagiarism and Collusion
SEARCH
Go
Enter search terms.
SHOW SOURCE
Last updated on Sep 24, 2021. Created using Sphinx 1
COMP396 Automated Trading Project
INDEX | NEXT | PREVIOUS
2021/10/31 Order types — COMP396 Automated Trading Project
https://student.csc.liv.ac.uk/internal/modules/comp396/ordertypes.html 2/2
2021/10/31 Profit Drawdown (PD) Ratio — COMP396 Automated Trading Project
https://student.csc.liv.ac.uk/internal/modules/comp396/pdratio.html 1/1
Profit Drawdown (PD) Ratio
Performance is measured using the profit drawdown ratio (which we used in Assignment 2
of COMP226) , which we will call the PD ratio for short. It is related to the Calmar Ratio and is
defined as:
the net profit divided by the maximum drawdown of profit/loss (not returns) when the
strategy has made a profit;
the net loss when the stratagy has made a loss (or zero profit).
If cumPnL is a vector of cumulative profit/loss then the maximum drawdown is defined as follows
(just as we saw in COMP226):
maxdrawdown <- function (cumPnL) {
max(cummax(cumPnL)-cumPnL)
}
Then the PD ratio is implemented as follows:
pdratio <- function(cumPnL) {
if (last(cumPnL) > 0)
ret <- last(cumPnL)/maxdrawdown(cumPnL)
else
ret <- last(cumPnL)
return(as.numeric(round(ret,2)))
}
TABLE OF CONTENTS
Home
Teams and Supervisors
Timetable
Backtester and data
Downloads
Budget Limit
Order types
Profit Drawdown (PD) Ratio
Lectures and slides
Exercises
Programming help sessions
Minutes
Assessments
Suggested reading
R resources
Plagiarism and Collusion
SEARCH
Go
Enter search terms.
SHOW SOURCE
Last updated on Sep 24, 2021. Created using Sphinx 1
COMP396 Automated Trading Project
INDEX | NEXT | PREVIOUS
2021/10/31 Exercises — COMP396 Automated Trading Project
https://student.csc.liv.ac.uk/internal/modules/comp396/exercises.html 1/1
Exercises
1. Integrated Development Environment (IDE). Experiment with the backtester in the
RStudio IDE, which is available in the labs and freely available for download for home
use (on Windows, Mac, or Linux).
2. Revision control. Setup a shared Dropbox folder, or a Bitbucket team to share your
source code.
3. Understand the example strategies. Make sure you understand all the example
strategies. It can be very instructive to develop minor adjustments of the strategies and
then implement these adjustments. to share your source code.
4. Use limit orders. Several of the example strategies like rsi_contrarian.R and
bbands_contrarian.R currently use market orders. Extend them to use limit orders. You will
need to decide what to use as the limit prices.
5. Extend the store. In the two example strategies that use the store, it so far only
contains the current iteration and the history of close prices. Extend one of these
strategies to also store a window of all price data and volume data (not just the close).
6. Use open/high/low data. Investigate and implement strategies that also use the
open, high, and low prices as well as the close.
7. Use volume data. You are given volume as well a price data. Investigate strategies
that incorporate this too.
8. Proportion of time in the market. The evaluation of trading strategies (Assessment
2) will give some marks for taking postions. That is, a strategy should be active enough.
The backtest method in backtester.R computes this for you and returns it (and it is printed
at the top of the ggplot2 chart). On some simple strategies monitor the proportion of
time in the market as you vary strategy parameters.
9. Optimization of parameters. Adapt the file main_optimize.R to work on strategies other
than bbands_contrarian.R.
10. Existing indicators. Investigate the indicators in the TTR package, study their
definition and source code. Using some of these indicators develop trading strategies
and investigate their performance.
11. Position sizing. The series have different price levels and different price volatilities.
Investigate methods to choose position sizes across the series so as to avoid unintended
dominance of the trading results of any one series. See Lecture 4 for some ideas.
12. Spending all your money. You have a budget limit of £1,000,000. See what happens
when you spend all of this money by placing very large orders. Think about how you
would avoid spending more than you have. The Big Spender example strategy gives one
way of doing this.
13. Shorting Shorting is allowed in COMP396. Try shorting a stock by sending an order for a
negative position size. Check your available balance before and after (info$balance), you
should see it increase. Be careful though – a strategy that shorts has the possibility of
going bankrupt! Check out the Bankrupt example strategy for an example. Try changing
the leverage parameter of this strategy, and see how it affects how quickly the strategy
goes bankrupt.
14. Correlation of returns. Compute the correlation between the trading strategy returns
of the same trading strategy applied to two different series. Investigate how the
correlation of returns of the underlying series relates to correlation of returns for the
trading strategies that trade them.
15. Producing a trading strategy evaluation report. Using only R code, produce a PDF
evaluation report for a trading strategy. Things you might include are: results of in-
sample parameter optimization, sample equity curves for in-sample and out-of-sample
performance, summary of performance measures for in-sample and out-of-sample
performance, results of robustness tests.
16. Producing a portfolio-level evaluation report. Extend the previous exercise by
producing an evaluation report for multiple trading strategies and series.
TABLE OF CONTENTS
Home
Teams and Supervisors
Timetable
Backtester and data
Lectures and slides
Exercises
Programming help sessions
Minutes
Assessments
Suggested reading
R resources
Plagiarism and Collusion
SEARCH
Go
Enter search terms.
SHOW SOURCE
Last updated on Sep 24, 2021. Created using Sphinx 1
COMP396 Automated Trading Project
INDEX | NEXT | PREVIOUS


essay、essay代写