def get_annotated_questions(input_dicts, question_dicts): input_by_quid = sort_by_key(input_dicts, ['quid']) questions_by_quid = sort_by_key(question_dicts, ['quid']) questions_annotated = [] for quid in input_by_quid: if quid in questions_by_quid: question = questions_by_quid[quid][0] questions_annotated.append(question) return questions_annotated
def collect_invalid(input_dicts, question_dicts): questions_by_pair = sort_by_key(question_dicts, ['property', 'concept']) questions_annotated = get_annotated_questions(input_dicts, question_dicts) questions_anntotated_by_pair = sort_by_key(questions_annotated, ['property', 'concept']) invalid_annotations = [] for pair, questions_annotated in questions_anntotated_by_pair.items(): questions = questions_by_pair[pair] if len(questions) != len(questions_annotated): #print('missing annotations for pair:', pair, len(questions), len(questions_annotated)) invalid_annotations.extend(questions) return invalid_annotations
def get_futures(self): # Get all current futures instruments self.futures_prv = cp.deepcopy(self.futures) insts = self.client.fetchMarkets() #print(insts[0]) self.futures = sort_by_key({ i['symbol']: i for i in insts if 'BTC/USD' in i['symbol'] or ( 'ETH/USD' in i['symbol'] and '7D' not in i['symbol'] and '.' not in i['symbol'] ) #or ('XBT' in i['symbol'] and '7D' not in i['symbol']) and '.' not in i['symbol'] }) self.futures['XBTUSD'] = self.futures['BTC/USD'] del self.futures['BTC/USD'] self.futures['ETHUSD'] = self.futures['ETH/USD'] del self.futures['ETH/USD'] #print(self.futures['XBTH20']) for k in self.futures.keys(): if self.futures[k]['info']['expiry'] == None: self.futures[k]['expi_dt'] = datetime.strptime( '3000-01-01 15:00:00', '%Y-%m-%d %H:%M:%S') else: self.futures[k]['expi_dt'] = datetime.strptime( self.futures[k]['info']['expiry'][:-5], '%Y-%m-%dT%H:%M:%S') print(self.futures[k]['expi_dt'])
def get_futures( self ): # Get all current futures instruments self.futures_prv = cp.deepcopy( self.futures ) insts = self.client.fetchMarkets() #print(insts[0]) self.futures = sort_by_key( { i[ 'symbol' ]: i for i in insts if i['symbol'] == 'BTC/USDT' } )
def collect_not_annotated(input_dicts, question_dicts): questions_not_annotated = [] input_by_quid = sort_by_key(input_dicts, ['quid']) for d in question_dicts: quid = d['quid'] if quid not in input_by_quid: questions_not_annotated.append(d) return questions_not_annotated
def test_for_wrong_questions(questions_for_annotation): wrong_n_questions = [] for_annotation_by_pair = sort_by_key(questions_for_annotation, ['property', 'concept']) for pair, questions in for_annotation_by_pair.items(): if len(questions) > 10 or len(questions) < 3: wrong_n_questions.append((n, pair)) assert len( wrong_n_questions) == 0, 'Number of questions per pair not correct.'
def create_inspection_batch(group, run): pairs = get_pairs(group) question_path = f'../questions/run{run}-all-restricted_True.csv' question_dicts = read_csv(question_path) questions_by_pair = sort_by_key(question_dicts, ['property', 'concept']) inspection_questions = [] for p in pairs: questions = questions_by_pair[p] inspection_questions.extend(questions) return inspection_questions
def get_available_questions(input_dicts, question_dicts): questions_for_annotation = [] questions_not_annotated = collect_not_annotated(input_dicts, question_dicts) print('not annotated yet:', len(questions_not_annotated)) invalid_annotations = collect_invalid(input_dicts, question_dicts) print('not valid', len(invalid_annotations)) not_annotated_pair = sort_by_key(questions_not_annotated, ['property', 'concept']) invalid_pair = sort_by_key(invalid_annotations, ['property', 'concept']) for pair, questions in not_annotated_pair.items(): if pair in invalid_pair: questions_for_annotation.extend(invalid_pair[pair]) else: questions_for_annotation.extend(questions) test_for_wrong_questions(questions_for_annotation) return questions_for_annotation, invalid_annotations
def get_futures(self): # Get all current futures instruments self.futures_prv = cp.deepcopy(self.futures) insts = self.client.getinstruments() self.futures = sort_by_key( {i['instrumentName']: i for i in insts if i['kind'] == 'future'}) for k, v in self.futures.items(): self.futures[k]['expi_dt'] = datetime.strptime( v['expiration'][:-4], '%Y-%m-%d %H:%M:%S')
def get_futures(self): # Get all current futures instruments self.futures_prv = cp.deepcopy(self.futures) insts = [{ 'kind': 'future', 'baseCurrency': 'BTC', 'currency': 'USD', 'minTradeSize': 1, 'instrumentName': 'BTC-PERPETUAL', 'isActive': True, 'settlement': 'perpetual', 'created': '2018-08-14 10:24:47 GMT', 'tickSize': 0.25, 'pricePrecision': 1, 'expiration': '3000-01-01 08:00:00 GMT', 'contractSize': 10.0 }, { 'kind': 'future', 'baseCurrency': 'BTC', 'currency': 'USD', 'minTradeSize': 1, 'instrumentName': 'BTC-28JUN19', 'isActive': True, 'settlement': 'month', 'created': '2018-12-29 08:01:00 GMT', 'tickSize': 0.25, 'pricePrecision': 1, 'expiration': '2019-06-28 08:00:00 GMT', 'contractSize': 10.0 }, { 'kind': 'future', 'baseCurrency': 'BTC', 'currency': 'USD', 'minTradeSize': 1, 'instrumentName': 'BTC-29MAR19', 'isActive': True, 'settlement': 'month', 'created': '2018-10-04 04:51:34 GMT', 'tickSize': 0.25, 'pricePrecision': 1, 'expiration': '2019-03-29 08:00:00 GMT', 'contractSize': 10.0 }] self.futures = sort_by_key( {i['instrumentName']: i for i in insts if i['kind'] == 'future'}) for k, v in self.futures.items(): self.futures[k]['expi_dt'] = datetime.strptime( v['expiration'][:-4], '%Y-%m-%d %H:%M:%S')
def get_batch(questions_to_annotate, n_qu=70): batch = [] properties = set() pairs = set() # shuffle questions: shuffle(questions_to_annotate) questions_by_pair = sort_by_key(questions_to_annotate, ['property', 'concept']) available_properties = set( [p.split('-')[0] for p in questions_by_pair.keys()]) print('total number of questions to select:', n_qu) print('available properties', available_properties) print('# available questions', len(questions_to_annotate)) if n_qu > len(questions_to_annotate): print(f'only {len(questions_to_annotate)} left - adding all to batch.') batch.extend(questions_to_annotate) else: print(f'still more than {n_qu} questions available.') while len(batch) < n_qu: for pair, questions in questions_by_pair.items(): prop = pair.split('-')[0] if len(batch) < n_qu: if prop not in properties: print('found a new property:', prop, len(batch)) batch.extend(questions) properties.add(prop) pairs.add(pair) else: print('no more new properties') props_not_used = available_properties.difference( properties) print('properties not used:', len(props_not_used), len(batch)) if len(props_not_used) > 0: continue else: if pair not in pairs: batch.extend(questions) properties.add(prop) print('no more properties, adding quetions:', len(questions)) else: print('pair already added', pair) else: print('found enough questions', len(batch)) break return batch
def distribute_over_lists(new_batch, n_qu, n_lists, test_question_dicts): batch_with_listnumbers = [] new_batch_by_pair = sort_by_key(new_batch, ['property', 'concept']) pair_by_n = defaultdict(list) for p, data in new_batch_by_pair.items(): pair_by_n[len(data)].append(p) pairs_assigned = set() batch_dict = defaultdict(list) for n, pairs in pair_by_n.items(): for list_n in range(n_lists): list_n = list_n + 1 for p in pairs: if p not in pairs_assigned: questions = new_batch_by_pair[p] batch_dict[list_n].extend(questions) pairs_assigned.add(p) if len(batch_dict[list_n]) >= (n_qu - 10): break return batch_dict
def get_batch(questions_to_annotate, n_qu=70): batch = [] properties = set() # shuffle questions: shuffle(questions_to_annotate) questions_by_pair = sort_by_key(questions_to_annotate, ['property', 'concept']) available_properties = set( [p.split('-')[0] for p in questions_by_pair.keys()]) if n_qu > len(questions_to_annotate): print(f'only {len(questions_to_annotate)} left - adding all to batch.') batch.extend(questions_to_annotate) else: print(f'still more than {n_qu} questions available.') for pair, questions in questions_by_pair.items(): prop = pair.split('-')[0] if len(batch) < n_qu: if prop not in properties: #print('found a new one:', prop, len(batch)) batch.extend(questions) properties.add(prop) else: props_not_used = available_properties.difference( properties) #print('properties not used:', len(props_not_used), len(batch)) if len(props_not_used) > 0: continue else: batch.extend(questions) properties.add(prop) #print('no more properties, adding quetions:', len(questions)) else: print('found enough questions', len(batch)) break return batch
] #order size * (higher of pct lim long/short * num fut) * 10 / lev therisk = ((250) * ((50 * 3) / 100) * 10) * 1 if therisk < 0: therisk = therisk * -1 tty = datetime(2019, 12, 27).strftime('%s') theyield = 0.1541 amts = {} spot = client.index()['btc'] lower = math.floor((spot - 5000) / 1000) * 1000 higher = math.ceil((spot + 5000) / 1000) * 1000 insts = client.getinstruments() options = sort_by_key({ i['instrumentName']: i for i in insts if i['kind'] == 'option' and 'BTC' in i['instrumentName'] }) exps = [] strikes = [] calls = [] profits = {} puts = [] es = {} names = [] remember = {} for o in options: remember[options[o]['instrumentName']] = options[o] names.append(options[o]['instrumentName']) exp = datetime.strptime(options[o]['expiration'][:-13], '%Y-%m-%d')