bybit swap perpetual leverage strategy

Author: gulishiduan_high frequency ordering, Date: 2020-05-07 22:20:01
Tags:

// Recently a friend has reacted with a small bug, first test the net. The parameters can be freely adjusted as needed. The essence of the strategy is to track the coupon price of the k-line to determine how much space, simply by the inversion of the mean line, real-time detection of the signal. // to register a new account, please use my registration link:https://www.bytick.com/zh-CN/register/?affiliate_id=7586&language=en&group_id=0&group_type=2// The link provides a number of strategies for linking third parties.// // basic principle: if the k-line continues upwards, it will continue to increase until the maximum position.//

// If long: Not suitable for the short position market, but not a continuous increase in the falling position.// // If short: not suitable for multi-head markets, but not a continuous increase in the upside.

// Pay attention, you can also open two accounts with more space.

// Other purchasing strategies please consult: weixin:ying5737 // need to pair your own exchanges./ simulated account pre-test.

// the daylight level, or the circumference level, we'll use daylight as an example. // Detecting ma5ma10, k-line closing price above ma5, ma10, and ma5 up (judging as yesterday's k-line closing price > previous 5th root k closing price), then open a daily hanging order or directly buy 500u, continue up, continue to raise the position. // If two negative lines appear in the uptrend, then buy 500u on the third day. Each negative line is counted separately.

// Sold, k-line three-linked down stock 1000u。(or k-line four-linked down stock 2000u) // It's been going round and round. // Policy runs, 13 days ((or 21 days), automatically stops, breaks or clears positions and orders. // Maximum holding 5000u larger than the position only down position.

The picture above:

https://wx1.sinaimg.cn/mw1024/c5775633ly1gbsjvtrgnhj20m80dmmxy.jpg https://wx1.sinaimg.cn/mw1024/c5775633ly1gbsjvty48uj21hc0u077o.jpg https://wx2.sinaimg.cn/mw1024/c5775633ly1gbsjvu4iipj20lr0h775f.jpg

The unilateral trend strategy of the medium frequency

Monitoring of variables

  1. Quickly MA
  2. The slow MA
  3. The closing price

Configuration parameters

  1. Amount to be downloaded once
  2. One-time depreciation CloseAmount
  3. Maximum holdings MaxPosition

Do more

Requirements

  1. The k-line closing price is greater than the fast MA and the slow MA
  2. and fast MA up (judging that yesterday's k-line closing price is greater than the previous 5th k-line closing price)

List below

  1. Three Liansang, reduced stock CloseAmount
  2. Two negative sequences, adding Amount. That is, two negative sequences will be subtracted from 2*Amount.
  3. As usual, open Amount.

Restrictions

  1. Maximum holdings greater than MaxPosition will not be opened

Withdrawal

  1. Exit after running the N-Root K line

Doing nothing

Requirements

  1. The k-line closing price is less than the fast MA and the slow MA
  2. and fast MA upwards (judging that yesterday's k-line closing price is less than the previous number N (fast MA cycle) root k closing price)

List below

  1. Three-point lead, down stock Close Amount
  2. Two consecutive moons, adding Amount. That is, two consecutive moons will add 2*Amount.
  3. As usual, open Amount.

Restrictions

  1. Maximum holdings greater than MaxPosition will not be opened

Withdrawal

  1. Exit after running the N-Root K line

Please note:

  1. Each time the program gets the account's holdings information, the holdings are used as a strategy.
  2. Please bind fmz WeChat, the program will push WeChat where it matters

Parameters

  1. Fast MA cycle
  2. Slow MA cycle
  3. Intervals between calls (ms)
  4. Multiple options
  5. Leverage size: 0 for full-load mode
  6. Contract type: Currently fmex only supports swaps, only swaps can be filled. When retesting, OKEx retesting is available, which can be set to this_week, this_month etc.
  7. One-time decrease in stock. One-time decrease in stock when the decrease condition is reached
  8. Maximum holdings (u)
  9. The API base address; can be set to https://api.fmex.com, or https://api.testnet.fmex.com
  10. The number of K-lines that the policy exits. The number of K-lines that the policy exits.
  11. The strategy is to take the initiative to exit or to liquidate.
  12. Whether interaction is required. The policy will exit normally after the exit condition is met. If interaction is required, it will wait for commands such as manual intervention. If not, the program will exit directly.
  13. Whether to buy order. Select, then the order is market price list, not selected, then the listing, the order is hanging on buy one, sell order hangs on sell one.
  14. Continuous sun ((阴) K line number ((do more, continuous sun line)); decrease signals, such as do more, continuous sun line, decrease
  15. The number of consecutive K-strings (multiple, continuous) ⇒ the number of consecutive K-strings (multiple, continuous)
  16. I'm not sure if it's a shocking market.

Interacting

The interaction is only是否需要交互Effective when Interaction occurs when a strategy is out of order.

  1. Continue. Continue is a policy reset, running the same parameters again.
  2. Stop. Stop withdrawal policy
  3. The switching strategy continues after the market. The switching market continues to operate after a shock or trend, which is an extension of the interaction of the one-way continuous swing.


/*联系微信:ying5737(策略讨论,支持付费编写)
# 中频单边趋势策略
## 监测变量
1. 快MA
2. 慢MA
3. 收盘价
## 配置参数
1. 单次下单量Amount
2. 单次减仓量CloseAmount
3. 最大持仓量MaxPosition
## 做多
### 必要条件
1. k线收盘价大于快MA与慢MA
2. 且快MA上行(判断为昨日k线收盘价大于往前数第5根k收盘价)
### 下单
1. 三连阳,减仓CloseAmount
2. 两连阴,加仓Amount。也就是出现两连阴会下单2*Amount
3. 正常情况,开单Amount
### 限制
1. 最大持仓量大于MaxPosition则不开单
### 退出
1. 运行N根K线之后退出

## 做空
### 必要条件
1. k线收盘价小于快MA与慢MA
2. 且快MA下行(判断为昨日k线收盘价小于往前数第N(快MA5周期)根k收盘价)
### 下单
1. 三连阴,减仓CloseAmount
2. 两连阳,加仓Amount。也就是出现两连阴会下单2*Amount
3. 正常情况,开单Amount
### 限制
1. 最大持仓量大于MaxPosition则不开单
### 退出
1. 运行N根K线之后退出
## 注意事项
1. 程序会每次会获取账户的持仓信息做为策略的持仓量
2. 请绑定fmz微信,程序会重要的地方推送微信
## 参数
1. 快MA周期
2. 慢MA周期	
3. 轮询间隔(ms)	
4. 多空选择
5. 杠杆大小: 0表示全仓模式
6. 合约类型: 目前fmex只支持swap,只能填写swap。回测时可用OKEx回测,此处可设置为this_week, this_month等
7. 单次减仓量。达到减仓条件时,一次减仓量
8. 最大持仓(u)
9. API基地址。可设置为https://api.fmex.com,或测试网https://api.testnet.fmex.com
10. 策略K数退出. 策略运行多少个K线后正常退出
11. 策略主动退出时是否清仓。
12. 是否需要交互。策略在满足退出条件后,正常退出时。如果需要交互,则会等待人工干预等命令。如果不需要,则程序直接退出了
13. 是否吃单。勾选,则下单是市价单,不勾选,则是挂单,买单挂在买一,卖单挂在卖一
14. 连续阳(阴)K线数(做多时,连续阳线)。减仓信号,如做多时,连续阳线,减仓
15. 连续阴(阳)K线数(做多时,连续阴线)。连续阴(阳)K线数(做多时,连续阴线)	
16. 是否是震荡行情。勾选是震荡行情
## 交互
**交互只有在`是否需要交互`时有效**
**交互是在策略正常退出时进行交互**
1. 继续。继续是策略复位,重新运行相同的参数
2. 停止。策略停止退出
3. 切换策略行情后继续.切换行情为震荡或趋势后继续运行,是交互1'继续'的一种扩展
*/
////////////////// params ////////////////////////
//var fastMaPeriod = 5
//var slowMaPeriod = 10
//var direction = 做多|做空
//var interval = 1000
//var amount = 500
//var maxHoldAmount = 5000
//var closeAmount = 1000
//var runNBars = 13
//var marginLevel = 0
//var contractType = 'swap'
//var enableCommand = false
//var isTaker = true
//var maxOppositeDirKNum = 2
//var maxSameDirKNum = 3
//var isShock = false
////////////////// variable ////////////////////////

var makeLong = direction == 0 ? true:false
var startTime = null
var holdAmount = 0
var lastBar = null
var yinxianCnt = 0
var yangxianCnt = 0
var lastClose = 0
var last5thClose = 0
var fastMa = []
var slowMa = []
var barCnt = 0
var localIsShock = false
////////////////////////////////////////////////
var PreBarTime = 0
var isFirst = true

function PlotMA_Kline(records){
    $.PlotRecords(records, 'K')
    if(fastMa.length == 0) {
        fastMa = TA.MA(records, fastMaPeriod)
    }
    if(slowMa.length == 0) {
        slowMa = TA.MA(records, slowMaPeriod)
    }
    if(isFirst){
        $.PlotFlag(records[records.length - 1].Time, 'Start', 'STR')
        for(var i = records.length - 1; i >= 0; i--){
            if(fastMa[i] !== null){
                $.PlotLine('ma'+fastMaPeriod, fastMa[i], records[i].Time)
            }
            if(slowMa[i] !== null){
                $.PlotLine('ma'+slowMaPeriod, slowMa[i], records[i].Time)
            }
        }
        PreBarTime = records[records.length - 1].Time
        isFirst = false
    } else {
        if(PreBarTime !== records[records.length - 1].Time){
            $.PlotLine('ma'+fastMaPeriod, fastMa[fastMa.length - 2], records[fastMa.length - 2].Time)
            $.PlotLine('ma'+slowMaPeriod, slowMa[slowMa.length - 2], records[slowMa.length - 2].Time)
            PreBarTime = records[records.length - 1].Time
        }
        $.PlotLine('ma'+fastMaPeriod, fastMa[fastMa.length - 1], records[fastMa.length - 1].Time)
        $.PlotLine('ma'+slowMaPeriod, slowMa[slowMa.length - 1], records[slowMa.length - 1].Time)
}
}

function init () {
    if (fastMaPeriod > slowMaPeriod) {
        throw '快MA周期 > 慢MA周期, 请检查设置'
    }
    Log('快MA周期	    :'  + fastMaPeriod)
    Log('慢MA周期	    :' + slowMaPeriod)
    Log('轮询间隔(ms)   :' + interval)
    Log('是否是震荡策略  :' + (isShock?'是':'否'))
    Log('多空选择	    :' + (direction == 0 ? '多':'空'))
    Log('杠杆大小	    :' + (marginLevel == 0 ? '全仓':marginLevel))
    Log('连续阳(阴)K线数(做多时,连续阳线)数   :' + maxSameDirKNum)
    Log('连续阴(阳)K线数(做多时,连续阴线)   :' + maxOppositeDirKNum)
    Log('运行多少根K后退出   :' + runNBars)
    startTime = new Date()
    localIsShock = isShock
}

function onexit() {
    Log('退出')
}

function onerror() {
    Log('出错退出')
}

function openLong(ex, openAmount) {
    if (holdAmount + openAmount <= maxHoldAmount) {
        Log('已持仓: ' + holdAmount + ', 加仓:' + openAmount)
        ex.SetDirection('buy')
        if(isTaker) {
            ex.Buy(-1, openAmount, '吃单')
            holdAmount += openAmount
        } else {
            var ticker = _C(ex.GetTicker)
            if(ticker == null) {
                return false
            }
            ex.Buy(ticker.Buy, openAmount, '挂单')
        }
        return true
    } else {
        Log('持仓('+holdAmount+') 过多,不加仓')
        return false
    }
}

function closeLong(ex, closeAmount) {
    if (holdAmount >= closeAmount) {
        Log('已持仓: ' + holdAmount + ', 减仓:' + closeAmount)
        ex.SetDirection('closebuy')
        ex.Sell(-1, closeAmount)
        holdAmount -= closeAmount
        return true
    } else {
        Log('持仓('+holdAmount+') 过少,不减仓')
        return false
    }
}

function openShort(ex, openAmount) {
    if (holdAmount + openAmount <= maxHoldAmount) {
        Log('已持仓: ' + holdAmount + ', 加仓:' + openAmount)
        ex.SetDirection('sell')
        if(isTaker) {
            ex.Sell(-1, openAmount, '吃单')
            holdAmount += openAmount
        } else {
            var ticker = _C(ex.GetTicker)
            if(ticker == null) {
                return false
            }
            ex.Sell(ticker.Sell, openAmount, '挂单')
        }
        return true
    } else {
        Log('持仓('+holdAmount+') 过多,不加仓')
        return false
    }
}

function closeShort(ex, closeAmount) {
    if (holdAmount >= closeAmount) {
        Log('已持仓: ' + holdAmount + ', 减仓:' + closeAmount)
        ex.SetDirection('closesell')
        ex.Buy(-1, closeAmount)
        holdAmount -= closeAmount
        return true
    } else {
        Log('持仓('+holdAmount+') 过少,不减仓')
        return false
    }
}

function cancelOrders(ex) {
    Log('取消所有挂单')
    while(true) {
        var orders = _C(ex.GetOrders)
        if (orders.length == 0) {
            break
        }
        for(var i = 0; i < orders.length;i++) {
            ex.CancelOrder(orders[i].Id)
        }
    }
}

function updatePosition(ex) {
    var pos = ex.GetPosition()
    if(typeof(pos) === 'undefined' || pos === null || 
        pos.length == 0 || typeof(pos[0].Type) == 'undefined'  || typeof(pos[0].Amount) == 'undefined' ) {
        return
    }
    Log('仓位信息:' + (pos[0].Type == 0?'多仓,   ':'空仓,  ') + JSON.stringify(pos))
    if(pos.length>0){
        holdAmount = pos[0].Amount
        // if(pos[0].Type == 0){ //多仓
        //     ordersInfo.pos = pos[0].Amount
        // }else{
        //     ordersInfo.pos = -pos[0].Amount
        // }
    }
}

function longStrategy(ex, records) {
    var lastSecondBar = records[records.length-2]

    if ((   lastSecondBar.Close > fastMa[fastMa.length - 2] && 
            lastSecondBar.Close > slowMa[slowMa.length - 2] && 
            lastSecondBar.Close > records[records.length - 2 - fastMaPeriod].Close
        ) || localIsShock){
            var openAmount = amount
            if (lastSecondBar.Close < lastSecondBar.Open) {
                yinxianCnt += 1
                yangxianCnt = 0
            } else if (lastSecondBar.Close > lastSecondBar.Open){
                yinxianCnt = 0
                yangxianCnt += 1
            } else {
                yangxianCnt = 0
                yinxianCnt = 0
            }

            if (yinxianCnt >= maxOppositeDirKNum) {
                Log('两连阴')
                openAmount += amount
                yinxianCnt = 0
            }

            if (yangxianCnt >= maxSameDirKNum) {
                Log('三连阳')
                yangxianCnt = 0
                Log('准备减仓')
                if(closeLong(ex, closeAmount)){
                    $.PlotFlag(records[records.length - 1].Time, 'closeLong', 'CL')
                }
            } else {
                Log('准备开仓')
                if(localIsShock) {
                    openAmount -= amount
                }
                if(openLong(ex, openAmount)){
                    $.PlotFlag(records[records.length - 1].Time, 'openLong', 'OL')
                }
            }
    }
}

function shortStrategy(ex, records) {
    var lastSecondBar = records[records.length-2]

    if ((   lastSecondBar.Close < fastMa[fastMa.length - 2] && 
            lastSecondBar.Close < slowMa[slowMa.length - 2] && 
            lastSecondBar.Close < records[records.length - 2 - fastMaPeriod].Close
        ) || localIsShock){
            var openAmount = amount
            if (lastSecondBar.Close < lastSecondBar.Open) {
                yinxianCnt += 1
                yangxianCnt = 0
            } else if (lastSecondBar.Close > lastSecondBar.Open){
                yinxianCnt = 0
                yangxianCnt += 1
            } else {
                yangxianCnt = 0
                yinxianCnt = 0
            }

            if (yangxianCnt >= maxOppositeDirKNum) {
                Log('两连阳')
                yangxianCnt = 0
                openAmount += amount
            } 

            if (yinxianCnt >= maxSameDirKNum) {
                Log('三连阴')
                yinxianCnt = 0
                Log('准备减仓')
                if(closeShort(ex, closeAmount)){
                    $.PlotFlag(records[records.length - 1].Time, 'closeShort', 'CS')
                }
            } else {
                Log('准备开仓')
                if(localIsShock) {
                    openAmount -= amount
                }
                if(openShort(ex, openAmount)){
                    $.PlotFlag(records[records.length - 1].Time, 'openShort', 'OS')
                }
            }
    }
}

function onBar (ex) {
    var records = _C(ex.GetRecords)
    if (records === null || records.length < slowMaPeriod) {
        return 
    }
    if (lastBar == null) {
        lastBar = records[records.length-1]
    }
    
    if (lastBar.Time == records[records.length-1].Time) {
        return
    }
    lastBar = records[records.length-1]
    updatePosition(ex)
    PlotMA_Kline(records)
    barCnt += 1

    var lastSecondBar = records[records.length-2]
    fastMa = TA.MA(records, fastMaPeriod)
    slowMa = TA.MA(records, slowMaPeriod)
    lastClose = lastSecondBar.Close
    last5thClose = records[records.length - 2 - 5].Close

    Log('收盘价:' +lastSecondBar.Close + 
    ', 前第5个收盘价:' +records[records.length - 2 - 5].Close + 
    ', 快MA:' + _N(fastMa[fastMa.length - 2]) +
    ', 慢MA:' + _N(slowMa[slowMa.length - 2]))
    if (makeLong) {
        longStrategy(ex, records)
    } else {
        shortStrategy(ex, records)
    }
}

function runLife(ex) {
    // var pass = new Date() - startTime
    if (barCnt >= runNBars) {
        if(isCleanPosition) {
            Log('已运行'+barCnt+'K周期,结束,取消订单,清仓#ff0000@')
            cancelOrders(ex)
            updatePosition(ex)
            $.PlotFlag(lastBar.Time, 'Exit', 'EXT')
            
            if (makeLong) {
                closeLong(ex, holdAmount)
            } else {
                closeShort(ex, holdAmount)
            }    
        } else {
            Log('已运行'+barCnt+'K周期,结束,不取消订单,不清仓#ff0000@')
            updatePosition(ex)
            $.PlotFlag(lastBar.Time, 'Exit', 'EXT')
        }
        return true
    } else {
        return false
    }
}

function status() {
    var table = {
        type: 'table',
        title: '信息',
        cols: [
            '运行K数',
            '持仓量',
            '阳线',
            '阴线',
            '收盘价',
            '前第5个收盘价',
            'MA'+fastMaPeriod,
            'MA'+slowMaPeriod,
        ],
        rows: []
      }
      table.rows.push([
            barCnt,
            holdAmount,
            yangxianCnt,
            yinxianCnt,
            lastClose,
            last5thClose,
            fastMa.length == 0 ? 0 : _N(fastMa[fastMa.length - 2]),
            slowMa.length == 0 ? 0 : _N(slowMa[slowMa.length - 2])
      ])
    LogStatus(
        '现在时间:' +_D() +
        '\n启动时间:' +startTime +
        '\n`' +
        JSON.stringify(table)+
        '`\n' +
        '\n托管者版本:' +Version() +
        '\n联系Wechat:ying5737#00ff00' +
        '\nWechat: ying5737info#ff000f'
      )

}

function reset() {
    holdAmount = 0
    lastBar = null
    yinxianCnt = 0
    yangxianCnt = 0
    lastClose = 0
    last5thClose = 0
    fastMa = []
    slowMa = []
    barCnt = 0
}

function main () {
    var ex = exchanges[0]

    Log('开工   '+ex.GetName())
 //   if(ex.GetName() != 'Futures_FMex' && !IsVirtual()) {
  //      throw '仅仅支持FMex'
  //  }
    Log('基地址  ' + baseUrl)
    if(!IsVirtual()){
        ex.IO('base', baseUrl) //切换基地址,方便切换实盘和模拟盘,实盘地址:https://api.fmex.com
    }
    ex.SetTimeout(1000);
    _CDelay(500)
    ex.SetContractType(contractType)
    ex.SetMarginLevel(marginLevel)
    updatePosition(ex)
    while (true) {
        try {
            if(!IsVirtual() && runLife(ex)) {
                if((typeof(GetCommand) == 'function') && enableCommand){
                    Log('等待指令, 继续 | 停止 #ff0000@')
                    while (true) {
                        var cmd = GetCommand()
                        if (cmd) {
                            Log('收到指令: '+cmd)
                            switch(cmd) {
                                case '停止':
                                    Log('停止, 退出!#ff0000@')
                                    return
                                case '继续':
                                    reset()
                                    Log('继续, 复位,开工!#ff0000@')
                                    break
                                case '切换策略行情:0':
                                    reset()
                                    localIsShock = true
                                    Log('切换策略行情为震荡行情继续, 复位,开工!#ff0000@')
                                    break
                                case '切换策略行情:1':
                                    reset()
                                    localIsShock = false
                                    Log('切换策略行情为趋势行情继续, 复位,开工!#ff0000@')
                                    break
                            }
                            if (cmd == '停止'){
                                Log('停止, 退出!#ff0000@')
                                return
                            } else if (cmd == '继续') {
                                reset()
                                Log('继续, 复位,开工!#ff0000@')
                                break
                            }
                        }
                        updatePosition(ex)
                        status()
                        Sleep(1000)
                    }
                } else {
                    Log('停止, 退出!#ff0000@')
                    return
                }
            }
            onBar(ex)
            status()
        } catch(e) {
            Log('出错了:'+e+', 请及时处理#ff0000@')
        }
        Sleep(interval)
    }
}


More

gulishiduan_high frequency orderingThanks to the support of Eric from Wuhan, the idea of monetizing IQ is a great idea.

gulishiduan_high frequency ordering 沟通//或其他策略购买请咨询:weixin:ying5737

gulishiduan_high frequency orderingSome common optimization strategies include: 1, position management: position management with open positions/percentage of total capital is better suited for quantitative use, especially in some high frequency/mover/medium long line systems. 2, the principle of the bull market model is that, based on the currency: leverage; strategy, after the return reaches 20%, it will start what is known as the bull market model. That is, on the basis of 20% profit, to increase the speed of trading, increase the volume of orders, increase the potential earnings, of course, the potential risk, in the profit of more than 20% bull market model will also be amplified. It is recommended to replenish your capital every month if it is profitable, there are many potential risks in the market, even the risk of the exchange, etc. If it is not a complete set of strategies that you have developed yourself, then consider: monthly replenishment is a very good mechanism. 4, timing: The timing strategy of the robot is better than the manual or subjective summarized trading strategy. Many friends develop a strategy without timing parameters, leading to some strategies used against market trends or market conditions, to use the strategy, rarely to choose the time and/or by the volatility rate to consider whether to trade, not to do the strategy pause and stop, which is a misunderstanding with the blind zone. The blind zone is an opportunity.