I am somewhat new to programming, and am having problems running and getting it to take the input arguments from a list. Here is my current code:
import pandas as pd
import numpy as np
from pandas.io.data import DataReader
from datetime import datetime
def pairfinder(ticker1, ticker2):
symbols = [ticker1, ticker2]
stock_data = DataReader(symbols, "yahoo", datetime(2011,1,1), datetime(2011,12,31))
price = stock_data['Adj Close']
returns = np.log(price / price.shift(1))
diff = (returns[ticker1] - returns[ticker2])**2
mindif = ((1/(returns[ticker1].count()))*diff.sum())
corr = (returns[ticker1].corr(returns[ticker2]))
print(ticker1, ticker2, mindif, corr)
tickers = ['AKSO.OL', 'BWLPG.OL', 'DETNOR.OL', 'DNB.OL', 'DNO.OL', 'FOE.OL', 'FRO.OL']
The function downloads stock data from yahoo finance and places the adjustet close price in a dataframe, and then it calcualtes the returns, and takes the squared difference between them and sums it up. in the end it displayes the two tickers, the result of the summed squared difference, and the correlation between the two stocks.
The problem now is that I want to make this function run through the list of tickers, and I want it to take the first ticker, AKSO.OL and run the function on it and all the rest of the tickers, and then move on to the next one and do the same. I tried to construct a for loop to do this, but I am not that steady using for loops and combining it with functions.
In the end I would like to place the result into another dataframe and save it as a csv file or something like it, but I think I would be able to figure that out in my own if someone could point me in the right direction for the first part of the problem.