Loading ...

使用研究环境分析三角对冲细节以及手续费对于可对冲差价的影响

Author: 小小梦, Created: 2019-10-19 12:14:03, Updated: 2019-10-19 20:30:39

使用研究环境分析三角对冲细节以及手续费对于可对冲差价的影响

  • 没有调整手续费的研究版本

    var fmz = require("fmz")                           // 引入后自动导入 talib, TA, plot 库
    var task = fmz.VCtx({
    start: '2019-04-09 17:49:00',
    end: '2019-04-09 18:00:00',
    period: '1m',
    exchanges: [{"eid":"Huobi","currency":"ETH_BTC", "balance":1, "stocks":10},{"eid":"OKEX","currency":"ETH_USDT","balance":10000,"stocks":1}, {"eid":"OKEX","currency":"BTC_USDT","balance":10000,"stocks":1}]
    })

    显示最初交易所账户信息和行情:

    • A交易所交易对为:ETH_BTC
    • B交易所交易对为:ETH_USDT
    • C交易所交易对为:BTC_USDT
    var accA = exchanges[0].GetAccount()
    accA
    { Balance: 1, FrozenBalance: 0, Stocks: 10, FrozenStocks: 0 }
    var accB = exchanges[1].GetAccount()
    accB
    { Balance: 10000, FrozenBalance: 0, Stocks: 1, FrozenStocks: 0 }
    var accC = exchanges[2].GetAccount()
    accC
    { Balance: 10000, FrozenBalance: 0, Stocks: 1, FrozenStocks: 0 }
    var initSumBTC = accA.Balance + accC.Stocks
    initSumBTC
    2
    var initSumETH = accA.Stocks + accB.Stocks
    initSumETH
    11
    var initSumUSDT = accB.Balance + accC.Balance
    initSumUSDT
    20000

    可以看到,初始BTC总数是2个,ETH总数是11个,USDT总数是20000个,下面我们获取一下当前的各个交易对的行情信息。

    var tickerA = exchanges[0].GetTicker()
    tickerA
    { Time: 1554831960000,
      High: 0.03396501,
      Low: 0.03396499,
      Sell: 0.03396501,
      Buy: 0.03396499,
      Last: 0.033965,
      Volume: 4696.555,
      OpenInterest: 0 }
    var tickerB = exchanges[1].GetTicker()
    tickerB
    { Time: 1554831960000,
      High: 175.08000001,
      Low: 175.07999999,
      Sell: 175.08000001,
      Buy: 175.07999999,
      Last: 175.08,
      Volume: 20730.37,
      OpenInterest: 0 }
    var tickerC = exchanges[2].GetTicker()
    tickerC
    { Time: 1554831960000,
      High: 5161.90000001,
      Low: 5161.89999999,
      Sell: 5161.90000001,
      Buy: 5161.89999999,
      Last: 5161.9,
      Volume: 2109.9292,
      OpenInterest: 0 }

    可以看到A交易所,B交易所,交易的标的物都是ETH,即A/B交易所可以进行间接对冲操作,无法直接对冲是因为,计价币种不同。A交易所计价币种是BTC,B交易所的计价币种是USDT。 假设对于A交易所做买入操作exchanges[0].Buy(price, amount),买入时根据对手卖出的价格作为下单价格,即tickerA.Sell,B交易所为了对冲,就要进行卖出操作,即exchanges[1].Sell(price, amount),卖出时根据对手要买入的价格作为下单价格,即:tickerB.Buy。因为A交易所买入时消耗了BTC,B交易所卖出时得到了USDT,需要把USDT换成BTC,补缺A交易所消耗使用的BTC,即C交易所执行exchanges[2].Buy(price, amount),把USDT换成BTC,C交易所要买入,需要看对手的卖出价格,即tickerC.Sell,A交易所买入的价格一定要比B交易所卖出的价格低,那么C交易所只用花少一些的USDT就能买回来A交易所消耗掉的BTC。就赚到了USDT金额差。 所以如下:

    var diffB2A = tickerA.Sell - tickerB.Buy / tickerC.Sell    // diffB2A 即 B交易所卖出ETH币,A交易所买入ETH币,逻辑上相当于ETH从B交易所转移到A交易所。
    diffB2A
    0.000047266535449966285

    显然,A交易所买入的价格比B、C交易所组合成的价格要高,对冲显然是亏钱的。 另外一个对冲方向同理,A交易所执行卖出ETH操作,B交易所执行买入ETH操作,C交易所卖出BTC换回USDT。达到币始终是平衡,只有USDT增减(即赚取的差价)。

    var diffA2B = tickerA.Buy - tickerB.Sell / tickerC.Buy
    diffA2B
    0.000047246531444007644

    我们执行一次tickerA.Buy - tickerB.Sell / tickerC.Buy的对冲操作,对其中的各个数值变化一探究竟。

    var idA = exchanges[0].Sell(tickerA.Buy, 1)
    var nowAccA = exchanges[0].GetAccount()
    nowAccA       // 可以看出手续费扣除的是BTC
    { Balance: 1.03389706,
      FrozenBalance: 0,
      Stocks: 9,
      FrozenStocks: 0 }
    var orderA = exchanges[0].GetOrder(idA)
    orderA
    { Id: 1,
      Price: 0.03396499,
      Amount: 1,
      DealAmount: 1,
      AvgPrice: 0.03396499,
      Type: 1,
      Offset: 0,
      Status: 1,
      ContractType: 'ETH_BTC_Huobi' }
    var feeRatioA = 0.002      // 回测默认的手续费是 千分之2,即 0.002
    var feeA = orderA.DealAmount * orderA.AvgPrice * feeRatioA   // A交易所交易的手续费,BTC计价
    feeA
    0.00006792998000000001
    var idB = exchanges[1].Buy(tickerB.Sell, 1)
    var nowAccB = exchanges[1].GetAccount()
    nowAccB
    { Balance: 9824.56983998,
      FrozenBalance: 0,
      Stocks: 2,
      FrozenStocks: 0 }
    var orderB = exchanges[1].GetOrder(idB)
    orderB      // 可以看出手续费扣除的是USDT
    { Id: 1,
      Price: 175.08000001,
      Amount: 1,
      DealAmount: 1,
      AvgPrice: 175.08000001,
      Type: 0,
      Offset: 0,
      Status: 1,
      ContractType: 'ETH_USDT_OKEX' }
    var feeRatioB = 0.002
    var feeB = orderB.DealAmount * orderB.AvgPrice * feeRatioB / tickerC.Last    // B交易所的手续费,换算成BTC计价
    feeB
    0.00006783548693698057
    var idC = exchanges[2].Sell(tickerC.Buy, nowAccA.Balance - accA.Balance)
    var nowAccC = exchanges[2].GetAccount()
    nowAccC
    { Balance: 10174.12327555,
      FrozenBalance: 0,
      Stocks: 0.9662,
      FrozenStocks: 0 }
    var orderC = exchanges[2].GetOrder(idC)
    orderC     // 可以看出手续费扣除的是 USDT
    { Id: 1,
      Price: 5161.89999999,
      Amount: 0.0338,
      DealAmount: 0.0338,
      AvgPrice: 5161.89999999,
      Type: 1,
      Offset: 0,
      Status: 1,
      ContractType: 'BTC_USDT_OKEX' }
    var feeRatioC = 0.002
    var feeC = orderC.DealAmount * orderC.AvgPrice * feeRatioC / tickerC.Last      // C交易所交易的手续费,BTC计价
    feeC
    0.00006759999999986903

    计算一下对冲后的总资产信息:

    var nowSumBTC = nowAccA.Balance + nowAccC.Stocks
    nowSumBTC
    2.00009706
    var nowSumETH = nowAccA.Stocks + nowAccB.Stocks
    nowSumETH
    11
    var nowSumUSDT = nowAccB.Balance + nowAccC.Balance
    nowSumUSDT
    19998.69311553
    nowSumBTC - initSumBTC
    0.00009705999999987114
    tickerC.Buy * (nowSumBTC - initSumBTC)    // 多出的BTC换算成USDT
    0.5010140139983642
    nowSumUSDT + tickerC.Buy * (nowSumBTC - initSumBTC) - initSumUSDT    // 根据账户资产变动计算盈亏,计价单位为USDT
    -0.8058704560025944
    (diffA2B - (feeA + feeB + feeC)) * tickerC.Buy     // 根据差价计算的盈亏,计价单位为USDT
    -0.8058703331189396

    可以看到,对冲时的差价是diffA2B : 0.000047246531444007644 三次对冲时的手续费,换算成BTC为:feeA + feeB + feeC

    feeA + feeB + feeC
    0.0002033654669368496

    可以看出这样的三角对冲,差价至少要大于feeA + feeB + feeC才是不亏不赚,要盈利就必须大于这个差价。 目前通过账户对比计算、通过差价计算,都是亏损,亏损大概为:-0.8058704560025944 我修改了一个版本,让手续费参数计算出的手续费正好等于这个模型测试的行情时的差价盈利,用于对比观察。

    可以看到,在手续费默认的情况下(默认为千分之2),对冲差价为:0.000047246531444007644 BTC时,对冲是亏钱的,大概亏损:-0.8058704560025944

  • 调整手续费的研究版本

    var fmz = require("fmz")                           // 引入后自动导入 talib, TA, plot 库
    var task = fmz.VCtx({
    start: '2019-04-09 17:49:00',
    end: '2019-04-09 18:00:00',
    period: '1m',
    exchanges: [{"eid":"Huobi","currency":"ETH_BTC", "balance":1, "stocks":10,"fee":[0.04,0.04]},{"eid":"OKEX","currency":"ETH_USDT","balance":10000,"stocks":1,"fee":[0.04,0.04]}, {"eid":"OKEX","currency":"BTC_USDT","balance":10000,"stocks":1,"fee":[0.04,0.04]}]
    })

    显示最初交易所账户信息和行情:

    • A交易所交易对为:ETH_BTC
    • B交易所交易对为:ETH_USDT
    • C交易所交易对为:BTC_USDT
    var accA = exchanges[0].GetAccount()
    accA
    { Balance: 1, FrozenBalance: 0, Stocks: 10, FrozenStocks: 0 }
    var accB = exchanges[1].GetAccount()
    accB
    { Balance: 10000, FrozenBalance: 0, Stocks: 1, FrozenStocks: 0 }
    var accC = exchanges[2].GetAccount()
    accC
    { Balance: 10000, FrozenBalance: 0, Stocks: 1, FrozenStocks: 0 }
    var initSumBTC = accA.Balance + accC.Stocks
    initSumBTC
    2
    var initSumETH = accA.Stocks + accB.Stocks
    initSumETH
    11
    var initSumUSDT = accB.Balance + accC.Balance
    initSumUSDT
    20000

    可以看到,初始BTC总数是2个,ETH总数是11个,USDT总数是20000个,下面我们获取一下当前的各个交易对的行情信息。

    var tickerA = exchanges[0].GetTicker()
    tickerA
    { Time: 1554831960000,
      High: 0.03396501,
      Low: 0.03396499,
      Sell: 0.03396501,
      Buy: 0.03396499,
      Last: 0.033965,
      Volume: 4696.555,
      OpenInterest: 0 }
    var tickerB = exchanges[1].GetTicker()
    tickerB
    { Time: 1554831960000,
      High: 175.08000001,
      Low: 175.07999999,
      Sell: 175.08000001,
      Buy: 175.07999999,
      Last: 175.08,
      Volume: 20730.37,
      OpenInterest: 0 }
    var tickerC = exchanges[2].GetTicker()
    tickerC
    { Time: 1554831960000,
      High: 5161.90000001,
      Low: 5161.89999999,
      Sell: 5161.90000001,
      Buy: 5161.89999999,
      Last: 5161.9,
      Volume: 2109.9292,
      OpenInterest: 0 }

    可以看到A交易所,B交易所,交易的标的物都是ETH,即A/B交易所可以进行间接对冲操作,无法直接对冲是因为,计价币种不同。A交易所计价币种是BTC,B交易所的计价币种是USDT。 假设对于A交易所做买入操作exchanges[0].Buy(price, amount),买入时根据对手卖出的价格作为下单价格,即tickerA.Sell,B交易所为了对冲,就要进行卖出操作,即exchanges[1].Sell(price, amount),卖出时根据对手要买入的价格作为下单价格,即:tickerB.Buy。因为A交易所买入时消耗了BTC,B交易所卖出时得到了USDT,需要把USDT换成BTC,补缺A交易所消耗使用的BTC,即C交易所执行exchanges[2].Buy(price, amount),把USDT换成BTC,C交易所要买入,需要看对手的卖出价格,即tickerC.Sell,A交易所买入的价格一定要比B交易所卖出的价格低,那么C交易所只用花少一些的USDT就能买回来A交易所消耗掉的BTC。就赚到了USDT金额差。 所以如下:

    var diffB2A = tickerA.Sell - tickerB.Buy / tickerC.Sell    // diffB2A 即 B交易所卖出ETH币,A交易所买入ETH币,逻辑上相当于ETH从B交易所转移到A交易所。
    diffB2A
    0.000047266535449966285

    显然,A交易所买入的价格比B、C交易所组合成的价格要高,对冲显然是亏钱的。 另外一个对冲方向同理,A交易所执行卖出ETH操作,B交易所执行买入ETH操作,C交易所卖出BTC换回USDT。达到币始终是平衡,只有USDT增减(即赚取的差价)。

    var diffA2B = tickerA.Buy - tickerB.Sell / tickerC.Buy
    diffA2B
    0.000047246531444007644

    我们执行一次tickerA.Buy - tickerB.Sell / tickerC.Buy的对冲操作,对其中的各个数值变化一探究竟。

    var idA = exchanges[0].Sell(tickerA.Buy, 1)
    var nowAccA = exchanges[0].GetAccount()
    nowAccA       // 可以看出手续费扣除的是BTC
    { Balance: 1.0339514,
      FrozenBalance: 0,
      Stocks: 9,
      FrozenStocks: 0 }
    var orderA = exchanges[0].GetOrder(idA)
    orderA
    { Id: 1,
      Price: 0.03396499,
      Amount: 1,
      DealAmount: 1,
      AvgPrice: 0.03396499,
      Type: 1,
      Offset: 0,
      Status: 1,
      ContractType: 'ETH_BTC_Huobi' }
    var feeRatioA = 0.0004      // 回测默认的手续费是 千分之2,即 0.002
    var feeA = orderA.DealAmount * orderA.AvgPrice * feeRatioA   // A交易所交易的手续费,BTC计价
    feeA
    0.000013585996
    var idB = exchanges[1].Buy(tickerB.Sell, 1)
    var nowAccB = exchanges[1].GetAccount()
    nowAccB
    { Balance: 9824.84996798,
      FrozenBalance: 0,
      Stocks: 2,
      FrozenStocks: 0 }
    var orderB = exchanges[1].GetOrder(idB)
    orderB      // 可以看出手续费扣除的是USDT
    { Id: 1,
      Price: 175.08000001,
      Amount: 1,
      DealAmount: 1,
      AvgPrice: 175.08000001,
      Type: 0,
      Offset: 0,
      Status: 1,
      ContractType: 'ETH_USDT_OKEX' }
    var feeRatioB = 0.0004
    var feeB = orderB.DealAmount * orderB.AvgPrice * feeRatioB / tickerC.Last    // B交易所的手续费,换算成BTC计价
    feeB
    0.000013567097387396117
    var idC = exchanges[2].Sell(tickerC.Buy, nowAccA.Balance - accA.Balance)
    var nowAccC = exchanges[2].GetAccount()
    nowAccC
    { Balance: 10174.91841463,
      FrozenBalance: 0,
      Stocks: 0.9661,
      FrozenStocks: 0 }
    var orderC = exchanges[2].GetOrder(idC)
    orderC     // 可以看出手续费扣除的是 USDT
    { Id: 1,
      Price: 5161.89999999,
      Amount: 0.0339,
      DealAmount: 0.0339,
      AvgPrice: 5161.89999999,
      Type: 1,
      Offset: 0,
      Status: 1,
      ContractType: 'BTC_USDT_OKEX' }
    var feeRatioC = 0.0004
    var feeC = orderC.DealAmount * orderC.AvgPrice * feeRatioC / tickerC.Last      // C交易所交易的手续费,BTC计价
    feeC
    0.000013559999999973732

    计算一下对冲后的总资产信息:

    var nowSumBTC = nowAccA.Balance + nowAccC.Stocks
    nowSumBTC
    2.0000514000000003
    var nowSumETH = nowAccA.Stocks + nowAccB.Stocks
    nowSumETH
    11
    var nowSumUSDT = nowAccB.Balance + nowAccC.Balance
    nowSumUSDT
    19999.76838261
    nowSumBTC - initSumBTC
    0.000051400000000256796
    tickerC.Buy * (nowSumBTC - initSumBTC)    // 多出的BTC换算成USDT
    0.26532166000081153
    nowSumUSDT + tickerC.Buy * (nowSumBTC - initSumBTC) - initSumUSDT    // 根据账户资产变动计算盈亏,计价单位为USDT
    0.0337042700011807
    (diffA2B - (feeA + feeB + feeC)) * tickerC.Buy     // 根据差价计算的盈亏,计价单位为USDT
    0.03372495390449328

    可以看到,对冲时的差价是diffA2B : 0.000047246531444007644 三次对冲时的手续费,换算成BTC为:feeA + feeB + feeC

    feeA + feeB + feeC
    0.00004071309338736985

    可以看到在回测配置中手续费修改为"fee":[0.04,0.04]即万分之四,这个时候feeA + feeB + feeC等于0.00004071309338736985,基本接近对冲时的差价diffA2B,最后计算出来盈亏就很小了只有:0.03372495390449328,并且通过账户变动计算出的盈亏和通过差价计算出来的盈亏数值基本一致。计算三角对冲多少差价可以对冲,可以用此研究文件分析计算。


More