Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(I added the required AAPL.txt data at the bottom of the question, thanks!) In this assignment you will write a python program which: Load the

(I added the required AAPL.txt data at the bottom of the question, thanks!)

In this assignment you will write a python program which:

Load the daily stock prices for Apple Corporation (stock ticker AAPL) data from yahoo finance, from Jan 4th 2021 through Dec 31st 2021. note: you will not include Jan 1st-3rd 2020 since its a holiday (New Years day) and a weekend.

Run a mean reversion trading strategy on the data.

Output the results of the strategy including the profits (see sample output below).

You may be unfamiliar with the stock market, or stock market trading. That is ok! You actually have already learned all the python programming you need to accomplish this task. And the mean reversion trading strategy itself is fairly simple.

Mean Reversion (background information)

From Investopedia.com

What is the 'Mean Reversion'

Mean reversion is the theory suggesting that prices and returns eventually move back toward the mean or average. This mean or average can be the historical average of the price or return, or another relevant average such as the growth in the economy or the average returnLinks to an external site. of an industry.

In other words, when the price goes too low, (i.e. 98% of the 5-day average) you buy it, believing the price will increase and return to the average. Likewise, if the price goes too high (i.e. 102% of the 5-day average price) you sell it, believing the price will decrease and return to the average. This view believes the average price is more correct than the current price, and that the current price will come back to the average.

Data Requirements

You will download one years worth of stock market prices for Apple Corporation (stock ticker AAPL) data from yahoo finance, from Jan 4th 2021 through Dec 31st 2021.

To download the prices in a spreadsheet go to:

finance.yahoo.com -> search AAPL -> Historical data -> enter Time Period: start date 01/04/2021, end date 01/01/2022 (one day higher than 12/31/2021) -> Apply -> click Download (right under Apply)

This will save a AAPL.csv file on your local computer.

The csv file will have the columns: Date, Open, High, Low, Close, Adj Close, Volume.

You only want the Adj Close column. Copy all the prices in the Adj Close column. Create a folder in C9 called hw4. Create a file in the hw4 folder called AAPL.txt. Paste the prices into the AAPL.txt file. There should be one price per line and nothing else.

Now you can open your file, and read the lines, from your Python program, like this:

# Note: your file path below needs to match the full file path of the file your created in c9

# Some of your paths will be /home/ubuntu/environment

# You can check that in cloud9 by looking next to the top level folder with the same

# name as your environment

file = open("/home/ec2-user/environment/hw4/AAPL.txt")

lines = file.readlines()

for line in lines:

price = float(line)

price = round(price, 2) # to round the price to 2 decimals

print(price)

Coding Requirements

-Loop through all the prices, calculating a 5 day moving average each time.

-Each iteration of the loop should update the current_price (the newest line), and the 5 day moving average (the average of the previous 5 days).

-Add an if statement which checks to see if the current price is below the 5 day moving average * .98. If it is, buy the stock, meaning keep track of the price you bought at. Update a variable called buy with the buy price.

-Add an elif statement along with the if statement which checks to see if the current price is above the 5 day moving average * 1.02. If it is, sell the stock, meaning calculate the profit of that trade: sell - buy. You want to buy low and sell high, so sell - buy is hopefully positive.

-Add an else statement along with the if else if which does not buy or sell.

# the basic logic, explained above, will look like this

# this is not the complete logic, but you are allowed to copy/paste and start with this

Current_price = #the next price in the list

Avg_price = #average price for the 5 previous days

if current_price < average * 0.98: # .96 if you use 2022 data

#buy

#update buy variable

#update first_buy variable if this is the first time you buy

elif current_price > average * 1.02: # 1.04 if you use 2022 data

#sell

#calculate profit of this individual trade

#keep a running total of all profit

else:

# do nothing this iteration

- Keep track of the first price you buy.

- Keep track of the profit each time you sell. Print out the profit for each trade. Keep a running total of profit from all trades.

- After the loop is complete, calculate the final_profit_percentage and print it to the console:

final_profit_percentage = ( total_profit / first_buy ) * 100

Other Requirements: Round the stocks prices to two decimals using the round() function, as shown in the code example above.

Also round the output so they are two decimals as well.

Add a "%" sign to your output.

Since we're all using the same stock and the same dates, the output should be the same as mine below.

Code Style and Naming Conventions:

You need to follow the style and naming conventions used in class. Name variables with good descriptive names, and give proper spacing and indentation to your code to make it easy to follow. Add good comments to your code, that explain what your program does.

Deliverables in Cloud9: Create a folder, HW4, and put your code (hw4.py) and data (AAPL.txt) in that folder.

Deliverables in Canvas: Submit your console login url, and your ide url, in Canvas.

Grading Criteria: 50 points possible

40 points: Program compiles, runs, and successfully fulfills program requirements outlined above.

10 points: Coding Style, Naming Conventions, and Comments are all descriptive and well organized

Sample Output (for AAPL)

buy at: 125.84 sell at: 130.68 trade profit: 4.84 buy at: 135.69 sell at: 135.99 trade profit: 0.3 buy at: 129.7 sell at: 126.67 trade profit: -3.03 buy at: 119.08 sell at: 122.91 trade profit: 3.83 buy at: 119.48 sell at: 124.8 trade profit: 5.32 buy at: 126.73 sell at: 129.56 trade profit: 2.83 buy at: 141.44 sell at: 147.51 trade profit: 6.07 buy at: 148.14 sell at: 145.73 trade profit: -2.41 buy at: 156.16 sell at: 164.62 trade profit: 8.46 buy at: 171.55 sell at: 175.55 trade profit: 4.0 ---------------------- Total profit: 30.21 First buy: 125.84 Percentage return%: 24.01

--------------------------AAPL DATA (adj close)--------------------

127.680023 129.258606 124.90757 129.169815 130.284729 127.255722 127.078156 129.140213 127.186684 125.440331 126.121132 130.264954 135.040253 137.210861 141.009384 141.246185 140.160889 135.257324 130.195923 132.346771 133.185425 132.149429 135.553329 135.133362 135.28157 134.392273 133.779648 133.522766 133.759903 131.60585 129.283783 128.167221 128.325317 124.501358 124.363014 123.859077 119.550941 119.817719 126.270058 123.631813 120.608208 118.701164 119.975822 114.976006 119.649742 118.552956 120.509399 119.590454 122.515251 124.076469 123.276093 119.096413 118.562836 121.922386 121.082512 118.661644 119.155701 119.768326 119.946198 118.473907 120.697136 121.537025 124.402542 124.708847 126.378738 128.809479 131.418076 129.679031 132.831055 130.459641 132.900284 132.564316 133.236206 131.526764 131.912155 130.370712 132.722397 133.11763 132.791565 131.991196 131.89238 129.896408 130.963547 126.329338 126.576378 128.196869 128.879837 125.554138 124.623749 121.515823 123.693352 126.14801 124.980064 123.57457 123.416214 126.009445 124.148643 125.801598 125.603638 125.554138 124.000191 123.337021 123.010384 123.782433 122.277946 124.603958 124.613846 125.445259 125.831284 124.821701 126.049034 129.147064 128.315628 128.820435 130.443649 129.127274 130.948486 132.611298 132.334152 132.047134 131.750183 133.403137 134.937317 135.560867 135.867706 138.530228 140.569153 143.09314 141.776718 143.627609 143.023849 144.152191 147.626312 146.963165 144.894516 140.994781 144.656967 143.914642 145.300339 147.042374 147.467972 145.270676 143.498917 144.152191 144.369949 144.033432 145.85463 145.448807 145.557678 144.863815 144.814224 144.328506 144.586243 147.589783 147.797958 149.800308 148.878433 145.081894 145.4189 146.895905 148.402618 148.3134 147.064423 146.251587 147.302338 151.782837 150.50412 151.178177 152.308212 152.95256 155.321671 153.755478 152.724564 147.669098 148.244019 146.826508 147.728577 147.490662 144.78447 141.691757 142.177444 144.576324 145.547775 145.636963 144.10051 140.670731 141.582703 140.264328 141.404282 137.924927 139.877716 140.759949 142.038681 141.6521 141.562881 140.274231 139.679474 142.504562 143.575134 145.270233 147.460907 147.956543 148.174622 147.391541 147.34198 148.016037 147.550156 151.237656 148.491852 147.65918 148.70993 150.167099 149.641708 150.177765 149.343887 149.711166 146.842255 146.792633 148.897186 148.907104 149.899811 152.371674 156.719742 159.380234 159.846802 160.233994 160.760117 155.66748 159.07251 164.095627 163.569489 162.566849 160.660828 164.115479 169.93277 173.804367 173.288147 178.142532 174.459564 173.05983 177.993622 171.004898 169.893066 168.513199 171.729584 174.360291 174.995605 179.016113 177.983673 178.073044 176.901627 176.27623

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

3319234609, 978-3319234601

More Books

Students also viewed these Databases questions

Question

2. I have a number of good qualities.

Answered: 1 week ago

Question

Is it clear what happens if an employee violates the policy?

Answered: 1 week ago