Contoh Desain Strategi dYdX

Penulis:Ninabadass, Dibuat: 2022-04-08 16:47:32, Diperbarui: 2022-04-08 17:47:39

Contoh Desain Strategi dYdX

Sebagai tanggapan terhadap kebutuhan banyak pengguna, platform FMZ baru-baru ini telah mendukung platform terdesentralisasi dYdX. Teman-teman dengan strategi dapat dengan senang hati menambang di dYdX. Baru-baru ini, saya ingin menulis strategi perdagangan acak. Tidak masalah apakah saya menghasilkan keuntungan atau tidak. Tujuannya adalah untuk berlatih teknik saya dan mengajarkan desain strategi dengan cara. Jadi selanjutnya, mari kita merancang strategi platform acak bersama. Jangan khawatir tentang kinerja strategi, dan hanya belajar desain strategi.

Pertama-tama Berbagi Pertambangan

Screenshot strategi penambangan di artikel.

img

Selamat datang teman-teman yang memiliki ide strategi pertambangan yang baik untuk dibagikan!

Desain Strategi Perdagangan acak

Mari kita melakukan brainstorming! Kami berencana untuk merancang strategi untuk menempatkan pesanan secara acak tanpa melihat indikator atau harga. Pemesanan tidak lebih dari melakukan long dan short, yang bertaruh pada probabilitas. Kemudian kami menggunakan angka acak dari 1 sampai 100 untuk menentukan apakah melakukan long atau melakukan short.

Kondisi melakukan panjang: angka acak dari 1 sampai 50. Kondisi melakukan pendek: angka acak dari 51 sampai 100.

Untuk melakukan long dan short keduanya membutuhkan 50 angka. Selanjutnya, mari kita pikirkan bagaimana menutup posisi. Karena ini adalah taruhan, harus ada standar menang atau kalah. Kemudian, mari kita atur stopProfit dan stopLoss tetap sebagai standar menang atau kalah. Ambil stopProfit sebagai menang, dan stopLoss sebagai kalah. Adapun kesesuaian stopProfit dan stopLoss, itu sebenarnya mempengaruhi rasio keuntungan dan kerugian, dan tingkat kemenangan juga! (Apakah efektif untuk merancang strategi dengan cara ini? Dapatkah dijamin menjadi harapan matematika yang positif? Bagaimanapun, mari kita lakukan terlebih dahulu! Karena ini untuk pembelajaran dan penelitian!)

Perdagangan tidak bebas biaya, dan ada faktor-faktor seperti slippoint dan biaya yang cukup untuk menarik tingkat kemenangan perdagangan acak kami ke sisi kurang dari 50%. Berpikir tentang itu, bagaimana untuk melanjutkan desain dari sini? Lebih baik untuk mendesain skala dengan ganda untuk meningkatkan posisi. Karena ini adalah taruhan, kemungkinan kehilangan berturut-turut 10 atau 8 kali seharusnya tidak terlalu besar. Jadi saya ingin mendesain menempatkan jumlah pesanan kecil dalam perdagangan pertama, sesedikit mungkin. Kemudian jika saya kehilangan taruhan, tingkatkan jumlah pesanan dan terus menempatkan pesanan acak.

Strategi ini bagus, sesederhana ini.

Kode sumber desain:

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

Parameter strategi:

img

Baiklah! Strategi ini membutuhkan nama, dan mari kita menyebutnya "Tebak mana yang lebih besar (versi dYdX).

Backtest

Backtest hanya untuk referensi! Ini terutama untuk memeriksa apakah ada bug dalam strategi; backtest dengan Binance Futures.

img

img

img

img

Tes balik selesai, tidak ada bug, tapi aku merasa sistem backtestnya cocok... mari kita jalankan di robot sungguhan untuk observasi.

Jalankan di Bot

img

img

img

Strategi ini hanya untuk pembelajaran dan referensi.Jangan!! JanganGunakan itu di robot sungguhan!


Lebih banyak