Loading ...

测试-关于马丁长期效果

Author: xxs1xxs1, Date: 2021-09-04 01:47:51
Tags:

这个我也不知道算不算马丁。本身是想做多。随便找一个价位开仓。然后补仓。 比如一开始的小补仓是预备了10%的跌幅。 大补仓可以慢慢设置10% 20% 50% 如果可以在某种程度上感觉跌的可能性大。就拉大补仓位形成抄底。 所以预补仓就很重要了。。。 发挥各位的才智吧,希望能有多一些建议一起做好机械交易 最重要的是计算好承受点。不要开爆了。目前这个应该最多会开到8倍


/*backtest
start: 2021-05-1 00:00:00
end: 2021-08-28 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
args: [["OpType",1]]
*/

//各功能测试
exchange.SetContractType("swap") //合约设置
// exchange.SetCurrency("TRX_USDT");
//    exchange.SetMarginLevel(20) //合约倍数设置 

//exchange.IO("trade_margin")
//exchange.IO("currency", "STPT_USDT")
var account = _C(exchange.GetAccount) //帐户信息
var log_profit = 0
var log_profit_intervel = 1000 * 60 * 5
var symbol_list = [] //"1.5倍vol,kdj与ma多头"
var symbol_list1 = [] //"3倍vol,当前涨幅4%或者vol5倍"
var symbol_list2 = [] //"三连涨"
var order_list = []
var num = 0 //记录补仓数量
if (_G("symbol")) {
    symbol_list = _G("symbol")
    order_list = _G("orderlist")
    Log("上一次数据", symbol_list)
    Log("上一次数据完成数据", order_list)
}

// 撤单函数
function CancelPendingOrders() {
    Sleep(1000); // 休眠 1秒
    var ret = false;
    while (true) {
        var orders = null;
        // 持续获取未成交订单数组,如果返回异常,则继续获取
        while (!(orders = exchange.GetOrders())) {
            Sleep(1000); // 休眠 1秒
        }
        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); // 休眠 1秒
            }
        }
    }
}

function symbols() {


    log_profit_intervel = 1000 * 60 * 5


    if (Date.now() - log_profit > log_profit_intervel && !IsVirtual()) { //全部交易对 
        log_profit = Date.now()
        var symbol = JSON.parse(HttpQuery("https://www.binance.com/fapi/v1/exchangeInfo"))
        let symbol_all = []
        symbol.symbols.forEach(function(v, k, arr) {
            if (v.quoteAsset == "USDT" && v.contractType == "PERPETUAL") {
                let str = v.symbol.split("USDT", 1)
                str = str + "_USDT"
                symbol_all.push([str, v.pricePrecision, v.quantityPrecision])
            }
        })

        // exchange.SetCurrency("TRX_USDT");                        //交易对设置
        //exchange.SetPrecision(priceScale, amountScale)            //精度设置


        for (let i = 0; i < symbol_all.length; i++) {

            if (symbol_list.length > 0) {

                let flag = false
                symbol_list.forEach(function(v, k, arr) {
                    //   Log(v[0],symbol_all[i][0])
                    if (v[0] == symbol_all[i][0]) {
                        flag = true;
                    }
                })
                if (flag) {
                    continue;
                }
            }



            exchange.SetCurrency(symbol_all[i][0])
            //  exchange.SetCurrency("DOGE_USDT")
            exchange.SetPrecision(symbol_all[i][1], symbol_all[i][2])
            let records = exchange.GetRecords(60 * 60 * 1)
            let kdj = TA.KDJ(records, 9, 3, 3)
            let ma7 = TA.EMA(records, 7)
            let ma25 = TA.EMA(records, 25)

            //      let records1 = exchange.GetRecords(60 * 15)
            //      let kdj1 = TA.KDJ(records1, 9, 3, 3)

            let len = records.length - 1
            let rs = records[len]
            //      let rs1 = records[len]
            //      if ((rs.Close > rs.Open && rs.Volume > rs1.Volume * 1.5 && rs.Close / rs.Open > 1.04) || (rs.Close > rs.Open && rs.Volume > rs1.Volume * 2)) 
            //     if (rs.Close > rs.Open && _Cross(kdj[0], kdj[1]) > 0 && _Cross(kdj[0], kdj[1]) < 3 && rs.Close / rs.Low < 1.03 && _Cross(kdj1[0], kdj1[1]) > 0 && _Cross(kdj1[0], kdj1[1]) < 3) 

            if (rs.Close > rs.Open && rs.Volume > rs1.Volume * 1.5 && _Cross(kdj[0], kdj[1]) > 0 && _Cross(kdj[0], kdj[1]) < 5 && _Cross(ma7, ma25) > 0) {

                symbol_list.push([symbol_all[i][0], symbol_all[i][1], symbol_all[i][2]]) //加入数据保存

            }
            if ((rs.Close > rs.Open && rs.Volume > rs1.Volume * 2 && rs.Close / rs.Open > 1.04) || (rs.Close > rs.Open && rs.Volume > rs1.Volume * 4)) {
                symbol_list1.push([symbol_all[i][0], symbol_all[i][1], symbol_all[i][2]]) //加入数据保存
            }
            if (records[len - 3].Close > records[len - 3].Open && records[len - 1].Close > records[len - 1].Open && records[len - 2].Close > records[len - 2].Open) {
                symbol_list2.push([symbol_all[i][0], symbol_all[i][1], symbol_all[i][2]]) //加入数据保存
            }

            Sleep(100)
        }
        Log("没数据", symbol_list)
        Log("没数据", symbol_list1)
        Log("三连涨", symbol_list2)

    }
}







function main() {

    let loss = 2
    let loss_m = 0

    while (1) {
        // symbols()



        exchange.SetPrecision(priceScale, amountScale) //精度设置

        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 Amount = position[0] ? position[0].Amount : 0
        let ticker = _C(exchange.GetTicker); // 获取 Tick 数据
        let ma7 = TA.EMA(records, 7)
        let ma25 = TA.EMA(records, 25)
        let money = bet * MarginLevel //买入数量为 2U的商品                

        if (_N(money / ticker.Sell, amountScale) == 0) {
            continue;
        }
        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, money / ticker.Sell, ticker.Last,"开仓价:", ticker.Sell)


        } 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, ticker.Last, "#ff0000")
            loss = 0.6
            num = 0 //计数清零
            CancelPendingOrders() //清理无效定单

        } else if (position[0] &&  _Cross(kdj[0], kdj[1]) < 0 && position[0].Profit > position[0].Margin * 0.1) { //死叉平仓

            //     Log(loss_m,position[0])
            exchange.SetDirection("closebuy")
            exchange.Sell(-1, position[0].Amount,ticker.Last, "死叉平仓", position[0].Profit, ticker.Last, "#00009c")
            loss = 0.6
            num = 0 //计数清零
            CancelPendingOrders() //清理无效定单

        }else if (position[0] && position[0].Margin / bet < 2.5 && position[0].Profit * -1 > position[0].Margin * 0.4) { //本金亏完在补一次

            let nn = 0.2 //指数
            if (position[0].Profit * -1 / position[0].Margin > 0.4) {
                nn = position[0].Profit * -1 / position[0].Margin
                Log(nn, "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")

            }

            exchange.SetDirection("buy")
            exchange.Buy(-1, position[0].Amount * nn, ticker.Last,"小补仓", ticker.Last, "持仓数量:", position[0].Amount, "| 浮动亏盈:", position[0].Profit, "| Margin:", position[0].Margin, "| 现有资金:", account.Balance, "| 持仓均价:", position[0].Price, "-------|最大单次浮亏", loss_m, "#0000ff")

            //      CancelPendingOrders() //清理无效定单
        } else if (position[0] && position[0].Margin / bet >= 2.5 && position[0].Margin / bet < 6 && position[0].Profit * -1 > position[0].Margin * 2) { //本金亏完在补一次


            exchange.SetDirection("buy")
            exchange.Buy(-1, position[0].Amount * 2, ticker.Last,"大补仓", ticker.Last, "持仓数量:", position[0].Amount, "| 浮动亏盈:", position[0].Profit, "| Margin:", position[0].Margin, "| 现有资金:", account.Balance, "| 持仓均价:", position[0].Price, "-------|最大单次浮亏", loss_m, "#ccff00")

            //    CancelPendingOrders() //清理无效定单
        }

        Sleep(1000)

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

        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)

            }



        }
    }
Log( _C(exchange.GetPosition))





}

More

Alshyib 大佬,有联系方式,我想请教多个问题,比如如何计算所有币种最小下单数量

xxs1xxs1 你要把你补仓的点位计算出来。比如亏多少或者把现有持仓均价到目前的价格差是多少才触发补仓才好

Alshyib KDJ一直处于条件满足状态,后续的每一分钟都会触发补仓,我要如何处理

xxs1xxs1 大概就是0.25U*20倍/当前币价,大概就能得到最小下单数量 。注意币量精度别被砍没了。