dYdX 戦略設計例

作者: リン・ハーンニナバダス, 作成日:2022-04-08 16:47:32, 更新日:2022-04-08 17:47:39

dYdX 戦略設計例

FMZプラットフォームは,多くのユーザーのニーズに応えて,最近分散型プラットフォーム dYdX をサポートしています. 戦略を持つ友人は,喜んで dYdX でマイニングすることができます. ちょうど長い間,私はランダムな取引戦略を書きたいと思いました. 利益を得るか否かは重要ではありません. 目的は私のテクニックを実践し,その方法によって戦略デザインを教えることです. 次に,一緒にランダムなプラットフォーム戦略をデザインしましょう. 戦略のパフォーマンスについて心配しないでください. 戦略デザインを学ぶだけです.

先ず 採掘 を 共有 する

記事の戦略のマイニングスクリーンショットです

img

素晴らしい鉱山戦略のアイデアを 共有できる友人を歓迎します!

ランダムな取引戦略設計

ブラインドストーミングをしよう! 指数や価格を見ることなくランダムに注文を出す戦略を設計する予定です. 注文は,確率を賭ける,ロングとショートを行う以上のものではありません. その後,1から100までのランダムな数字を使用して,ロングやショートを行うかどうかを決定します.

1から50までのランダムな数 短くする条件: 51から100までのランダムな数字

ロングとショートの両方が50の数字を必要とします.次に,ポジションを閉める方法について考えましょう.賭けであるため,勝つか負ける基準が必要です.次に,勝つか負ける基準として固定されたストッププロフィットとストップロスを設定しましょう.ストッププロフィートを勝ちとストップロスを失うとしましょう.ストッププロフィットとストップロスの適性については,実際に利益と損失比,そして勝利率に影響します! (この方法で戦略を設計することは効果的ですか?それは肯定的な数学的な期待であると保証できますか?とにかく,まずそれをしましょう!それは学習と研究のためにです!)

ランダムな取引の勝率を50%以下に引き上げるのに十分です. それについて考えれば,ここから設計をどのように進めますか? ポジションを増やすために複数でスケーリングをデザインするのが良いです. これは賭けなので,順番に10回または8回負ける確率はあまり大きくありません. ですから,最初の取引で可能な限り小さな注文金額を設定します. その後,賭けを負ければ,注文金額を増加し,ランダムな注文を継続します.

戦略は簡単です

デザインのソースコード:

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("reset all data", "#FF0000")
    }

    exchange.SetContractType(ct)

    var initPos = _C(exchange.GetPosition)
    if (initPos.length != 0) {
        throw "Position detected when starting the strategy!"
    }
    
    exchange.SetPrecision(pricePrecision, amountPrecision)
    Log("setPrecision", 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 "fail to obtain the initial equity"
            }
        } else {
            totalEq = recoverTotalEq
        }
    } else {
        totalEq = _C(exchange.GetAccount).Balance
    }
    
    while (1) {
        if (openPrice == 0) {
            // update account information, and calculate the profit
            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("place", direction > 50 ? "buy order" : "sell order", ",price:", 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
            }
            
            // detect close positions 
            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 
                
                // plot 
                $.PlotLine("bid", depth.Bids[0].Price)
                $.PlotLine("ask", depth.Asks[0].Price)
                
                // stop loss
                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
                }
                
                // stop profit 
                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
                }
                
                // detect pending orders 
                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 {
                    // cancel pending orders
                    cancelAll()
                    Sleep(2000)
                    pos = _C(exchange.GetPosition)
                    // after canceling, update positions, which needs to be detected again 
                    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 {
            // cancel pending orders 
            // reset openPrice
            cancelAll()
            openPrice = 0
        }
        Sleep(1000)
    }
}

戦略パラメータ:

img

この戦略には名前が必要で,これを"どちらが大きいか推測する (dYdX版) "と呼びましょう.

バックテスト

バックテストは参考に! 主に戦略にバグがあるかどうかを確認するためです.

img

img

img

img

バックテストは終わりました バグはありませんが バックテストシステムが一致しているように感じます

ボットで実行する

img

img

img

この戦略は 学習と参考のためにのみですしない!! しない本物のロボットで使うんだ!


もっと