def get_u_l(u,l,u_,l_,length,n,old): global days global hours_4 global hours global mins_30 global mins_15 global mins_5 global mins_1 if u_==0: x=l+random.random()*(u-l) if old>-1: easylog.debug("====== NOW TIME ====== u:%s, l:%s, u_:%s, l_:%s, length:%s, n:%s, old:%s",u,l,u_,l_,length,n,old) x1=old*(0.9995+0.001*random.random()) if x1<u and x1>l: x=x1 set_basic_data(x) return False,x,x tm=(length-(u_-l_)) _l=l_-tm _u=u_+tm if _u<l or _l>u: reallocate(n,u,l) if n==6: dates_point=days if n==5: dates_point=hours_4 if n==4: dates_point=hours if n==3: dates_point=mins_30 if n==2: dates_point=mins_15 if n==1: dates_point=mins_5 if n==0: dates_point=mins_1 return get_u_l(u,l,dates_point[0],dates_point[1],dates_point[2],n,old) _l=_l if _l>l else l _u=_u if _u<u else u return True,_l,_u
def wrapper(*args, **kwargs): easylog.debug('Begin %s', method.__name__) start_point= time.time() method_rc = method(*args, **kwargs) easylog.info('End %s() FUNC TIME USAGE:%.3fs', method.__name__, time.time()-start_point) return method_rc
def wrapper(self, *args, **kwargs): easylog.debug('Begin %s.%s', self.__class__.__name__, method.__name__) start_point= time.time() method_rc = method(self, *args, **kwargs) easylog.info('End call %s.%s() FUNC TIME USAGE:%.3fs', self.__class__.__name__, method.__name__, time.time()-start_point) return method_rc
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)
def store_data(profits,count,spend,spend_c): easylog.debug('flag1:filled store_data: profits,count,spend,spend_c %s %s %s %s', profits,count,spend,spend_c) with open('./robot_data.json',"w") as file: file.write(json.dumps({"profits":profits,"count":count,"spend":spend,"spend_c":spend_c}))
def reset_days(tm,lv,_u): global days global hours_4 global hours global mins_30 global mins_15 global mins_5 global mins_1 if lv >5: k=random_beta(15,24)*0.6+0.4 k=k*_u if tm>0: days=[0,0,k,now_to_day_number(),0] else: tn=days[3] days=[0,0,k,tn,0] if lv >4: k=(random_beta(15,24)*0.5+0.5)*days[2] if k<0.31*_u: k=0.31*_u if tm>0: hours_4=[0,0,k,now_to_hours_4_number(),0] else: tn=hours_4[3] hours_4=[0,0,k,tn,0] if lv >3: k=(random_beta(4,7.5)*0.6+0.4)*hours_4[2] if k<0.15*_u: k=0.15*_u if tm>0: hours=[0,0,k,now_to_hours_number(),0] else: tn=hours[3] hours=[0,0,k,tn,0] if lv >2: k=(random_beta(3.8,7.3)*0.7+0.3)*hours[2] if k<0.07*_u: k=0.07*_u if tm>0: mins_30=[0,0,k,now_to_mins_30_number(),0] else: tn=mins_30[3] mins_30=[0,0,k,tn,0] if lv >1: k=(random_beta(3.5,7)*0.7+0.3)*mins_30[2] if k<0.03*_u: k=0.03*_u if tm>0: mins_15=[0,0,k,now_to_mins_15_number(),0] else: tn=mins_15[3] mins_15=[0,0,k,tn,0] if lv >0: k=(random_beta(2.5,6.2)*0.8+0.2)*mins_15[2] if k<0.015*_u: k=0.015*_u if tm>0: mins_5=[0,0,k,now_to_mins_5_number(),0] else: tn=mins_5[3] mins_5=[0,0,k,tn,0] if lv >-1: k=(random_beta(2.5,6)*0.8+0.2)*mins_5[2] if k<0.007*_u: k=0.007*_u if tm>0: mins_1=[0,0,k,now_to_mins_1_number(),0] else: tn=mins_1[3] mins_1=[0,0,k,tn,0] easylog.debug("flag_generate_x: u=%s \n days:%s \n hours_4:%s \n hours:%s \n mins_30:%s \n mins_15:%s \n mins_5:%s \n mins1_:%s \n",_u,days,hours_4,hours,mins_30,mins_15,mins_5,mins_1)