Example #1
0
 def check_shared_coin_list(self, coinlist):
     #比较多个市场之间的价格
     sharedcoinlist = []
     for market_base in self.__market_list:
         market_vs_list = list(self.__market_list)
         #移除作为base 的市场
         market_vs_list.remove(market_base)
         #对比每一个其它的市场
         for market_vs in market_vs_list:
             #对每个coin进行分析
             for coin in coinlist:
                 try:
                     #base的市场价格
                     price_base = pricemanage.PriceManage(
                         market_base, coin).get_coin_price()
                     #需要对比的市场价格
                     price_vs = pricemanage.PriceManage(
                         market_vs, coin).get_coin_price()
                     x = price_base.buy_cny
                     if x:
                         sharedcoin = sharedmarketcoin.SharedMarketCoin(
                             market_base, market_vs, coin)
                         sharedcoinlist.append(sharedcoin)
                         print(str(sharedcoin))
                 except Exception as e:
                     pass
                     #print(str(e))
     coinlist = {}
     for x1 in sharedcoinlist:
         coinlist[x1.coin_code_base] = x1.coin_code_base
     for coin in coinlist:
         print(coin)
     return coinlist
Example #2
0
 def find_bal_coin(self):
     #比较多个市场之间的价格
     for market_base in self.__market_list:
         market_vs_list = list(self.__market_list)
         #移除作为base 的市场
         market_vs_list.remove(market_base)
         #对比每一个其它的市场
         for market_vs in market_vs_list:
             #对每个coin进行分析
             for coin in self.__coin_list:
                 #base的市场价格
                 price_base = pricemanage.PriceManage(
                     market_base, coin).get_coin_price()
                 #需要对比的市场价格
                 price_vs = pricemanage.PriceManage(market_vs,
                                                    coin).get_coin_price()
                 match_bal_flag = self.__match_balance(price_base, price_vs)
                 if match_bal_flag:
                     # 专门找bter的
                     if market_vs == 'bter':
                         logging.info(
                             '%s:Coin:%s 适合作为%s的均衡Coin,从%s卖出来得到Money,同时从%s买入!'
                             % (common.CommonFunction.get_curr_time(), coin,
                                market_vs, market_vs, market_base))
                         logging.info(
                             '买入价格:%f, 卖出价格:%f' %
                             (price_base.sell_cny, price_vs.buy_cny))
Example #3
0
 def __check_money_pool(self,market_base,market_vs,coin_code):
     try:
         order_base= ordermanage.OrderManage(market_base)
         order_vs=ordermanage.OrderManage(market_vs)
         #返回标志
         result_flag=False
         #只检查两个市场的RMB金额,默认coin是足够交易的
         bal_base=order_base.getMyBalance('cny')
         bal_vs=order_vs.getMyBalance('cny')
         #检查coin的数量是不是满足当前市场是总市场容量的一半
         #bal_coin_base=float(order_base.getMyBalance(coin_code))
         bal_coin_vs=float(order_vs.getMyBalance(coin_code))
         #大概的交易价格
         # vs的市场价格
         price_vs = pricemanage.PriceManage(market_vs, coin_code).get_coin_price()
         #币种 的阀值倍数
         coiin_bal_times=5
         #每次交易的单位数
         trans_unit_per_time=self.__std_amount/price_vs.buy_cny
         money_pool_amt=self.__std_amount*self.__money_pool_size
         #帐户标准交易额的X倍,如果低于这个金额则进行两个市场的平衡
         if bal_vs<money_pool_amt:
             #只有待均衡的COIN的金额在资金池的3成以上才进行均衡
             if bal_coin_vs*price_vs.buy_cny/money_pool_amt>0.3:
                 result_flag=True
     except Exception as e:
         print(str(e))
         print('获取资金池状态是否需要均衡时出错')
     return result_flag
     pass
Example #4
0
 def __check_coin_pool(self,market_base,market_vs,coin_code):
     try:
         order_base= ordermanage.OrderManage(market_base)
         order_vs=ordermanage.OrderManage(market_vs)
         #返回标志
         result_flag=False
         #只检查两个市场的RMB金额,默认coin是足够交易的
         bal_base=float(order_base.getMyBalance('cny'))
         bal_vs=float(order_vs.getMyBalance('cny'))
         #检查coin的数量是不是满足当前市场是总市场容量的一半
         bal_coin_base=float(order_base.getMyBalance(coin_code))
         bal_coin_vs=float(order_vs.getMyBalance(coin_code))
         #大概的交易价格
         # vs的市场价格
         price_vs = pricemanage.PriceManage(market_vs, coin_code).get_coin_price()
         #币种 的阀值倍数
         coiin_bal_times=5
         #每次交易的单位数
         trans_unit_per_time=self.__std_amount/price_vs.buy_cny
         money_pool_amt=float(self.__std_amount*self.__money_pool_size)
         total_coin_bal=(bal_coin_base+bal_coin_vs)
         #资金量在3成以上才进行同步
         if float(bal_base/(bal_base+bal_vs))>0.3 or float(bal_base)>float(money_pool_amt*0.8):
             # 帐户低于总数的30%时进行均衡,只有总的币数大于每次交易的10倍 以上才进行均衡
             if bal_coin_base/total_coin_bal<0.3 and total_coin_bal>trans_unit_per_time*10:
                 result_flag=True
     except Exception as e:
         print(str(e))
         print('获取资金池状态是否需要均衡时出错')
     return result_flag
     pass
Example #5
0
    def test_trans_apply(self):
        self.__market_base = 'bter'
        self.__market_vs = 'btc38'
        self.__trans_coin_code = 'doge'
        price_base = pricemanage.PriceManage('bter', 'doge').get_coin_price()
        # 需要对比的市场价格
        price_vs = pricemanage.PriceManage('btc38', 'doge').get_coin_price()
        self.__price_base = price_base
        self.__price_vs = price_vs
        self.__price_base.sell_cny = 0.03
        self.__price_vs.buy_cny = 0.04

        trans_success = self.__trans_apply()
        if trans_success:
            print('交易成功!')
        else:
            print('交易处理失败!')
Example #6
0
    def exch_balance(self,market_base, market_vs, coin_code):
        order_base = ordermanage.OrderManage(market_base)
        order_vs = ordermanage.OrderManage(market_vs)
        # base的市场价格
        price_base = pricemanage.PriceManage(market_base, coin_code).get_coin_price()
        # 需要对比的市场价格
        price_vs = pricemanage.PriceManage(market_vs, coin_code).get_coin_price()
        #trans_price = float(self.__get_trans_price(price_base, price_vs, coin_code))

        match_price_flag=self.__price_check(coin_code,price_base,price_vs)
        #在约定的范围价格变动范围之内
        if match_price_flag:
            trans_price_buy=price_base.sell_cny
            trans_price_sell=price_vs.buy_cny
        else:
            return False
        #TODO test purpose
        #trans_price=0.01598
        # 价格两个市场变动范围不在约定的范围 内则不返回价格,不需要进行交易
        #if trans_price == None:
        #    return False
        trans_amount=self.__std_amount*self.__exch_times
        # 交易的单位是每次交易的X倍,
        # 要非常 小心 这里面的小数位,需要BTC38的接口小数位数太长会报错,接口太SB了, 不知道自己处理一下
        robot=traderobot.TradeRobot()
        trans_units = round(float(self.__std_amount / trans_price_buy * self.__exch_times),robot.get_rounding_num(coin_code))

        tradeapi=robot
        trans_status=False
        # 调用买入操作
        buy_success = tradeapi.twin_trans(order_base, 'buy', coin_code, trans_units, trans_price_buy)
        if buy_success:
            sell_success = tradeapi.twin_trans(order_vs, 'sell', coin_code, trans_units, trans_price_sell)
            # 目前的方案只要买入成功,则卖出订单不取消,只是检查状态当时有没有成交
            if sell_success:
                trans_status = True
                print('%s:均衡交易:成功,已经成功卖出:%s@%s,金额:%f,从市场@%s同样买入:当前成交!' \
                             % (self.__get_curr_time(), coin_code, market_vs, trans_amount, market_base))
                logging.info('%s:均衡交易:成功,已经成功卖出:%s@%s,金额:%f,从市场@%s同样买入:当前成交!' \
                             % (self.__get_curr_time(), coin_code, market_vs, trans_amount, market_base))
            else:
                logging.warning('%s:均衡交易:在途,已经成功卖出:%s@%s,金额:%f,从市场@%s同样买入:当前未成交!' \
                             % (self.__get_curr_time(), coin_code, market_vs, trans_amount, market_base))
        return trans_status
        pass
Example #7
0
    def __coin_rearch(self):
        # 比较多个市场之间的价格
        for market_base in self.__market_list:
            market_vs_list = list(self.__market_list)
            # 移除作为base 的市场
            market_vs_list.remove(market_base)
            # 对比每一个其它的市场
            for market_vs in market_vs_list:
                # 对每个coin进行分析
                for coin in self.__coin_list:
                    # base的市场价格
                    price_base = pricemanage.PriceManage(
                        market_base, coin).get_coin_price()
                    # 需要对比的市场价格
                    price_vs = pricemanage.PriceManage(market_vs,
                                                       coin).get_coin_price()

                    price_check_result = self.__price_check(
                        coin, price_base, price_vs)
                    # 如果价格可以进行买卖刚返回
                    if price_check_result:
                        # 对于达到要求的保存到类变量中,交易函数进行处理
                        self.__market_base = market_base
                        self.__market_vs = market_vs
                        self.__price_base = price_base
                        self.__price_vs = price_vs
                        # 满足交易的coin code
                        self.__trans_coin_code = coin
                        # 把交易实例保存起来
                        # Base account的交易实例
                        self.__order_base = ordermanage.OrderManage(
                            self.__market_base)
                        # VS市场
                        self.__order_vs = ordermanage.OrderManage(
                            self.__market_vs)

                        # 预期盈利金额
                        profitamt = (self.__std_amount / price_base.sell_cny
                                     ) * (price_vs.buy_cny -
                                          price_base.sell_cny)
                        currtime = time.strftime('%Y-%m-%d %H:%M:%S',
                                                 time.localtime(time.time()))
                        print('Date:%s,Coin:%s,BaseMarket(Buy):%s, VSmarket(Sell):%s,Buy price:%f,Sell price:%f,投资%f预期盈利:%f' \
                              % (currtime,coin, market_base, market_vs, price_base.sell_cny, price_vs.buy_cny, self.__std_amount,
                                 profitamt))
Example #8
0
    def __check_balance_flag(self,market_base, market_vs,coin_code):
        order_base= ordermanage.OrderManage(market_base)
        order_vs=ordermanage.OrderManage(market_vs)
        #只检查两个市场的RMB金额,默认coin是足够交易的
        bal_base=order_base.getMyBalance('cny')
        bal_vs=order_vs.getMyBalance('cny')

        #检查coin的数量是不是满足当前市场是总市场容量的一半
        bal_coin_base=float(order_base.getMyBalance(coin_code))
        bal_coin_vs=float(order_vs.getMyBalance(coin_code))
        total_coin_bal=bal_coin_base+bal_coin_vs

        #大概的交易价格
        # base的市场价格
        price_base = pricemanage.PriceManage(market_base, coin_code).get_coin_price()
        # 需要对比的市场价格
        #price_vs = pricemanage.PriceManage(market_vs, coin_code).get_coin_price()


        need_exchange=False
        #均衡市场是标准交易的多少倍
        bal_times=100
        #币种 的阀值倍数
        coiin_bal_times=5
        #每次交易的单位数
        trans_unit_per_time=self.__std_amount/price_base.sell_cny
        #帐户标准交易额的100倍,如果低于这个金额则进行两个市场的平衡
        if bal_vs<float(self.__std_amount*bal_times):
            #需要卖出市场的COIN的比例是不是占总COIN的X成以上并且余额是交易单位的指定倍数以上
            if float(bal_coin_vs)>float(total_coin_bal*0.3):
                # Coin is enough and need sell coin to get money, the estimated amount is greater than 1000
                if float(bal_coin_vs)>float(total_coin_bal*0.3) and (price_base.sell_cny*bal_coin_vs)>1000:
                    need_exchange=True
                else:
                    need_exchange=False
            else:
                # 对于COIN来说,只要是小于总量的30%才需要进行均衡
                if bal_coin_vs<float(trans_unit_per_time*coiin_bal_times) and bal_coin_vs<float(total_coin_bal*0.3):
                    need_exchange=True
        else:
            # 对于COIN来说,只要是小于总量的30%才需要进行均衡
            if bal_coin_vs < float(trans_unit_per_time * coiin_bal_times) and bal_coin_vs < float(total_coin_bal * 0.3):
                need_exchange = True
            else:
                need_exchange=False
        return need_exchange
Example #9
0
    def get_shared_coin_list(self):
        bter_coin_list = self.get_all_pairs_bter()
        btc38_coin_list = self.get_all_pairs_btc38()
        share_list = list(
            set(bter_coin_list).intersection(set(btc38_coin_list)))
        veri_share_list = []
        # 验证是不是能取得价格
        for coin in share_list:
            veri_flag = True

            for market in self.__market_list:
                try:
                    price_base = pricemanage.PriceManage(
                        market, coin).get_coin_price()

                except:
                    veri_flag = False
                    break
                    pass
            # 两个市场同时都能取得价格的才是真正的共享coin
            if veri_flag:
                veri_share_list.append(coin)
        return (veri_share_list)
Example #10
0
    def exch_balanceX(self,market_base, market_vs, coin_code):
        order_base= ordermanage.OrderManage(market_base)
        order_vs=ordermanage.OrderManage(market_vs)
        # base的市场价格
        price_base = pricemanage.PriceManage(market_base, coin_code).get_coin_price()
        # 需要对比的市场价格
        price_vs = pricemanage.PriceManage(market_vs, coin_code).get_coin_price()

        trans_price=self.__get_trans_price(price_base,price_vs,coin_code)
        #价格两个市场变动范围不在约定的范围 内则不返回价格,不需要进行交易
        if trans_price==None:
            return False
        sell_status=None
        buy_status=None
        #交易的单位是每次交易的X倍, TODO test
        trans_units=self.__std_amount/trans_price*self.__exch_times
        #同步交易,先卖后买
        sell_trans=order_vs.submitOrder(coin_code+'_cny','sell',trans_price,trans_units)
        sell_order_id=sell_trans.order_id
        if sell_order_id==1111111:
            sell_status='closed'
        #买入交易
        #buy_trans=order_base.submitOrder(coin_code+'_cny','buy',trans_price,trans_units)
        #buy_order_id=buy_trans.order_id
        #if buy_order_id == 1111111:
        #    buy_status = 'closed'
        #循环检查状态,直至交易成功后才结束返回
        trans_status=False
        waiting_seconds=0
        #最多待时间,持续检查均衡订单的状态
        max_waiting_seconds=5
        #按每次交易金额的X倍来进行帐户之间平衡
        trans_amount=self.__std_amount*self.__exch_times
        while(not trans_status and waiting_seconds<max_waiting_seconds):
            try:
                if sell_status!='closed':
                    sell_status = order_vs.getOrderStatus(sell_order_id, coin_code)
                else:
                    # 成功后再开始买入操作
                    if buy_status==None:
                        buy_trans = order_base.submitOrder(coin_code + '_cny', 'buy', trans_price, trans_units)
                        buy_order_id = buy_trans.order_id
                        time.sleep(0.1)
                        if sell_order_id == 1111111:
                            sell_status = 'closed'
                        else:
                            buy_status = order_base.getOrderStatus(buy_order_id, coin_code)
                    elif buy_status!='closed':
                        buy_status = order_base.getOrderStatus(buy_order_id, coin_code)
            except Exception as e:
                print(str(e))
                pass
            finally:
                time.sleep(1)
                waiting_seconds = waiting_seconds + 1
                if sell_status=='closed' and buy_status=='closed':
                    logging.info('%s:均衡交易:成功,已经成功卖出:%s@%s,金额:%f,从市场@%s同样买入:当前成交!'\
                    %(self.__get_curr_time(),coin_code,market_vs,trans_amount,market_base))
                    trans_status=True
                    waiting_seconds=max_waiting_seconds+5

        if waiting_seconds== max_waiting_seconds:
            #卖出一直未成功,则取消本次的操作
            if sell_status!='closed' and buy_status==None:
                cancel_status=order_base.cancelOrder(sell_order_id,coin_code)
                if cancel_status=='fail':
                    logging.warning('%s:取消卖单状态:失败,均衡交易中卖出%s@%s,金额:%f在指定时间未内成交!'%(self.__get_curr_time(),coin_code),market_vs,trans_amount)
                else:
                    logging.info('%s:取消卖单状态:成功,均衡交易中卖出%s@%s,金额:%f在指定时间未内成交!' % (self.__get_curr_time(), coin_code),
                                 market_vs, trans_amount)
            else:
                logging.warning('%s:均衡交易(buyOrderId:%s):在途,已经成功卖出:%s@%s,金额:%f,从市场@%s同样买入:当前未成交!' \
                             % (self.__get_curr_time(),buy_order_id, coin_code, market_vs, trans_amount, market_base))

        return trans_status
        pass
Example #11
0
    def price_analyze(self):
        #比较多个市场之间的价格
        for market_base in self.__market_list:
            market_vs_list = list(self.__market_list)
            #移除作为base 的市场
            market_vs_list.remove(market_base)
            #对比每一个其它的市场
            for market_vs in market_vs_list:
                #对每个coin进行分析
                for coin in self.__coin_list:
                    #base的市场价格
                    #print('%s:is requesting price@%s' % (self.__get_curr_time(),market_base))
                    price_base = pricemanage.PriceManage(
                        market_base, coin).get_coin_price()
                    #需要对比的市场价格
                    #print('%s:is requesting price@%s' % (self.__get_curr_time(),market_vs))
                    price_vs = pricemanage.PriceManage(market_vs,
                                                       coin).get_coin_price()
                    # print('%s:is comparing price'%self.__get_curr_time())
                    price_check_result = self.__price_check(
                        coin, price_base, price_vs)

                    self.__check_price_num = self.__check_price_num + 1
                    if self.__check_price_num % 10 == 0:
                        print('%s: 已经检查了%d次的价格' %
                              (self.__get_curr_time(), self.__check_price_num))

                    # TODO for testing
                    #price_check_result=True
                    #如果价格可以进行买卖刚返回
                    if price_check_result:
                        #对于达到要求的保存到类变量中,交易函数进行处理
                        self.__market_base = market_base
                        self.__market_vs = market_vs
                        self.__price_base = price_base
                        self.__price_vs = price_vs
                        # 满足交易的coin code
                        self.__trans_coin_code = coin
                        #把交易实例保存起来
                        # Base account的交易实例
                        self.__order_base = ordermanage.OrderManage(
                            self.__market_base)
                        # VS市场
                        self.__order_vs = ordermanage.OrderManage(
                            self.__market_vs)

                        #预期盈利金额
                        profitamt = (self.__std_amount / price_base.sell_cny
                                     ) * (price_vs.buy_cny -
                                          price_base.sell_cny)

                        print('%s: Coin:%s,BaseMarket(Buy):%s, VSmarket(Sell):%s,Buy price:%f,Sell price:%f,投资%f预期盈利:%f'\
                              %(self.__get_curr_time(),coin,market_base,market_vs,price_base.sell_cny,price_vs.buy_cny,self.__std_amount,profitamt))
                        #开始处理交易
                        # 检查帐户的仓位并调整相应的盈利比例
                        #print('%s: is checking account balance'%self.__get_curr_time())
                        account_balance = self.__check_account()
                        # 如果过多的买入成功,卖出失败则停止进行自动交易,防止异常的一直出现买入的情况
                        if account_balance and self.__sell_fail_times < self.__max_sell_fail_times:
                            #检查帐户的仓位并调整相应的盈利比例
                            #print('%s: is doing transaction.'%self.__get_curr_time())
                            trans_status = self.__trans_apply()
                            if trans_status:
                                print('交易成功!')
                            else:
                                print('交易失败/或卖出当时未成功!')
                        else:
                            print('帐户余额不足!')