0bps

Strategy vault / Other

REST Version OKEx Cross Period Hedging Strategy Tutorial

- Only do bull spreads; bear spreads can be done by swapping contracts, which is the bear spread. - Add Two Exchange Objects, the First for the Quarter, the Second for the Week. - Streamlined all code that could be…

JavaScriptOther
Author
发明者量化-小小梦
Complexity
Starter · 40 lines
Parameters
5

Minimal OKEX cross-period hedging strategy (tutorial)

IMG

  • Only do bull spreads; bear spreads can be done by swapping contracts, which is the bear spread.

  • Add Two Exchange Objects, the First for the Quarter, the Second for the Week.

  • Streamlined all code that could be simplified, with significant room for optimization; teaching strategy is cautious in real trading, and there is some risk across periods.

  • Feedback welcomeBUG.

Teaching strategy, use in live trading with caution.

Teaching strategy, use in live trading with caution.

Teaching strategy, use in live trading with caution.

Parameters

NameDefaultWhat it controls
_BegintrueStarting spread
_AddtruePrice difference
_ProfittrueClosing price spread profit
_Count10Number of nodes
_ContractNumtrueNode order quantity

Source JavaScript

function Hedge (isOpen, priceA, priceB) {
    exchanges[0].SetDirection(isOpen ? "sell" : "closesell")
    exchanges[1].SetDirection(isOpen ? "buy" : "closebuy");
    (function (routineA, routineB) {
        Log(routineA.wait(), routineB.wait(), priceA, priceB)
    })(exchanges[0].Go(isOpen ? "Sell" : "Buy", priceA, _ContractNum), exchanges[1].Go(isOpen ? "Buy" : "Sell", priceB, _ContractNum));
}

var slidePrice = 5
function main () {
    var tickerA, tickerB 
    var arr = []
    for (var i = 0 ; i < _Count ; i++) {
        arr.push({open: _Begin + i * _Add, cover: _Begin + i * _Add - _Profit, isHold: false})
    }
    exchanges[0].SetContractType("quarter")
    exchanges[1].SetContractType("this_week")
    while (1) {
        var tab = {type: "table", title: "Status", cols: ["Node Information"], rows: []}
        tickerA = exchanges[0].GetTicker()
        tickerB = exchanges[1].GetTicker()

        if (tickerA && tickerB) {
            $.PlotLine("Spread:Aall-Ball", tickerA.Last - tickerB.Last)
            for (var j = 0 ; j < arr.length; j++) {
                if (tickerA.Buy - tickerB.Sell > arr[j].open && !arr[j].isHold) {
                    Hedge(true, tickerA.Buy - slidePrice, tickerB.Sell + slidePrice)
                    arr[j].isHold = true
                }
                if (tickerA.Sell - tickerB.Buy < arr[j].cover && arr[j].isHold) {
                    Hedge(false, tickerA.Sell + slidePrice, tickerB.Buy - slidePrice)
                    arr[j].isHold = false 
                }
                tab.rows.push([JSON.stringify(arr[j])])
            }
        }
        LogStatus(_D(), "\n `" + JSON.stringify(tab) + "`")
        Sleep(500)
    }
}

Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2019-04-17. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/REST-Version-OKEx-Cross-Period-Hedging-Strategy-Tutorial.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open