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

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

xxs1xxs1 老大,我把你的小修改了一下,收益看起来很好。 你给点建议呗。风险与优化 https://www.fmz.com/m/edit-strategy/359761

天降财神 小梦老师~你这测试的教学策略 是多少倍的??

天降财神 开始盘 没反应呢~~~

天降财神 /upload/asset/2152afd2a78355e8ce25b.png

天降财神 Buy(-1, 5): 400: {"code":-2019,"msg":"Margin is insufficient."} 什么意思啊~一分钟一次

天降财神 小梦老师~可否加您微信 请给我私聊

天降财神 这个策略可以跑实盘吗??小梦老师~可否改成比本位?

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

13826543292 谢谢大神,非常厉害

小小梦 好的,这个研究研究。

xxs1xxs1 老大,能出个锁仓解套的策略吗?或者思路也行。。 咱们这个策略直接止损太爆力了。想加上锁仓,等待下一次行情

小小梦 好的,今后平台会分享更多策略,感谢支持FMZ。

xxs1xxs1 只能说老大你才是真的666 我这个只是用了复利优势。你去测试EOS试试。就今年至现在的。10000% 我在去研究研究看看可能实盘不。目前这个实盘估计是不够支付手续费的。可以试试去做币安的BUSD合约。好像挂单是负率的?

小小梦 就是账户资产不足下单。

小小梦 一般回测的时候杠杆是10倍的。

小小梦 X.X 额,我也说不准。。。 不过话说你的这个优化版 666 , 夏普真么高 /upload/asset/16ab6c51ed93be64d383.png

天降财神 是U本位合约吧吧??不是杠杆吧??

小小梦 您的账户,保证金不足,不够下单了。

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