def start_fetch_blockid(id_dict, conf_dict, latest_block_info): id_dict_size = 3600 last_clean_ts = 0 while True: try: url = "%s/v1/chain/get_info" % (conf_dict['eoshost']) res = requests.get(url, timeout=3.0) if res.status_code / 100 != 2: easylog.error("Failed to get_info:%s", res.text) continue result = json.loads(res.text) now_ts = time.time() latest_block_info['ts'], latest_block_info['info'] = now_ts, result if result['head_block_num'] in id_dict and result['head_block_id'] == id_dict[result['head_block_num']]['id']: continue easylog.info('%d : %s', result['head_block_num'], result['head_block_id']) id_dict[result['head_block_num']] = {'id':result['head_block_id'],'ts':time.time()} nums = list(id_dict.keys()) if now_ts-last_clean_ts > id_dict_size/2 and len(nums)>id_dict_size: last_clean_ts = now_ts for key in nums: if now_ts - id_dict[key]['ts'] > id_dict_size/2: del id_dict[key] except Exception as e: easylog.exception("%s", e) finally: time.sleep(conf_dict['get_info_sec'])
def start_httpserver(handler_map, opts): service_conf = read_json_conf(opts.conf_file) if not service_conf: easylog.error('Failed to get the config file:%s', opts.conf_file) return app = Application(handler_map, opts, service_conf) app.listen(opts.port) tornado.ioloop.IOLoop.current().start()
def start_httpserver(handler_map, opts): service_conf = read_json_conf(opts.conf_file) if not service_conf: easylog.error('Failed to get the config file:%s', opts.conf_file) return app = Application(handler_map, opts, service_conf) fetch_th = threading.Thread(target=start_fetch_blockid, args=(app.local_cache, app.service_conf, app.latest_block_info)) fetch_th.start() app.listen(opts.port) tornado.ioloop.IOLoop.current().start()
async def fetch_info(self): http_client = AsyncHTTPClient() url = "%s/v1/chain/get_info" % ( self.application.service_conf['eoshost']) try: result = await http_client.fetch(url) if result.code / 100 != 2: easylog.error("Failed to get_info error:%s", result.body) return None data = json.loads(result.body) return data except Exception as e: easylog.exception('block_num:%d %s', block_num, e) return None
async def fetch_block(self, block_num): http_client = AsyncHTTPClient() body_params = '{"block_num_or_id":%d}' % (block_num) url = "%s/v1/chain/get_block" % (self.application.service_conf['eoshost']) try: result = await http_client.fetch(url, method="POST", body=body_params) if result.code / 100 != 2: easylog.error("Failed to get %d error:%s", block_num, result.body) return None data = json.loads(result.body) return data['id'] except Exception as e: easylog.exception('block_num:%d %s', block_num, e) return None
def calculate_count(price_u,L,old_profits,old_count,price_rate_D1,allocate_rate_D2,allocate_count_D2,price_rate_D2,old_money,V,alpha,step,a): l=L+(price_rate_D1[step]*1.01-price_rate_D1[0])*price_u # l=price_u*price_rate_D1[step]*1.01-ds if step<len(price_rate_D1)-1: l=L+(price_rate_D1[step+1]-price_rate_D1[0])*price_u x=allocate_count_D2[step]*((price_rate_D2[step]*l)/(price_rate_D1[step]*price_u)*(1+random_normal())) if price_u<V: x=allocate_count_D2[step]*((price_rate_D2[step]*l)/(price_rate_D1[step]*price_u)*random_normal()) price=price_u*price_rate_D1[step] tms=0 while(True): xc=0 profits1=x*(price-V*alpha) if profits1<0: profits1=0 profits=profits1+old_profits ## 计算E sm=0 for i in range(len(allocate_rate_D2)): sm+=allocate_rate_D2[i]*l*math.pow(1/(1+a),i) rb=calculate_rebuy_rate(old_money,profits) real_money=rb*profits+old_money c=real_money/sm total_px=0 for i in range(len(allocate_rate_D2)): ar=allocate_rate_D2[i] pr=l*math.pow(1/(1+a),i) if pr>V: tp=(pr-V)*ar*c total_px=total_px+tp xc+=1 else: break if xc==len(allocate_rate_D2): if profits*(1-rb)>old_money*0.4: return x else: if total_px<=rb*profits: return x x=x*1.01 tms=tms+1 if tms>1000: easylog.error("calculate_count price_u:%s,L:%s,old_money:%s", price_u, L, old_money) return x
def calculate_u(allocate_rate,price_rate,profits,count,rb,old_money,V,alpha,old_u,E,a): """ """ pp=profits-alpha*V*count if pp<0: pp=0 total_money=pp+old_money ds=old_u*(1-1/(1+a*2))*random.random() l=old_u-ds # while(l*1.02<u0): # ds=u0*(1-1/1.02)*random.random() # l=u0-ds tms=0 while(True): c=total_money/E/l total_px=0 xc=0 for i in range(len(allocate_rate)): ar=allocate_rate[i] pr=price_rate[i]*l if pr>V: tp=(pr-V)*ar*c total_px=total_px+tp xc+=1 else: break if xc==len(allocate_rate): if profits*(1-rb)>old_money: return l else: if total_px<=pp: return l l=l*0.99 tms=tms+1 if tms>100000: easylog.error("calculate_u profits:%s,count:%s", profits, count) return l return l
def main_process(conf_file): global V_,self_unfilled_orders_sell,self_filled_orders_sell,self_unfilled_orders_buy,self_filled_orders_buy global unfilled_orders,self_unfilled_orders,orders_helper,self_unfilled_orders_sell_helper,self_unfilled_orders_buy_helper global g_config, g_config_file global days global mins_1 g_config_file = conf_file reload_robot_config(1, None) if not init_hooclient(g_config['dbfile'], g_config['client_id'], g_config['client_key'], g_config['dding_atoken'], g_config['bos_pairid']): easylog.error('Failed to init_hooclient()') return D1=[] D2=[] old_x=0 back_start_time=0 analyse_circle_startime=0 self_buy_delimited=0 back_pow=0 profits=0 count=0 spend=0 spend_c=0 jd=load_data() if jd: profits=jd["profits"] count=jd["count"] spend=jd["spend"] spend_c=jd["spend_c"] time_circle=date2num(datetime.datetime.now()) up_count=0 up_spend_c=0 #U_value=0.04 #V_=0.005633 U_value= 1.02 * V_ tm=date2num(datetime.datetime.now()) week_end=tm+7 day_end=tm+1 hour_end=tm+1/24 week_spend=0 day_spend=0 hour_spend=0 old_self_sell_max=10000 old_self_buy_min=sys.maxsize t_spend=0 t_profits=0 t_spend_c=0 t_profits_C=0 hit_hour_max_check_wait_interval=2.0 D1_l=0 D_l=0 self_buy_under_V=0 distance=0 model=0 update_D=True setup_env() while True : try: ##查询orders unfilled_orders_ = query_unfilled_orders(TARGET_PID, HOST_ACT) filled_orders_=query_filled_orders(TARGET_PID, HOST_ACT) tm=date2num(datetime.datetime.now()) buy_sell_1_cash_amount = [0,0] # calculate the buy&sell first order dealed by others instead of robot buy_sell_1_amount = [0,0] # 0: buy, 1: sell ## 分析 orders #1.计算profits 和count 同时找出 下探点 profits_=0 count_=0 spend_=0 spend_c_=0 self_sell_price_min=0 self_buy_min=sys.maxsize rows=unfilled_orders_["rows"] for row in rows: if row["side"]=='sell': if row["order_id"] in sell_1: buy_sell_1_cash_amount[1] = float(row["filled_cash_amount"]) buy_sell_1_amount[1]=float(row["filled_amount"]) continue if row["order_id"] in self_unfilled_orders_sell.keys(): old_record=self_unfilled_orders_sell[row["order_id"]] filled_amount=float(row["filled_amount"]) filled_cash_amount=float(row["filled_cash_amount"]) D1_l=min(D1_l,float(row["price"])) if filled_cash_amount>old_record["filled_cash_amount"]: profits_+=filled_cash_amount-old_record["filled_cash_amount"] count_+=filled_amount-old_record["filled_amount"] easylog.debug("flag1:unfilled sell %s filled_cash_amount:%s old_filled_cash_amount:%s,profits_:%s,count_:%f", row["order_id"], filled_cash_amount,old_record["filled_cash_amount"],profits_,count_) self_unfilled_orders_sell[row["order_id"]]["filled_cash_amount"]=filled_cash_amount self_unfilled_orders_sell[row["order_id"]]["filled_amount"]=filled_amount self_sell_price_min=max(self_sell_price_min,float(row["price"])) else: #ret = cancel_order(row["order_id"],{'trade_no':row["trade_no"]},HOST_ACT) easylog.info("unfilled SELL Order not exist,order:%s", row) else: if row["order_id"] in buy_1: buy_sell_1_cash_amount[0]= float(row["filled_cash_amount"]) buy_sell_1_amount[0]=float(row["filled_amount"]) if row["order_id"] in back_buy_orders: cancel_order_for_back_buy(row["order_id"]) continue if row["order_id"] in self_unfilled_orders_buy.keys(): old_record=self_unfilled_orders_buy[row["order_id"]] filled_amount=float(row["filled_amount"]) filled_cash_amount=float(row["filled_cash_amount"]) if filled_cash_amount>old_record["filled_cash_amount"]: spend_+=filled_cash_amount-old_record["filled_cash_amount"] spend_c_+=filled_amount-old_record["filled_amount"] easylog.debug("flag1:unfilled no-sell %s filled_cash_amount:%s old_filled_cash_amount:%s,spend_:%s,spend_c_:%f", row["order_id"], filled_cash_amount,old_record["filled_cash_amount"],spend_,spend_c_) self_unfilled_orders_buy[row["order_id"]]["filled_cash_amount"]=filled_cash_amount self_unfilled_orders_buy[row["order_id"]]["filled_amount"]=filled_amount self_buy_min=min(self_buy_min,float(row["price"])) else: #ret = cancel_order(row["order_id"],{'trade_no':row["trade_no"]},HOST_ACT) easylog.info("unfilled BUY Order not exist,order:%s", row) rows=filled_orders_["rows"] for row in rows: if row["status"]=='canceled': if row['order_id'] in self_unfilled_orders_buy.keys(): del self_unfilled_orders_buy[row['order_id']] delete_list(row['order_id'],self_unfilled_orders_buy_helper,1) elif row['order_id'] in self_unfilled_orders_sell.keys(): del self_unfilled_orders_sell[row["order_id"]] delete_list(row["order_id"],self_unfilled_orders_sell_helper,1) continue if row["side"]=='sell': if row["order_id"] in sell_1: buy_sell_1_cash_amount[1]= float(row["filled_cash_amount"]) buy_sell_1_amount[1]=float(row["filled_amount"]) sell_1.remove(row["order_id"]) continue if row["order_id"] in self_unfilled_orders_sell.keys(): old_record=self_unfilled_orders_sell[row["order_id"]] filled_amount=float(row["filled_amount"]) filled_cash_amount=float(row["filled_cash_amount"]) if filled_cash_amount>old_record["filled_cash_amount"]: profits_+=filled_cash_amount-old_record["filled_cash_amount"] count_+=filled_amount-old_record["filled_amount"] easylog.debug("flag1:filled sell %s filled_cash_amount:%s old_filled_cash_amount:%s,profits_:%s,count_:%f", row["order_id"], filled_cash_amount,old_record["filled_cash_amount"],profits_,count_) self_sell_price_min=max(self_sell_price_min,float(row["price"])) del self_unfilled_orders_sell[row["order_id"]] delete_list(row["order_id"],self_unfilled_orders_sell_helper,1) else: if row["order_id"] in buy_1: buy_1.remove(row["order_id"]) buy_sell_1_cash_amount[0]= float(row["filled_cash_amount"]) buy_sell_1_amount[0]=float(row["filled_amount"]) if row["order_id"] in back_buy_orders: del back_buy_orders[row["order_id"]] if row["order_id"] in self_unfilled_orders_buy.keys(): old_record=self_unfilled_orders_buy[row["order_id"]] filled_amount=float(row["filled_amount"]) filled_cash_amount=float(row["filled_cash_amount"]) if filled_cash_amount>old_record["filled_cash_amount"]: spend_+=filled_cash_amount-old_record["filled_cash_amount"] spend_c_+=filled_amount-old_record["filled_amount"] easylog.debug("flag1:filled no-sell %s filled_cash_amount:%s old_filled_cash_amount:%s,spend_:%s,spend_c_:%f", row["order_id"], filled_cash_amount,old_record["filled_cash_amount"],spend_,spend_c_) self_buy_min=min(self_buy_min,float(row["price"])) del self_unfilled_orders_buy[row["order_id"]] delete_list(row["order_id"],self_unfilled_orders_buy_helper,1) depths = query_pair_book(TARGET_PID) buy_depth=depths["data"]["bids"] sell_depth=depths["data"]["asks"] sell_edge,self_edge,sum_d,under_v,D_l=calculate_depth(sell_depth) profits+=profits_ count+=count_ spend+=spend_ spend_c+=spend_c_ if buy_sell_1_cash_amount[0]>buy_sell_1_cash_amount[1]: spend+=buy_sell_1_cash_amount[0]-buy_sell_1_cash_amount[1] spend_c+=buy_sell_1_amount[0]-buy_sell_1_amount[1] else: profits+=buy_sell_1_cash_amount[1]-buy_sell_1_cash_amount[0] count+=buy_sell_1_amount[1]-buy_sell_1_amount[0] if buy_sell_1_cash_amount[0] != buy_sell_1_cash_amount[1]: easylog.info("buy_sell_1_cash_amount:%s buy_sell_1_amount:%s", buy_sell_1_cash_amount, buy_sell_1_amount) profits_and_spen = profits-spend count_and_spen_c = count-spend_c store_data(profits,count,spend,spend_c) if len(buy_depth)>0 and len(sell_depth)>0 and float(buy_depth[0]['price']) > float(sell_depth[0]['price']): easylog.error("WRONG depth occur:%s", depths) sys.exit(1) if len(sell_depth)>0 and len(D2)>0 and len(D2[0])>0 and float(sell_depth[0]['price']) < D2[0][1]: easylog.warning("WARNING depth occur: depths %s D1:%s D2:%s", depths, D1, D2) x=0 if model==0: easylog.info("model==0 self_sell_price_min:%s D_l:%s", self_sell_price_min,D_l) U_value=max(self_sell_price_min,D_l) # self_buy_min 自我买单边 # D1_l=0 自我挂卖单边 # D_l=0 所有挂单边 # self_buy_under_V=0 # distance 距离差 #被吃单判断 触发还可以更改一下 if spend_c_>0: old_self_buy_min=self_buy_min #等待 t_spend_c+=spend_c_ t_spend+=spend_ t_profits+=profits_ t_profits_C+=count_ time.sleep(hit_hour_max_check_wait_interval) continue if t_spend_c>0: if sell_edge<V_ and t_profits_C>0: U_value=min(sell_edge,old_self_buy_min) tv=max(profits_and_spen,0) tc=max(count_and_spen_c,0) if profits_and_spen<0 or count_and_spen_c<0 : tv=0 tc=0 D1,D2=calculate_D1_D2(g_config['whole_depth_money'],tv,tc,U_value,V_) easylog.info("D1:%s D2:%s whole_depth_money:%s profits:%s count:%s spend:%s spend_c:%s old_self_buy_min:%s sell_edge:%s V_:%s", D1,D2,g_config['whole_depth_money'],profits,count,spend,spend_c,old_self_buy_min,sell_edge,V_) time_circle=date2num(datetime.datetime.now()) create_sell_orders(D1) create_buy_orders(D2) depths = query_pair_book(TARGET_PID) buy_depth=depths["data"]["bids"] sell_depth=depths["data"]["asks"] sell_edge,self_edge,sum_d,under_v,D_l=calculate_depth(sell_depth) # 判断 D1_l与D_l之间的关系 if D1[0][1] <D_l: #随机游走模式 model=2 else: #缓慢上买模式 model=1 t_spend_c=0 t_spend=0 t_profits=0 t_profits_C=0 tp=date2num(datetime.datetime.now()) if tp-time_circle > 1/96: easylog.info("tp:%s time_circle:%s", tp, time_circle) update_D=True time_circle=tp if model==0 and profits_>0: U_value=self_sell_price_min easylog.info("model==0 and profits_>0 set update_D=True,self_sell_price_min:%s",self_sell_price_min) update_D=True if update_D: #U_value? tv=max(profits_and_spen,0) tc=max(count_and_spen_c,0) if profits_and_spen<0 or count_and_spen_c<0 : tv=0 tc=0 D1,D2=calculate_D1_D2(g_config['whole_depth_money'],tv,tc,U_value,V_) easylog.info("D1:%s D2:%s whole_depth_money':%s profits:%s count:%s spend:%s spend_c:%s U_value:%s V_:%s", D1,D2,g_config['whole_depth_money'],profits,count,spend,spend_c,U_value,V_) create_sell_orders(D1) create_buy_orders(D2) time_circle=date2num(datetime.datetime.now()) depths = query_pair_book(TARGET_PID) buy_depth=depths["data"]["bids"] # all buy orders sell_depth=depths["data"]["asks"] # all sell orders sell_edge,self_edge,sum_d,under_v,D_l=calculate_depth(sell_depth) update_D = False if model==1: easylog.debug("==1=> model1") if sum_d>0 and D1[0][1] >=D_l: tm=date2num(datetime.datetime.now()) while tm>week_end: week_end+=7 week_spend=0 while tm>day_end: day_end+=1 day_spend=0 while tm>hour_end: hour_end+=1/24 hour_spend=0 canspend=calculate_back_count(sum_d,g_config['week_max_buy'],g_config['day_max_buy'],g_config['hour_max_buy'],week_spend,day_spend,hour_spend) easylog.info("week_end:%s,day_end:%s,hour_end:%s,tm:%s,canspend:%s,hour_spend:%s sum_d:%s hour_spend:%s week_spend:%s,day_spend:%s", week_end,day_end,hour_end,tm,canspend,hour_spend,sum_d,hour_spend,week_spend,day_spend) x=generate_buy(week_end,day_end,hour_end,tm,canspend,hour_spend,(g_config['buy1_sell1_interval_sec']+g_config['market_check_interval_sec'])/24/3600) x_ = x if x > 0: under_v.sort(key=take_second) if under_v[0][0]*under_v[0][1]<x: countx=under_v[0][0] x=under_v[0][1] else: countx=x/under_v[0][1] x=under_v[0][1] easylog.info("t3:sum_d:%s canspend:%s x_:%s x:%s countx:%s under_v[0]:%s", sum_d,canspend, x_, x, countx, under_v[0]) create_buy_order((countx, x),True) xc=x*countx week_spend+=xc day_spend+=xc hour_spend+=xc U_value=x if g_config['buy1_sell1_interval_sec']>0: time.sleep(g_config['buy1_sell1_interval_sec']) else: model=2 if model==2: easylog.debug("==2=> model2") #随机设置U_value #判断结束条件 if D1[0][1]>V_ and D_l>V_: model=0 continue if sum_d>0: model=1 continue #1.查看是否有人买我的卖单,并得出吃单位置 #需要迅速上移U_value if self_sell_price_min>0: U_value=self_sell_price_min tv=max(profits_and_spen,0) tc=max(count_and_spen_c,0) if profits_and_spen<0 or count_and_spen_c<0 : tv=0 tc=0 D1,D2=calculate_D1_D2(g_config['whole_depth_money'],tv,tc,U_value,V_) easylog.info("model2: D1:%s D2:%s whole_depth_money:%s profits:%s count:%s old_self_buy_min:%s sell_edge:%s V_:%s", D1,D2,g_config['whole_depth_money'],profits,count,old_self_buy_min,sell_edge,V_) time_circle=date2num(datetime.datetime.now()) create_sell_orders(D1) create_buy_orders(D2) continue db_max=0 for item in buy_depth: dprice=float(item["price"]) db_max=max(db_max,dprice) if db_max>D1[0][1]: easylog.error("db_max>D1[0][1]: %s > %s",db_max,D1[0][1]) l=min(old_self_buy_min,db_max) U_value=l+(D1[0][1]-l)*random_beta(7,3) x=0 countx=0 if x==0: easylog.debug("model = %d", model) tm=date2num(datetime.datetime.now()) x=generate_k_line(min(U_value,D1[0][1]),D2[0][1],tm,old_x) old_x=x countx=(g_config['buy1_sell1_interval_sec']+g_config['market_check_interval_sec'])*g_config['oneday_min_volume']/(min(U_value,D1[0][1])*0.98)/600 if days[2]>0 and mins_1[2]>0: tmv=mins_1[2]/days[2] else: tmv=1/144*(0.6+random.random()) if tmv>1/70: tmv=1/70.0*(0.5+0.5*random.random()) countx*=tmv countx=countx*(0.2+random.random()*0.8) easylog.info("countx:%s x:%s U_value:%s tmv:%s", countx, x, U_value, tmv) x = round(x, PRICE_RNUM) buy_max,buy_count=calculate_buy_edge(buy_depth) normal=False if buy_max<x: normal=True else: tr=random.random() if tr<0.08: if buy_count < MIN_TOKEN_AMOUNT: buy_count = MIN_TOKEN_AMOUNT create_sell_order((buy_count, buy_max)) elif tr<0.2: buy_count_tp = buy_count*(0.1+random.random()*0.9) if buy_count_tp < MIN_TOKEN_AMOUNT: buy_count_tp = buy_count create_sell_order((buy_count_tp, buy_max)) else: normal=True ds=D_l-buy_max if ds>0: x=buy_max+ds*random_beta(0.08,11) x = round(x, PRICE_RNUM) easylog.debug("normal:%s tr:%f ds:%s buy_max:%s x:%s", normal, tr, D_l-buy_max, buy_max, x) if normal: if countx < MIN_TOKEN_AMOUNT: countx = MIN_TOKEN_AMOUNT * (1+random.random()) rrt=random.random() if rrt<0.5: create_buy_order((countx, x)) if g_config['buy1_sell1_interval_sec']>0: time.sleep(g_config['buy1_sell1_interval_sec']) create_sell_order((countx, x)) else: create_sell_order((countx, x)) if g_config['buy1_sell1_interval_sec']>0: time.sleep(g_config['buy1_sell1_interval_sec']) create_buy_order((countx, x)) time.sleep(g_config['market_check_interval_sec']) except Exception as ee: try: easylog.exception("Exception:%s", ee) send_dingding_msg("HooRobot restarted Exception:"+str(ee), g_config['dding_atoken']) store_data(profits,count,spend,spend_c) D1=[] D2=[] old_x=0 back_start_time=0 analyse_circle_startime=0 self_buy_delimited=0 back_pow=0 profits=0 count=0 spend=0 spend_c=0 jd=load_data() if jd: profits=jd["profits"] count=jd["count"] spend=jd["spend"] spend_c=jd["spend_c"] time_circle=date2num(datetime.datetime.now()) up_count=0 up_spend_c=0 #U_value=0.04 #V_=0.005633 U_value= 1.02 * V_ tm=date2num(datetime.datetime.now()) week_end=tm+7 day_end=tm+1 hour_end=tm+1/24 week_spend=0 day_spend=0 hour_spend=0 old_self_sell_max=10000 old_self_buy_min=sys.maxsize t_spend_c=0 t_spend=0 t_profits=0 t_profits_C=0 hit_hour_max_check_wait_interval=2.0 D1_l=0 D_l=0 self_buy_under_V=0 distance=0 model=0 update_D=True setup_env() except Exception as eee: easylog.exception("Exception:%s", eee)