1
$\begingroup$

I am building a simple stock model with Pandas and part of that is calculating moving averages.

I would like to understand what are and how to measure accuracy implications of using strict vs dynamic time window size when calculating long-term moving averages.

Here is an example what I mean:

I have a csv file with TLSA stock history between 1.1.2000 - 28.2.2017

df = pd.read_csv('TSLA.csv', index_col=0, parse_dates=True)
# add new col to dataframe consisting of a 100 day moving avarage
df['100ma'] = df['Adj Close'].rolling(window=100, min_periods=0).mean()

min_periods=0 allows me to calculate moving average for the first 100 days where don't have 100 days using first 0, 1, 2, 3... moving average.

My alternative to this is call df.dropna() it would drop first 100 rows that don't have a 100-day moving average. Downside I would have exact average but fewer data points.

I am building a tool using a large number of moving averages. What would be a correct method to figure which data frame to use? Correlation to a moving average of a representing stock index? Also which technique is usually applied in the quant world?

Intuitively I think dynamic version is more representing but is that true also with minute and shorter timeframe data?

$\endgroup$
1
$\begingroup$

What I do in my code is to calculate the MA for "whatever number of days is available or 100 whichever is smaller". But that is just a pragmatic hack to make sure my program produces some value instead of dying of unexplained causes, and matches the "correct" values once T>= 100. I would not claim that this is the definitive solution by any means. The routine should probably return a Warning Code that the full MA cannot yet be computed, so the caller can disregard those fake values if he wants. Of course before the date of TSLA IPO I return a Hard Error code saying "can't do it". This is programming style rather than Quant Finance.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.