Python算法与数据结构代写 | Data Structures and Algorithms Homework 2


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:

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:

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:

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.

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 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

Complete the function moving_average in 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

Complete the function compare_mas in 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

Complete the function ma_strategy in 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.

You can test your function with