ग्रिड रणनीति का नया संस्करण केवल 50 पंक्तियों का कोड है!

लेखक:अच्छाई, बनाया गयाः 2018-08-28 14:18:15, अद्यतन किया गयाः 2019-12-03 17:27:50

ग्रिड रणनीति का नया संस्करण केवल 50 पंक्तियों का कोड है!www.fmz.com

क्या आपको अभी भी प्रसिद्ध ग्रिड रणनीति याद है (https://www.fmz.com/bbs-topic/2268)?

एक जिसमें बहुत सी कोड टिप्पणी है (कोड से अधिक), हमारे एफएमजेड प्लेटफॉर्म पर रणनीति लिखने के तरीके सीखने के लिए एक उत्कृष्ट उदाहरण है।

कुछ मित्रों ने पाया कि इस रणनीति का मूल संस्करण बहुत जटिल है, वे आश्चर्य करते हैं कि क्या इसका छोटा संस्करण है।

यहाँ यह आता है, हमारे प्रतिभाशाली प्रोग्रामर की अनन्य कोडिंग कौशल के तहत, हम इसे फिर से लिखते हैं (अभी भी जावास्क्रिप्ट का उपयोग करते हुए) केवल 50 लाइनों के कोड का उपयोग करके। मूल एक 700 से अधिक कोड लाइनों है।

यह आपके लिए यह पता लगाने का भी एक अच्छा अवसर है कि हमारा प्लेटफॉर्म कैसे विकसित हुआ है, कैसे जटिल कोडिंग प्रगति को मूल उद्देश्य के किसी भी कार्य को खोए बिना केवल दर्जनों कोडिंग वाक्यों में संकुचित किया जा सकता है।

यहाँ कोड के मूल 700+ पंक्तियाँ हैंःhttps://www.fmz.com/bbs-topic/2268

var _StopLoss = 0
var _StopWin = 0
var _Grid = []

function UpdateGrid(nowBidsPrice, nowAsksPrice, direction){    // up 1, down -1
    if(_Grid.length == 0 || (direction == 1 && nowBidsPrice - _Grid[_Grid.length - 1].price > _GridPointDis) || 
        (direction == -1 && _Grid[_Grid.length - 1].price - nowAsksPrice > _GridPointDis)){

        var nowPrice = direction == 1 ? nowBidsPrice : nowAsksPrice
        _Grid.push({
            price: _Grid.length == 0 ? nowPrice : _Grid[_Grid.length - 1].price + _GridPointDis * direction,
            hold : {price: 0, amount: 0}, 
            coverPrice : _Grid.length == 0 ? nowPrice - direction * _GridCovDis : _Grid[_Grid.length - 1].price + _GridPointDis * direction - direction * _GridCovDis
        })

        var tradeInfo = direction == 1 ? $.Sell(_GridPointAmount) : $.Buy(_GridPointAmount)
        _Grid[_Grid.length - 1].hold.price = tradeInfo.price
        _Grid[_Grid.length - 1].hold.amount = tradeInfo.amount
        $.PlotFlag(new Date().getTime(), JSON.stringify(tradeInfo), "O")
    }
    if(_Grid.length > 0 && 
        ((direction == 1 && nowAsksPrice < _Grid[_Grid.length - 1].coverPrice) || (direction == -1 && nowBidsPrice > _Grid[_Grid.length - 1].coverPrice))){
        
        var coverInfo = direction == 1 ? $.Buy(_Grid[_Grid.length - 1].hold.amount) : $.Sell(_Grid[_Grid.length - 1].hold.amount)
        _Grid.pop()
        $.PlotFlag(new Date().getTime(), JSON.stringify(coverInfo), "C")
        _StopWin++
    } else if(_Grid.length > _GridNum){
        var coverfirstInfo = direction == 1 ? $.Buy(_Grid[0].hold.amount) : $.Sell(_Grid[0].hold.amount)
        _Grid.shift()
        $.PlotFlag(new Date().getTime(), JSON.stringify(coverfirstInfo), "C")
        _StopLoss++
    }

}

function main(){
    while(1){
        var ticker = _C(exchange.GetTicker)
        var records = _C(exchange.GetRecords)
        $.PlotRecords(records, "kline")
        UpdateGrid(ticker.Buy, ticker.Sell, direction)       
        var msg = ""
        for(var i = 0; i < _Grid.length; i++){
            msg += JSON.stringify(_Grid[i]) + "\n"
        }
        LogStatus(_D(), "_StopWin:", _StopWin, "_StopLoss:", _StopLoss, _C(exchange.GetAccount), "\n", "_Grid.length:", _Grid.length, "_GridNum:", _GridNum, "\n", msg)
        Sleep(500)
    }
}

अधिक

छोटे सपनेबहुत बढ़िया!