FMZ量化交易dYdX策略设计范例–随机交易策略
admin
2023-08-02 16:12:00
0

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

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

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

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

2、老牌交易所比特儿现改名叫芝麻开门 :https://www.gate.win/signup/649183

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

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

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

dYdX策略设计范例

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

先来晒一波挖矿

本篇的策略挖矿截图。

171ae7f5f6eeb7a3a977171ae7f5f6eeb7a3a977

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

随机交易策略设计

我们来“胡思乱想”一通!计划设计一个不看指标、不看价格随机下单的策略,下单无非就是做多、做空而已,堵的都是概率。那我们就用随机数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)
    }
}

策略参数:

1774ccb4c531333d96ec1774ccb4c531333d96ec

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

回测

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

16668f2aac9b8642361b16668f2aac9b8642361b

166183dd960364f31622166183dd960364f31622

178bd94c3daf625671f6178bd94c3daf625671f6

16696fe5bc71fdbf1d3e16696fe5bc71fdbf1d3e

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

实盘跑跑

16a557dfccb3c7f6879216a557dfccb3c7f68792

163e177a6455396c096b163e177a6455396c096b

167a21e95674da97ca9d167a21e95674da97ca9d

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

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

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

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

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

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

2、老牌交易所比特儿现改名叫芝麻开门 :https://www.gate.win/signup/649183

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

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

全球最大交易所币安,

国区邀请链接:https://accounts.suitechsui.mobi/zh-CN/register?ref=16003031 支持86手机号码,网页直接注册。

相关内容

热门资讯

Windows 11 和 10... Windows 11/10 文件夹属性中缺少共享选项卡 – 已修复 1.检查共享选项卡是否可用 右键...
Radmin VPN Wind... Radmin VPN 是一款免费且用户友好的软件,旨在牢固地连接计算机以创建一个有凝聚力的虚拟专用网...
如何修复 Steam 内容文件... Steam 内容文件锁定是当您的 Steam 文件无法自行更新时出现的错误。解决此问题的最有效方法之...
在 Windows 11 中打... 什么是链路状态电源管理? 您可以在系统控制面板的电源选项中看到链接状态电源管理。它是 PCI Exp...
Hive OS LOLMine... 目前不清退的交易所推荐: 1、全球第二大交易所OKX欧意 国区邀请链接: https://www.m...
事件 ID 7034:如何通过... 点击进入:ChatGPT工具插件导航大全 服务控制管理器 (SCM) 负责管理系统上运行的服务的活动...
在 iCloud 上关闭“查找... 如果您是 Apple 的长期用户,您肯定会遇到过 Find My 应用程序,它本机安装在 iPhon...
iPhone 屏幕上有亮绿色斑... iPhone 是市场上最稳定的智能手机之一,这主要归功于专为它们设计的 iOS 操作系统。然而,他们...
balenaEtcher烧录后... balenaEtcher烧录后u盘或者内存卡无法识别不能使用的解决方法想要恢复原来的方法,使用win...
统信UOS每次开机后不直接进入... 统信UOS每次开机后不直接进入系统而是进入到recovery模式 按方向上键选择UOS 20 SP1...