本次代写是Python算法与数据结构的一个assignment

## Using OK

We use a program called OK for assignment feedback. You should have OK in the starter files downloaded for this assignment. To use OK to test a specified function, run the following command:

`python ok -q function_name`

By default, only tests that did not pass will show up. You can use the `-v`

option to show all tests, including tests you have passed:

`python ok -q function_name -v`

## First part: Trading based on technical analysis

Some stock traders vouch for so-called “technical analysis”: using mathematics and statistics to predict stock-price movements from historical data.

Others say that this is like driving a car by only looking at the rear-view mirror.

You will build a trading strategy based on technical analysis of stock prices and explore which group is right.

The technical analysis idea we’ll study is using so-called moving-average strategies. A moving average is defined as follows. Consider a daily quoted time series of a stock price, pt, eg the daily adjusted closing prices for the stock. An n-step (or n-lag) moving average is the average of the n last values of the price. More precisely, the n-step moving average is defined as follows

The one-step MA is simply the current price: MA1(t)=pt, and the two-step MA is the average of the current price and the previous day’s price. Tf the moving average span n is short, the MA reacts quickly to any shocks to the stock price. If the span n is long, it reacts slowly. If the moving average spans the stock’s entire period of existence, it is just the all-time average price.

The figure below shows an example of the Nasdaq index’s development, along with two different moving averages. Notice how the two-step moving average follows the index more closely than the five-step one, which reacts to price changes more slowly and less strongly.

A theory put forward by proponents for technical analysis has it that moving averages give an indication of the “momentum” of a price. In particular, by comparing two moving averages of different lengths, one should be able to infer that the momentum of a the stock is changing, giving a signal when to buy or sell the stock.

This comparison between two moving averages is done by finding so-called *crossover points* between different moving averages. This works as follows:

- We keep track of two moving averages of different lengths: a shorter one (a lower lag nL, eg nL=2 in the figure) and a longer one (higher lag nH, eg nH=5 in the figure)
**When the shorter MA “crosses” the longer one**(it has been lower but becomes higher), this is a**signal to buy the stock**as it seems to be gaining momentum. In the figure, these points are the ones where the green line has been lower than the red line but then crosses it to become higher, eg around 29 June. More specifically, these are defined as follows. If for any time t, MAnH(t−1)≥MAnL(t−1) and MAnL(t)>MAnH(t), you should buy the stock.- Conversely, when the longer MA “crosses” the shorter one in the same way, this is a signal to sell the stock as it seems to be losing momentum. In the figure, these are the points in time where the red line has been lower than the green one, but then becomes higher, eg around 10 June.

Inspecting the figure, it looks like those dates may indeed have been good times to trade. But in general, can you make money based on such strategies? Your task is to evaluate this claim by writing a Python script that calculates two moving averages of a (historical) stock/index price, finds all the crossover points, and finally performs trades based on those points.

Some advocates of technical analysis claim, for example, that by looking at the 20-day and 50-day moving averages of a stock price, one can identify longer trends and benefit from them. Let’s create a trading strategy to this idea.

### Question 1: Buy and hold (2p)

A standard benchmark strategy is buy and hold: you buy the stock in the beginning of the horizon and hold it until the end. Complete the function `buy_and_hold`

in `hw02.py`

. The function takes as input a list of prices, a starting index, and a starting cash position. It should return the value of the final position if the entire cash position is used to purchase stock at the starting index.

You can test your function with

`python ok -q buy_and_hold -v`

### Question 2: Moving average (6p)

Complete the function `moving_average`

in `hw02.py`

. The function takes as input a list of prices and the step n, and return the n-step moving average as a list, calculated using the formula above. We cannot calculate the moving average for the first n−1 steps so these values in the list should be set to `None`

. For example, a three-step moving average can only be calculated for the third price, so there would be two `None`

values in the beginning of the list.

**Please do not use Python libraries such as numpy or pandas for calculating moving averages (but you may want to use the sum function on a list).**

You can test your function with

`python ok -q moving_average -v`

### Question 3: Comparing MAs for crossovers (6p)

Complete the function `compare_mas`

in `hw02.py`

. The function takes as input two lists `ma1`

and `ma2`

(of moving averages of prices) and returns a list of indicator numbers by comparing the elements pairwise. Each indicator in the returned list takes either the value 1 if the value in `ma1`

is strictly greater or 0 if not. A crossover occurs when the indicator changes value. The moving averages may contain `None`

-values in the beginning. If either value is `None`

, the indicator is `None`

.

You can test your function with

`python ok -q compare_mas -v`

### Question 4: Trading based on moving averages (6p)

Complete the function `ma_strategy`

in `hw02.py`

. The function takes as input an initial cash position (a float), a list of prices, and a list of comparison indicators as described above. It uses these data to make trades at crossover points of the moving averages as follows.

- Start out with initial cash position. Look for the first crossover with an indication to buy stock.
- If the indicator variable at the previous index was 0 and becomes 1, this is an indication of a moving average crossover, to buy stock.
`None`

values count as neither 0 nor 1. - If the indicator variable at the previous index was 1 and becomes 0, this is the opposite crossover and an indication to sell stock.
- When you buy stock, you buy with our entire cash position. You will then hold stock only, and zero cash. You may assume that whenever a crossover happens, you’re able to make a trade at the current day’s price, without waiting for the next day.
- If you hold stock, the value of your position fluctuates with the value of the stock price. You look for the next crossover which gives an indication to sell stock.
- If there is an indication to sell, you convert all stock to cash at the current stock price.
- As a result, you will always either hold cash or hold stock, but never both. If you hold cash, the value of your position stays constant.
- Assume you can buy fractional amounts of stocks, and there are no trading fees.

You can test your function with

`python ok -q ma_strategy -v`

**程序辅导定制C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB**

本网站支持 Alipay WeChatPay PayPal等支付方式

**E-mail:** vipdue@outlook.com **微信号:**vipnxx

如果您使用手机请先保存二维码，微信识别。如果用电脑，直接掏出手机果断扫描。