Loading ...

测试研究环境

Author: bixia1994, Created: 2020-01-29 11:20:37, Updated:

#测试研究环境 https://www.fmz.com/digest-topic/4459 三角对冲细节,以及手续费

var fmz = require("fmz");
var task = fmz.VCtx({
    start: '2020-01-01 00:00:00',
    end: '2020-01-20 18:00:00',
    period: '1d',
    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}]
});
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

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

var tickerA = exchanges[0].GetTicker()
tickerA
{ Time: 1577839560000,
  High: 0.01792601,
  Low: 0.01792599,
  Sell: 0.01792601,
  Buy: 0.01792599,
  Last: 0.017926,
  Volume: 256.2683,
  OpenInterest: 0 }
_D(tickerA.Time)
'2020-01-01 00:00:00'
var tickerB = exchanges[1].GetTicker()
tickerB
{ Time: 1577840280000,
  High: 128.90000001,
  Low: 128.89999999,
  Sell: 128.90000001,
  Buy: 128.89999999,
  Last: 128.9,
  Volume: 4036.4944,
  OpenInterest: 0 }
_D(tickerB.Time)
'2020-01-01 00:00:00'
var tickerC = exchanges[2].GetTicker()
tickerC
{ Time: 1577839024800,
  High: 7176.30000001,
  Low: 7176.29999999,
  Sell: 7176.30000001,
  Buy: 7176.29999999,
  Last: 7176.3,
  Volume: 120.3639,
  OpenInterest: 0 }

as we can see, exchange A & B trades ETH, while C trades USDT

A/B交易所可以进行间接对冲操作,无法直接对冲,原因在于计价币种不同。A交易所的计价币种是BTC,B交易所的计价币种是USDT。假设对于A交易所买入,exchanges[0].Buy(price, amount); 买入时根据对手卖出的价格作为下单价格,即ticker.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交易所买入,需要看对手的卖出价格,即ticker.Sell。 A交易所买入的价格一定要比B交易所卖出的价格更低,那么C交易所就只用花少一点的USDT就能买回A交易所消耗掉的BTC,就赚到了USDT金额差 所以如下:

var diffB2A = tickerA.Sell - tickerB.Buy / tickerC.Sell
diffB2A
-0.000035892371670689927

说明A交易所买入的价格比BC交易所组合成的价格要低,对冲产生的USDT差额为diffB2A

另一个对冲方向同理,A交易所执行卖出ETH的操作,B交易所执行买入ETH的操作,C交易所执行卖出BTC的操作换回USDT。达到ETH,BTC都是平衡的,只有USDT增减

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

我们执行一次tickerA.Buy - tickerB.Sell / tickerC.Buy 的操作,对其中的各个数值变化查看

var idA = exchanges[0].Sell(tickerA.Buy, 1);
var nowAccA = exchanges[0].GetAccount()
nowAccA
{ Balance: 1.01792407,
  FrozenBalance: 0,
  Stocks: 9,
  FrozenStocks: 0 }

通过查看账户变化来看卖出ETH的影响

diffBalanceA = nowAccA.Balance - accA.Balance
diffBalanceA
0.01792407000000007
nowAccA.Stocks - accA.Stocks
-1

通过查看订单来看卖出ETH的影响

var orderA = exchanges[0].GetOrder(idA)
orderA
{ Id: 1,
  Price: 0.01792599,
  Amount: 1,
  DealAmount: 1,
  AvgPrice: 0.01795999,
  Type: 1,
  Offset: 0,
  Status: 1,
  ContractType: 'ETH_BTC_Huobi' }
orderA.Price*orderA.Amount - diffBalanceA
0.000001919999999929617
var feeRatioA = 0.00107
var feeA = orderA.DealAmount * orderA.AvgPrice * feeRatioA
feeA
0.000019217189299999998
var idB = exchanges[1].Buy(tickerB.Sell, 1);
var nowAccB = exchanges[1].GetAccount();
nowAccB
{ Balance: 9870.84219998,
  FrozenBalance: 129.15780001,
  Stocks: 1,
  FrozenStocks: 0 }
var orderB = exchanges[1].GetOrder(idB)
orderB
{ Id: 2,
  Price: 128.90000001,
  Amount: 1,
  DealAmount: 0,
  AvgPrice: 0,
  Type: 0,
  Offset: 0,
  Status: 0,
  ContractType: 'ETH_USDT_OKEX' }
var feeRatioB = 0.002
var feeB = orderB.Amount * orderB.Price * feeRatioB
feeB
0.25780000002000003
var idC = exchanges[2].Sell(tickerC.Buy, nowAccA.Balance - accA.Balance);
idC
1
var nowAccC = exchanges[2].GetAccount()
nowAccC
{ Balance: 10128.53291899,
  FrozenBalance: 0,
  Stocks: 0.9821,
  FrozenStocks: 0 }
var orderC = exchanges[2].GetOrder(idC)
orderC
{ Id: 1,
  Price: 7176.29999999,
  Amount: 0.0179,
  DealAmount: 0.0179,
  AvgPrice: 7194.99999999,
  Type: 1,
  Offset: 0,
  Status: 1,
  ContractType: 'BTC_USDT_OKEX' }
var feeRatioC = 0.002
var feeC = orderC.DealAmount * orderC.AvgPrice * feeRatioC / tickerC.Last
feeC
0.00003589328762727896
var nowSumBTC = nowAccA.Balance + nowAccC.Stocks
nowSumBTC
2.0000240700000003
var nowSumETH = nowAccA.Stocks + nowAccB.Stocks + nowAccA.FrozenStocks
nowSumETH
10
var nowSumUSDT = nowAccB.Balance + nowAccC.Balance
nowSumUSDT
19999.375118969998
nowSumBTC- initSumBTC
0.000024070000000264713
nowSumETH - initSumETH
-1
nowSumUSDT - initSumUSDT
-0.6248810300021432


More