Hello all! I was bored and upon my father's challenge I made a program that tells you the 20 day SMA for a specific date, I only have 100 days of data but can up that to over 8000 whenever I get my logic sorted out. The problem is I have to put the date in as a number: ex) 5. That would give me the SMA for 11/30/2009, however I want to be able to input the date in '11/30/2009' format and receive the moving average that way. I'm a bit confused as to how I should 'tie' my lists together so that this is possible.

Please excuse my poor coding.

list = [8.64,
8.73,
8.94,
8.98,
8.71,
8.41,
8.2,
8.33,
8.24,
8.18,
7.75,
7.45,
7.27,
7.44,
7.58,
7,
7.3,
6.96,
7.33,
7.47
]



list2 = [13.29,
13.73,
14.1,
13.49,
13.4,
13.34,
12.91,
12.82,
12.8,
12.93,
13,
12.79,
12.69,
12.22,
12.41,
11.74,
11.78,
11.73,
11.6,
11.21,
11.29,
11.38,
11.49,
11.32,
11.12,
11.18,
10.94,
11.15,
10.97,
10.91,
11.06,
11.64,
11.39,
11.12,
10.84,
11.41,
11.55,
11.19,
11.03,
10.52,
11.18,
11.51,
11.75,
11.6,
11.76,
11.68,
11.87,
12.11,
11.69,
11.66,
11.37,
10.96,
10.28,
10,
9.99,
10.11,
10.2,
10.13,
10.08,
9.9,
9.67,
9.68,
9.39,
9.49,
9.39,
9.08,
9,
9.05,
8.86,
8.82,
8.91,
8.94,
8.94,
9.01,
8.88,
8.89,
8.73,
8.81,
8.81,
8.73
]

date = ['3/19/2010',
'3/18/2010',
'3/17/2010',
'3/16/2010',
'3/15/2010',
'3/12/2010',
'3/11/2010',
'3/10/2010',
'3/9/2010',
'3/8/2010',
'3/5/2010',
'3/4/2010',
'3/3/2010',
'3/2/2010',
'3/1/2010',
'2/26/2010',
'2/25/2010',
'2/24/2010',
'2/23/2010',
'2/22/2010',
'2/19/2010',
'2/18/2010',
'2/17/2010',
'2/16/2010',
'2/12/2010',
'2/11/2010',
'2/10/2010',
'2/9/2010',
'2/8/2010',
'2/5/2010',
'2/4/2010',
'2/3/2010',
'2/2/2010',
'2/1/2010',
'1/29/2010',
'1/28/2010',
'1/27/2010',
'1/26/2010',
'1/25/2010',
'1/22/2010',
'1/21/2010',
'1/20/2010',
'1/19/2010',
'1/15/2010',
'1/14/2010',
'1/13/2010',
'1/12/2010',
'1/11/2010',
'1/8/2010',
'1/7/2010',
'1/6/2010',
'1/5/2010',
'1/4/2010',
'12/31/2009',
'12/30/2009',
'12/29/2009',
'12/28/2009',
'12/24/2009',
'12/23/2009',
'12/22/2009',
'12/21/2009',
'12/18/2009',
'12/17/2009',
'12/16/2009',
'12/15/2009',
'12/14/2009',
'12/11/2009',
'12/10/2009',
'12/9/2009',
'12/8/2009',
'12/7/2009',
'12/4/2009',
'12/3/2009',
'12/2/2009',
'12/1/2009',
'11/30/2009',
'11/27/2009',
'11/25/2009',
'11/24/2009',
'11/23/2009',
]

date.reverse()

def sma():
    count = 1
    n = 0
    list2.reverse()
    list.reverse()
    while count <= 5:
        
        for a in list2:
            a = list2[n]

        list.append(a)    
        list.pop(0)

        x = sum(list)
        avg = x/20
        
        count = count +1
        n = n+1
        
    print ('20 day SMA for', date[count-2], 'was', avg)
        
sma()

Play a little bit with Datetime module. There lies an answer. Long time haven't played with Python. So sorry I'm lagging somehow behind Pythonners

I'm confused as to how datetime will help me. Any hints/nudges?

He possibly means subtracting Jan 1st from today.

import datetime

today = datetime.datetime.now()
date_diff = today - datetime.datetime(today.year, 1, 1)
days_of_year = date_diff.days + 1
print days_of_year

I've made a MySQL database and am wondering if I could make my python code grab the data from there. Thoughts?

I've made a MySQL database and am wondering if I could make my python code grab the data from there. Thoughts?

Sure you can.
Check mySQLDb then Read this then this

Sorry but to be a programmer you have to read a lot

That's fine, I like reading. Thanks for the links.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.