1

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.

2
  • Hum, could you show me how you call your function please? pairfinder(tickers[0], tickers[1])? Commented Jul 29, 2015 at 11:11
  • I have no called the function, because I was thinking about doing that somewhere in the loop, but I could not figure out how to do that. Commented Jul 29, 2015 at 11:33

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

6 Comments

Your solution worked perfectly, after I dropped the print t1 part since it printet the curent ticker for every iteration throughout the function, now I just need to write the result to a dataframe. Thank you for the help! This website is great.
I am happy to have helped. I have edited my answer properly. Thank you
+1 This is of course the correct solution. It's just the expanded version of what's already available in itertools. See may answer for reference.
yes, in fact I am adding the proper way of doing it, using permutations
No permutations - the order is not important. You just need combinations without repeated elements docs.python.org/2/library/itertools.html#itertools.combinations as in my answer.
|
1

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.

1 Comment

upvoted. It was just permutations instead of combinations.
0

This will iterate over every item in the list tickers, and do something with each item

for tick in tickers:
    doSomething(tick)

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.