Strategi Arbitrage lintas periode mata uang digital berdasarkan Bollinger Band

Penulis:Lydia, Dibuat: 2022-12-02 16:58:27, Diperbarui: 2023-09-20 09:33:29

img

Strategi Arbitrage lintas periode mata uang digital berdasarkan Bollinger Band

I. Ringkasan

George Soros mengemukakan proposisi penting dalam The Alchemy of Finance yang ditulis pada tahun 1987: Saya percaya harga pasar selalu salah dalam arti bahwa mereka menyajikan pandangan bias tentang masa depan. Hipotesis efisiensi pasar hanyalah hipotesis teoritis. Sebenarnya, peserta pasar tidak selalu rasional, dan pada setiap titik waktu, peserta tidak dapat sepenuhnya memperoleh dan secara obyektif menafsirkan semua informasi. Selain itu, bahkan jika itu adalah informasi yang sama, umpan balik semua orang berbeda. Artinya, harga itu sendiri sudah mengandung harapan yang salah dari peserta pasar, jadi pada dasarnya, harga pasar selalu salah. Ini mungkin menjadi sumber keuntungan dari arbitrager.

II. Prinsip-prinsip strategis

Menurut prinsip-prinsip di atas, kita dapat tahu bahwa di pasar berjangka yang tidak efektif, alasan mengapa dampak pasar pada kontrak pengiriman di periode yang berbeda tidak selalu sinkron, dan penetapan harga tidak sepenuhnya efektif. Kemudian, berdasarkan harga kontrak pengiriman objek transaksi yang sama di periode yang berbeda, jika ada perbedaan harga yang besar antara kedua harga, kita dapat membeli dan menjual kontrak berjangka di periode yang berbeda pada saat yang sama untuk arbitrage lintas periode. Seperti komoditas berjangka, mata uang digital juga memiliki portofolio kontrak arbitrage lintas periode. Misalnya, di bursa OKEX, ada: ETC minggu ini, ETC minggu depan, ETC kuartal. Misalnya, misalkan perbedaan harga antara minggu ETC saat ini dan kuartal ETC tetap sekitar 5 untuk waktu yang lama. Jika perbedaan harga mencapai 7 suatu hari, kita berharap bahwa perbedaan harga akan kembali ke 5 di masa depan. Kemudian kita dapat menjual ETC minggu itu dan membeli kuartal ETC pada saat yang sama untuk memotong perbedaan harga, dan sebaliknya. Meskipun perbedaan harga ini ada, ada banyak ketidakpastian dalam arbitrase manual karena operasi manual yang memakan waktu, akurasi yang buruk dan dampak perubahan harga. Pesona arbitrase kuantitatif terletak pada menangkap peluang arbitrase melalui model kuantitatif dan merumuskan strategi perdagangan arbitrase, serta menempatkan pesanan perdagangan secara otomatis ke bursa melalui algoritma terprogram, sehingga dapat menangkap peluang dengan cepat dan akurat dan menghasilkan keuntungan secara efisien dan stabil.

III. Logika Strategi

Artikel ini akan mengajarkan Anda cara menggunakan Platform Perdagangan Kuantum FMZ dan kontrak berjangka ETC di bursa OKEX untuk menunjukkan cara menangkap peluang arbitrage instan, merebut keuntungan yang dapat dilihat setiap saat, dan lindung nilai risiko yang mungkin terjadi dalam perdagangan mata uang digital dengan strategi arbitrage sederhana.

Membuat strategi arbitrage lintas periode untuk mata uang digital

Kesulitan: Normal

Lingkungan Strategi

  • Objek transaksi: Ether Classic (ETC)
  • Data spread: ETC minggu saat ini - ETC kuartal (menghilangkan tes kointegrasi)
  • Periode transaksi: 5 menit
  • Pencocokan posisi: 1:1
  • Jenis transaksi: periode silang dari jenis yang sama

Logika strategi

  • Kondisi untuk membuka posisi dengan pergi panjang selisih harga: jika akun arus tidak memiliki posisi dan perbedaan harga kurang dari batas bawah boll, maka pergi panjang selisih harga. yaitu, membeli posisi pembukaan ETC untuk minggu, menjual posisi pembukaan ETC untuk kuartal.
  • Kondisi untuk membuka posisi dengan shorting perbedaan harga: jika tidak ada posisi di rekening current, dan perbedaan harga lebih besar dari batas atas boll, maka shorting perbedaan harga. yaitu, menjual posisi pembukaan ETC untuk minggu, membeli posisi pembukaan ETC untuk kuartal.
  • Kondisi untuk menutup posisi dengan pergi panjang selisih harga: jika akun saat ini memegang pesanan panjang ETC dalam minggu saat ini dan memegang pesanan pendek ETC kuartal, dan perbedaan harga lebih besar dari batas tengah bola, maka tutup panjang selisih harga. yaitu, jual posisi penutupan ETC untuk minggu, beli posisi penutupan ETC untuk kuartal.
  • Kondisi untuk menutup posisi dengan pergi pendek perbedaan harga: jika akun saat ini memegang pergi pesanan pendek ETC dalam minggu saat ini, dan memegang pergi pesanan panjang ETC kuartal, dan perbedaan harga kurang dari batas tengah bol, maka menutup pendek perbedaan harga. yaitu, membeli posisi penutupan ETC untuk minggu, menjual posisi penutupan ETC untuk kuartal.

IV. Menulis kerangka strategi

Di atas adalah deskripsi logika sederhana dari strategi arbitrage lintas periode mata uang digital. jadi bagaimana menerapkan ide-ide kami dalam program? kami mencoba membangun kerangka kerja di FMZ Quant Trading Platform.

function Data() {}  // Basic data function
Data.prototype.mp = function () {}  // Position function
Data.prototype.boll = function () {}  // Indicator function
Data.prototype.trade = function () {}  // Order placement function
Data.prototype.cancelOrders = function () {}  // Order withdrawal function
Data.prototype.isEven = function () {}  // Processing single contract function
Data.prototype.drawingChart = function () {}  // Drawing function

// Trading conditions
function onTick() {
    var data = new Data(tradeTypeA, tradeTypeB);  // Create a basic data object
    var accountStocks = data.accountData.Stocks;  // Account balance
    var boll = data.boll(dataLength, timeCycle);  // Calculate the technical indicators of boll
    data.trade();  // Calculate trading conditions to place an order
    data.cancelOrders();  // Cancel orders
    data.drawingChart(boll);  // drawing
    data.isEven();  // Processing of holding individual contract
}

//Entry function
function main() {
    while (true) {  // Enter the polling mode
        onTick();  // Execute onTick function
        Sleep(500);  // Sleep for 0.5 seconds
    }
}

V. Strategi Penulisan

Kerangka kerja strategi dapat dengan mudah diatur sesuai dengan ide strategi dan proses transaksi. Pemrosesan pra sebelum transaksi. Dapatkan dan hitung data. Tempatkan pesanan dan tangani nanti. Selanjutnya, kita perlu mengisi kode detail yang diperlukan dalam kerangka strategi sesuai dengan proses transaksi yang sebenarnya dan rincian transaksi.

Pemrosesan pra sebelum transaksi Langkah 1: Mengumumkan variabel global yang diperlukan dalam ruang lingkup global.

//Declare a chart object for the configuration chart
var chart = { }

//Call Chart function and initialize the chart
var ObjChart = Chart ( chart )

//Declare an empty array to store price difference series
var bars = [ ]

//Declare a record history data timestamp variable
var oldTime = 0

Langkah 2: Mengkonfigurasi parameter eksternal dari strategi.

// parameters
var tradeTypeA = "this_week"; // Arbitrage A Contract
var tradeTypeB = "quarter"; // Arbitrage B Contract
var dataLength = 10; //Indicator period length
var timeCycle = 1; // K-line period
var name = "ETC"; // Currencies
var unit = 1; // Order quantity

Langkah 3: Mendefinisikan fungsi pemrosesan data Fungsi data dasar: Data Buat constructor, Data, dan tentukan sifat internalnya. Termasuk: data akun, data posisi, data timestamp K-line, harga beli/jual kontrak arbitrage A/B, dan perbedaan harga arbitrage positif/negatif.

// Basic data
function Data(tradeTypeA, tradeTypeB) { // Pass in arbitrage A contract and arbitrage B contract
    this.accountData = _C(exchange.GetAccount); // Get account information
    this.positionData = _C(exchange.GetPosition); // Get position information
    var recordsData = _C(exchange.GetRecords); // Get K-line data
    exchange.SetContractType(tradeTypeA); // Subscription arbitrage A contract
    var depthDataA = _C(exchange.GetDepth); // Depth data of arbitrage A contract
    exchange.SetContractType(tradeTypeB); // Subscription arbitrage B contract
    var depthDataB = _C(exchange.GetDepth); // Depth data of arbitrage B contract
    this.time = recordsData[recordsData.length - 1].Time; // Time of obtaining the latest data
    this.askA = depthDataA.Asks[0].Price; // Sell one price of Arbitrage A contract
    this.bidA = depthDataA.Bids[0].Price; // Buy one price of Arbitrage A contract
    this.askB = depthDataB.Asks[0].Price; // Sell one price of Arbitrage B contract
    this.bidB = depthDataB.Bids[0].Price; // Buy one price of Arbitrage B contract
    // Positive arbitrage price differences (Sell one price of contract A - Buy one price of contract B)
    this.basb = depthDataA.Asks[0].Price - depthDataB.Bids[0].Price;
    // Negative arbitrage price differences (Buy one price of contract A - Sell one price of contract B)
    this.sabb = depthDataA.Bids[0].Price - depthDataB.Asks[0].Price;
}

Dapatkan fungsi posisi: mp ()) Melalui seluruh array posisi dan mengembalikan jumlah posisi kontrak dan arah yang ditentukan.

// Get positions
Data.prototype.mp = function (tradeType, type) {
    var positionData = this.positionData; // Get position information
    for (var i = 0; i < positionData.length; i++) {
        if (positionData[i].ContractType == tradeType) {
            if (positionData[i].Type == type) {
                if (positionData[i].Amount > 0) {
                    return positionData[i].Amount;
                }
            }
        }
    }
    return false;
}

K-line dan fungsi indikator: boll() Sebuah urutan K-line baru disintesis sesuai dengan data perbedaan harga arbitrage positif / arbitrage negatif. Data trek atas, trek tengah dan trek bawah yang dihitung oleh indikator boll dikembalikan.

// Synthesis of new K-line data and boll indicator data
Data.prototype.boll = function (num, timeCycle) {
    var self = {}; // Temporary objects
    // Median value of positive arbitrage price difference and negative arbitrage price difference
    self.Close = (this.basb + this.sabb) / 2;
    if (this.timeA == this.timeB) {
        self.Time = this.time;
    } // Compare two depth data timestamps
    if (this.time - oldTime > timeCycle * 60000) {
        bars.push(self);
        oldTime = this.time;
    } // Pass in the price difference data object into the K-line array according to the specified time period
    if (bars.length > num * 2) {
        bars.shift(); // Control the length of the K-line array
    } else {
        return;
    }
    var boll = TA.BOLL(bars, num, 2); // Call the boll indicator in the talib library
    return {
        up: boll[0][boll[0].length - 1], // boll indicator upper track
        middle: boll[1][boll[1].length - 1], // boll indicator middle track
        down: boll[2][boll[2].length - 1] // boll indicator down track
    } // Return a processed boll indicator data
}

Fungsi pesanan: perdagangan Pass in the order contract name and order type, then place the order with consideration, and return the result after placing the order. Karena perlu untuk menempatkan dua order di arah yang berbeda pada saat yang sama, membeli / menjual satu harga dikonversi dalam fungsi sesuai dengan nama kontrak dari pesanan.

// place the order
Data.prototype.trade = function (tradeType, type) {
    exchange.SetContractType(tradeType); // Resubscribe to a contract before placing an order
    var askPrice, bidPrice;
    if (tradeType == tradeTypeA) { // If the order is placed in contract A
        askPrice = this.askA; // set askPrice
        bidPrice = this.bidA; // set bidPrice
    } else if (tradeType == tradeTypeB) { // If the order is placed in contract B
        askPrice = this.askB; // set askPrice
        bidPrice = this.bidB; // set bidPrice
    }
    switch (type) { // Match order placement mode
        case "buy":
            exchange.SetDirection(type); // Set order placement mode
            return exchange.Buy(askPrice, unit);
        case "sell":
            exchange.SetDirection(type); // Set order placement mode
            return exchange.Sell(bidPrice, unit);
        case "closebuy":
            exchange.SetDirection(type); // Set order placement mode
            return exchange.Sell(bidPrice, unit);
        case "closesell":
            exchange.SetDirection(type); // Set order placement mode
            return exchange.Buy(askPrice, unit);
        default:
            return false;
    }
}

Batalkan Perintah Fungsi: Batalkan Perintah Dapatkan array dari semua pesanan yang belum selesai dan batalkan satu per satu. Selain itu, false dikembalikan jika ada pesanan yang belum terisi, dan true dikembalikan jika tidak ada pesanan yang belum terisi.

// Cancel order
Data.prototype.cancelOrders = function () {
    Sleep(500); // Delay before cancellation, because some exchanges, you know what I mean
    var orders = _C(exchange.GetOrders); // Get an array of unfilled orders
    if (orders.length > 0) { // If there are unfilled orders
        for (var i = 0; i < orders.length; i++) { //Iterate through the array of unfilled orders
            exchange.CancelOrder(orders[i].Id); //Cancel unfilled orders one by one
            Sleep(500); //Delay 0.5 seconds
        }
        return false; // Return false if an unfilled order is cancelled
    }
    return true; // Return true if there are no unfilled orders
}

Tangani memegang kontrak tunggal: isEven ((() Dalam kasus satu kaki dalam transaksi arbitrage, kita hanya akan menutup semua posisi.

// Handle holding a single contract
Data.prototype.isEven = function () {
    var positionData = this.positionData; // Get position information
    var type = null; // Switch position direction
    // If the remaining 2 of the position array length is not equal to 0 or the position array length is not equal to 2
    if (positionData.length % 2 != 0 || positionData.length != 2) {
        for (var i = 0; i < positionData.length; i++) { // Iterate through the position array
            if (positionData[i].Type == 0) { // If it is a long order
                type = 10; // Set order parameters
            } else if (positionData[i].Type == 1) { // If it is a short order
                type = -10; // Set order parameters
            }
            // Close all positions
            this.trade(positionData[i].ContractType, type, positionData[i].Amount);
        }
    }
}

Fungsi gambar: gambarGambar ()) Panggil metode ObjChart Add (), tarik data pasar dan data indikator yang diperlukan dalam grafik: jalur atas, jalur tengah, jalur bawah, perbedaan harga arbitrage positif/negatif.

// Drawing
Data.prototype.drawingChart = function (boll) {
    var nowTime = new Date().getTime();
    ObjChart.add([0, [nowTime, boll.up]]);
    ObjChart.add([1, [nowTime, boll.middle]]);
    ObjChart.add([2, [nowTime, boll.down]]);
    ObjChart.add([3, [nowTime, this.basb]]);
    ObjChart.add([4, [nowTime, this.sabb]]);
    ObjChart.update(chart);
}

Langkah 4: Dalam fungsi entri main ((), eksekusi kode pra-pengolahan pra-transaksi, yang hanya akan dijalankan sekali setelah program dimulai, termasuk:

  • SetErrorFilter (()) untuk menyaring informasi yang tidak penting di konsol
  • exchange.IO ( https://www.squadhelp.com/name/exchange.io?lp=dUntuk menetapkan mata uang digital yang akan diperdagangkan
  • ObjChart reset ( ) untuk menghapus grafik sebelumnya yang ditarik sebelum memulai program
  • LogProfitReset (()) untuk menghapus informasi bilah status sebelum memulai program
//entry function
function main() {
    // Filter the unimportant information in the console
    SetErrorFilter("429|GetRecords:|GetOrders:|GetDepth:|GetAccount|:Buy|Sell|timeout|Futures_OP");
    exchange.IO("currency", name + '_USDT'); //Set the digital currency to be traded
    ObjChart.reset(); // Clear the previous chart drawn before starting the program
    LogProfitReset(); // Clear the status bar information before starting the program
}

Setelah pra-pengolahan pra-transaksi di atas didefinisikan, langkah selanjutnya adalah memasuki mode polling dan mengeksekusi fungsi onTick ()) berulang kali.

//entry function
function main() {
    // Filter the unimportant information in the console
    SetErrorFilter("429|GetRecords:|GetOrders:|GetDepth:|GetAccount|:Buy|Sell|timeout|Futures_OP");
    exchange.IO("currency", name + '_USDT'); //Set the digital currency to be traded
    ObjChart.reset(); //Clear the previous chart drawn before starting the program
    LogProfitReset(); //Clear the status bar information before starting the program
    while (true) { // Enter the polling mode
        onTick(); // Execute onTick function
        Sleep(500); // Sleep for 0.5 seconds
    }
}

Dapatkan dan hitung data Langkah 1: Dapatkan objek data dasar, saldo akun, dan data indikator boll untuk digunakan dalam logika perdagangan.

// Trading conditions
function onTick() {
    var data = new Data(tradeTypeA, tradeTypeB); // Create a basic data object
    var accountStocks = data.accountData.Stocks; // Account balance
    var boll = data.boll(dataLength, timeCycle); // Get boll indicator data
    if (!boll) return; // Return if there is no boll data
}

Memesan dan menangani tindak lanjut Langkah 1: Melakukan operasi pembelian dan penjualan sesuai dengan logika strategis di atas. Pertama, menilai apakah harga dan kondisi indikator yang valid, kemudian menilai apakah kondisi posisi yang valid, dan akhirnya melaksanakan fungsi order perdagangan ()

// Trading conditions
function onTick() {
    var data = new Data(tradeTypeA, tradeTypeB); // Create a basic data object
    var accountStocks = data.accountData.Stocks; // Account balance
    var boll = data.boll(dataLength, timeCycle); // Get boll indicator data
    if (!boll) return; // Return if there is no boll data
    // Explanation of the price difference
    // basb = (Sell one price of contract A - Buy one price of contract B)
    // sabb = (Buy one price of contract A - Sell one price of contract B)
    if (data.sabb > boll.middle && data.sabb < boll.up) { // If sabb is higher than the middle track
        if (data.mp(tradeTypeA, 0)) { // Check whether contract A has long orders before placing an order
            data.trade(tradeTypeA, "closebuy"); // Contract A closes long position
        }
        if (data.mp(tradeTypeB, 1)) { // Check whether contract B has short orders before placing an order
            data.trade(tradeTypeB, "closesell"); // Contract B closes short position
        }
    } else if (data.basb < boll.middle && data.basb > boll.down) { // If basb is lower than the middle track
        if (data.mp(tradeTypeA, 1)) { // Check whether contract A has short orders before placing an order
            data.trade(tradeTypeA, "closesell"); // Contract A closes short position
        }
        if (data.mp(tradeTypeB, 0)) { // Check whether contract B has long orders before placing an order
            data.trade(tradeTypeB, "closebuy"); // Contract B closes long position
        }
    }
    if (accountStocks * Math.max(data.askA, data.askB) > 1) { // If there is balance in the account
        if (data.basb < boll.down) { // If basb price difference is lower than the down track
            if (!data.mp(tradeTypeA, 0)) { // Check whether contract A has long orders before placing an order
                data.trade(tradeTypeA, "buy"); // Contract A opens long position
            }
            if (!data.mp(tradeTypeB, 1)) { // Check whether contract B has short orders before placing an order
                data.trade(tradeTypeB, "sell"); // Contract B opens short position
            }
        } else if (data.sabb > boll.up) { // If sabb price difference is higher than the upper track
            if (!data.mp(tradeTypeA, 1)) { // Check whether contract A has short orders before placing an order
                data.trade(tradeTypeA, "sell"); // Contract A opens short position
            }
            if (!data.mp(tradeTypeB, 0)) { // Check whether contract B has long orders before placing an order
                data.trade(tradeTypeB, "buy"); // Contract B opens long position
            }
        }
    }
}

Langkah 2: Setelah pesanan ditempatkan, perlu untuk menangani situasi yang tidak normal seperti pesanan yang belum diselesaikan dan memegang kontrak tunggal.

// Trading conditions
function onTick() {
    var data = new Data(tradeTypeA, tradeTypeB); // Create a basic data object
    var accountStocks = data.accountData.Stocks; // Account balance
    var boll = data.boll(dataLength, timeCycle); // Get boll indicator data
    if (!boll) return; // Return if there is no boll data
    // Explanation of the price difference
    //basb = (Sell one price of contract A - Buy one price of contract B)
    // sabb = (Buy one price of contract A - Sell one price of contract B)
    if (data.sabb > boll.middle && data.sabb < boll.up) { // If sabb is higher than the middle track
        if (data.mp(tradeTypeA, 0)) { // Check whether contract A has long orders before placing an order
            data.trade(tradeTypeA, "closebuy"); // Contract A closes long position
        }
        if (data.mp(tradeTypeB, 1)) { // Check whether contract B has short orders before placing an order
            data.trade(tradeTypeB, "closesell"); // Contract B closes short position
        }
    } else if (data.basb < boll.middle && data.basb > boll.down) { // If basb is lower than the middle track
        if (data.mp(tradeTypeA, 1)) { // Check whether contract A has short orders before placing an order
            data.trade(tradeTypeA, "closesell"); // Contract A closes short position
        }
        if (data.mp(tradeTypeB, 0)) { // Check whether contract B has long orders before placing an order
            data.trade(tradeTypeB, "closebuy"); // Contract B closes long position
        }
    }
    if (accountStocks * Math.max(data.askA, data.askB) > 1) { // If there is balance in the account
        if (data.basb < boll.down) { // If basb price difference is lower than the down track
            if (!data.mp(tradeTypeA, 0)) { // Check whether contract A has long orders before placing an order
                data.trade(tradeTypeA, "buy"); // Contract A opens long position
            }
            if (!data.mp(tradeTypeB, 1)) { // Check whether contract B has short orders before placing an order
                data.trade(tradeTypeB, "sell"); // Contract B opens short position
            }
        } else if (data.sabb > boll.up) { // If sabb price difference is higher than the upper track
            if (!data.mp(tradeTypeA, 1)) { // Check whether contract A has short orders before placing an order
                data.trade(tradeTypeA, "sell"); // Contract A opens short position
            }
            if (!data.mp(tradeTypeB, 0)) { // Check whether contract B has long orders before placing an order
                data.trade(tradeTypeB, "buy"); // Contract B opens long position
            }
        }
    }
    data.cancelOrders(); // cancel orders
    data.drawingChart(boll); // drawing
    data.isEven(); // Handle holding individual contracts
}

VI. Strategi Lengkap

Seperti di atas, kami telah membuat strategi arbitrage cross-period sederhana dari mata uang digital sepenuhnya melalui lebih dari 200 baris kode.

// Global variable
// Declare a chart object for the configuration chart
var chart = {
    __isStock: true,
    tooltip: {
        xDateFormat: '%Y-%m-%d %H:%M:%S, %A'
    },
    title: {
        text: 'transaction profit and loss curve (detailed)'
    },
    rangeSelector: {
        buttons: [{
            type: 'hour',
            count: 1,
            text: '1h'
        }, {
            type: 'hour',
            count: 2,
            text: '3h'
        }, {
            type: 'hour',
            count: 8,
            text: '8h'
        }, {
            type: 'all',
            text: 'All'
        }],
        selected: 0,
        inputEnabled: false
    },
    xAxis: {
        type: 'datetime'
    },
    yAxis: {
        title: {
            text: 'price difference'
        },
        opposite: false,
    },
    series: [{
        name: "upper track",
        id: "line1,up",
        data: []
    }, {
        name: "middle track",
        id: "line2,middle",
        data: []
    }, {
        name: "down track",
        id: "line3,down",
        data: []
    }, {
        name: "basb",
        id: "line4,basb",
        data: []
    }, {
        name: "sabb",
        id: "line5,sabb",
        data: []
    }]
};
var ObjChart = Chart(chart); // Drawing object
var bars = []; // Storage price difference series
var oldTime = 0; // Record historical data timestamp

// parameters
var tradeTypeA = "this_week"; // Arbitrage A contract
var tradeTypeB = "quarter"; // Arbitrage B contract
var dataLength = 10; //Indicator period length
var timeCycle = 1; // K-line period
var name = "ETC"; // Currencies
var unit = 1; // Order quantity

// basic data
function Data(tradeTypeA, tradeTypeB) { // Pass in arbitrage A contract and arbitrage B contract
    this.accountData = _C(exchange.GetAccount); // Get account information
    this.positionData = _C(exchange.GetPosition); // Get position information
    var recordsData = _C(exchange.GetRecords); //Get K-line data
    exchange.SetContractType(tradeTypeA); // Subscribe to arbitrage A contract
    var depthDataA = _C(exchange.GetDepth); // Arbitrage A contract depth data
    exchange.SetContractType(tradeTypeB); // Subscribe to arbitrage B contract
    var depthDataB = _C(exchange.GetDepth); // Arbitrage B contract depth data
    this.time = recordsData[recordsData.length - 1].Time; // Time to get the latest data
    this.askA = depthDataA.Asks[0].Price; // Sell one price of arbitrage A contract
    this.bidA = depthDataA.Bids[0].Price; // Buy one price of arbitrage A contract
    this.askB = depthDataB.Asks[0].Price; // Sell one price of arbitrage B contract
    this.bidB = depthDataB.Bids[0].Price; // Buy one price of arbitrage B contract
    // Positive arbitrage price difference (Sell one price of contract A - Buy one price of contract B)
    this.basb = depthDataA.Asks[0].Price - depthDataB.Bids[0].Price;
    // Negative arbitrage price difference (Buy one price of contract A - Sell one price of contract B)
    this.sabb = depthDataA.Bids[0].Price - depthDataB.Asks[0].Price;
}

// Get position
Data.prototype.mp = function (tradeType, type) {
    var positionData = this.positionData; // Get position information
    for (var i = 0; i < positionData.length; i++) {
        if (positionData[i].ContractType == tradeType) {
            if (positionData[i].Type == type) {
                if (positionData[i].Amount > 0) {
                    return positionData[i].Amount;
                }
            }
        }
    }
    return false;
}

// Synthesis of new K-line data and boll indicator data
Data.prototype.boll = function (num, timeCycle) {
    var self = {}; // Temporary objects
    // Median value of between positive arbitrage price difference and negative arbitrage price difference
    self.Close = (this.basb + this.sabb) / 2;
    if (this.timeA == this.timeB) {
        self.Time = this.time;
    } // Compare two depth data timestamps
    if (this.time - oldTime > timeCycle * 60000) {
        bars.push(self);
        oldTime = this.time;
    } // Pass in the price difference data object into the K-line array according to the specified time period
    if (bars.length > num * 2) {
        bars.shift(); // Control the length of the K-line array
    } else {
        return;
    }
    var boll = TA.BOLL(bars, num, 2); // Call the boll indicator in the talib library
    return {
        up: boll[0][boll[0].length - 1], // boll indicator upper track
        middle: boll[1][boll[1].length - 1], // boll indicator middle track
        down: boll[2][boll[2].length - 1] // boll indicator down track
    } // Return a processed boll indicator data
}

// Place an order
Data.prototype.trade = function (tradeType, type) {
    exchange.SetContractType(tradeType); // Resubscribe to a contract before placing an order
    var askPrice, bidPrice;
    if (tradeType == tradeTypeA) { // If the order is placed in contract A
        askPrice = this.askA; // Set askPrice
        bidPrice = this.bidA; // Set bidPrice
    } else if (tradeType == tradeTypeB) { // If the order is placed in contract B
        askPrice = this.askB; // Set askPrice
        bidPrice = this.bidB; // Set bidPrice
    }
    switch (type) { // Match order placement mode
        case "buy":
            exchange.SetDirection(type); // Set order placement mode
            return exchange.Buy(askPrice, unit);
        case "sell":
            exchange.SetDirection(type); // Set order placement mode
            return exchange.Sell(bidPrice, unit);
        case "closebuy":
            exchange.SetDirection(type); // Set order placement mode
            return exchange.Sell(bidPrice, unit);
        case "closesell":
            exchange.SetDirection(type); // Set order placement mode
            return exchange.Buy(askPrice, unit);
        default:
            return false;
    }
}

// Cancel orders
Data.prototype.cancelOrders = function () {
    Sleep(500); // Delay before cancellation, because some exchanges, you know what I mean
    var orders = _C(exchange.GetOrders); //Get an array of unfilled orders
    if (orders.length > 0) { // If there are unfilled orders
        for (var i = 0; i < orders.length; i++) { //Iterate through the array of unfilled orders
            exchange.CancelOrder(orders[i].Id); //Cancel unfilled orders one by one
            Sleep(500); //Sleep for 0.5 seconds
        }
        return false; // Return false if an unfilled order is cancelled
    }
    return true; //Return true if there are no unfilled orders
}

// Handle holding individual contracts
Data.prototype.isEven = function () {
    var positionData = this.positionData; // Get position information
    var type = null; // Switch position direction
    // If the remaining 2 of the position array length is not equal to 0 or the position array length is not equal to 2
    if (positionData.length % 2 != 0 || positionData.length != 2) {
        for (var i = 0; i < positionData.length; i++) { // Iterate through the position array
            if (positionData[i].Type == 0) { // If it is a long order
                type = 10; // Set order parameters
            } else if (positionData[i].Type == 1) { // If it is a short order
                type = -10; // Set order parameters
            }
            // Close all positions
            this.trade(positionData[i].ContractType, type, positionData[i].Amount);
        }
    }
}

// Drawing
Data.prototype.drawingChart = function (boll) {
    var nowTime = new Date().getTime();
    ObjChart.add([0, [nowTime, boll.up]]);
    ObjChart.add([1, [nowTime, boll.middle]]);
    ObjChart.add([2, [nowTime, boll.down]]);
    ObjChart.add([3, [nowTime, this.basb]]);
    ObjChart.add([4, [nowTime, this.sabb]]);
    ObjChart.update(chart);
}

// Trading conditions
function onTick() {
    var data = new Data(tradeTypeA, tradeTypeB); // Create a basic data object
    var accountStocks = data.accountData.Stocks; // Account balance
    var boll = data.boll(dataLength, timeCycle); // Get boll indicator data
    if (!boll) return; // Return if there is no boll data
    // Explanation of price difference
    // basb = (Sell one price of contract A - Buy one price of contract B)
    // sabb = (Buy one price of contract A - Sell one price of contract B)
    if (data.sabb > boll.middle && data.sabb < boll.up) { // If sabb is higher than the middle track
        if (data.mp(tradeTypeA, 0)) { // Check whether contract A has long orders before placing an order
            data.trade(tradeTypeA, "closebuy"); // Contract A closes long position
        }
        if (data.mp(tradeTypeB, 1)) { // Check whether contract B has short orders before placing an order
            data.trade(tradeTypeB, "closesell"); // Contract B closes short position
        }
    } else if (data.basb < boll.middle && data.basb > boll.down) { // If basb is lower than the middle track
        if (data.mp(tradeTypeA, 1)) { // Check whether contract A has short orders before placing an order
            data.trade(tradeTypeA, "closesell"); // Contract A closes short position
        }
        if (data.mp(tradeTypeB, 0)) { // Check whether contract B has long orders before placing an order
            data.trade(tradeTypeB, "closebuy"); // Contract B closes long position
        }
    }
    if (accountStocks * Math.max(data.askA, data.askB) > 1) { // If there is a balance in the account
        if (data.basb < boll.down) { // If basb price difference is lower than the down track
            if (!data.mp(tradeTypeA, 0)) { // Check whether contract A has long orders before placing an order
                data.trade(tradeTypeA, "buy"); // Contract A opens long position
            }
            if (!data.mp(tradeTypeB, 1)) { // Check whether contract B has short orders before placing an order
                data.trade(tradeTypeB, "sell"); // Contract B opens short position
            }
        } else if (data.sabb > boll.up) { // If sabb price difference is higher than the upper track
            if (!data.mp(tradeTypeA, 1)) { // Check whether contract A has short orders before placing an order
                data.trade(tradeTypeA, "sell"); // Contract A opens short position
            }
            if (!data.mp(tradeTypeB, 0)) { // Check whether contract B has long orders before placing an order
                data.trade(tradeTypeB, "buy"); // Contract B opens long position
            }
        }
    }
    data.cancelOrders(); // Cancel orders
    data.drawingChart(boll); // Drawing
    data.isEven(); // Handle holding individual contracts
}

//Entry function
function main() {
    // Filter unimportant information in the console
    SetErrorFilter("429|GetRecords:|GetOrders:|GetDepth:|GetAccount|:Buy|Sell|timeout|Futures_OP");
    exchange.IO("currency", name + '_USDT'); //Set the digital currency to be traded
    ObjChart.reset(); //Clear the previous chart drawn before starting the program
    LogProfitReset(); //Clear the status bar information before starting the program
    while (true) { // Enter polling mode
        onTick(); // Execute the onTick function
        Sleep(500); // Sleep for 0.5 seconds
    }
}

Alamat strategi:https://www.fmz.com/strategy/104964

VII. Ringkasan

Strategi dalam artikel ini hanyalah contoh. Bot nyata tidak sederhana, tetapi Anda dapat mengikuti contohnya dan menggunakan imajinasi liar Anda sendiri Harus diingat bahwa, berdasarkan pengalaman terbatas saya, di pasar mata uang digital saat ini, hampir semua strategi arbitrage berjangka-berjangka murni tidak layak dijalankan, apakah itu adalah arbitrage segitiga bebas risiko atau arbitrage lintas pasar. Alasan adalah bahwa tidak peduli pasar berjangka pertukaran mata uang digital mana, marginnya bukan mata uang legal. Pada saat ini, hampir semua mata uang digital telah turun sekitar 70% sejak awal tahun. Artinya, strategi selalu menghasilkan mata uang, tetapi harga mata uang menurun. Secara keseluruhan, pasar mata uang digital tampaknya telah lepas dari blockchain. Seperti tulip saat itu, harga selalu berasal dari harapan dan kepercayaan orang, dan kepercayaan berasal dari harga...


Berkaitan

Lebih banyak