Algorithm 1: Moving Average The first analysis you will perform will be a moving
ID: 3905211 • Letter: A
Question
Algorithm 1: Moving Average
The first analysis you will perform will be a moving average of the stock data. For this analysis assign yourself 1,000 fake US dollars to use to invest as your initial principle amount. Your program should decide to buy stock, sell stock, or take no action based on the average of the stock prices for the previous twenty days.
Your program should not take any action during the first twenty days so that it can use them to calculate your first average. You will use this average to compare to the 21st day. Then your program should recalculate the average of the 20 days starting at 2 and ending at day 21. This pattern should be repeated until the last day of data.
A visual display of the moving average algorithm.
For example: if you notice that the current day stock price is 20% lower than the average of the previous 20 days, you might decide that your program should choose to purchase some stock because you believe it will rise above that price soon. Then, if your algorithm observes that the current stock price is higher by a certain percent than the average, you might decided to have your algorithm sell. The time period of 20 days and other parameters are for you to decide and play with to maximize the amount of money your algorithm generates.
When your algorithm reaches the last day of data, have it sell all remaining stock. Your function will return the number of stocks you own (should be zero, at this point), and your cash balance.
At this point, you can start building up your main() function.
First Iteration Day Day Day Day Day Day Day 19 20 21 20.7187 21.3125 20.6875 22.55 22.09 21.36 19.76 Average of first 20 Days 20.8264299 Compare To Next teration Day Day Day Day Day Day Day 19 20 21 20.7187,21.3125 20.6875 22.55 22.09 21.3619.76 Average of Days 2-21 20.7784949 Compare ToExplanation / Answer
Answer:
import pandas as pd
#load data from the CSV file and display data frame
stockdata=pd.DataFrame.from_csv('AAPL.csv')
print(stockdata.head())
#Find the number of trading days
N=stockdata[ 'price'].count()
#calculating simple moving average
days=20
SMA = pd.Series((stockdata['price']).rolling(window=days).mean(),name = 'SMA')
stockdata = stockdata.join(SMA)
#introduce a new column which is equal to 0.8 times the stock price to compare with SME
stockdata$SMAcomp <-stockdata$SMA*0.8
stockdata=stockdata.join(SMAcomp)
#initial balance is $1000
balance=1000
buyvol=0
sellvol=0
vol=0
for i in range(20,(N-1)):
# on the last trading day sell off everything at the current stock price and exit
if i==N-1:
sellvol =vol
balance =balance +sellvol*df2.iloc[N-1,price]
v= " volume sold in last trading day is % 8.2f:" %(sellvol)
p= " Final balance net of all buy and sell tranaction is %8.2f :"% (balance)
print(v)
print(p)
break
elif df2.iloc[i,price] <= df2.iloc[i,SMAcomp]:
# if current day stock price is 20% less than SMA then buy
buyvol = balance / df2.iloc[i,price]
vol=vol +buyvol
balance=balance -buyvol*df2.iloc[i,price]
else:
#if current price is 20% more than SMA then sell the current stock in possession
sellvol =vol
balance =balance +sellvol*df2.iloc[i,price]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.