디지털 화폐 계약은 로봇을 쉽게 추적합니다.

저자:작은 꿈, 날짜: 2021-04-07 15:14:23
태그:무역 지원

디지털 화폐 계약은 로봇을 쉽게 추적합니다.

관련 기사:https://www.fmz.com/bbs-topic/6821


/*backtest
start: 2021-03-18 00:00:00
end: 2021-04-07 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_OKCoin","currency":"BTC_USD"},{"eid":"Futures_OKCoin","currency":"BTC_USD"},{"eid":"Futures_OKCoin","currency":"BTC_USD"}]
*/

function test() {
    // 测试函数
    var ts = new Date().getTime()    
    if (ts % (1000 * 60 * 60 * 6) > 1000 * 60 * 60 * 5.5) {
        Sleep(1000 * 60 * 10)
    	var nowPosAmount = getPosAmount(_C(exchange.GetPosition), refCt)
    	var longPosAmount = nowPosAmount.long
    	var shortPosAmount = nowPosAmount.short
        var x = Math.random()
        if (x > 0.7) {
        	exchange.SetDirection("buy")
            exchange.Buy(-1, _N(Math.max(1, x * 10), 0), "参考账户测试开单#FF0000")
        } else if(x < 0.2) {
        	exchange.SetDirection("sell")
            exchange.Sell(-1, _N(Math.max(1, x * 10), 0), "参考账户测试开单#FF0000")
        } else if(x >= 0.2 && x <= 0.5 && longPosAmount > 4) {
        	exchange.SetDirection("closebuy")
        	exchange.Sell(-1, longPosAmount, "参考账户测试平仓#FF0000")
        } else if(shortPosAmount > 4) {
        	exchange.SetDirection("closesell")
        	exchange.Buy(-1, _N(shortPosAmount / 2, 0), "参考账户测试平仓#FF0000")
        }
    }
}

function getPosAmount(pos, ct) {
    var longPosAmount = 0
    var shortPosAmount = 0
    _.each(pos, function(ele) {
    	if (ele.ContractType == ct && ele.Type == PD_LONG) {
    		longPosAmount = ele.Amount
    	} else if (ele.ContractType == ct && ele.Type == PD_SHORT) {
    		shortPosAmount = ele.Amount
    	}
    })
    return {long: longPosAmount, short: shortPosAmount}
}

function trade(e, ct, type, delta) {
    var nowPosAmount = getPosAmount(_C(e.GetPosition), ct)
    var nowAmount = type == PD_LONG ? nowPosAmount.long : nowPosAmount.short
    if (delta > 0) {
        // 开仓
        var tradeFunc = type == PD_LONG ? e.Buy : e.Sell
        e.SetDirection(type == PD_LONG ? "buy" : "sell")
        tradeFunc(-1, delta)
    } else if (delta < 0) {
        // 平仓
        var tradeFunc = type == PD_LONG ? e.Sell : e.Buy
        e.SetDirection(type == PD_LONG ? "closebuy" : "closesell")
        if (nowAmount <= 0) {
        	Log("未检测到持仓")
        	return 
        }
        tradeFunc(-1, Math.min(nowAmount, Math.abs(delta)))
    } else {
    	throw "错误"
    }
}

function main() {
    LogReset(1)
    if (exchanges.length < 2) {
        throw "没有跟单的交易所"
    }
    var exName = exchange.GetName()
    // 检测参考交易所
    if (!exName.includes("Futures_")) {
        throw "仅支持期货跟单"
    }
    Log("开始监控", exName, "交易所", "#FF0000")
    
    // 检测跟单交易所
    for (var i = 1 ; i < exchanges.length ; i++) {
        if (exchanges[i].GetName() != exName) {
            throw "跟单的期货交易所和参考交易所不同!"
        }
    }
    
    // 设置交易对、合约
    _.each(exchanges, function(e) {
    	if (!IsVirtual()) {
    		e.SetCurrency(refCurrency)
            if (isSimulate) {
                if (e.GetName() == "Futures_OKCoin") {
                    e.IO("simulate", true)
                }
            }
    	}
        e.SetContractType(refCt)
        // 设置精度
        e.SetPrecision(pricePrecision, amountPrecision)
        Log("设置", e.GetName(), e.GetLabel(), "价格精度:", pricePrecision, "下单量精度:", amountPrecision)
    })

    var initRefPosAmount = getPosAmount(_C(exchange.GetPosition), refCt)
    while(true) {
        if (IsVirtual()) {    // 回测时才模拟
        	test()            // 测试函数,模拟参考账户主动交易,触发跟单账户跟单        
        }
    	Sleep(5000)
        var nowRefPosAmount = getPosAmount(_C(exchange.GetPosition), refCt)
        var tbl = {
            type : "table", 
            title : "持仓",
            cols : ["名称", "标签", "多仓", "空仓", "账户资产(Stocks)", "账户资产(Balance)"],
            rows : []
        }
        _.each(exchanges, function(e) {
            var pos = getPosAmount(_C(e.GetPosition), refCt)
            var acc = _C(e.GetAccount)
            tbl.rows.push([e.GetName(), e.GetLabel(), pos.long, pos.short, acc.Stocks, acc.Balance])
        })
        LogStatus(_D(), "\n`" + JSON.stringify(tbl) + "`")
        
        // 计算仓位变动量
        var longPosDelta = nowRefPosAmount.long - initRefPosAmount.long
        var shortPosDelta = nowRefPosAmount.short - initRefPosAmount.short

        // 检测变动
        if (longPosDelta == 0 && shortPosDelta == 0) {
        	continue
        } else {
        	// 检测到仓位变动
        	for (var i = 1 ; i < exchanges.length ; i++) {
        		// 执行多头动作
        		if (longPosDelta != 0) {
        			Log(exchanges[i].GetName(), exchanges[i].GetLabel(), "执行多头跟单,变动量:", longPosDelta)
        		    trade(exchanges[i], refCt, PD_LONG, longPosDelta)
        		}
        		// 执行空头动作
        		if (shortPosDelta != 0) {
        			Log(exchanges[i].GetName(), exchanges[i].GetLabel(), "执行空头跟单,变动量:", shortPosDelta)
        		    trade(exchanges[i], refCt, PD_SHORT, shortPosDelta)
        		}
        	}
        }

        // 执行跟单操作后,更新
        initRefPosAmount = nowRefPosAmount
    }
}


관련

더 많은

pw1013안녕하세요, 어떻게 여러 거래를 추가할 수 있나요?

886v를 더할 수 있을까요?

lt4691888@163.com이 탭은 플랫폼을 넘어서 사용할 수 있나요?

iqctV를 더할 수 있을까요?

17331171117자, 정책 변수를 추가해 주세요.

fxhover2020년 1월 1일부터 재검토한 결과 현재까지 실질적인 성과는 없다.

작은 꿈이 방법은 코드를 수정하고, 모든 전체 저장 정보를 확인하고, 개별적으로 처리하는 것을 요구할 수 있습니다.

pw1013답변 감사합니다. 하지만 제가 어떤 계정에서 모든 트랜잭션 쌍을 복사하고 싶으면 어떻게 하면 되는지 물어보세요. 하나만 더하면 너무 많은 번거로움이 생깁니다.

작은 꿈전략적으로 여러 거래소 개체를 추가할 수 있습니다.

작은 꿈안녕하세요, 당신은 FMZ 공식 전보 그룹에 가입할 수 있습니다.

작은 꿈이쪽은 V3이고, API KEY는 V5가 설정된 것을 확인하고 FMZ에서 설정할 때 V5 옵션을 선택하도록 설정합니다.

17331171117v5를 만들었는데 몇 개나 실패했습니다.

작은 꿈설정된 API KEY가 잘못되었으니 확인해 주세요.

17331171117/upload/asset/18859d9a5a19f334f87be.png 어떻게 된 거야?

17331171117/upload/asset/18859d9a5a19f334f87be.png

작은 꿈/upload/asset/1695c1168b60c8160718.png

작은 꿈정책 설명에는 문서 주소가 있습니다. 문서를 참조하십시오. 코드는 오픈 소스 또는 직접 연구 코드입니다.

fxhover이 로봇은 어떻게 작동할까요?

작은 꿈이 로봇은 돈을 지불하는 로봇입니다.