Reputation: 471
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.
Upvotes: 1
Views: 123
Reputation: 11590
if I understand your problem correctly, this may do:
for t1 in tickers:
for t2 in tickers:
if t2 == t1:
continue
print "t1=%s, t2=%s" % (t1, t2)
pairfinder(t1, t2)
produces
t1=AKSO.OL, t2=BWLPG.OL
t1=AKSO.OL, t2=DETNOR.OL
t1=AKSO.OL, t2=DNB.OL
t1=AKSO.OL, t2=DNO.OL
t1=AKSO.OL, t2=FOE.OL
t1=AKSO.OL, t2=FRO.OL
t1=BWLPG.OL, t2=AKSO.OL
t1=BWLPG.OL, t2=DETNOR.OL
t1=BWLPG.OL, t2=DNB.OL
t1=BWLPG.OL, t2=DNO.OL
t1=BWLPG.OL, t2=FOE.OL
t1=BWLPG.OL, t2=FRO.OL
t1=DETNOR.OL, t2=AKSO.OL
t1=DETNOR.OL, t2=BWLPG.OL
t1=DETNOR.OL, t2=DNB.OL
t1=DETNOR.OL, t2=DNO.OL
t1=DETNOR.OL, t2=FOE.OL
t1=DETNOR.OL, t2=FRO.OL
t1=DNB.OL, t2=AKSO.OL
t1=DNB.OL, t2=BWLPG.OL
t1=DNB.OL, t2=DETNOR.OL
t1=DNB.OL, t2=DNO.OL
t1=DNB.OL, t2=FOE.OL
t1=DNB.OL, t2=FRO.OL
t1=DNO.OL, t2=AKSO.OL
t1=DNO.OL, t2=BWLPG.OL
t1=DNO.OL, t2=DETNOR.OL
t1=DNO.OL, t2=DNB.OL
t1=DNO.OL, t2=FOE.OL
t1=DNO.OL, t2=FRO.OL
t1=FOE.OL, t2=AKSO.OL
t1=FOE.OL, t2=BWLPG.OL
t1=FOE.OL, t2=DETNOR.OL
t1=FOE.OL, t2=DNB.OL
t1=FOE.OL, t2=DNO.OL
t1=FOE.OL, t2=FRO.OL
t1=FRO.OL, t2=AKSO.OL
t1=FRO.OL, t2=BWLPG.OL
t1=FRO.OL, t2=DETNOR.OL
t1=FRO.OL, t2=DNB.OL
t1=FRO.OL, t2=DNO.OL
t1=FRO.OL, t2=FOE.OL
Basically it runs through the list of tickers and it applies the function to each ticker (t1) and all the other tickers (t2) skipping t1.
Note: it allows repetitions of the same pairs (i.e. t1, t2 differs from t2, t1). In case that is not the desired behavior, please use itertools.combinations
(as described below in another answer you got).
In fact, the best way to do it is through itertools.permutations
import itertools
for ticker1, ticker2 in itertools.permutations(tickers, 2):
print "t1=%s, t2=%s" % (ticker1, ticker2)
Upvotes: 2
Reputation: 11290
Enjoy:
import itertools
for ticker1, ticker2 in itertools.combinations(tickers, 2):
pairfinder(ticker1, ticker2)
Itertools is one of the most useful modules in Python's standard library. It saved me from a headache multiple times.
Upvotes: 1
Reputation: 67733
replace this
def pairfinder(ticker1, ticker2):
symbols = [ticker1, ticker2]
with this
def pairfinder(symbols):
voila, your function now takes an arbitrarily long list. Now, take the last chunk of the function, that really does operate on exactly two symbols, and move it into another function:
def calcpair(ticker1, ticker2, stock_data):
price = stock_data['Adj Close']
# etc.
so now pairfinder
just needs to pass calcpair
every possible pair, along with the right subset of stock_data
. ElmoVanKielmo showed a good way to get the pairs - if you get pairs of indices instead, you can use them to subscript both symbols and the various stock_data fields (or you could just pass the indices to calcpair
in the first place, along with the symbols
list).
Upvotes: 0
Reputation: 818
This will iterate over every item in the list tickers, and do something with each item
for tick in tickers:
doSomething(tick)
Upvotes: 0