Phiên bản mới của "chiến lược lưới" chỉ chứa 50 dòng mã hóa!

Tác giả:Tốt, Tạo: 2018-08-28 14:18:15, Cập nhật: 2019-12-03 17:27:50

Phiên bản mới của "chiến lược lưới" chỉ chứa 50 dòng mã hóa!www.fmz.com

Bạn có còn nhớ chiến lược grid nổi tiếng (https://www.fmz.com/bbs-topic/2268)?

Một trong những có rất nhiều bình luận mã (hơn mã chính nó), là một ví dụ tuyệt vời để học cách viết chiến lược trên nền tảng FMZ của chúng tôi.

Một số người bạn thấy rằng phiên bản ban đầu của chiến lược này quá phức tạp, họ tự hỏi liệu có phiên bản ngắn hơn của nó hay không.

Đây là nó, dưới kỹ năng mã hóa độc quyền của lập trình viên thiên tài của chúng tôi, chúng tôi viết lại nó (vẫn sử dụng JavaScript) bằng cách chỉ sử dụng 50 dòng mã hóa.

Đây cũng là một cơ hội tốt cho bạn để tìm ra nền tảng của chúng tôi đã phát triển như thế nào, làm thế nào các tiến bộ mã hóa phức tạp có thể được nén thành chỉ hàng chục câu mã hóa mà không mất bất kỳ chức năng của dự định ban đầu.

Đây là 700 dòng mã gốc: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)
    }
}

Thêm nữa

Giấc mơ nhỏTốt lắm!