SMA (simple moving avg) Not receiving Data (stock prices). - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: SMA (simple moving avg) Not receiving Data (stock prices). (/thread-36903.html) |
SMA (simple moving avg) Not receiving Data (stock prices). - gdbengo - Apr-10-2022 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)
RE: SMA (simple moving avg) Not receiving Data (stock prices). - deanhystad - Apr-10-2022 list.append(value) appends value to the end of list and returns None. quotes = sma_values.append(stats.mean(history)) #<- quotes = None RE: SMA (simple moving avg) Not receiving Data (stock prices). - paulyan - Jul-31-2022 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 |