def test_coin_trans(self): pricebuffer = priceupdate.PriceBuffer('btc38', save_log_flag=False) priceitem = pricebuffer.getpriceitem('btc38', 'doge_btc') # 循环检查OPEN订单是不是满足卖出条件 orderstatus1 = self.coin_trans( 'btc38', 'buy', 0.01224, priceitem) # orderstatus2 = self.coin_trans( 'btc38', 'sell', 0.01345, priceitem) # print('order status 1: {0}, 2: {1}'.format(orderstatus1, orderstatus2)) runtimes = 0 while (True): self.update_order_status() pricebuffer = priceupdate.PriceBuffer('btc38', save_log_flag=False) newpriceitem = pricebuffer.getpriceitem('btc38', 'doge_btc') order_list = ormmysql.openorderlist() for openorderitem in order_list: if openorderitem.buy_status == const.ORDER_STATUS_CLOSED: # 满足卖出条件后提交卖出订单 if newpriceitem.buy_price>openorderitem.buy_price*(1+self.__sell_profit_rate) and openorderitem.sell_status ==const.ORDER_STATUS_OPEN: self.coin_trans('btc38', 'sell', newpriceitem.buy_price, openorderitem.priceitem) print('满足卖出条件,提交了定单{0}, 卖出价格:{1}'.format(openorderitem.sell_order_id, newpriceitem.buy_price)) runtimes = runtimes + 1 time.sleep(5) print('has run %d times'%runtimes) pass # 更新订单的状态 self.update_order_status()
def stop_lost(self, curr_pricitem): open_order_list = ormmysql.openorderlist() for open_order in open_order_list: # 如果当前价格和买入价格小于止损的比例,则执行先取消订单再按当前价格的直接卖出操作 curr_price = curr_pricitem.sell_price if curr_price/open_order.buy_price<(1 - publicparameters.STOP_LOST_RATE) and curr_pricitem.coin == open_order.coin: # if curr_pricitem.coin == open_order.coin: status = self.order_market.cancelOrder(open_order.sell_order_id, coin_code=curr_pricitem.coin) # # TEST usage # status = const.CANCEL_STATUS_SUCC if status == const.CANCEL_STATUS_FAIL: pass elif status == const.CANCEL_STATUS_SUCC: # 更新订单的状态为取消 # ormmysql.updateorder(open_order) # 重新卖出,以当前价卖出进行止损 sell_status = self.coin_trans(self.market,const.TRANS_TYPE_SELL,curr_price,open_order.priceitem) # # test only # sell_status = True # 止损卖出成功 if sell_status is True: print("-------:(--------订单进行了止损操作,coin:{0},操作时间:{1}".format(open_order.coin,common.get_curr_time_str())) self.update_order_status() return True pass # 止损卖出失败,继续进行循环操作进行下一次的自动卖出 else: return False pass pass
def update_order_status(self): order_list = ormmysql.openorderlist() for orderitem in order_list: if orderitem.buy_status == const.ORDER_STATUS_OPEN: order_status = self.order_market.getOrderStatus(orderitem.buy_order_id, orderitem.priceitem.coin) # 处理状态取值时错误的情况 if order_status is None: continue orderitem.buy_status = order_status ormmysql.updateorder(orderitem) if orderitem.sell_status == const.ORDER_STATUS_OPEN: order_status = self.order_market.getOrderStatus(orderitem.sell_order_id, orderitem.priceitem.coin) # 处理状态取值时错误的情况 if order_status is None: continue orderitem.sell_status = order_status ormmysql.updateorder(orderitem) if order_status == const.ORDER_STATUS_CLOSED: orderitem.sell_date = common.get_curr_time_str() ormmysql.updateorder(orderitem) # 把交易记录从交易表转移到LOG表 ormmysql.delorder(orderitem) profit_amount = round(orderitem.sell_amount - orderitem.buy_amount,10) print('{0}:[{1}]{smile}已经成功交易,盈利{2}! BuyPrice:{3}, SellPrice:{4}, ProfiteRate:{5}'.format(\ common.get_curr_time_str(), orderitem.priceitem.coin, profit_amount, orderitem.buy_price, orderitem.sell_price,\ publicparameters.SELL_PROFIT_RATE, smile='^_^ '*5)) # the sell status is closed to move log table # 这里会导致出问题,出现上面更新后又再次删除的情况 # if orderitem.sell_status == const.ORDER_STATUS_CLOSED: # # 把交易记录从交易表转移到LOG表 # ormmysql.delorder(orderitem) pass
def get_order_item(self, priceitem): orderitem_result = None order_list = ormmysql.openorderlist() for item in order_list: if item.priceitem.pricedate == priceitem.pricedate and item.priceitem.coin == priceitem.coin\ and item.priceitem.buy_price == priceitem.buy_price: orderitem_result = item break return orderitem_result
def get_coin_rate_in_open_orders(self, coin): open_order_list = ormmysql.openorderlist() # total_count = ormmysql.openordercount() # 用POOL的最大值来检查单个币种的比例 total_count = publicparameters.MAX_OPEN_ORDER_POOL coin_count = 0 for open_order in open_order_list: if open_order.coin == coin: coin_count = coin_count + 1 if total_count == 0: rate =0 else: rate = round(coin_count/total_count,2) return rate
def sell_check(self): open_order_list = ormmysql.openorderlist() for curroderitem in open_order_list: # The transaction of selling is in progress then check next if curroderitem.sell_status ==const.ORDER_STATUS_OPEN: continue pricebuffer = priceupdate.PriceBuffer(self.market, save_log_flag=False) # 获取当前的价格 newpriceitem = pricebuffer.getpriceitem(self.market, curroderitem.coin+'_btc') if newpriceitem is not None and curroderitem.buy_status == const.ORDER_STATUS_CLOSED: # 测试,卖单提前生成好,等待直接成效 rounding_num_unit = publicparameters.rounding_unit(curroderitem.coin) rounding_num_price = publicparameters.rounding_price(curroderitem.coin) default_sell_price = round(curroderitem.buy_price*(1+publicparameters.SELL_PROFIT_RATE),rounding_num_price)
def cancle_ot_buy_order(self, duration): open_order_list = ormmysql.openorderlist() curr_time = common.CommonFunction.get_curr_date() for curr_order_item in open_order_list: diff = curr_time - common.CommonFunction.strtotime(curr_order_item.buy_date) diffseconds = diff.seconds # Only cancel the buy status =open if diffseconds >duration and curr_order_item.buy_status == const.ORDER_STATUS_OPEN: cancel_status = self.order_market.cancelOrder(curr_order_item.buy_order_id, curr_order_item.coin) if cancel_status == 'success': curr_order_item.buy_status=const.ORDER_STATUS_CANCEL # update the buy status ormmysql.updateorder(curr_order_item) # move the records to log table ormmysql.delorder(curr_order_item) pass