50行网格策略(教学)

教学 网格
创建日期: 2018-08-24 16:32:24 最后修改: 2018-08-25 18:19:22
复制: 604 点击次数: 12503
4
关注
1111
关注者
策略源码
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)
    }
}
相关推荐
更多内容
全部留言
avatar of 326538268
326538268
bitmex回测显示没设置合约
2021-06-20 16:59:33
avatar of wufuhao100w
wufuhao100w
大神有空写个python版的吗?我小白好多看不懂,
2019-11-23 02:57:32
avatar of afanxingzhou
afanxingzhou
提问:期货软件平仓时,得选择某个开仓,平掉该仓。我看代码里在平仓的时候,操作和开仓一样的,只是新开了一个和之前相反的仓位。所以就比较纠结,希望小梦老大有时间了解答下:)
2019-03-04 16:47:49
avatar of 发明者量化-小小梦
发明者量化-小小梦
这个策略是个现货的,BITMEX是期货交易所。
2021-06-21 09:05:29
avatar of 发明者量化-小小梦
发明者量化-小小梦
好的。
2019-11-23 11:27:56
avatar of afanxingzhou
afanxingzhou
好的,我再看下期货相关的:)
2019-03-05 10:06:06
avatar of 发明者量化-小小梦
发明者量化-小小梦
哦 这个策略仅仅是个现货版的,有兴趣可以改造成期货版的。 现货只有买卖。买了就是开多了,卖了就是 平多(如果之前有个对应的买入操作)或者开空。
2019-03-04 18:11:59