交易信息大全
2026币圈信息集合

FMZ量化交易dYdX策略设计范例--随机交易策略

欧意OKX  币安Binance  芝麻开门GATE

目前不清退的交易所推荐:

1、全球第二大交易所OKX欧意

国区邀请链接: https://www.promooboost.com/join/1837888   币种多,交易量大!

国际邀请链接:https://www.okx.com/join/1837888 注册简单,交易不需要实名,新用户能开合约,币种多,交易量大!

2、老牌交易所比特儿现改名叫芝麻开门 :https://www.gateport.business/share/XgRDAQ8

全球最大交易所币安,国区邀请链接:https://accounts.binance.com/zh-CN/register?ref=16003031  币安注册不了IP地址用香港,居住地选香港,认证照旧,邮箱推荐如gmail、outlook。支持币种多,交易安全!

买好币上KuCoinhttps://www.kucoin.com/r/af/1f7w3  CoinMarketCap前五的交易所,注册友好操简单快捷!

FMZ量化交易平台邀请链接:https://www.fmz.com/

dYdX策略设计范例

应不少用户需求,最近FMZ平台支持了dYdX这个去中心化交易所。有策略的小伙伴们可以愉快的挖矿dYdX了。正好很久以前就想写一个随机交易策略,赚钱亏钱无所谓目的是练练手顺便教学一下策略设计。所以接下来我们一起来设计一个随机交易所策略,策略绩效好坏不用在意,我们权且来学习策略设计。

先来晒一波挖矿

本篇的策略挖矿截图。

171ae7f5f6eeb7a3a977

有好的挖矿策略思路的小伙伴也欢迎留言哇!

随机交易策略设计

我们来“胡思乱想”一通!计划设计一个不看指标、不看价格随机下单的策略,下单无非就是做多、做空而已,堵的都是概率。那我们就用随机数1~100来确定多空。

做多条件:随机数1~50。
做空条件:随机数51~100。

多空都是50个数。接下来我们来思考下如何平仓,既然是赌那么就必须要有个输赢标准。那么在交易中我们就设置固定止盈、止损来作为输赢标准吧。止盈了就是赢,止损了就是输。至于这个止盈止损多少合适,这个其实就是影响盈亏比了,哦对!还影响胜率!(这样设计策略有效么?能保证是个正向的数学期望么?先干了再说!反正是学习、研究!)

交易并不是无成本的,有滑点、手续费等因素足以把我们的随机交易胜率拉向小于50%那一边了。想到这里继续怎么设计呢?
不如设计个倍数加仓吧,既然是赌那么连续10次8次随机交易都输的概率应该不会很大。所以我想设计第一笔交易下单量很小,能多小就多小。然后如果赌输了,就增加下单量继续随机下单。

OK了,策略就设计这么简单就行了。

设计源码:

var openPrice = 0
var ratio = 1
var totalEq = null
var nowEq = null

function cancelAll() {
    while (1) {
        var orders = _C(exchange.GetOrders)
        if (orders.length == 0) {
            break
        }
        for (var i = 0 ; i < orders.length ; i++) {
            exchange.CancelOrder(orders[i].Id, orders[i])
            Sleep(500)
        }
        Sleep(500)
    }
}

function main() {
    if (isReset) {
        _G(null)
        LogReset(1)
        LogProfitReset()
        LogVacuum()
        Log("重置所有数据", "#FF0000")
    }

    exchange.SetContractType(ct)

    var initPos = _C(exchange.GetPosition)
    if (initPos.length != 0) {
        throw "策略启动时有持仓!"
    }

    exchange.SetPrecision(pricePrecision, amountPrecision)
    Log("设置精度", pricePrecision, amountPrecision)

    if (!IsVirtual()) {
        var recoverTotalEq = _G("totalEq")
        if (!recoverTotalEq) {
            var currTotalEq = _C(exchange.GetAccount).Balance   // equity
            if (currTotalEq) {
                totalEq = currTotalEq
                _G("totalEq", currTotalEq)
            } else {
                throw "获取初始权益失败"
            }
        } else {
            totalEq = recoverTotalEq
        }
    } else {
        totalEq = _C(exchange.GetAccount).Balance
    }

    while (1) {
        if (openPrice == 0) {
            // 更新账户信息,计算收益
            var nowAcc = _C(exchange.GetAccount)
            nowEq = IsVirtual() ? nowAcc.Balance : nowAcc.Balance  // equity
            LogProfit(nowEq - totalEq, nowAcc)

            var direction = Math.floor((Math.random()*100)+1)   // 1~50 , 51~100
            var depth = _C(exchange.GetDepth)
            if (depth.Asks.length <= 2 || depth.Bids.length <= 2) {
                Sleep(1000)
                continue
            }
            if (direction > 50) {
                // long
                openPrice = depth.Bids[1].Price
                exchange.SetDirection("buy")
                exchange.Buy(Math.abs(openPrice) + slidePrice, amount * ratio)
            } else {
                // short
                openPrice = -depth.Asks[1].Price
                exchange.SetDirection("sell")
                exchange.Sell(Math.abs(openPrice) - slidePrice, amount * ratio)
            }
            Log("下", direction > 50 ? "买单" : "卖单", ",价格:", Math.abs(openPrice))
            continue
        }

        var orders = _C(exchange.GetOrders)
        if (orders.length == 0) {
            var pos = _C(exchange.GetPosition)
            if (pos.length == 0) {
                openPrice = 0
                continue
            }

            // 平仓检测
            while (1) {
                var depth = _C(exchange.GetDepth)
                if (depth.Asks.length <= 2 || depth.Bids.length <= 2) {
                    Sleep(1000)
                    continue
                }
                var stopLossPrice = openPrice > 0 ? Math.abs(openPrice) - stopLoss : Math.abs(openPrice) + stopLoss
                var stopProfitPrice = openPrice > 0 ? Math.abs(openPrice) + stopProfit : Math.abs(openPrice) - stopProfit
                var winOrLoss = 0 // 1 win , -1 loss 

                // 画线
                $.PlotLine("bid", depth.Bids[0].Price)
                $.PlotLine("ask", depth.Asks[0].Price)

                // 止损
                if (openPrice > 0 && depth.Bids[0].Price < stopLossPrice) {
                    exchange.SetDirection("closebuy")
                    exchange.Sell(depth.Bids[0].Price - slidePrice, pos[0].Amount)
                    winOrLoss = -1
                } else if (openPrice < 0 && depth.Asks[0].Price > stopLossPrice) {
                    exchange.SetDirection("closesell")
                    exchange.Buy(depth.Asks[0].Price + slidePrice, pos[0].Amount)
                    winOrLoss = -1
                }

                // 止盈
                if (openPrice > 0 && depth.Bids[0].Price > stopProfitPrice) {
                    exchange.SetDirection("closebuy")
                    exchange.Sell(depth.Bids[0].Price - slidePrice, pos[0].Amount)
                    winOrLoss = 1
                } else if (openPrice < 0 && depth.Asks[0].Price < stopProfitPrice) {
                    exchange.SetDirection("closesell")
                    exchange.Buy(depth.Asks[0].Price + slidePrice, pos[0].Amount)
                    winOrLoss = 1
                }

                // 检测挂单
                Sleep(2000)
                var orders = _C(exchange.GetOrders)
                if (orders.length == 0) {
                    pos = _C(exchange.GetPosition)
                    if (pos.length == 0) {
                        if (winOrLoss == -1) {
                            ratio++
                        } else if (winOrLoss == 1) {
                            ratio = 1
                        }
                        break
                    }
                } else {
                    // 撤销挂单
                    cancelAll()
                    Sleep(2000)
                    pos = _C(exchange.GetPosition)
                    // 撤销后更新持仓,需要再次检查
                    if (pos.length == 0) {
                        if (winOrLoss == -1) {
                            ratio++
                        } else if (winOrLoss == 1) {
                            ratio = 1
                        }
                        break
                    }
                }

                var tbl = {
                    "type" : "table",
                    "title" : "info",
                    "cols" : ["totalEq", "nowEq", "openPrice", "bid1Price", "ask1Price", "ratio", "pos.length"],
                    "rows" : [],
                }
                tbl.rows.push([totalEq, nowEq, Math.abs(openPrice), depth.Bids[0].Price, depth.Asks[0].Price, ratio, pos.length])
                tbl.rows.push(["pos", "type", "amount", "price", "--", "--", "--"])
                for (var j = 0 ; j < pos.length ; j++) {
                    tbl.rows.push([j, pos[j].Type, pos[j].Amount, pos[j].Price, "--", "--", "--"])
                }
                LogStatus(_D(), "\n", "`" + JSON.stringify(tbl) + "`")
            }
        } else {
            // 撤销挂单
            // 重置openPrice
            cancelAll()
            openPrice = 0
        }
        Sleep(1000)
    }
}

策略参数:

1774ccb4c531333d96ec

哦对!策略需要起个名字,就叫“猜大小 (dYdX版)”吧。

回测

回测仅供参考就好,>_<!
主要检查策略是不是有什么BUG,用币安期货回测。

16668f2aac9b8642361b

166183dd960364f31622

178bd94c3daf625671f6

16696fe5bc71fdbf1d3e

回测完了,没什么BUG。不过感觉我是不是拟合回测系统了..T_T,实盘跑着玩一下。

实盘跑跑

16a557dfccb3c7f68792

163e177a6455396c096b

167a21e95674da97ca9d

本策略仅供学习、参考,千万~千万不要实盘使用!!

FMZ量化交易平台邀请链接:https://www.fmz.com/

全球最大交易所币安,国区邀请链接:https://accounts.binance.com/zh-CN/register?ref=16003031  币安注册不了IP地址用香港,居住地选香港,认证照旧,邮箱推荐如gmail、outlook。支持币种多,交易安全!

买好币上KuCoinhttps://www.kucoin.com/r/af/1f7w3  CoinMarketCap前五的交易所,注册友好操简单快捷!

目前不清退的交易所推荐:

1、全球第二大交易所OKX欧意,邀请链接:https://www.promooboost.com/join/1837888 注册简单,交易不需要实名,新用户能开合约,币种多,交易量大!。

2、老牌交易所比特儿现改名叫芝麻开门 :https://www.gateport.business/share/XgRDAQ8

买好币上币库:https://www.kucoin.com/r/1f7w3

火必所有用户现在可用了,但是要重新注册账号火币https://www.huobi.com

全球最大交易所币安

国区邀请链接: 支持86手机号码,网页直接注册。

赞(0)
未经允许不得转载:2026币圈信息集合 » FMZ量化交易dYdX策略设计范例--随机交易策略