Python Forum
SMA (simple moving avg) Not receiving Data (stock prices).
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SMA (simple moving avg) Not receiving Data (stock prices).
#1
Hello All!
I'm using historical stock prices for which I am using to calculate the SMA, but the SMA returns "None". Could you help figure out why my SMA is not returning a numerical value? Thank you for your help.
'''
The Simple Moving Average (SMA) is calculated
by adding the price of an instrument over a number of time periods
and then dividing the sum by the number of time periods. The SMA
is basically the average price of the given time period, with equal
weighting given to the price of each period.

Simple Moving Average
SMA = ( Sum ( Price, n ) ) / n    

Where: n = Time Period
'''
from tracemalloc import start
from matplotlib import ticker
import pandas_datareader as pdr
import datetime as dt
import statistics as stats

ticker = "AAPL"
start = dt. datetime(2021, 1, 14)
end = dt.datetime(2021, 3, 18)

data = pdr.get_data_yahoo(ticker, start, end)

print(data["Adj Close"])

#================================================================================
close = data['Adj Close']

time_period = 20 # number of days over which to average
history = [] # to track a history of prices
sma_values = [] # to track simple moving average values
for close_price in close:
  history.append(close_price)
  if len(history) > time_period: # we remove oldest price because we only average over last 'time_period' prices
    del (history[0])

  sma_values.append(stats.mean(history))

quotes = sma_values.append(stats.mean(history))
print(quotes)
Error:
Date 2021-01-14 127.957146 2021-01-15 126.200218 2021-01-19 126.885124 2021-01-20 131.054077 2021-01-21 135.858307 2021-01-22 138.042068 2021-01-25 141.863586 2021-01-26 142.101807 2021-01-27 141.009964 2021-01-28 136.076660 2021-01-29 130.984619 2021-02-01 133.148483 2021-02-02 133.992203 2021-02-03 132.949982 2021-02-04 136.374451 2021-02-05 135.951965 2021-02-08 136.101074 2021-02-09 135.206390 2021-02-10 134.590073 2021-02-11 134.331604 2021-02-12 134.570175 2021-02-16 132.403061 2021-02-17 130.066925 2021-02-18 128.943619 2021-02-19 129.102676 2021-02-22 125.255547 2021-02-23 125.116371 2021-02-24 124.609383 2021-02-25 120.275139 2021-02-26 120.543556 2021-03-01 127.034966 2021-03-02 124.380745 2021-03-03 121.338821 2021-03-04 119.420227 2021-03-05 120.702599 2021-03-08 115.672493 2021-03-09 120.374542 2021-03-10 119.271118 2021-03-11 121.239410 2021-03-12 120.314903 2021-03-15 123.257416 2021-03-16 124.828079 2021-03-17 124.022865 2021-03-18 119.817863 Name: Adj Close, dtype: float64 None
Reply
#2
list.append(value) appends value to the end of list and returns None.
quotes = sma_values.append(stats.mean(history))  #<- quotes = None
Reply
#3
Hi,
See the code below.


import yfinance as yf
 
ticker = "AAPL"
date1 = '2010-01-01'   # start and end date 
date2 = '2020-01-01'    
df=yf.download(ticker,date1, date2)

def SMA(data, period=10):
    return data['Close'].rolling(window=period).mean()
#
df['SMA10']=SMA(df)

df.head(11)
Out[895]: 
                Open      High       Low     Close  Adj Close     Volume  \
Date                                                                       
2010-01-04  7.622500  7.660714  7.585000  7.643214   6.535085  493729600   
2010-01-05  7.664286  7.699643  7.616071  7.656429   6.546385  601904800   
2010-01-06  7.656429  7.686786  7.526786  7.534643   6.442255  552160000   
2010-01-07  7.562500  7.571429  7.466071  7.520714   6.430344  477131200   
2010-01-08  7.510714  7.571429  7.466429  7.570714   6.473095  447610800   
2010-01-11  7.600000  7.607143  7.444643  7.503929   6.415992  462229600   
2010-01-12  7.471071  7.491786  7.372143  7.418571   6.343011  594459600   
2010-01-13  7.423929  7.533214  7.289286  7.523214   6.432483  605892000   
2010-01-14  7.503929  7.516429  7.465000  7.479643   6.395229  432894000   
2010-01-15  7.533214  7.557143  7.352500  7.354643   6.288351  594067600   
2010-01-19  7.440357  7.685357  7.401429  7.680000   6.566539  730007600   

               SMA10  
Date                  
2010-01-04       NaN  
2010-01-05       NaN  
2010-01-06       NaN  
2010-01-07       NaN  
2010-01-08       NaN  
2010-01-11       NaN  
2010-01-12       NaN  
2010-01-13       NaN  
2010-01-14       NaN  
2010-01-15  7.520571  
2010-01-19  7.524250  

np.mean(df.Close[0:10])
Out[900]: 7.520571374893189
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 1,913 Aug-13-2023, 12:16 AM
Last Post: cubangt
  Need help with creating dynamic columns with for loops for stock prices PaDat 2 859 Feb-22-2023, 04:34 AM
Last Post: PaDat
  Create simple live plot of stock data dram 2 2,859 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
  Moving data from one Excel to another and finding maximum profit azizrasul 7 1,414 Oct-06-2022, 06:13 PM
Last Post: azizrasul
  Exporting Stock Fundamental Data to a CSV file with yahoo_fin DustinKlent 2 4,638 Aug-01-2022, 06:08 PM
Last Post: paulyan
  Yfinance - Intraday stock data with yf.download diogo_80 2 5,872 Apr-29-2022, 05:07 AM
Last Post: processingclouds
  Receiving snmp traps with more than one Community String ilknurg 0 2,154 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  simple html page with update data korenron 3 2,587 Nov-15-2021, 09:31 AM
Last Post: jamesaarr
  Plotting sum of data files using simple code Laplace12 3 2,992 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Suggestions for a simple data analysis program t4keheart 0 1,761 Mar-08-2021, 03:45 PM
Last Post: t4keheart

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020