数字货币合约简易跟单机器人

辅助交易
创建日期: 2021-04-07 15:14:23 最后修改: 2022-09-28 18:24:28
复制: 130 点击次数: 6044
avatar of 发明者量化-小小梦 发明者量化-小小梦
4
关注
1153
关注者

数字货币合约简易跟单机器人

相关文章: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
    }
}
相关推荐
全部留言
avatar of pw1013
pw1013
你好,请问如何添加多个交易对呢
2023-11-09 12:11:25
avatar of 发明者量化-小小梦
发明者量化-小小梦
这个可能要修改一下代码,检查一个账户的所有整体持仓信息,然后逐个处理。
2023-11-12 19:43:40
avatar of pw1013
pw1013
感谢您的解答,但是我如果想复制某个账户所有的交易对,请问该如何实现呢,一个个添加太麻烦了,这个问题困扰我很久了 - -
2023-11-12 00:23:42
avatar of 发明者量化-小小梦
发明者量化-小小梦
策略上可以添加多个交易所对象。
2023-11-09 20:28:07
avatar of 小白菜886
小白菜886
可以加v吗
2022-08-03 11:16:01
avatar of lt4691888@163.com
[email protected]
老哥,这个跟单能跨平台使用吗
2021-09-28 18:29:30
avatar of iqct
iqct
可以加一下V吗?
2021-08-10 22:22:37
avatar of 发明者量化-小小梦
发明者量化-小小梦
您好,您可以加FMZ官方电报群,就在平台首页,我也在群里。@LittleLittleDream
2022-08-03 11:18:08
avatar of 17331171117
17331171117
请问 策略参数 在哪加
2021-04-26 11:09:24
avatar of 发明者量化-小小梦
发明者量化-小小梦
你的这个是V3的吧,API KEY 确定配置的是V5的, 在FMZ上配置的时候确定勾选了V5选项。确定托管者是最新的。
2021-04-26 13:53:43
avatar of 17331171117
17331171117
创建的 v5 的 创建好几个 都不行
2021-04-26 13:41:57
avatar of 发明者量化-小小梦
发明者量化-小小梦
配置的API KEY不对,检查下。
2021-04-26 13:39:31
avatar of 17331171117
17331171117
/upload/asset/18859d9a5a19f334f87be.png 这是怎么回事啊
2021-04-26 13:31:52
avatar of 17331171117
17331171117
/upload/asset/18859d9a5a19f334f87be.png
2021-04-26 13:31:40
avatar of 发明者量化-小小梦
发明者量化-小小梦
/upload/asset/1695c1168b60c8160718.png
2021-04-26 11:33:43
avatar of fxhover
fxhover
从2020.1.1开始回测,到现在,基本上没有收益啊。
2021-04-19 17:16:46
avatar of 发明者量化-小小梦
发明者量化-小小梦
策略描述里有文章地址,看下文章,代码开源的或者直接研究代码。
2021-04-20 14:05:18
avatar of fxhover
fxhover
你这个跟单机器人怎么用?
2021-04-20 13:56:23
avatar of 发明者量化-小小梦
发明者量化-小小梦
这是跟单机器人。。。。自己怎么会有收益。。汗~
2021-04-19 17:17:52