def calcSpread(left, right, minute='5'): """计算价差""" leftDf = sa.get_future_data(left, minute) rightDf = sa.get_future_data(right, minute) data = leftDf.join(rightDf, lsuffix=left, rsuffix=right) spreadName = 'sp' + left[-2:] + right[-2:] data[spreadName] = data['close' + left] - data['close' + right] return data[['close' + left, 'close' + right, spreadName]].sort_index()
def calcSpread(self, minute='5'): """计算价差""" leftDf = sa.get_future_data(self.left, minute) rightDf = sa.get_future_data(self.right, minute) data = leftDf.join(rightDf, lsuffix=self.left, rsuffix=self.right) self.spreadName = 'sp' + self.left[-2:] + self.right[-2:] data[self.spreadName] = data['close' + self.left] - data['close' + self.right] self.data = data[[ 'close' + self.left, 'close' + self.right, self.spreadName ]].sort_index()
def calc(self): while True: future = sa.get_future_data(self.start)[['date', 'close']] df = pd.merge(future, self.spot, on='date', suffixes=[self.start, 'hs300']) df['spread'] = df['close' + self.start] - df['close' + 'hs300'] df['name'] = '{}-{}'.format(self.start, 'hs300') self.ret = self.ret.append(df) self.start = nextmonthsymbol(self.start) if int(self.start[-4:]) > int(self.end[-4:]): break
def __init__(self, symbol, start, end): """ Paramters: symbol : str IH like start, end : str 合约时间, 1801 like """ self.left = symbol + start self.right = symbol + start self.end = symbol + end self.df = sa.get_future_data(self.left) self.spreadDataList = [ ] # [{name:spreadname, t:[timeseries], spread:[spreadlist], spot:[dataframe]}, spread2, ...]
def calc(self): """ 计算spread数据 :return: """ d = {} df1 = self.df df2 = sa.get_future_data(self.right) suffixes = [self.left, self.right] data = pd.merge(df1, df2, left_index=True, right_index=True, suffixes=suffixes) spread = data['close' + self.left] - data['close' + self.right] d['name'] = self.left + '-' + self.right d['spread'] = list(spread) d['timeser'] = list(data['date' + self.left]) d['spot'] = list(data['close' + self.left]) self.spreadDataList.append(d) self.df = df2