class Collector: def __init__(self, category='50ETF', host='localhost', port=6379): self.r = redis.Redis(host, port, db=0) self.category = category self.option = OptionData(category) self.convertor = CodeToDescriptionName(category) def run(self): "collect options kline" "[{'name': 2700, sellprice': 0.11, 'buyprice':0.12, 'remainday':15, 'optionvalue':450, 'timevalue':200}, {}]" counter = 0 contracts = self.option.get_option_contracts(self.category) for contract in contracts: end_day, remain = self.option.get_option_expire_days(contract) calls, puts = self.option.get_option_codes(contract) for call in calls: kdatas = self.option.get_option_day_kline(call) for kdata in kdatas: remaining_day = get_remaining_day(end_day, kdata['d']) description = self.convertor.code_to_name(call) #(category, month, call, name) key = option_key(self.category, contract, kdata['d']) key += ' ' + description[2] + ' ' + description[3] kdata['remainday'] = remaining_day value = double_quotation(str(kdata)) if not self.r.exists(key): self.r.set(key, value) #print('set new key {}'.format(key)) counter += 1 else: #print('key already exist {}'.format(key)) pass for put in puts: kdatas = self.option.get_option_day_kline(put) for kdata in kdatas: description = self.convertor.code_to_name(put) #(category, month, call, name) key = option_key(self.category, contract, kdata['d']) key += ' ' + description[2] + ' ' + description[3] kdata['remainday'] = remaining_day value = double_quotation(str(kdata)) if not self.r.exists(key): self.r.set(key, value) #print('set new key {}'.format(key)) counter += 1 else: #print('key already exist {}'.format(key)) pass print('collect new data: {}'.format(counter))
class CodeToDescriptionName: def __init__(self, category='50ETF'): self.category = category self.codes = {} self.etf = OptionData(category) def code_to_name(self, code): price = self.etf.get_option_price(code) r = re.compile(r'^(\d+ETF)(.*)(\d+)月(\d+)') m = r.match(price['期权合约简称']) category = m.group(1) call = m.group(2) month = m.group(3) name = m.group(4) if call == '购': call = 'call' else: call = 'put' description = (category, month, call, name) if code in self.codes.keys(): return self.codes[code] else: self.codes[code] = description return description
class RTOptionGenerator(IGenerator): def __init__(self, category='50ETF', contract='202002'): super(RTOptionGenerator).__init__() self.category = category self.option = OptionData(category) self.contract = contract def run(self): calls, puts = self.option.get_option_all_prices(self.contract) new_calls = {} for call in calls: name = call['name'] new_calls[name] = call new_puts = {} for put in puts: name = put['name'] new_puts[name] = put contract_prices = {} name = self.category + ' ' + self.contract contract_prices[name] = (new_calls, new_puts) return contract_prices
def __init__(self, category='50ETF'): self.category = category self.codes = {} self.etf = OptionData(category)
def __init__(self, category='50ETF', contract='202002'): super(RTOptionGenerator).__init__() self.category = category self.option = OptionData(category) self.contract = contract
def __init__(self, category='50ETF', host='localhost', port=6379): self.r = redis.Redis(host, port, db=0) self.category = category self.option = OptionData(category) self.fix = 0 pass
class ETFCollector(): def __init__(self, category='50ETF', host='localhost', port=6379): self.r = redis.Redis(host, port, db=0) self.category = category self.option = OptionData(category) self.fix = 0 pass def run(self): datas = self.option.get_etf_day_kline() for data in datas: key = self.category + ' ' + data['day'] counter = 0 if not self.r.exists(key): value = str(data) self.r.set(key, value) counter += 1 else: pass self.fix_option_price(data) print('collect new data: {}'.format(counter)) print("fix data: {}".format(self.fix)) def fix_option_price(self, data): etf = data contracts = self.option.get_option_contracts() for contract in contracts: key = option_key(self.category, contract, etf['day']) key += '*' keys = self.r.keys(key) for key in keys: key = key.decode('GBK') option_data = self.r.get(key) option_data.decode('GBK') price = json.loads(option_data) if 'call' in key: price['name'] = key[-4:] price['sellprice'] = price['c'] price['buyprice'] = price['c'] price['etfprice'] = etf['close'] # price['remainday'] = 10 if float(price['name']) / 1000 > float(etf['close']): price['optionvalue'] = 0 price['timevalue'] = float(price['buyprice']) else: price['optionvalue'] = float( etf['close']) - float(price['name']) / 1000 price['timevalue'] = float( price['buyprice']) - price['optionvalue'] if 'put' in key: price['name'] = key[-4:] price['sellprice'] = price['c'] price['buyprice'] = price['c'] price['etfprice'] = etf['close'] # price['remainday'] = 10 if float(price['name']) / 1000 < float(etf['close']): price['optionvalue'] = 0 price['timevalue'] = float(price['buyprice']) else: price['optionvalue'] = float( price['name']) / 1000 - float(etf['close']) price['timevalue'] = float( price['buyprice']) - price['optionvalue'] self.r.set(key, double_quotation(str(price))) self.fix += 1 pass
def __init__(self, category='50ETF', host='localhost', port=6379): self.r = redis.Redis(host, port, db=0) self.category = category self.option = OptionData(category) self.convertor = CodeToDescriptionName(category)