Loading ...

后市看多网格(残血版)

Author: Alshyib, Date: 2021-10-12 09:51:27
Tags:

只做多网格(未完成版) 订单保证金就是首开,1U 就是1U保证金(因为我不知道怎么计算当前杠杆最小可开数量) 因为有自动获取所有交易对精度和数量,所以想要回测需要注释while函数下的symbol(); 加//就可以 补仓数量为根据持仓浮亏来动态计算补仓数量 可修改的参数目前只有止盈,止损为代码内1小时kdj死叉(有点问题) 后续版本会优化固定百分比止损,回撤止盈,错峰等待等 我已经没有什么好的想法了,与其穷思,不如一起参考,有好的想法可以加我

代码处于未完成状态,实盘盈亏自负,仅为互相学习参考 代码处于未完成状态,实盘盈亏自负,仅为互相学习参考 代码处于未完成状态,实盘盈亏自负,仅为互相学习参考





exchange.SetContractType("swap")
var account = _C(exchange.GetAccount) //帐户信息
var symbol_list = []
var accountTabledata = {}
var order_list = []
var Fristopen = 0
var num = 0 //记录补仓数量


exchange.SetContractType("swap")
var account = _C(exchange.GetAccount) //帐户信息
var symbol_list = []
var accountTabledata = {}
var order_list = []
var Fristopen = 0
var num = 0 //记录补仓数量
if (_G("symbol")) {
    symbol_list = _G("symbol")
    order_list = _G("orderlist")
    Log("上一次数据", symbol_list)
    Log("上一次数据完成数据", order_list)
}

// 撤单函数
function CancelPendingOrders() {
    Sleep(1000);
    var ret = false;
    while (true) {
        var orders = null;
        // 持续获取未成交订单数组,如果返回异常,则继续获取
        while (!(orders = exchange.GetOrders())) {
            Sleep(1000);
        }
        if (orders.length == 0) { // 如果订单数组为空
            return ret; // 返回撤单状态
        }
        for (var j = 0; j < orders.length; j++) { // 遍历未成交订单数组
            exchange.CancelOrder(orders[j].Id); // 依次取消未成交订单
            ret = true;
            if (j < (orders.length - 1)) {
                Sleep(1000);
            }
        }
    }
}

function symbols() {
    var symbol = JSON.parse(HttpQuery("https://www.binance.com/fapi/v1/exchangeInfo"))
    let symbol_all = []
    var symbolallmap = {}
    symbol.symbols.forEach(function (v) {
        if (v.quoteAsset == "USDT" && v.contractType == "PERPETUAL") {
            let str = v.symbol.split("USDT", 1)
            symbolallmap[str] = str
            symbolallmap[str + 'pricePrecision'] = v.pricePrecision
            symbolallmap[str + 'quantityPrecision'] = v.quantityPrecision

        }

    })
    var symbol_set = symbolarray.split(",");
    for (var sind = 0; sind < symbol_set.length; sind++) {
        symbol_all.push([symbol_set[sind] + "_USDT", symbolallmap[symbol_set[sind] + 'pricePrecision'], symbolallmap[symbol_set[sind] + 'quantityPrecision']])
    }

    for (let i = 0; i < symbol_all.length; i++) {
        if (symbol_list.length > 0) {
            let flag = false
            symbol_list.forEach(function (v) {
                if (v[0] == symbol_all[i][0]) {
                    flag = true;
                }
            })
            if (flag) {
                continue;
            }
        }
        exchange.SetCurrency(symbol_all[i][0])
        exchange.SetPrecision(symbol_all[i][1], symbol_all[i][2])
        Sleep(100)
    }

}


function main() {
    let loss = 2
    let loss_m = 0
    while (1) {
        symbols();
        exchange.SetMarginLevel(MarginLevel) //合约倍数
        let records = exchange.GetRecords(60 * 60 * 4)
        let kdj = TA.KDJ(records, 9, 3, 3)
        let account = exchange.GetAccount()
        let position = _C(exchange.GetPosition) //持仓信息
        let ticker = _C(exchange.GetTicker); // 获取 Tick 数据
        let money = bet * MarginLevel //买入数量
        let len = records.length - 1
        if (!position[0] && _Cross(kdj[0], kdj[1]) > 0 && kdj[2][len] > kdj[1][len] + 2) {
            exchange.SetDirection("buy")
            exchange.Buy(-1, _N(money / ticker.Sell), exchange.GetCurrency().replace("_USDT", ""), "开仓价格:", ticker.Sell)
            Fristopen = ticker.Sell
             Log("首次开仓补仓", Fristopen - (Fristopen * (Smallcover / 100)))

        } else if (position[0] && position[0].Profit > position[0].Margin * 0.2) { //盈利20%就清仓
            //     Log(loss_m,position[0])
            exchange.SetDirection("closebuy")
            exchange.Sell(-1, position[0].Amount, "止盈", ticker.Last, "盈利", position[0].Profit, "#ff0000")
            loss = 0.6
            num = 0 //计数清零
            CancelPendingOrders()
        } else if (position[0] && _Cross(kdj[0], kdj[1]) < 0 && position[0].Profit > position[0].Margin * 0.8) { //死叉平仓
            exchange.SetDirection("closebuy")
            exchange.Sell(-1, position[0].Amount, "止损", position[0].Profit, ticker.Last, "#00009c")
            loss = 0.6
            num = 0 //计数清零
            CancelPendingOrders()
        } else if (ticker.Last <= Fristopen - (Fristopen * (Smallcover / 100)) && num < numberof && position.length > 0) {

            num++ //计数
            let nn = 0.2 //指数
            if (position[0].Profit * -1 / position[0].Margin > 0.4) {
                nn = position[0].Profit * -1 / position[0].Margin
            }
            Log(exchange.GetCurrency(), "补仓", "数量", _N(position[0].Amount * nn))
            exchange.SetDirection("buy")
            exchange.Buy(-1, _N(position[0].Amount * nn), exchange.GetCurrency().replace("_USDT", ""), "价格", ticker.Last, "均价:", position[0].Price)
            Fristopen = ticker.Last
            //      CancelPendingOrders() 
        } else if (position[0] && position[0].Margin / bet >= 2.5 && position[0].Margin / bet < 6 && position[0].Profit * -1 > position[0].Margin * 2) {

            Log(exchange.GetCurrency(), "补仓", "数量", _N(position[0].Amount * 2))
            exchange.SetDirection("buy")
            //    CancelPendingOrders() 
        }
        if (position.length > 0) {
            if (ticker.Last / Fristopen - (Fristopen * (Smallcover / 100)) > 0.8) {
                Log(ticker.Last / Fristopen - (Fristopen * (Smallcover / 100)))
                // Log( position[0].Amount)
                 exchange.SetDirection("closebuy")
                 exchange.Sell(-1, position[0].Amount, "止损平仓", position[0].Profit, ticker.Last, "#00009c")
            }
        }

        Sleep(1000)
        // Log("已经清算的数据",order_list)
        //     Log("结束", symbol_list)
        _G("orderlist", order_list)
        _G("symbol", symbol_list)
        Sleep(1000)

        if (position[0]) loss_m = position[0].Profit < loss_m ? position[0].Profit : loss_m

        let cmd = GetCommand()
        if (cmd) {
            Log(cmd)
            let arr = cmd.split(":")
            if (arr[0] == "要做空") {
                dan = 100
            } else if (arr[0] == "检查symbol_list") {
                Log("没数据就是没有符合条件的", symbol_list)
            } else if (arr[0] == "已经清算的数据") {
                Log("已经清算的数据", order_list)
            } else if (arr[0] == "清除持久数据") {

                Log("已经清算的数据")
                _G(null)

            }
        }
    }

}



More