资源加载中... loading...

60行三角对冲策略(教学)

Author: 发明者量化-小小梦, Date: 2019-04-16 12:55:22
Tags: StudyHedgetriangular

三角对冲教学策略

原理

  • 例如 A 交易所 : ETH_BTC B 交易所 : ETH_USDT C 交易所(其实为 B交易所 另一个交易对而已,逻辑上认为是C。) : BTC_USDT

  • B 、C 交易所交易对组合成 ETH_BTC 和A 交易所对冲。这样BC实际是一个交易所账户。

优化空间

  • 币平衡。
  • 根据手续费率算对冲差价,动态差价。
  • 硬搬砖。
  • 订单薄深度扫描,计算最佳对冲量。 …

BUG反馈

  • 如有错误欢迎留言。

// 交易对以 ETH_BTC , ETH_USDT , BTC_USDT 为例
// 教学策略,还有很大优化空间,例如:币平衡模块,根据手续费率控制对冲差价,硬搬砖等等。
function main () {                                                                                      
    if (exchanges[0].GetQuoteCurrency() != exchanges[2].GetCurrency().split("_")[0] || 
        exchanges[0].GetCurrency().split("_")[0] != exchanges[1].GetCurrency().split("_")[0]) {
        throw "交易所 交易对 配置错误。"
    }
    var marketSlideRate = 1 // 1.02
    var hedgeDiff = 0.0007
    var hedgeAmount = 0.1
    var isFirst = true
    var concurrenter = function (funcName, isWait, tasks, amounts) {
        for (var i = 0 ; i < exchanges.length; i++) {
            if (isFirst) {
                exchanges[i].acc = _C(exchanges[i].GetAccount)
                exchanges[i].initAcc = exchanges[i].acc
            }
            if (isWait) {
                var a = funcName == "GetTicker" ? exchanges[i].ticker = exchanges[i].tiR.wait() : exchanges[i].id = exchanges[i].trR.wait()
                if (funcName == "trade") {
                    exchanges[i].acc = _C(exchanges[i].GetAccount)
                }
            } else {
                var b = funcName == "GetTicker" ? exchanges[i].tiR = exchanges[i].Go(funcName) : exchanges[i].trR = exchanges[i].Go(tasks[i], -1, amounts[i], exchanges[i].GetCurrency())
            }
        }
        if (funcName == "trade" && isWait) {
            Log("BTC:", exchange.BTC = exchanges[0].acc.Balance + exchanges[2].acc.Stocks, "ETH:", exchange.ETH = exchanges[0].acc.Stocks + exchanges[1].acc.Stocks, "USDT:", 
                exchange.USDT = exchanges[1].acc.Balance + exchanges[2].acc.Balance, "#FF0000")
            LogProfit(exchange.USDT - (exchanges[1].initAcc.Balance + exchanges[2].initAcc.Balance) - 
                (exchanges[0].initAcc.Balance + exchanges[2].initAcc.Stocks - exchange.BTC) * exchanges[2].ticker.Last -
                (exchanges[0].initAcc.Stocks + exchanges[1].initAcc.Stocks - exchange.ETH) * exchanges[1].ticker.Last)
        }
        isFirst = false
    }

    while (1) {
        concurrenter("GetTicker", false)
        concurrenter("GetTicker", true)
        var tickerA = exchanges[0].ticker
        var tickerB = exchanges[1].ticker
        var tickerC = exchanges[2].ticker

        var tickerBC = {
            Buy : tickerB.Buy / tickerC.Sell,
            Sell : tickerB.Sell / tickerC.Buy,
        }

        if (tickerA.Buy - tickerBC.Sell > hedgeDiff && exchanges[0].acc.Stocks > 0.2 && exchanges[1].acc.Balance > 500 && exchanges[2].acc.Stocks > 0.03) {                          // Sell A , Buy BC (Buy B Sell C)
            concurrenter("trade", false, ["Sell", "Buy", "Sell"], [hedgeAmount, marketSlideRate * tickerB.Sell * hedgeAmount, tickerB.Sell * hedgeAmount / tickerC.Buy])
            concurrenter("trade", true)
        }
        if (tickerBC.Buy - tickerA.Sell > hedgeDiff && exchanges[0].acc.Balance > 0.03 && exchanges[1].acc.Stocks > 0.2 && exchanges[2].acc.Balance > 500) {                          // Buy A , Sell BC (...)
            concurrenter("trade", false, ["Buy", "Sell", "Buy"], [marketSlideRate * hedgeAmount * tickerA.Sell, hedgeAmount, marketSlideRate * hedgeAmount * tickerA.Sell * tickerC.Sell])
            concurrenter("trade", true)
        }
        Sleep(500)
    }
}


template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6