结合三个函数

时间:2017-02-09 12:57:15

标签: python list function for-loop

我创建了以下脚本: 1. function1从雅虎财务中获取数据并将其放入DataFrame并为所有股票执行此操作 2. function2执行滚动回归并将残差保存到列表中。 我想为function1中的每个库存重复功能2,获取残差的标准差,同时将输出保存到最终列表。 我似乎无法弄清楚如何组合这些功能。任何帮助都会非常感激。

更新:

  • 我现在遇到的问题是该脚本仅适用于stock_list中的最后一个库存。 IE浏览器。该脚本只将一个数字附加到xx列表,而不是循环遍历所有股票并从每个股票中获取(0,90)范围,然后为每个股票附加一个stdev编号。

当我执行最后一个函数(stdev(xx,xxx)时,我不确定如何在stock_list中使用stock和在范围(0,90)中使用n。

非常感谢任何帮助!

以下更新代码:

def stockprice(stock, y, X, xx, xxx):
        try:
            start = datetime.datetime(2016,8,1)
            end = datetime.datetime(2017,1,1)
            f = web.get_data_yahoo(stock, start, end, interval='d')
            f.drop(f.columns[[0,1,2,3,4]], axis=1, inplace=True)
            f['LnReturn'] = np.log(f['Adj Close']) - np.log(f['Adj Close'].shift(1))
            data2 = pd.concat([f,kf], axis = 1)
            data2['XRtrn'] = (data2['LnReturn']*100 - data2['RF'])
            df = data2[np.isfinite(data2['XRtrn'])]
            df = pd.DataFrame(df)
            y = df['XRtrn'].shift(-1)
            y = y.dropna()
            y = pd.DataFrame(y)
            x = df.ix[:,[2,3,4]]
            x = x[:-1]
            X = sm.add_constant(x)
        except:
            xx.append(['nan'])
        return y, X, xx, xxx

def regression(n, y, X, xx, xxx):
                try:
                    model = sm.OLS(y[n:(90+n)], X[n:(90+n)])
                    results = model.fit()
                    r2 = results.rsquared
                    xxx.append(r2)
                except:
                    xxx.append(['nan'])
                return y, X, xx, xxx

def stdev(xx, xxx):
                try:
                    xxx1 = pd.DataFrame(xxx)
                    std = xxx1.std()
                    stdv = std.tolist()
                    xx.append(stdv)
                except:
                    xxx.append(['nan'])
                return xx, xxx

# set the initial value for the variables you will use later on
xx = []
xxx = []
X = []
y = []

for stock in stock_list:
    y, X, xx, xxx = stockprice(stock, y, X, xx, xxx)

for n in range(0,90):
    y, X, xx, xxx = regression(n, y, X, xx, xxx)

xx, xxx = stdev(xx, xxx)

1 个答案:

答案 0 :(得分:0)

我不确定你的代码应该做什么或你的变量是什么,但我认为你可以做这样的事情(你需要调整,但我只是想让你理解所使用的原则):

def function1(stock, kf, xx, X): # provide all varibles you will use as parameters
    try:
        ## your code here, using stock, kf, xx and/or X
    except:
        ## your code here, using stock, kf, xx and/or X
    return kf, xx, X # return all variables you will need on next steps, except 'stock' because it will be managed by the for-loop

def function2(n, y, X, xxx, xx): # provide all varibles you will use as parameters
    try:
        ## your code here, using n, y, X, xxx and/or  xx
    except:
        ## your code here, using n, y, X, xxx and/or  xx
    return y, X, xxx, xx # return all variables you will need on next steps, except 'n' because it will be managed by the for-loop


# set the initial value for the variables you will use later on
kf = ***whatever***
xx = ***whatever***
xxx = ***whatever***
X = ***whatever***
Y = ***whatever***


for stock in stock_list:
    kf, xx, X = function1(stock, kf, xx, X)

for n in range(0,90):
    y, X, xxx, xx = function2(n, y, X, xxx, xx)

如果它解决了您的问题,请告诉我