dYdX 戦略設計の例

作者: リン・ハーンリディア作成日:2022年11月17日 10:59:29 更新日:2023年9月15日 21:03:43 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2023年9月15日 更新日:2020年9月15日 更新日:2020年9月15日 更新日:2020年9月15日 更新日:2020年9月15日 更新日:2020年9月15日 更新日:2020年9月15日

img

FMZプラットフォームは,多くのユーザーの需要に応えて,最近,分散型取引所であるdYdXにアクセスしました. 戦略を持っている人は,デジタル通貨dYdXを取得するプロセスを楽しむことができます. 私は長い間ストーカスティック取引戦略を書きたかったのです. 利益を得るかどうでもいいです. 次に,ストーカスティック取引戦略をデザインするために一緒に来ます. 戦略がうまくいかなくても,戦略デザインを学ぶだけです.

ストカスティック取引戦略の設計

ランダムな指標と価格で注文をランダムに配置する戦略を設計する計画です. 注文を配置することは,単にロングまたはショートに行くこと,単に確率を賭けるだけです. その後,1~100というランダムな数字を使用して,ロングまたはショートに行くかどうかを決定します.

長期取引の条件:ランダム数 1~50 ショートに行く条件: ランダムナンバー 51~100

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

ストカスティック取引の勝利率は50%未満の方向に引くのに十分なスリップ,手数料などがあります. では,それを継続的に設計するにはどうすればよいですか? ポジションを増やすためにマルチプリキュアを設計したらどうですか? これは賭けなので,ランダムな取引の連続で 8 ~ 10 回損する確率は低いはずです. ですから,最初の取引は,可能な限り小さい小量のオーダーを配置するように設計されました. その後,私が負けた場合,私はオーダーの量を増加し,ランダムに注文を継続します.

戦略はシンプルです

設計されたソースコード:

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 "Strategy starts with a position!"
    }
    
    exchange.SetPrecision(pricePrecision, amountPrecision)
    Log("set the pricePrecision", 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 "failed to obtain initial interest"
            }
        } else {
            totalEq = recoverTotalEq
        }
    } else {
        totalEq = _C(exchange.GetAccount).Balance
    }
    
    while (1) {
        if (openPrice == 0) {
            // Update account information and calculate profits
            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 ? "buying order" : "selling 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
            }
            
            // Test for closing the position
            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 
                
                // drawing the line
                $.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
                }
                
                // Test the 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)
                    // update the position after cancellation, and check it 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 the pending orders
            // reset openPrice
            cancelAll()
            openPrice = 0
        }
        Sleep(1000)
    }
}

戦略パラメータ:

img

戦略には名前が必要で,これを"サイズを推測する (dYdX版) "としましょう.

バックテスト

バックテストは参考にします >_

img

img

img

img

バックテストは完了しました バグはありません

本物のボットでは使用しないでください!


関連性

もっと