Perdagangan Kuantitatif Cryptocurrency untuk pemula - Membawa anda lebih dekat dengan Kuantitatif Cryptocurrency (7)

Penulis:Lydia, Dicipta: 2022-08-09 11:15:23, Dikemas kini: 2023-09-21 21:00:10

img

Dalam artikel sebelumnya, kami memikirkan dan merancang strategi grid pelbagai spesies yang mudah bersama-sama. Seterusnya, kami akan terus belajar dan bergerak ke hadapan di jalan perdagangan kuantitatif. Dalam artikel ini, kami akan meneroka reka bentuk strategi yang lebih kompleks - reka bentuk strategi lindung nilai. Artikel ini merancang untuk merancang strategi lindung nilai intertemporal pelbagai spesies. Apabila ia datang kepada strategi lindung nilai intertemporal, mereka yang biasa dengan perdagangan niaga hadapan mestilah biasa dengannya. Bagi pemula, mereka mungkin tidak memahami konsep ini, jadi mari kita mulakan dengan penjelasan ringkas mengenai konsep lindung nilai intertemporal.

Penjimatan antara masa

Secara ringkasnya, lindung nilai intertemporal adalah untuk pergi panjang kontrak, pergi pendek kontrak, dan menunggu tiga situasi (panjang dan pendek) untuk menutup kedudukan pada masa yang sama:

  • Jika anda membuat wang dengan pergi panjang dan kehilangan wang dengan pergi pendek, dan menutup kedudukan anda apabila keuntungan (seperti yang dikatakan) adalah lebih daripada kerugian (seperti yang dikatakan), anda akan membuat keuntungan selepas baki keuntungan / kerugian.
  • Posisi panjang kehilangan wang, kedudukan pendek membuat wang, dan menutup kedudukan apabila yang lebih menguntungkan daripada yang kehilangan,... (sama seperti di atas).
  • Buat wang jika anda pergi panjang, dan buat wang jika anda pergi pendek, jadi apa ragu-ragu? tutup kedudukan anda!

Dalam kes lain adalah kehilangan terapung, membawa atau terus untuk skala dalam kedudukan. (kerana turun naik penyebaran adalah lebih sederhana daripada turun naik satu sisi, risiko relatif adalah lebih kecil, ambil perhatian bahawa hanya relatif!)

Let A1 be the price of contract A at moment 1, and set B1 to be the price of contract B at moment 1. At this time, short contract A, short price A1, long contract B, and long price B1.
Let A2 be the price of contract A at moment 2, and set B2 to be the price of contract B at moment 2. At this time, close the position contract A (short), close short A2, close the position B contract (long), and close long price B2.

Moment 1 difference:A1 - B1 = X 
Moment 2 difference:A2 - B2 = Y 
X - Y = A1 - B1 - (A2 - B2)
X - Y = A1 - B1 - A2 + B2
X - Y = A1 - A2 + B2 - B1

It can be seen that A1 - A2 is the profit difference in closing the position of contract A.
B2 - B1 is the profit spread of closing the position of contract B. As long as the two closed positions are overall positive, ie: A1 - A2 + B2 - B1 > 0 is profitable. That is, as long as X - Y > 0.
Because of: X - Y = A1 - A2 + B2 - B1

It is concluded that as long as the difference X of opening a position is greater than the difference Y of closing a position, it is profitable (note that it is short A, long B to open a position, the reverse will be the opposite), of course, this is theoretical, practical factors such as commission and slippage should also be considered.

Kerana bursa mata wang kripto mempunyai kedua-dua kontrak penghantaran dan kontrak kekal. Dan harga kontrak kekal sentiasa dekat dengan harga spot kerana kadar pembiayaan. Kemudian kita memilih untuk menggunakan kontrak penghantaran dan kontrak kekal untuk lindung nilai. Kontrak penghantaran memilih kontrak jangka panjang, sehingga kontrak lindung nilai tidak perlu ditetapkan dengan kerap.

Memanaskan dengan statistik penyebaran pelbagai spesies terlebih dahulu

Apabila anda sudah terbiasa dengan prinsip-prinsip asas, anda tidak perlu tergesa-gesa menulis strategi. Pertama sekali, anda boleh mendapatkan statistik penyebaran, melukis carta dan memerhatikan penyebaran. Mari belajar bagaimana untuk melukis strategi pelbagai spesies bersama-sama.

Kami merancang berdasarkanKontrak OKEX. Ia adalah sangat mudah untuk melukis pada FMZ. Ia adalah sangat mudah untuk menggunakan fungsi yang dibungkus untuk melukis. Perpustakaan carta adalahCarta tinggi. Penerangan fungsi lukisan pada dokumentasi API:https://www.fmz.com/api#chart...

Oleh kerana ia adalah pelbagai spesies, pertama sekali, adalah perlu untuk menentukan perbezaan harga spesies itu sebelum menarik. dalam kod, menulis dua array pertama, mewakili kontrak yang akan dilakukan.

var arrSwapContractType = ["BTC-USDT-SWAP", "LTC-USDT-SWAP", "ETH-USDT-SWAP", "ETC-USDT-SWAP"]   // Perpetual contracts
var arrDeliveryContractType = ["BTC-USDT-210924", "LTC-USDT-210924", "ETH-USDT-210924", "ETC-USDT-210924"]  // Delivery contracts

Memulakan konfigurasi carta mengikut kod kontrak yang ditetapkan di sini. Konfigurasi carta ini pastinya tidak boleh dikodkan keras, kerana anda tidak tahu spesies apa dan berapa banyak spesies yang perlu dilakukan (ini ditentukan oleh nilai arrDeliveryContractType dan arrSwapContractType), jadi konfigurasi carta dikembalikan oleh fungsi.

function createCfg(symbol) {
    var cfg = {
        extension: {
            // No grouping, displayed separately, default is 'group'
            layout: 'single', 
            // Specify the height, which can be set as a string, "300px", and the value 300 will be replaced with "300px" automatically
            height: 300,      
            // The unit value of the specified width, the total value is 12
            col: 6
        },
        title: {
            text: symbol
        },
        xAxis: {
            type: 'datetime'
        },
        series: [{
            name: 'plus',
            data: []
        }]
    }

    return cfg
}

function main() {
    // Declare arrCfg
    var arrCfg = []                                    // Declare an array to store chart configuration information
    _.each(arrSwapContractType, function(ct) {         // Record the array of perpetual contract codes iteratively, pass the XXX-USDT part of the contract name as a parameter to the createCfg function, construct the chart configuration information, and return
        arrCfg.push(createCfg(formatSymbol(ct)[0]))    // The chart configuration information returned by createCfg is pushed into the arrCfg array
    })
    var objCharts = Chart(arrCfg)                      // Call the chart function Chart of the FMZ platform to create the chart control object objCharts
    objCharts.reset()                                  // Initialize chart content
    
    // Hereafter omitted ...
}

Seterusnya, mari kita menyediakan data.

Kontrak kekal USDT:

https://www.okex.com/api/v5/market/tickers?instType=SWAP

Kontrak penghantaran USDT:

https://www.okex.com/api/v5/market/tickers?instType=FUTURES

Kami menulis fungsi untuk mengendalikan panggilan kedua-dua antara muka ini dan meletakkan data dalam format:

function getTickers(url) {
    var ret = []
    try {
        var arr = JSON.parse(HttpQuery(url)).data
        _.each(arr, function(ele) {
            ret.push({
                bid1: parseFloat(ele.bidPx),             // Price of stock buy order
                bid1Vol: parseFloat(ele.bidSz),          // Amount for the price of stock buy order
                ask1: parseFloat(ele.askPx),             // Price of stock sell order
                ask1Vol: parseFloat(ele.askSz),          // Amount for the price of stock sell order
                symbol: formatSymbol(ele.instId)[0],     // Formats into trading pairs
                type: "Futures",                         // Type
                originalSymbol: ele.instId               // Original contract code
            })
        })
    } catch (e) {
        return null 
    }
    return ret 
}

Menulis fungsi lain untuk memproses kod kontrak

function formatSymbol(originalSymbol) {
    var arr = originalSymbol.split("-")
    return [arr[0] + "_" + arr[1], arr[0], arr[1]]
}

Semua yang tinggal adalah untuk menyamakan data yang diperoleh secara berulang, mengira penyebaran, carta output, dan lain-lain. Ujian di sini adalah selisih antara kontrak suku kedua 210924 dan kontrak kekal. Kod lengkap:

// Temporary parameters
var arrSwapContractType = ["BTC-USDT-SWAP", "LTC-USDT-SWAP", "ETH-USDT-SWAP", "ETC-USDT-SWAP"]
var arrDeliveryContractType = ["BTC-USDT-210924", "LTC-USDT-210924", "ETH-USDT-210924", "ETC-USDT-210924"]
var interval = 2000

function createCfg(symbol) {
    var cfg = {
        extension: {
            // No grouping, displayed separately, default is 'group'
            layout: 'single', 
            // Specify the height, which can be set as a string, "300px", and the value 300 will be replaced with "300px" automatically
            height: 300,      
            // The unit value of the specified width, the total value is 12
            col: 6
        },
        title: {
            text: symbol
        },
        xAxis: {
            type: 'datetime'
        },
        series: [{
            name: 'plus',
            data: []
        }]
    }

    return cfg
}

function formatSymbol(originalSymbol) {
    var arr = originalSymbol.split("-")
    return [arr[0] + "_" + arr[1], arr[0], arr[1]]
}

function getTickers(url) {
    var ret = []
    try {
        var arr = JSON.parse(HttpQuery(url)).data
        _.each(arr, function(ele) {
            ret.push({
                bid1: parseFloat(ele.bidPx), 
                bid1Vol: parseFloat(ele.bidSz), 
                ask1: parseFloat(ele.askPx), 
                ask1Vol: parseFloat(ele.askSz), 
                symbol: formatSymbol(ele.instId)[0], 
                type: "Futures", 
                originalSymbol: ele.instId
            })
        })
    } catch (e) {
        return null 
    }
    return ret 
}

function main() {
    // Declare arrCfg
    var arrCfg = []
    _.each(arrSwapContractType, function(ct) {
        arrCfg.push(createCfg(formatSymbol(ct)[0]))
    })
    var objCharts = Chart(arrCfg)
    objCharts.reset()
    
    while (true) {
        // Obtain market data        
        var deliveryTickers = getTickers("https://www.okex.com/api/v5/market/tickers?instType=FUTURES")
        var swapTickers = getTickers("https://www.okex.com/api/v5/market/tickers?instType=SWAP")
        if (!deliveryTickers || !swapTickers) {
            Sleep(2000)
            continue
        }

        var tbl = {
            type : "table",
            title : "delivery - perpetual spread",
            cols : ["trading pairs", "delivery", "perpetual", "positive hedging", "negative hedging"],
            rows : []
        }
        
        var subscribeDeliveryTickers = []
        var subscribeSwapTickers = []
        _.each(deliveryTickers, function(deliveryTicker) {
            _.each(arrDeliveryContractType, function(symbol) {
                if (deliveryTicker.originalSymbol == symbol) {
                    subscribeDeliveryTickers.push(deliveryTicker)
                }
            })
        })
        _.each(swapTickers, function(swapTicker) {
            _.each(arrSwapContractType, function(symbol) {
                if (swapTicker.originalSymbol == symbol) {
                    subscribeSwapTickers.push(swapTicker)
                }
            })
        })
        
        var pairs = []
        var ts = new Date().getTime()
        _.each(subscribeDeliveryTickers, function(deliveryTicker) {
            _.each(subscribeSwapTickers, function(swapTicker) {
                if (deliveryTicker.symbol == swapTicker.symbol) {
                    var pair = {symbol: swapTicker.symbol, swapTicker: swapTicker, deliveryTicker: deliveryTicker, plusDiff: deliveryTicker.bid1 - swapTicker.ask1, minusDiff: deliveryTicker.ask1 - swapTicker.bid1}
                    pairs.push(pair)
                    tbl.rows.push([pair.symbol, deliveryTicker.originalSymbol, swapTicker.originalSymbol, pair.plusDiff, pair.minusDiff])
                    for (var i = 0 ; i < arrCfg.length ; i++) {
                        if (arrCfg[i].title.text == pair.symbol) {
                            objCharts.add([i, [ts, pair.plusDiff]])
                        }                        
                    }                    
                }
            })
        })

        LogStatus(_D(), "\n`" + JSON.stringify(tbl) + "`")        
        Sleep(interval)
    }
}

Bot sebenar yang berjalan

img

Berlari untuk seketika~

img

Perhatikan penyebaran dan kemudian bercakap mengenainya!img


Berkaitan

Lebih lanjut