0

So I'm currently learning how to analyse financial data in python using numpy, pandas, etc... and I'm starting off with a small script that will hopefully rank some chosen equities by the price change between 2 chosen dates. My first script was:

import numpy as np
import pandas as pd
from pandas_datareader import data as web
from pandas import Series, DataFrame
import datetime
from operator import itemgetter

#Edit below for 2 dates you wish to calculate:
start = datetime.datetime(2014, 7, 15)
end = datetime.datetime(2017, 7, 25)

stocks = ('AAPL', 'GOOGL', 'YHOO', 'MSFT', 'AMZN', 'DAI')

#Getting the data:    
AAPL = web.DataReader('AAPL', 'google', start, end)
GOOGL = web.DataReader('GOOGL', 'google', start, end)
YHOO = web.DataReader('YHOO', 'google', start, end)
MSFT = web.DataReader('MSFT', 'google', start, end)
AMZN = web.DataReader('AMZN', 'google', start, end)
DAI = web.DataReader('DAI', 'google', start, end)

#Calculating the change:
AAPLkey = (AAPL.ix[start]['Close'])/(AAPL.ix[end]['Close'])
GOOGLkey = (GOOGL.ix[start]['Close'])/(GOOGL.ix[end]['Close'])
YHOOkey = (YHOO.ix[start]['Close'])/(YHOO.ix[end]['Close'])
MSFTkey = (MSFT.ix[start]['Close'])/(MSFT.ix[end]['Close'])
AMZNkey = (AMZN.ix[start]['Close'])/(AMZN.ix[end]['Close'])
DAIkey = (DAI.ix[start]['Close'])/(DAI.ix[end]['Close'])

#Formatting the output in a sorted order:
dict1 = {"AAPL" : AAPLkey, "GOOGL" : GOOGLkey, "YHOO" : YHOOkey, "MSFT" : MSFTkey, "AMZN" : AMZNkey, "DAI" : DAIkey}

out = sorted(dict1.items(), key=itemgetter(1), reverse = True)

for tick , change in out:
    print (tick,"\t", change)

I now obviously want to make this far shorter and this is what I've got so far:

import numpy as np
import pandas as pd
from pandas_datareader import data as web
from pandas import Series, DataFrame
import datetime
from operator import itemgetter

#Edit below for 2 dates you wish to calculate:
start = datetime.datetime(2014, 7, 15)
end = datetime.datetime(2017, 7, 25)

stocks = ('AAPL', 'GOOGL', 'YHOO', 'MSFT', 'AMZN', 'DAI')

for eq in stocks:
    eq = web.DataReader(eq, 'google', start, end)

for legend in eq:
    legend = (eq.ix[start]['Close'])/(eq.ix[end]['Close'])

print (legend)

The calculation works BUT the problem is this only outputs the last value for the item in the list (DAI). So what's next in order to get the same result as my first code?

  • 1
    when you set eq/legend inside the for loop, then it would overwrite the earlier values, leaving only the last one. I guess that is the problem here. So, if you save the outputs of for loops in lists then that should work. – Manish Goel Jul 26 '17 at 09:53

3 Answers3

0

You can just move print statement into loop.

Like:

for legend in eq:
    legend = (eq.loc[start]['Close'])/(eq.loc[end]['Close'])
    print(legend)

Improved answer: Get rid of label loop and print values from previous loop:

for eq in stocks:
    df = web.DataReader(eq, 'google', start, end)
    print((df.loc[start]['Close'])/(df.loc[end]['Close']))
snadis
  • 1
  • 2
  • The output I get for that is: `1.28323537643 1.28323537643 1.28323537643 1.28323537643 1.28323537643` – Louis Falconer-Flavin Jul 26 '17 at 09:55
  • But each equity should have different values. Like the results i got from the first code. – Louis Falconer-Flavin Jul 26 '17 at 09:57
  • This does work but displays the data much slower than my original "ugly" code for some reason. Thanks though. – Louis Falconer-Flavin Jul 26 '17 at 10:20
  • @LouisFalconer-Flavin Pase is the same. Just printing now seams slower because I print result after each round of fetching data from web. Process is as follows: 1) get data from web 2) wait for data 3) print result, 4) repeat. In your start example you are printing data after every thing is already fetched form web. – snadis Jul 26 '17 at 11:11
  • @LouisFalconer-Flavin My point is that overall time is the same, just printing is done in different place. – snadis Jul 26 '17 at 11:17
  • Ok sorry I only realised that after cos I'm a noob. But yh it works great now with the sort ect from the original code implemented. Thanks for help. – Louis Falconer-Flavin Jul 26 '17 at 11:25
0

When you loop over stocks at line for eq in stocks, you are saving results into eq. So at each iteration it gets overwritten. You should store the results in a list, like I have done using data.

Then loop over the data list which contains dataframes, and then use proper selection.

import numpy as np
import pandas as pd
from pandas_datareader import data as web
from pandas import Series, DataFrame
import datetime
from operator import itemgetter

# edit below for 2 dates you wish to calculate:
start = datetime.datetime(2014, 7, 15)
end = datetime.datetime(2017, 7, 25)

stocks = ('AAPL', 'GOOGL', 'YHOO', 'MSFT', 'AMZN', 'DAI')

# store all the dataframes in a list
data = []

for eq in stocks:
    data.append(web.DataReader(eq, 'google', start, end))

# print required fields from each dataframe
for df in data:
    print (df.ix[start]['Close'])/(df.ix[end]['Close'])

Output:

0.624067042032
0.612014075932
0.613225417599
0.572179539021
0.340850298595
1.28323537643
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
0

Thanks to the other answers, they both helped lot. This is my final improved script thanks to that help:

import numpy as np
import pandas as pd
from pandas_datareader import data as web
from pandas import Series, DataFrame
import datetime
from operator import itemgetter

# edit below for 2 dates you wish to calculate:
start = datetime.datetime(2014, 7, 15)
end = datetime.datetime(2017, 7, 25)

stocks = ('AAPL', 'GOOGL', 'YHOO', 'MSFT', 'AMZN', 'DAI')

dict1 = {}

for eq in stocks:
    df = web.DataReader(eq, 'google', start, end)
    k = ((df.loc[start]['Close'])/(df.loc[end]['Close']))
    dict1 [eq] = k

out = sorted(dict1.items(), key=itemgetter(1), reverse = True)

for tick , change in out:
    print (tick,"\t", change)