def getorder(self, time, countdown, lob, verbose): if len(self.orders) < 1: self.active = False order = None else: self.active = True self.limit = self.orders[0].price self.job = self.orders[0].atype #calculate price if self.job == 'Bid': self.price = self.calc_p_bid(self.holdings - 1, self.remaining_offer_ops - 1) if self.job == 'Ask': self.price = self.calc_p_ask(self.holdings - 1, self.remaining_offer_ops - 1) order = Order(self.tid, self.job, 'LIM', self.price, self.orders[0].qty, time, None, -1) self.lastquote = order if self.first_turn or self.price == -1: if self.job == 'Bid': order = Order(self.tid, self.job, 'LIM', bse_sys_minprice + 1, self.orders[0].qty, time, None, -1) if self.job == 'Ask': order = Order(self.tid, self.job, 'LIM', bse_sys_maxprice - 1, self.orders[0].qty, time, None, -1) return order
def getorder(self, time, countdown, lob, verbose): if verbose: print('SNPR getorder: self.orders[0]=%s' % str(self.orders[0])) lurk_threshold = 0.2 shavegrowthrate = 3 shave = int(1.0 / (0.01 + countdown / (shavegrowthrate * lurk_threshold))) if (len(self.orders) < 1) or (countdown > lurk_threshold): order = None else: limitprice = self.orders[0].price otype = self.orders[0].otype ostyle = self.orders[0].ostyle if otype == 'Bid': if lob['bids']['n'] > 0: oprice = lob['bids']['bestp'] + shave if oprice > limitprice: oprice = limitprice else: oprice = lob['bids']['worstp'] else: if lob['asks']['n'] > 0: oprice = lob['asks']['bestp'] - shave if oprice < limitprice: oprice = limitprice else: oprice = lob['asks']['worstp'] order = Order(self.tid, otype, ostyle, oprice, self.orders[0].qty, time, None, -1) self.lastquote = order return order
def getorder(self, time, countdown, lob, verbose): if verbose: print("SHVR getorder:") if len(self.orders) < 1: order = None else: if verbose: print(" self.orders[0]=%s" % str(self.orders[0])) limitprice = self.orders[0].price otype = self.orders[0].atype ostyle = self.orders[0].astyle if otype == 'Bid': if lob['bids']['n'] > 0: quoteprice = lob['bids']['bestp'] + 1 if quoteprice > limitprice : quoteprice = limitprice else: quoteprice = lob['bids']['worstp'] else: if lob['asks']['n'] > 0: quoteprice = lob['asks']['bestp'] - 1 if quoteprice < limitprice: quoteprice = limitprice else: quoteprice = lob['asks']['worstp'] order = Order(self.tid, otype, ostyle, quoteprice, self.orders[0].qty, time, None, -1) self.lastquote = order return order
def generate_QBO(self, time, countdown, lob, verbose): if len(self.qbo_orders) < 1: self.active = False order = None else: osrid = self.qbo_orders[0].osrid biid = self.qbo_orders[0].order.orderid bi_order = self.qbo_orders[0].order qty = bi_order.qty price = bi_order.price limitprice = bi_order.limitprice MES = bi_order.MES subtype = 'QBO' # check if the order request has expired >0.5? if time - self.qbo_orders[0].time > 0.5: self.active = False order = Order(self.tid, self.job, "CAN", price, qty, time, None, -1, limitprice, MES, subtype, None, osrid) else: self.active = True total_price = 0 total_qty = 0 for trn in self.qbo_orders[0].trns: total_price += trn['price']*trn['qty'] total_qty += trn['qty'] # price = total_price/total_qty # qty = random.randint(total_qty, qty) order = Order(self.tid, self.job, "LIM", price, qty, time, None, -1, limitprice, MES, subtype, 'Drk', osrid, biid) self.lastquote = order order.myref = bi_order.myref del self.qbo_orders[0] return order
def getorder(self, time, countdown, lob, verbose): if verbose: print("ISHV getorder:") shave_c = 2 # c in the y=mx+c linear mapping from imbalance to shave amount shave_m = 1 # m in the y=mx+c if len(self.orders) < 1: order = None else: if verbose: print(" self.orders[0]=%s" % str(self.orders[0])) limitprice = self.orders[0].price otype = self.orders[0].atype ostyle = self.orders[0].astyle microp = lob['microprice'] midp = lob['midprice'] if microp != None and midp != None: imbalance = microp - midp else: imbalance = 0 # if imbalance is undefined, proceed as if it is equal to zero if otype == 'Bid': # quantity sensitivity if imbalance < 0 : shaving = 1 # imbalance in favour of buyers, so shave slowly else: shaving = shave_c + (shave_m * int(imbalance*100)/100) # shave ever larger amounts if verbose: print('t:%f, ISHV (Bid) imbalance=%s shaving=%s' % (time, imbalance, shaving)) if len(lob['bids']['lob']) > 0: quoteprice = lob['bids']['bestp'] + shaving if quoteprice > limitprice : quoteprice = limitprice else: quoteprice = 1 #KLUDGE -- come back to fix todo else: # quantity sensitivity if imbalance > 0 : shaving = 1 else: shaving = shave_c - (shave_m * int(imbalance*100)/100) if verbose: print('t:%f, ISHV (Ask) imbalance=%s shaving=%s' % (time, imbalance, shaving)) if len(lob['asks']['lob']) > 0: quoteprice = lob['asks']['bestp'] - shaving if quoteprice < limitprice : quoteprice = limitprice else: quoteprice = 200 #KLUDGE -- come back to fix todo order = Order(self.tid, otype, ostyle, quoteprice, self.orders[0].qty, time, None, -1) self.lastquote = order return order
def getorder(self, time, countdown, lob, verbose): if verbose: print('GVWY getorder:') if len(self.orders) < 1: order = None else: quoteprice = self.orders[0].price order = Order(self.tid, self.orders[0].otype, self.orders[0].ostyle, quoteprice, self.orders[0].qty, time, None, -1) self.lastquote=order return order
def getorder(self, time, countdown, lob, verbose): if verbose: print('ZIC getorder:') if len(self.orders) < 1: # no orders: return NULL order = None else: minprice = lob['bids']['worstp'] maxprice = lob['asks']['worstp'] limit = self.orders[0].price otype = self.orders[0].atype ostyle = self.orders[0].astyle if otype == 'Bid': oprice = random.randint(minprice, limit) else: oprice = random.randint(limit, maxprice) # NB should check it == 'Ask' and barf if not order = Order(self.tid, otype, ostyle, oprice, self.orders[0].qty, time, None, -1) self.lastquote = order return order
def getorder(self, time, countdown, lob, verbose): def imbalance_alter(quoteprice_aa, lob, countdown, m): mlofi_list = [0 for i in range(m)] cd_list = [0 for i in range(m)] ad_list = [] n = 1 while len(self.es_list) >= n: for i in range(m): mlofi_list[i] += self.es_list[-n]['level' + str(i + 1)] n += 1 if n >= 11: break n = 1 while len(self.ds_list) >= n: for i in range(m): cd_list[i] += self.ds_list[-n]['level' + str(i + 1)] n += 1 if n >= 11: break for i in range(m): temp = None if n == 1: temp = cd_list[i] + 1 else: temp = cd_list[i] / (n - 1) + 1 ad_list.append(temp) c = 5 decay = 0.8 offset = 0 for i in range(m): offset += int(mlofi_list[i] * c * pow(decay, i) / ad_list[i]) benchmark = quoteprice_aa if (lob['midprice'] != None): benchmark = lob['midprice'] # print 'midprice is %d' % benchmark quoteprice_iaa = quoteprice_aa + 0.8 * (benchmark + offset - quoteprice_aa) if self.job == 'Bid' and quoteprice_iaa > self.limit: quoteprice_iaa = self.limit if self.job == 'Ask' and quoteprice_iaa < self.limit: quoteprice_iaa = self.limit if countdown < 0.3: print "insert" if self.job == 'Bid' and ( len(lob['asks']['lob']) >= 1) and lob['asks']['lob'][0][0] < self.limit: quoteprice_iaa = lob['asks']['lob'][0][0] if self.job == 'Ask' and ( len(lob['bids']['lob']) >= 1) and lob['bids']['lob'][0][0] > self.limit: quoteprice_iaa = lob['bids']['lob'][0][0] if self.job == 'Bid' and quoteprice_iaa < bse_sys_minprice: quoteprice_iaa = bse_sys_minprice + 1 if self.job == 'Ask' and quoteprice_iaa > bse_sys_maxprice: quoteprice_iaa = bse_sys_maxprice - 1 return quoteprice_iaa if len(self.orders) < 1: self.active = False order = None return order else: self.active = True self.limit = self.orders[0].price self.job = self.orders[0].atype #calculate price if self.job == 'Bid': self.price = self.calc_p_bid(self.holdings - 1, self.remaining_offer_ops - 1) if self.job == 'Ask': self.price = self.calc_p_ask(self.holdings - 1, self.remaining_offer_ops - 1) quoteprice = self.price # print "before:" # print self.price if (self.is_imbalance_significant(self.m, 0.6)): # print "abvious" quoteprice_igdx = imbalance_alter(quoteprice, lob, countdown, self.m) else: # print "not abvious" quoteprice_igdx = quoteprice self.price = quoteprice_igdx # # print "after:" # print self.price order = Order(self.tid, self.job, 'LIM', self.price, self.orders[0].qty, time, None, -1) self.lastquote = order if self.first_turn or self.price == -1: if self.job == 'Bid': order = Order(self.tid, self.job, 'LIM', bse_sys_minprice + 1, self.orders[0].qty, time, None, -1) if self.job == 'Ask': order = Order(self.tid, self.job, 'LIM', bse_sys_maxprice - 1, self.orders[0].qty, time, None, -1) # print order return order
def getorder(self, time, countdown, lob, verbose): if verbose: print('ZIP getorder(): LOB=%s' % lob) # random coefficient, multiplier on trader's own estimate of worst possible bid/ask prices # currently in arbitrarily chosen range [2, 5] worst_coeff = 2 + (3 * random.random()) if len(self.orders) < 1: self.active = False order = None else: self.active = True self.limit = self.orders[0].price self.job = self.orders[0].atype if self.job == 'Bid': # currently a buyer (working a bid order) self.margin = self.margin_buy # what is the worst bid price on the LOB right now? if len(lob['bids']['lob']) > 0: # take price of final entry on LOB worst_bid = lob['bids']['lob'][-1][0] else: # local pessimistic estimate of the worst bid price (own version of stub quote) worst_bid = max(1, int(self.limit / worst_coeff)) if self.worst_bidprice == None: self.worst_bidprice = worst_bid elif self.worst_bidprice > worst_bid: self.worst_bidprice = worst_bid else: # currently a seller (working a sell order) self.margin = self.margin_sell # what is the worst ask price on the LOB right now? if len(lob['asks']['lob']) > 0: # take price of final entry on LOB worst_ask = lob['asks']['lob'][-1][0] else: # local pessimistic estimate of the worst ask price (own version of stub quote) worst_ask = int(self.limit * worst_coeff) if self.worst_askprice == None: self.worst_askprice = worst_ask elif self.worst_askprice < worst_ask: self.worst_askprice = worst_ask quoteprice = int(self.limit * (1 + self.margin)) def imbalance_alter(quoteprice_aa, lob, countdown, m): mlofi_list = [0 for i in range(m)] cd_list = [0 for i in range(m)] ad_list = [] n = 1 while len(self.es_list) >= n: for i in range(m): mlofi_list[i] += self.es_list[-n]['level' + str(i + 1)] n += 1 if n >= 11: break n = 1 while len(self.ds_list) >= n: for i in range(m): cd_list[i] += self.ds_list[-n]['level' + str(i + 1)] n += 1 if n >= 11: break for i in range(m): temp = None if n == 1: temp = cd_list[i] + 1 else: temp = cd_list[i] / (n - 1) + 1 ad_list.append(temp) c = 10 decay = 1 offset = 0 for i in range(m): offset += int(mlofi_list[i] * c * pow(decay, i) / ad_list[i]) benchmark = quoteprice_aa if (lob['midprice'] != None): benchmark = lob['midprice'] # print 'midprice is %d' % benchmark quoteprice_iaa = quoteprice_aa + 0.8 * (benchmark + offset - quoteprice_aa) if self.job == 'Bid' and quoteprice_iaa > self.limit: quoteprice_iaa = self.limit if self.job == 'Ask' and quoteprice_iaa < self.limit: quoteprice_iaa = self.limit if countdown < 0.3: print "insert" if self.job == 'Bid' and ( len(lob['asks']['lob']) >= 1) and lob['asks']['lob'][0][0] < self.limit: quoteprice_iaa = lob['asks']['lob'][0][0] + 1 if self.job == 'Ask' and ( len(lob['bids']['lob']) >= 1) and lob['bids']['lob'][0][0] > self.limit: quoteprice_iaa = lob['bids']['lob'][0][0] - 1 if self.job == 'Bid' and quoteprice_iaa < bse_sys_minprice: quoteprice_iaa = bse_sys_minprice + 1 if self.job == 'Ask' and quoteprice_iaa > bse_sys_maxprice: quoteprice_iaa = bse_sys_maxprice - 1 return quoteprice_iaa # print "before" # print quoteprice # if(self.is_imbalance_significant(self.m,0.6)): # print "abvious" # quoteprice_izip = imbalance_alter(quoteprice, lob, countdown, self.m) # else: # print "not abvious" # quoteprice_izip = quoteprice quoteprice_izip = imbalance_alter(quoteprice, lob, countdown, self.m) # print "after" # print quoteprice_izip self.price = quoteprice_izip order = Order(self.tid, self.job, "LIM", quoteprice_izip, self.orders[0].qty, time, None, -1) self.lastquote = order return order
def getorder(self, time, countdown, lob, verbose): if verbose: print("ISHV getorder:") shave_c = 2 # c in the y=mx+c linear mapping from imbalance to shave amount shave_m = 1 # m in the y=mx+c if len(self.orders) < 1: order = None else: if verbose: print(" self.orders[0]=%s" % str(self.orders[0])) self.limit = self.orders[0].price self.job = self.orders[0].atype otype = self.orders[0].atype ostyle = self.orders[0].astyle microp = lob['microprice'] midp = lob['midprice'] if otype == 'Bid': if len(lob['bids']['lob']) > 0: quoteprice = lob['bids']['bestp'] if quoteprice > self.limit : quoteprice = self.limit else: quoteprice = 1 # KLUDGE -- come back to fix todo else: if len(lob['asks']['lob']) > 0: quoteprice = lob['asks']['bestp'] if quoteprice < self.limit: quoteprice = self.limit else: quoteprice = 200 # KLUDGE -- come back to fix todo def imbalance_alter(quoteprice_aa, lob, countdown, m): mlofi_list = [0 for i in range(m)] cd_list = [0 for i in range(m)] ad_list = [] n = 1 while len(self.es_list) >= n: for i in range(m): mlofi_list[i] += self.es_list[-n]['level' + str(i+1)] n += 1 if n >= 11: break n = 1 while len(self.ds_list) >= n: for i in range(m): cd_list[i] += self.ds_list[-n]['level' + str(i+1)] n += 1 if n >= 11: break for i in range(m): temp = None if n == 1: temp = cd_list[i]+1 else: temp = cd_list[i]/(n-1)+1 ad_list.append(temp) c = 5 decay = 0.8 offset = 0 for i in range(m): offset += int(mlofi_list[i]*c*pow(decay,i)/ ad_list[i]) benchmark = quoteprice_aa; if(lob['midprice'] != None): benchmark = lob['midprice'] # print 'midprice is %d' % benchmark quoteprice_iaa = quoteprice_aa + 0.8 * (benchmark + offset - quoteprice_aa) if self.job == 'Bid' and quoteprice_iaa > self.limit: quoteprice_iaa = self.limit if self.job == 'Ask' and quoteprice_iaa < self.limit: quoteprice_iaa = self.limit if countdown < 0.3 : print "insert" if self.job == 'Bid' and (len(lob['asks']['lob']) >= 1) and lob['asks']['lob'][0][0] < self.limit: quoteprice_iaa = lob['asks']['lob'][0][0] if self.job == 'Ask' and (len(lob['bids']['lob']) >= 1) and lob['bids']['lob'][0][0] > self.limit: quoteprice_iaa = lob['bids']['lob'][0][0] return quoteprice_iaa if(self.is_imbalance_significant(self.m,0.6)): # print "abvious" quoteprice_iaa = imbalance_alter(quoteprice, lob, countdown, self.m) else: # print "not abvious" quoteprice_iaa = quoteprice order = Order(self.tid, otype, ostyle, quoteprice_iaa, self.orders[0].qty, time, None, verbose) self.lastquote = order return order
def getorder(self, time, countdown, lob, verbose): if len(self.orders) < 1: self.active = False return None else: self.limit = self.orders[0].price otype = self.orders[0].atype self.job = self.orders[0].atype ostyle = self.orders[0].astyle if otype == 'Bid': if (lob['midprice'] != None and lob['midprice'] < self.limit): quoteprice = lob['midprice'] elif lob['bids']['n'] > 0: quoteprice = lob['bids']['bestp'] if quoteprice > self.limit: quoteprice = self.limit else: quoteprice = self.limit else: if (lob['midprice'] != None and lob['midprice'] > self.limit): quoteprice = lob['midprice'] elif lob['asks']['n'] > 0: quoteprice = lob['asks']['bestp'] if quoteprice < self.limit: quoteprice = self.limit else: quoteprice = self.limit def imbalance_alter(quoteprice, lob, countdown, m): mlofi_list = [0 for i in range(m)] cd_list = [0 for i in range(m)] ad_list = [] n = 1 while len(self.es_list) >= n: for i in range(m): mlofi_list[i] += self.es_list[-n]['level' + str(i + 1)] n += 1 if n >= 11: break n = 1 while len(self.ds_list) >= n: for i in range(m): cd_list[i] += self.ds_list[-n]['level' + str(i + 1)] n += 1 if n >= 11: break for i in range(m): temp = None if n == 1: temp = cd_list[i] + 1 else: temp = cd_list[i] / (n - 1) + 1 ad_list.append(temp) c = 5 decay = 0.8 offset = 0 for i in range(m): offset += int(mlofi_list[i] * c * pow(decay, i) / ad_list[i]) benchmark = quoteprice if (lob['midprice'] != None): benchmark = lob['midprice'] # print 'midprice is %d' % benchmark quoteprice_mlofi = quoteprice + 0.8 * (benchmark + offset - quoteprice) if self.job == 'Bid' and quoteprice_mlofi > self.limit: quoteprice_mlofi = self.limit if self.job == 'Ask' and quoteprice_mlofi < self.limit: quoteprice_mlofi = self.limit if countdown < 0.3: print "insert" if self.job == 'Bid' and ( len(lob['asks']['lob']) >= 1) and lob['asks']['lob'][0][0] < self.limit: quoteprice_mlofi = lob['asks']['lob'][0][0] if self.job == 'Ask' and ( len(lob['bids']['lob']) >= 1) and lob['bids']['lob'][0][0] > self.limit: quoteprice_mlofi = lob['bids']['lob'][0][0] if self.job == 'Bid' and quoteprice_mlofi < bse_sys_minprice: quoteprice_mlofi = bse_sys_minprice + 1 if self.job == 'Ask' and quoteprice_mlofi > bse_sys_maxprice: quoteprice_mlofi = bse_sys_maxprice - 1 return quoteprice_mlofi quoteprice_isimple = imbalance_alter(quoteprice, lob, countdown, self.m) order = Order(self.tid, self.orders[0].atype, 'LIM', quoteprice_isimple, self.orders[0].qty, time, None, -1) self.lastquote = order return order
def getorder(self, time, countdown, lob, verbose): if len(self.orders) < 1: self.active = False return None else: self.limit = self.orders[0].price otype = self.orders[0].atype self.job = self.orders[0].atype ostyle = self.orders[0].astyle if otype == 'Bid': if lob['bids']['n'] > 0: quoteprice = lob['bids']['bestp'] if quoteprice > self.limit: quoteprice = self.limit else: quoteprice = self.limit else: if lob['asks']['n'] > 0: quoteprice = lob['asks']['bestp'] if quoteprice < self.limit: quoteprice = self.limit else: quoteprice = self.limit def imbalance_alter(quoteprice, lob): level_1_ofi_cul = 0 level_2_ofi_cul = 0 level_3_ofi_cul = 0 n = 1 while (len(self.list_OFI) >= n): level_1_ofi_cul = level_1_ofi_cul + self.list_OFI[-n][ 'level1'] level_2_ofi_cul = level_2_ofi_cul + self.list_OFI[-n][ 'level2'] level_3_ofi_cul = level_3_ofi_cul + self.list_OFI[-n][ 'level3'] n = n + 1 if (n >= 6): break level_1_depth_cul = 0 level_2_depth_cul = 0 level_3_depth_cul = 0 m = 1 while (len(self.list_D) >= m): level_1_depth_cul = level_1_depth_cul + self.list_D[-m][ 'level1'] level_2_depth_cul = level_2_depth_cul + self.list_D[-m][ 'level2'] level_3_depth_cul = level_3_depth_cul + self.list_D[-m][ 'level3'] m = m + 1 if (m >= 4): break # if(level_1_depth_cul==0): level_1_depth_cul = 10000 # if(level_2_depth_cul==0): level_2_depth_cul = 10000 # if(level_3_depth_cul==0): level_3_depth_cul = 10000 if m == 1: level_1_depth_averge = level_1_depth_cul + 1 level_2_depth_averge = level_2_depth_cul + 1 level_3_depth_averge = level_3_depth_cul + 1 else: level_1_depth_averge = level_1_depth_cul / (m - 1) + 1 level_2_depth_averge = level_2_depth_cul / (m - 1) + 1 level_3_depth_averge = level_3_depth_cul / (m - 1) + 1 c = 0.5 decay = 0.8 # print 'level_1_depth_averge is %s'%level_1_depth_averge # print 'level_2_depth_averge is %s'%level_2_depth_averge # print 'level_3_depth_averge is %s'%level_3_depth_averge offset = level_1_ofi_cul * c / level_1_depth_averge + decay * level_2_ofi_cul * c / level_2_depth_averge + decay * decay * level_3_ofi_cul * c / level_3_depth_averge # quoteprice_iaa = (quoteprice_aa+offset)*0.9 + 0.1*quoteprice_aa benchmark = quoteprice if (lob['midprice'] != None): benchmark = lob['midprice'] # print 'midprice is %d' % benchmark # print 'benchmark = %d' % benchmark quoteprice_isimple = benchmark + offset if self.job == 'Bid' and quoteprice_isimple > self.limit: quoteprice_isimple = self.limit if self.job == 'Ask' and quoteprice_isimple < self.limit: quoteprice_isimple = self.limit # print 'IAA_MLOFI original quotaprice: %d' % (quoteprice) # print 'offset is %d'%offset # print 'level1 ofi is %d'%level_1_ofi_cul # print 'level2 ofi is %d'%level_2_ofi_cul # print 'level3 ofi is %d'%level_3_ofi_cul # print 'level1 depth is %d'%level_1_depth_averge # print 'level2 depth is %d'%level_2_depth_averge # print 'level3 depth is %d'%level_3_depth_averge # print 'offset is %d'%offset # print 'IAA_MLOFI final quotaprice: %d' % (quoteprice_isimple) # print 'IAAB_MLOFI JOB IS %s' % self.job return quoteprice_isimple quoteprice_isimple = imbalance_alter(quoteprice, lob) order = Order(self.tid, self.orders[0].atype, 'LIM', quoteprice_isimple, self.orders[0].qty, time, None, -1) self.lastquote = order return order
def getorder(self, time, countdown, lob, verbose): if len(self.orders) < 1: self.active = False return None else: self.active = True self.limit = self.orders[0].price self.job = self.orders[0].atype self.calcTarget() if self.prev_best_bid_p == None: o_bid = 0 else: o_bid = self.prev_best_bid_p if self.prev_best_ask_p == None: o_ask = self.marketMax else: o_ask = self.prev_best_ask_p if self.job == 'Bid': # BUYER if self.limit <= o_bid: return None else: if len(self.previous_transactions ) <= 0: ## has been at least one transaction o_ask_plus = (1 + self.r_shout_change_relative ) * o_ask + self.r_shout_change_absolute quoteprice = o_bid + ( (min(self.limit, o_ask_plus) - o_bid) / self.offer_change_rate) else: if o_ask <= self.buy_target: quoteprice = o_ask else: quoteprice = o_bid + ((self.buy_target - o_bid) / self.offer_change_rate) if self.job == 'Ask': if self.limit >= o_ask: return None else: if len(self.previous_transactions ) <= 0: ## has been at least one transaction o_bid_minus = (1 - self.r_shout_change_relative ) * o_bid - self.r_shout_change_absolute quoteprice = o_ask - ( (o_ask - max(self.limit, o_bid_minus)) / self.offer_change_rate) else: if o_bid >= self.sell_target: quoteprice = o_bid else: quoteprice = o_ask - ((o_ask - self.sell_target) / self.offer_change_rate) def imbalance_alter(quoteprice_aa, lob, countdown, m): mlofi_list = [0 for i in range(m)] cd_list = [0 for i in range(m)] ad_list = [] n = 1 while len(self.es_list) >= n: for i in range(m): mlofi_list[i] += self.es_list[-n]['level' + str(i + 1)] n += 1 if n >= 11: break n = 1 while len(self.ds_list) >= n: for i in range(m): cd_list[i] += self.ds_list[-n]['level' + str(i + 1)] n += 1 if n >= 11: break for i in range(m): temp = None if n == 1: temp = cd_list[i] + 1 else: temp = cd_list[i] / (n - 1) + 1 ad_list.append(temp) c = 5 decay = 0.8 offset = 0 for i in range(m): offset += int(mlofi_list[i] * c * pow(decay, i) / ad_list[i]) benchmark = quoteprice_aa if (lob['midprice'] != None): benchmark = lob['midprice'] # print 'midprice is %d' % benchmark quoteprice_iaa = quoteprice_aa + 0.8 * (benchmark + offset - quoteprice_aa) if self.job == 'Bid' and quoteprice_iaa > self.limit: quoteprice_iaa = self.limit if self.job == 'Ask' and quoteprice_iaa < self.limit: quoteprice_iaa = self.limit if countdown < 0.3: print "insert" if self.job == 'Bid' and ( len(lob['asks']['lob']) >= 1) and lob['asks']['lob'][0][0] < self.limit: quoteprice_iaa = lob['asks']['lob'][0][0] if self.job == 'Ask' and ( len(lob['bids']['lob']) >= 1) and lob['bids']['lob'][0][0] > self.limit: quoteprice_iaa = lob['bids']['lob'][0][0] if self.job == 'Bid' and quoteprice_iaa < bse_sys_minprice: quoteprice_iaa = bse_sys_minprice + 1 if self.job == 'Ask' and quoteprice_iaa > bse_sys_maxprice: quoteprice_iaa = bse_sys_maxprice - 1 return quoteprice_iaa quoteprice_iaa = imbalance_alter(quoteprice, lob, countdown, self.m) order = Order(self.tid, self.orders[0].atype, 'LIM', quoteprice_iaa, self.orders[0].qty, time, None, -1) self.lastquote = order return order
def getorder(self, time, countdown, lob, verbose): if verbose: print('ZIP getorder(): LOB=%s' % lob) # random coefficient, multiplier on trader's own estimate of worst possible bid/ask prices # currently in arbitrarily chosen range [2, 5] worst_coeff = 2 + (3 * random.random()) if len(self.orders) < 1: self.active = False order = None else: self.active = True self.limit = self.orders[0].price self.job = self.orders[0].atype if self.job == 'Bid': # currently a buyer (working a bid order) self.margin = self.margin_buy # what is the worst bid price on the LOB right now? if len(lob['bids']['lob']) > 0 : # take price of final entry on LOB worst_bid = lob['bids']['lob'][-1][0] else: # local pessimistic estimate of the worst bid price (own version of stub quote) worst_bid = max(1, int(self.limit / worst_coeff)) if self.worst_bidprice == None: self.worst_bidprice = worst_bid elif self.worst_bidprice > worst_bid: self.worst_bidprice = worst_bid else: # currently a seller (working a sell order) self.margin = self.margin_sell # what is the worst ask price on the LOB right now? if len(lob['asks']['lob']) > 0 : # take price of final entry on LOB worst_ask = lob['asks']['lob'][-1][0] else: # local pessimistic estimate of the worst ask price (own version of stub quote) worst_ask = int(self.limit * worst_coeff) if self.worst_askprice == None: self.worst_askprice = worst_ask elif self.worst_askprice < worst_ask: self.worst_askprice = worst_ask quoteprice = int(self.limit * (1 + self.margin)) self.price = quoteprice # set limitprice and MES limitprice = quoteprice MES = 0 marketid = None # only big block order can be BI or BDN order subtype = None big_block = 200 # cancel BI order when its reputation is lower than threshold threshold = 50 if self.reputation < threshold: self.biweight = 0 if self.orders[0].qty >= big_block: subtype = random.choices(population=['BI','BDN', None],weights=[0.5, 0.5, 0],k=1)[0] order = Order(self.tid, self.job, "LIM", quoteprice, self.orders[0].qty, time, None, -1, limitprice, MES, subtype, marketid) order.myref = self.orders[0].assignmentid if self.orders[0].qty < big_block: print('Why') if subtype != 'BI': self.lastquote = order return order