三角套利-基础版

Author: 红色的雪, Date: 2019-06-06 17:38:26
Tags: HedgePythontriangular


#!python2
# -*- coding:utf-8 -*-
from time import sleep

Q3 = 0.5
tax = 0.0015   #交易费率,0.15%


class fmz_market():

    # 基础行情数据处理,根据传入的交易对,获取数据,返回行情dict
    def basic_data_handle(self, pair):
        pair_depth = {'sale_volume': 0, 'sale_price': 0, 'buy_volume': 0, 'buy_price': 0}
        depth = exchanges[pair].GetDepth()
        asks_infor = depth.Asks[0]
        bids_infor = depth.Bids[0]
        pair_depth['sale_volume'] = asks_infor.Amount
        pair_depth['sale_price'] = asks_infor.Price
        pair_depth['buy_volume'] = bids_infor.Amount
        pair_depth['buy_price'] = bids_infor.Price
        #Log(pair_depth)
        return pair_depth



    #进行参数计算
    def profit_calculation(self):
        profit_obtain = 0
        #先获取行情数据
        P1_depth = self.basic_data_handle(0)
        P2_depth = self.basic_data_handle(1)
        P3_depth = self.basic_data_handle(2)
        #买一价格整理
        p1_sale_price =float(P1_depth['sale_price'])
        p1_buy_price = float(P1_depth['buy_price'])
        p2_sale_price = float(P2_depth['sale_price'])
        p2_buy_price = float(P2_depth['buy_price'])
        p3_sale_price = float(P3_depth['sale_price'])
        p3_buy_price = float(P3_depth['buy_price'])

        #深度数据整理
        p1_sale_volume = float(P1_depth['sale_volume'])
        p1_buy_volume = float(P1_depth['buy_volume'])
        p2_sale_volume = float(P2_depth['sale_volume'])
        p2_buy_volume = float(P2_depth['buy_volume'])
        p3_sale_volume = float(P3_depth['sale_volume'])
        p3_buy_volume = float(P3_depth['buy_volume'])
        # 进行数据分析,正向交易费率,P1:EOS_USDT,P2:BTC_USDT,P3:EOS_ETH,卖出EOS,买入BTC,买入EOS(卖,买,买)
        if p1_buy_price / p2_sale_price > p3_sale_price:
            #总的手续费计算
            tax_account = (p1_buy_price + p3_sale_price*p2_sale_price + p3_sale_price*p2_sale_price) * Q3 * tax
            #总的获利计算
            profit_sum = (p1_buy_price / p2_sale_price - p3_sale_price) * Q3 * p2_buy_price
            Log('P1;',p1_buy_price,',P2:',p2_sale_price,',P3:',p3_sale_price,',tax_account:',tax_account,',profit_sum:',profit_sum)
            #如果总的获利 < 手续费,则直接退出
            if profit_sum < tax_account:
                return 0
            p1_accout_receive = p1_buy_price * Q3 * (1 - tax) #卖出EOS得到usdt,同时减去税
            p3_accout_used = p3_sale_price * Q3 * (1 + tax)  #计算买入Q3 EOS的时候,需要的BTC
            p2_accout_used =  p2_sale_price *  p3_accout_used * (1 + tax) #计算买入的BTC 需要耗费的USDT数量
            #获利总量计算
            profit_obtain = p1_accout_receive - p2_accout_used  #卖EOS得到的usdt - 买入同样数量的EOS需要的USDT
            return profit_obtain

        # 进行数据分析,逆向交易费率,P1:EOS_USDT,P2:BTC_USDT,P3:EOS_BTC,卖出EOS->BTC,卖出BTC,买入EOS(卖,卖,买)
        if p1_buy_price / p2_sale_price < p3_sale_price:
            # 总的手续费计算
            tax_account = (p3_buy_price * p2_buy_price + p3_buy_price * p2_buy_price + p1_sale_price) * Q3 * tax
            # 总的获利计算
            profit_sum = (p3_sale_price - p1_buy_price / p2_sale_price) * Q3 * p2_buy_price
            Log('P1;', p1_buy_price, ',P2:', p2_sale_price, ',P3:', p3_sale_price, ',tax_account:', tax_account, ',profit_sum:', profit_sum)
            # 如果总的获利 < 手续费,则直接退出
            if profit_sum < tax_account:
                return 0
            p3_accout_receive = p3_buy_price * Q3 * (1 - tax)  # 卖出EOS得到BTC,同时减去税
            p1_accout_used = p1_sale_price * Q3 * (1 + tax)  # 计算买入Q3 EOS的时候,需要的USDT
            p2_accout_receive = p2_buy_price * p3_accout_receive * (1 - tax)  # 计算卖出的BTC 得到的USDT数量
            # 获利总量计算
            profit_obtain = p2_accout_receive - p1_accout_used   #卖出的BTC得到的usdt - 买入EOS锁需要消耗的usdt
            return profit_obtain

        return 0

    #循环计算
    def profit_calculation_cycle(self):
        usdt_remain = 0
        for i in range(10000):
            profit_obtain = self.profit_calculation()
            usdt_remain = usdt_remain + profit_obtain
            if profit_obtain > 0:
                Log(u'EOS:',Q3,u'.....USDT:',usdt_remain)



def main():
    Log(exchange.GetAccount())
    Log("测试")
    fmz_market_instances = fmz_market()
    fmz_market_instances.profit_calculation_cycle()
    # fmz_market_instances.basic_data_handle(0)

Related

More

evan 实在 没看懂什么策略,

BNMBNM 实盘显示第58行错误 怎么改可以跑起来

gurengjun@yeah.net 策略不错,但是没有把python的语法发挥到极致

铁拳无敌沈万三 怎么不更新了

红色的雪 有什么好的建议没,给点建议呗,哈哈