dYdX-Strategie-Entwurfsbeispiel

Schriftsteller:- Ich bin ein Idiot., Erstellt: 2022-04-08 16:47:32, aktualisiert: 2022-04-08 17:47:39

dYdX-Strategie-Entwurfsbeispiel

Als Reaktion auf die Bedürfnisse vieler Benutzer hat die FMZ-Plattform kürzlich die dezentrale Plattform dYdX unterstützt. Freunde mit Strategien können glücklich auf dYdX graben. Vor langer Zeit wollte ich eine zufällige Handelsstrategie schreiben. Es spielt keine Rolle, ob ich Profit mache oder nicht. Der Zweck ist es, meine Technik zu üben und das Strategie-Design übrigens zu lehren. Als nächstes entwerfen wir gemeinsam eine zufällige Plattform-Strategie. Mach dir keine Sorgen um die Leistung der Strategie und lerne einfach das Strategie-Design.

Erst die Bergbauarbeiten teilen

Der Mining-Screenshot der Strategie im Artikel.

img

Willkommen Freunde, die gute Bergbaustrategie-Ideen zu teilen haben!

Random Trading Strategy Design (Zufallsstrategieentwurf)

Lassen Sie uns einen Brainstorming haben! Wir planen, eine Strategie zu entwerfen, um Befehle zufällig zu platzieren, ohne Indikatoren oder Preise zu betrachten. Bestellen ist nichts anderes als Long und Short zu machen, was auf Wahrscheinlichkeit setzt. Dann verwenden wir zufällige Zahlen von 1 bis 100, um zu bestimmen, ob Long oder Short geht.

Bedingung für das Ausführen von Long: Zufallszahlen von 1 bis 50. Bedingung für die Kurzzeit: Zufallszahlen von 51 bis 100.

Beide benötigen 50 Zahlen. Als nächstes sollten wir darüber nachdenken, wie man Positionen schließt. Da es sich um eine Wette handelt, muss es einen Gewinn- oder Verluststandard geben. Dann setzen wir einen festen StopProfit und einen festen StopLoss als Gewinn- oder Verluststandard. Nehmen wir StopProfit als Gewinn und StopLoss als Verlust. Was die Angemessenheit von StopProfit und StopLoss betrifft, beeinflusst es tatsächlich das Gewinn-Verlust-Verhältnis und auch die Gewinnrate! (Ist es effektiv, eine Strategie auf diese Weise zu entwerfen? Kann es garantiert werden, dass es eine positive mathematische Erwartung ist? Jedenfalls machen wir es zuerst! Weil es für Lernen und Forschung ist!)

Der Handel ist nicht kostenlos, und es gibt Faktoren wie Slippoint und Gebühren, die genug sind, um unsere zufällige Handelsgewinnrate auf die Seite von weniger als 50% zu ziehen. Da es sich um eine Wette handelt, sollte die Wahrscheinlichkeit, dass ich 10 oder 8 Mal hintereinander verliere, nicht sehr groß sein. Also möchte ich einen kleinen Auftragsbetrag im ersten Trade platzieren, so klein es sein kann. Wenn ich dann die Wette verliere, erhöhe ich den Auftragsbetrag und lege weiterhin zufällige Aufträge auf.

Die Strategie ist so einfach wie das.

Quellcode des Designs:

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)
    }
}

Strategieparameter:

img

Die Strategie braucht einen Namen, und nennen wir sie "Raten Sie, welcher größer ist (dYdX Version).

Zurückprüfung

Der Backtest ist nur zur Referenz! Es ist hauptsächlich, um zu überprüfen, ob es irgendwelche Fehler in der Strategie gibt; Backtest mit Binance Futures.

img

img

img

img

Der Backtest ist vorbei, keine Fehler, aber ich habe das Gefühl, dass das Backtest-System übereinstimmt.

In einem Bot laufen

img

img

img

Diese Strategie dient nur zum Lernen und zur Referenz.Nicht tun.!! Nicht tun.Verwenden Sie es in einem echten Bot!


Mehr