# Generate features for future ML analysis of asset returns

I have built the following code to download stock data from Yahoo Finance. The plan is to then use the built-in pandas functions to calculate metrics from this data that will then be used as features for classification. I also try to calculate whether or not the asset performed better, worse, or equal to the median asset during the next month. I then want to shift this piece of information back to the month the metrics where calculated for. Then from this create a dataset that can be used in a classification algorithm to see if the metrics have any predictive power for whether or not the stock will outperform the median stock for the next month.

I'm new to Python and I feel my code is clunky and inefficient, so I was wondering if anyone could see any obviously better/smoother ways to do this.

My second issue is that I'm not sure if the "time" of the different metrics are preserved through my transformations. It's obvious that for a single sample in the future training set, the metrics all are calculated using the same n-datapoints back in time, and that the recording of whether or not the stock is a winner or loser actually is for that particular stock, one month ahead.

import pandas as pd
import pandas.io.data as web

names = ['AAPL','GOOG','MSFT']

# Functions
def get_px(stock, start, end):
return web.get_data_yahoo(stock, start, end)['Close']

def getWinners(stock, medRet, retsM):
return retsM[stock].shift(-1) >= medRet.shift(-1)

def getStd(stock, rets, period):

pxlol = {n: get_px(n,'1/1/2006','1/1/2014') for n in names}

px = pd.DataFrame(data={n: get_px(n,'1/1/2006','1/1/2014') for n in names})

rets = px.pct_change()

# Start
medRet = retsM.median(axis = 1)

# Generating initial feature dataframes
winners = pd.DataFrame({n: getWinners(n,medRet,retsM) for n in names})
stds = pd.DataFrame({n: getStd(n,rets,20) for n in names})

# Concatentating feature dataframes
retsM = pd.concat(retsM[n] for n in names)
winners = pd.concat(winners[n] for n in names)
stds = pd.concat(stds[n] for n in names)

# Main DataFrame
mainDict = {'Returns':retsM, 'Std.Devs':stds, 'Winners':winners}
frame = pd.DataFrame(mainDict)

• I'm not all that familiar with the pandas package, but I believe getWinners should be more aptly named getWinnersAndLosers since it doesn't just filter out the losers. – Joel Cornett Feb 28 '14 at 12:52
• That is true, the function has is poorly named, but that is not what I need help with. – L1meta Mar 3 '14 at 12:14