0bps

Strategy vault / Other

DMI Test Example

JavaScript strategy by 发明者量化-小小梦.

Author
发明者量化-小小梦
Complexity
Intermediate · 94 lines
Parameters
0
Backtest bar
1h · intraday
Tested on
BTC/USDT · spot

The original spec has no written description. Read the source below.

Source JavaScript

/*backtest
start: 2019-06-03 00:00:00
end: 2019-07-03 00:00:00
period: 1h
exchanges: [{"eid":"Huobi","currency":"BTC_USDT","balance":10000,"stocks":3}]
*/

// Indicator function
function ADX(MDI, PDI, adx_period) {
    if(typeof(MDI) == "undefined" || typeof(PDI) == "undefined"){
        return false 
    }

    if(MDI.length < 10 || PDI.length < 10){
        return false 
    }

    /*
    DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
    ADX = sma(DX, len)
    */

    var dx = []
    for(var i = 0; i < MDI.length; i++){
        if(!MDI[i] || !PDI[i]){
            continue
        }
        var dxValue = Math.abs((PDI[i] - MDI[i])) / (PDI[i] + MDI[i]) * 100
        var obj = {
            Close : dxValue,
        }
        dx.push(obj)
    }

    if(dx.length < adx_period){
        return false
    }

    var adx = talib.SMA(dx, adx_period)

    return adx
}

function DMI(records, pdi_period, mdi_period, adxr_period, adx_period) {    
    var recordsHLC = []
    for(var i = 0; i < records.length ; i++){
        var bar = {
            High : records[i].High,
            Low : records[i].Low, 
            Close : records[i].Close,
        }
        recordsHLC.push(bar)
    }
    
    var m_di = talib.MINUS_DI(recordsHLC, mdi_period) 
    var p_di = talib.PLUS_DI(recordsHLC, pdi_period)

    var adx = ADX(m_di, p_di, adx_period)
    
    // ADXR=(Of the DayADX+agonDay'sADX)÷2
    var n = 0
    var adxr = []
    for (var j = 0 ; j < adx.length; j++) {
        if (typeof(adx[j]) == "number") {
            n++
        }
        
        if (n >= adxr_period) {
            var currAdxr = (adx[j] + adx[j - adxr_period]) / 2
            adxr.push(currAdxr)
        } else {
            adxr.push(NaN)
        }
    }
    
    return [m_di, p_di, adx, adxr]
}



function main() {
    while(1){
        var records = exchange.GetRecords()
        var ret = DMI(records, 14, 14, 14, 14)
        var preTime = records[records.length - 2].Time
        $.PlotRecords(records, "K")
        $.PlotLine("m_di", ret[0][ret[0].length - 2], preTime)
        $.PlotLine("p_di", ret[1][ret[1].length - 2], preTime)
        $.PlotLine("adx", ret[2][ret[2].length - 2], preTime)
        $.PlotLine("adxr", ret[3][ret[3].length - 2], preTime)
        
        Sleep(1000)
    }
}

Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2019-07-03. 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/DMI-Test-Example.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