Python

用於確定配對交易的交易信號的 Python 程式碼中的 KeyError

  • April 22, 2015

我基本上正在執行一些程式碼,如下所示。基本上,我只是從 CSV 文件中檢索成對的股票(排列為第 1 行 - 第 1,2 行,第 2 行 - 第 1,2 行等等,其中每行中的第 1 行和第 2 行不同)。然後我從雅虎獲取與這些股票“配對”相關的數據。我計算股票的回報率,基本上檢查一對股票之間的距離(回報率差異)是否超過某個門檻值,如果是,我返回 1。但是,我遇到了下面的錯誤,我無法弄清楚為什麼,據我所知,與 CSV 文件中的第一個“股票對”關聯的密鑰 ADP_PAYX 實際上確實存在。

Distancefunc(self, tickers, begdate, enddate)
   111             data = Returns(ticker,begdate,enddate)
   112             key = ticker[0]+'_'+ticker[1]
--> 113             R1 = data[key]['Returns'][0]
   114             R2 = data[key]['Returns'][1]
   115             distance = sum[(R1-R2)^2]

KeyError: 'ADP_PAYX' 




from datetime import datetime
import pytz
#import zipline as zp
import csv
import pandas as pd
import pandas.io.data as web
import numpy as np
from matplotlib.pyplot import *
from matplotlib.finance import quotes_historical_yahoo


def Dataretriever():
       Pairs = []
       f1=open('C:\Users\Pairs_0420.csv') #Enter the location of the file
       csvdata= csv.reader(f1)
       for row in csvdata:          #reading tickers from the csv file
           Pairs.append(row)
       return Pairs

tickers = Dataretriever()
tickersasstrings = map(str, tickers)

def PricePort(tickers,begdate,enddate):
   """
       Returns historical adjusted prices of a portfolio of stocks.
       tickers=pairsd   """
   final=pd.read_csv('http://chart.yahoo.com/table.csv?s=^GSPC',usecols=[0,6],index_col=0)
   final.columns=['^GSPC']
   data = {}
   for ticker in tickers:
       #print ticker        
       key = ticker[0]+'_'+ticker[1]
       data1 = quotes_historical_yahoo(ticker[0], begdate, enddate,asobject=True, adjusted=True)
       data2 = quotes_historical_yahoo(ticker[1], begdate, enddate,asobject=True, adjusted=True)
       #url1 = 'http://chart.yahoo.com/table.csv?s=ttt'.replace('ttt',ticker[0])
       data[key] = {'Data': (data1,data2)}
   return data    

def Returns(tickers,begdate,enddate):
   begdate=(2014,1,1)
   enddate=(2014,6,1)    
   p = PricePort(tickers,begdate,enddate)
   for ticker in tickers:
       key = ticker[0]+'_'+ticker[1]
       data1 = p[key]['Data'][0]
       data2 = p[key]['Data'][1]
       ret1 = (data1.close[1:] - data1.close[:-1])/data1.close[1:]
       ret2 = (data2.close[1:] - data2.close[:-1])/data2.close[1:]
       p[key]['Returns'] = (ret1,ret2)

   return p

class ThresholdClass():    
   #constructor
   def __init__(self, Pairs,begdatae,enddate):
       self.Pairs = Pairs
       self.begdate = begdate
       self.enddate = enddate

   def Distancefunc(self, tickers, begdate, enddate):
       for ticker in tickers:
           data = Returns(ticker,begdate,enddate)
           key = ticker[0]+'_'+ticker[1]
           R1 = data[key]['Returns'][0]
           R2 = data[key]['Returns'][1]
           distance = sum[(R1-R2)^2]
           return distance

   def MeanofPairs(self, tickers, begdate, enddate):
       sum = self.Distancefunc(tickers, begdate, enddate)
       mean = np.mean(sum)
       return mean

   def StandardDeviation(self, tickers, begdate, enddate):
       sum = self.Distancefunc(tickers, begdate, enddate)
       standard_dev = np.std(sum)
       return standard_dev 

   def ThresholdandnewsChecker(self, tickers, begdate, enddate):
       threshold = self.MeanofPairs(tickers, begdate, enddate) + 2*self.StandardDeviation(tickers, begdate, enddate)
       if (self.Distancefunc(tickers, begdate, enddate) > threshold):
           news = self.newsfunc(binaryfromnews)
           return 1       

begdate=(2013,1,1)
enddate=(2013,12,31)      
Threshold_Class  = ThresholdClass(tickers[:1],begdate,enddate)   
Threshold_Class.ThresholdandnewsChecker(tickers[:1], begdate, enddate)

編輯:之前添加一個 print pdata1 = p[key]['Data'][0]表明鍵是A_DandP_A而不是必需的’ADP_PAYX’。所以,這就是我現在想要解決的問題。謝謝。

在方法中Distancefunc改變這個:

   for ticker in tickers:
       data = Returns(ticker,begdate,enddate)
       ...

對此:

   data = Returns(tickers,begdate,enddate)
   for ticker in tickers:
       ...

注意 的參數中從ticker到的變化。原因是它旨在循環所有程式碼,但目前您正在傳遞一個字元串而不是程式碼列表。這就是為什麼將鍵設置為和數據字典的原因:它是股票名稱 ADP_PAYX 的第一個和第二個字母。tickers``Returns``Returns``A_D

引用自:https://quant.stackexchange.com/questions/17485