期权高频数据准备

来源:https://uqer.io/community/share/55027e68f9f06c7a9ae9a53b

本notebook根据指定的时间区间整理并保存option_data.csv 文件,请与 期权市场一周纵览 notebook配合使用。

  1. import pandas as pd
  2. import numpy as np
  3. pd.options.display.float_format = '{:,>.4f}'.format
  1. calendar = Calendar('China.SSE')
  2. class _format_checker:
  3. def __init__(self, calendar):
  4. self.calendar = calendar
  5. def _format_check(self, instrumentID):
  6. contractType = instrumentID[6] + 'O'
  7. contractYear = int(instrumentID[7:9]) + 2000
  8. contractMonth = int(instrumentID[9:11])
  9. contractExp = Date.NthWeekDay(4, Wednesday, contractMonth, contractYear)
  10. contractExp = self.calendar.adjustDate(contractExp, BizDayConvention.Following)
  11. contractStrike = float(instrumentID[-4:]) / 1000.0
  12. return contractType, contractExp, contractStrike
  13. checker = _format_checker(calendar)
  1. tradingDays = calendar.bizDatesList(Date(2015,3,5), Date(2015,3,12))
  2. names, instrumentIDs = (OptionsDataSnapShot().optionId.unique(), OptionsDataSnapShot().instrumentID.unique())
  3. data = pd.DataFrame(names, columns = ['optionId'])
  4. instrumentIDs = pd.Series(instrumentIDs)
  5. data = data.join(pd.DataFrame(list(instrumentIDs.apply(checker._format_check)), columns= ['contractType', 'expDate', 'strikePrice']))
  6. data[:5]
optionIdcontractTypeexpDatestrikePrice
010000001COMarch 25th, 20152.2000
110000002COMarch 25th, 20152.2500
210000003COMarch 25th, 20152.3000
310000004COMarch 25th, 20152.3500
410000005COMarch 25th, 20152.4000
  1. tradingDaysStr = [''.join(date.toISO().split('-')) for date in tradingDays]
  2. tradingDaysStr
  3. ['20150305', '20150306', '20150309', '20150310', '20150311']
  1. res = pd.DataFrame()
  2. spotData = []
  3. for day in tradingDaysStr:
  4. tmp = spotData
  5. try:
  6. spotData = DataAPI.MktTicksHistOneDayGet('510050.XSHG', date = day, field = ['dataDate', 'datasTime', 'secOffset', 'lastPrice'])
  7. spotData = spotData.drop(0)
  8. except Exception, e:
  9. print e
  10. spotData = tmp
  11. for opt in names:
  12. try:
  13. sample = DataAPI.MktOptionTicksHistOneDayGet(optionId = opt,date = day)#field = ['optionId', 'dataDate', 'dataTime' 'secOffset', 'lastPrice'])
  14. sample = sample.drop_duplicates(['secOffset'])
  15. spotPrice = np.zeros((len(sample),))
  16. j = 0
  17. index = spotData.index
  18. for i, secOffset in enumerate(sample.secOffset):
  19. currentSpotSecOffset = spotData.loc[index[j], 'secOffset']*1000
  20. while currentSpotSecOffset < secOffset and j < len(index)-1:
  21. j = j + 1
  22. currentSpotSecOffset = spotData.loc[index[j], 'secOffset']*1000
  23. if j>=1:
  24. spotPrice[i] = spotData.loc[index[j-1], 'lastPrice']
  25. else:
  26. spotPrice[i] = spotData.loc[index[j], 'lastPrice']
  27. sample['spotPrice'] = spotPrice
  28. res = res.append(sample)
  29. except Exception, e:
  30. print e
  31. print day + ' finished!'
  32. 20150305 finished!
  33. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000030&date=20150306&startSecOffset=&endSecOffset=
  34. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000032&date=20150306&startSecOffset=&endSecOffset=
  35. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000033&date=20150306&startSecOffset=&endSecOffset=
  36. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000035&date=20150306&startSecOffset=&endSecOffset=
  37. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000054&date=20150306&startSecOffset=&endSecOffset=
  38. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000056&date=20150306&startSecOffset=&endSecOffset=
  39. 20150306 finished!
  40. 20150309 finished!
  41. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000039&date=20150310&startSecOffset=&endSecOffset=
  42. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000056&date=20150310&startSecOffset=&endSecOffset=
  43. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000064&date=20150310&startSecOffset=&endSecOffset=
  44. 20150310 finished!
  45. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000039&date=20150311&startSecOffset=&endSecOffset=
  46. -1:No Data Returned for request: /market/getOptionTicksHistOneDay.csv?field=&optionId=10000064&date=20150311&startSecOffset=&endSecOffset=
  47. 20150311 finished!
  1. res.optionId = res.optionId.astype('str')
  2. res = res.merge(data, how = 'left', on = 'optionId')
  3. dateData, idData, volumeData = res.dataDate, res.optionId, res['volume']
  4. previous = [dateData[0], idData[0], 0]
  5. newVolume = np.zeros((len(dateData),))
  6. count = 0
  7. for date, ids, volume in zip(dateData, idData, volumeData ):
  8. if date == previous[0] and ids == previous[1]:
  9. newVolume[count] = volume - previous[2]
  10. else:
  11. newVolume[count] = volume
  12. previous[0] = date
  13. previous[1] = ids
  14. previous[2] = volume
  15. count = count + 1
  16. res.volume = newVolume
  17. res['pdDateTime'] = res.expDate.apply(lambda x: x.toDateTime())
  18. optData = pd.DataFrame()
  19. optData['contractType'] = res['contractType']
  20. optData['valuationDate'] = res['dataDate']
  21. optData['expDate'] = res['expDate']
  22. optData['strikePrice'] = res['strikePrice']
  23. optData['lastPrice'] = res['lastPrice']
  24. optData['optionId'] = res['optionId'].astype('str')
  25. optData['Type'] = Option.Call
  26. optData['spotPrice'] = res.spotPrice
  27. optData.loc[optData['contractType'] == 'PO','Type'] = Option.Put
  28. optData['valuationDate'] = [Date(int(date.split('-')[0]),int(date.split('-')[1]),int(date.split('-')[2])) for date in optData['valuationDate']]
  29. dc = DayCounter('Actual/365 (Fixed)')
  30. optData['ttm'] = [dc.yearFraction(date1, date2) for date1, date2 in zip(optData['valuationDate'], optData['expDate'])]
  31. optData['lastPrice(vol)'] = BSMImpliedVolatity(optData['Type'], optData['strikePrice'], optData['spotPrice'], 0.0, 0.0, optData['ttm'], optData['lastPrice'])
  32. optData['bid1(vol)'] = BSMImpliedVolatity(optData['Type'], optData['strikePrice'], optData['spotPrice'], 0.0, 0.0, optData['ttm'], res.bidPrice1)
  33. optData['ask1(vol)'] = BSMImpliedVolatity(optData['Type'], optData['strikePrice'], optData['spotPrice'], 0.0, 0.0, optData['ttm'], res.askPrice1)
  34. res1 = res.merge(optData[[u'spotPrice', u'ttm', u'lastPrice(vol)', u'bid1(vol)', u'ask1(vol)']], left_index=True, right_index=True)
  35. res1 = res1.dropna(how = 'any')
  36. res1['bidAskSpread(bps)'] = (res1.askPrice1 - res1.bidPrice1) * 10000
  37. res1['bidAskSpread(vol bps)'] = (res1['ask1(vol)'] - res1['bid1(vol)']) * 10000
  38. res1.to_csv('option_data.csv')