Introduction
This tutorial presents a minimalist approach to implementing a cross-period hedging strategy on the OKEX platform. The strategy focuses on positive arbitrage (buying low and selling high across different contract periods), though it can be adapted for reverse arbitrage with minor modifications.
Key Features:
- Utilizes two exchange objects: one for quarterly contracts and another for weekly contracts.
- Highly simplified code structure with room for further optimization.
- Designed for educational purposes - exercise caution when considering live trading.
Core Strategy Implementation
Code Overview
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 Info"], rows: []}
tickerA = exchanges[0].GetTicker()
tickerB = exchanges[1].GetTicker()
if (tickerA && tickerB) {
$.PlotLine("Price Difference: Exchange A - B", 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)
}
}Strategy Components
- Hedge Function: Handles opening/closing positions across two contracts
- Main Loop: Continuously monitors price differentials and executes trades when conditions are met
- Price Slide Mechanism: Incorporates a 5-point buffer to improve execution success
๐ Learn more about advanced hedging strategies
Important Considerations
- Educational Purpose Only: This strategy is intended for learning purposes
- Market Risks: Cross-period arbitrage carries inherent market risks
- Optimization Potential: Significant room exists for performance enhancements
FAQ Section
Can this strategy be used for reverse arbitrage?
Yes, by simply swapping the contract references in the code, the strategy can be adapted for reverse arbitrage.
Is this strategy currently viable on OKEX?
The core principles remain valid, but market conditions and exchange APIs may have evolved since the original publication. Always test thoroughly before live deployment.
What's the purpose of the slidePrice variable?
The slidePrice (set to 5 points) creates a buffer to improve order execution success in volatile markets.
How can I monitor the strategy's performance?
The code includes real-time plotting of price differentials and status logging for performance monitoring.
๐ Explore professional trading tools
Conclusion
This tutorial provides a foundation for understanding cross-period hedging strategies. While simplified for educational clarity, it demonstrates core concepts that can be expanded into more sophisticated implementations. Remember that all trading strategies carry risk, and proper due diligence is essential before live deployment.
**Key SEO Elements Incorporated:**
1. Structured headings hierarchy
2. Natural keyword integration (hedging strategy, OKEX, arbitrage, etc.)
3. FAQ section addressing common queries
4. Engaging anchor texts with proper formatting
5. Clear content organization with logical flow
6. Removal of dated information and promotional content