Loading ...

数字货币期货双均线拐点策略(教学)

Author: 小小梦, Date: 2021-12-03 11:25:28
Tags:

相关文章:https://www.fmz.com/bbs-topic/8479


/*backtest
start: 2021-09-01 00:00:00
end: 2021-12-02 00:00:00
period: 1h
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/

var LONG = 1 
var SHORT = -1
var IDLE = 0

function getPosition(positions, direction) {
    var ret = {Price : 0, Amount : 0, Type : ""}
    _.each(positions, function(pos) {
        if (pos.Type == direction) {
            ret = pos
        }
    })
    return ret 
}

function cancellAll() {
    while (true) {
        var orders = _C(exchange.GetOrders)
        if (orders.length == 0) {
            break
        } else {
            for (var i = 0 ; i < orders.length ; i++) {
                exchange.CancelOrder(orders[i].Id, orders[i])
                Sleep(500)
            }
        }
        Sleep(500)
    }
}

function cover(tradeFunc, direction) {
    var mapDirection = {"closebuy": PD_LONG, "closesell": PD_SHORT}
    var positions = _C(exchange.GetPosition)
    var pos = getPosition(positions, mapDirection[direction])
    if (pos.Amount > 0) {
        cancellAll()
        exchange.SetDirection(direction)
        if (tradeFunc(-1, pos.Amount)) {
            return true 
        } else {
            return false 
        }
    }
    return true 
}

function main() {
    if (okexSimulate) {
        exchange.IO("simulate", true) // 切换到OKEX V5模拟盘测试 
        Log("切换到OKEX V5模拟盘")
    }    
    exchange.SetContractType(ct)
    var state = IDLE
    var holdPrice = 0
    var preTime = 0

    while (true) {
        var r = _C(exchange.GetRecords)
        var l = r.length
        if (l < Math.max(ema1Period, ema2Period)) {
            Sleep(1000)
            continue
        }
        var ema1 = TA.EMA(r, ema1Period)
        var ema2 = TA.EMA(r, ema2Period)
        
        // 画图
        $.PlotRecords(r, 'K线')
        if(preTime !== r[l - 1].Time){
            $.PlotLine('ema1', ema1[l - 2], r[l - 2].Time)
            $.PlotLine('ema2', ema2[l - 2], r[l - 2].Time)
            
            $.PlotLine('ema1', ema1[l - 1], r[l - 1].Time)
            $.PlotLine('ema2', ema2[l - 1], r[l - 1].Time)
            preTime = r[l - 1].Time
        } else {
            $.PlotLine('ema1', ema1[l - 1], r[l - 1].Time)
            $.PlotLine('ema2', ema2[l - 1], r[l - 1].Time)
        }
        
        var up = (ema1[l - 2] > ema1[l - 3] && ema1[l - 4] > ema1[l - 3]) && (ema2[l - 2] > ema2[l - 3] && ema2[l - 4] > ema2[l - 3])
        var down = (ema1[l - 2] < ema1[l - 3] && ema1[l - 4] < ema1[l - 3]) && (ema2[l - 2] < ema2[l - 3] && ema2[l - 4] < ema2[l - 3])
        if (up && (state == SHORT || state == IDLE)) {
            if (state == SHORT && cover(exchange.Buy, "closesell")) {
                state = IDLE
                holdPrice = 0
                $.PlotFlag(r[l - 1].Time, 'coverShort', 'CS')
            }
            exchange.SetDirection("buy")
            if (exchange.Buy(-1, amount)) {
                state = LONG
                holdPrice = r[l - 1].Close
                $.PlotFlag(r[l - 1].Time, 'openLong', 'L')
            }
        } else if (down && (state == LONG || state == IDLE)) {
            if (state == LONG && cover(exchange.Sell, "closebuy")) {
                state = IDLE
                holdPrice = 0
                $.PlotFlag(r[l - 1].Time, 'coverLong', 'CL')
            }
            exchange.SetDirection("sell")
            if (exchange.Sell(-1, amount)) {
                state = SHORT
                holdPrice = r[l - 1].Close
                $.PlotFlag(r[l - 1].Time, 'openShort', 'S')
            }
        }
        
        // 止盈
        if (state == LONG && r[l - 1].Close - holdPrice > profitTarget && cover(exchange.Sell, "closebuy")) {            
            state = IDLE
            holdPrice = 0
            $.PlotFlag(r[l - 1].Time, 'coverLong', 'CL')
        } else if (state == SHORT && holdPrice - r[l - 1].Close > profitTarget && cover(exchange.Buy, "closesell")) {            
            state = IDLE
            holdPrice = 0
            $.PlotFlag(r[l - 1].Time, 'coverShort', 'CS')
        }
        LogStatus(_D())
        Sleep(500)        
    }
}

More

cylk9 macd 交叉向上买入向下卖出同时开反手单,不止赢,只止损怎么写

13826543292 谢谢大神,非常厉害

逍遥量化 有联系方式吗 有事请教 大神

小小梦 根据需求编写代码。可以到策略广场找些例子参考。

小小梦 可以。

a9801102191 梦神,这个策略可以跑BUSD币兑吗

小小梦 您可以在FMZ.COM首页,点击电报加群链接,加电报群。