The resource loading... loading...

Digital currency futures with double-equal turning point strategies (Teaching)

Author: Inventors quantify - small dreams, Date: 2021-12-03 11:25:28
Tags:

Related articles: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

xxs1xxs1I've made some changes to your little one, and the results look good. You give me some advice. Risk and optimization. This is the first time I've seen this video.

Treasures from HeavenTeacher: How many times the teaching strategy of your test?

Treasures from HeavenI started playing and didn't respond.

Treasures from Heaven /upload/asset/2152afd2a78355e8ce25b.png

Treasures from HeavenBuy ((-1, 5): 400: {"code":-2019, "msg:"Margin is insufficient. "} What does that mean?

Treasures from HeavenTeacher Little Dream: Can you add your WeChat, please talk to me privately

Treasures from HeavenCan this strategy work on a real disk? Teacher: Can it be changed to a real one?

cylk9macd cross buy up buy down sell back and forth simultaneously, not just win, but stop loss how to write

13826543292Thank goodness, it's great.

Inventors quantify - small dreamsWell, this study study.

xxs1xxs1I'm not sure if this is a good idea, but I'm not sure if it's a good idea. We have a strategy of stopping the direct losses too quickly. We want to add a lock and wait for the next market.

Inventors quantify - small dreamsWell, we will share more strategies in the future, thanks for supporting FMZ.

xxs1xxs1All I can say is, "Boss, you're really 666". I'm just taking advantage of the bonus. You can try the EOS trial. I'm going to do some research to see if it's possible. The current estimate is not enough to pay the bills. I can try to do a BUSD contract in Yuan.

Inventors quantify - small dreamsThis is the reason why the account is underfunded.

Inventors quantify - small dreamsIn general, the leverage is 10 times greater when retesting.

Inventors quantify - small dreamsX. X amount, I also said not allowed. But speaking of your optimized version of 666, how tall is Sharp?

Treasures from HeavenIt's your contract, isn't it?

Inventors quantify - small dreamsYou have a bad account, not enough collateral, not enough money to pay.

Inventors quantify - small dreamsCode according to needs. You can find examples in Strategy Square.