Loading ...

币安涨幅榜波动率【策略仅供学习参考】

Author: wkc19891, Date: 2021-07-31 16:42:14
Tags:


/*
 * @Author: top.brids 
 * @Date: 2022-02-13 22:12:34 
 * @Last Modified by: top.brids
 * @Last Modified time: 2022-02-14 17:01:14
 * @Last Remark: 策略定制 vx:wkc19891
 */
const Ct = "swap";//合约类型
let OrgAccount = null;//账户信息
let OpAmount = { Amount: 0, Coin: '' };//首仓
let AddTimeDiff = null;//加仓时间
let TradeType = ORDER_TYPE_BUY;//多仓方向
let CoverPrice = 0;//止盈
let Profit = 0;//浮亏/赢
let HistoryMinPrice = 0;
let HistoryReturnRate = 0;
let UnIncomeRate = 0;//为实现盈亏率
let IsHaveLock = false;//是否锁仓
let Action = '';//执行趋势
let SymbolCoin = '';//当前交易对
let quantityPrecision = 0;//获取该币精度
//精度
let pricePrecision = 1;
let NRate = 1;
let AddCoinRate = 1;

let Counter = { buy: 0, sell: 0 };
let SymbolsEx = [];

let LastBarTime = 0
let Bra = {}
let LastNbar = {}
let Records = {}

//成交量
function GetTradesquoteQty(symbol) {
    let trades = exchange.IO("api", "GET", `/fapi/v1/trades`, `symbol=${symbol}&limit=${1000}`, ``)
    let quoteQty = 0
    let tradesquoteQty = { quoteQty: 0, symbol: '' }
    for (let i = 0; i < trades.length; i++) {
        let e = trades[i];
        quoteQty += Number(e.quoteQty)
    }
    tradesquoteQty.quoteQty = quoteQty;
    let newSymbol = symbol.slice(0, symbol.length - 4) + '_' + symbol.slice(symbol.length - 4)
    tradesquoteQty.symbol = newSymbol;
    return tradesquoteQty
}

//波动率
function VixRate(Records, N) {
    // 当每K结束时计算
    if (LastBarTime !== Bar.Time) {
        LastBarTime = Bar.Time
        // 当K达到计算根数开始计算vix_arr
        if (Records && Records.length > N) {
            // 获取vix 当前close自然对数 除以 前90根自然对数
            let vix = Math.log(Bar.Close) / Math.log(LastNbar.Close)
            return vix;
        } else {
            return 0
        }
    } else {
        return 0
    }
}

//自动选币
function AutoSelectCoin() {
    Log("刷新榜...")
    let symbols = []
    // let tradesquoteQtys = []//成交额
    let vixRates = []//波动率
    let upDownRates = []//24小时涨跌幅
    for (let i = 0; i < SymbolsEx.length; i++) {
        let e = SymbolsEx[i];
        symbols.push(e.symbol)
        let _symbol = e.symbol;
        let _symbol_l4 = _symbol.slice(-4)
        if (_symbol_l4 == 'USDT') {
            //成交榜
            // let tradesquoteQty = GetTradesquoteQty(_symbol)
            // tradesquoteQtys.push(tradesquoteQty)
            //24小时涨跌幅
            let newSymbol = ''
            let newSymbol1 = _symbol.slice(0, _symbol.length - 4)
            let newSymbol2 = _symbol.slice(_symbol.length - 4)
            newSymbol = newSymbol1 + '_' + newSymbol2;
            exchange.SetCurrency(newSymbol);
            let upDownRate = { rate: 0, symbol: '', VolumeP: 0 }
            let Records = exchange.GetRecords(PERIOD_H1)

            let begin = Records[Records.length - 25]
            let last = Records[Records.length - 1];

            let Open = begin.Open//涨跌=今日开盘价-现价(当前收盘价)
            let Close = last.Close//涨跌幅=涨跌/今日开盘价*100    

            let update = (Close - Open) / Open * 100

            for (let i = 1; i <= 25; i++) {
                let r = Records[Records.length - i];
                let volumeP = ((r.Open + r.Close) / 2) * r.Volume
                upDownRate.VolumeP += volumeP
            }
            upDownRate.rate = update
            upDownRate.symbol = newSymbol

            if (upDownRate.VolumeP >= 100000000) {
                upDownRates.push(upDownRate)
            }
        }
    }
    // tradesquoteQtys.sort(function (a, b) { if (a.quoteQty < b.quoteQty) { return 1 } else { return -1 } })
    // tradesquoteQtys = tradesquoteQtys.slice(0, 10)
    // Log('成交榜:', tradesquoteQtys)
    upDownRates.sort(function (a, b) { if (a.rate < b.rate) { return 1 } else { return -1 } })
    upDownRates = upDownRates.slice(0, 10)
    // Log('涨幅榜:', upDownRates)
    //波动率
    let N = 90;
    for (let i = 0; i < upDownRates.length; i++) {
        let e = upDownRates[i];
        exchange.SetCurrency(e.symbol);
        let rates = 0;
        let vixRate = { rate: 0, symbol: '' }
        let Records = exchange.GetRecords(PERIOD_M5)

        if (Records && Records.length > 2 * N + 2) {
            // 初始化前N个vix值
            for (let i = -2; i < N - 1; i++) {
                Bar = Records[Records.length - N + i]
                LastNbar = Records[Records.length - N + i - N]
                let rate = VixRate(Records, N)
                if (rate == 0) {
                    N--
                }
                rates += rate;
            }
        }
        rates = rates / N
        vixRate.symbol = e.symbol
        vixRate.rate = rates
        vixRates.push(vixRate)
    }
    vixRates.sort(function (a, b) { if (a.rate < b.rate) { return 1 } else { return -1 } })
    // Log('波动榜:', vixRates)
    PrintStatus(vixRates, upDownRates)
    let rData = { vixRate: vixRates[0], upDownRate: upDownRates[0] }
    return rData
}

//账户信息
function GetAccount(e) {
    let account = _C(e.GetAccount);
    return account;
}

//持仓信息
function MyGetPosition(e, orderType) {
    let positions = e.GetPosition();
    if (typeof (orderType) == 'undefined') {
        return positions;
    }
    for (let i = 0; i < positions.length; i++) {
        if (positions[i].Type == orderType) {
            return positions[i];
        }
    }
    return [];
}

//获取ticker
function GetTicker(e) {
    while (true) {
        let ticker = _C(e.GetTicker);
        if (ticker.Buy > 0 && ticker.Sell > 0 && ticker.Sell > ticker.Buy) {
            return ticker;
        }
        Sleep(300);
    }
}

//打印状态栏

function PrintStatus(vixRates, upDownRates) {

    // 波动榜: [{ "rate": 1.431375775201277, "symbol": "MATIC_USDT" }, { "rate": 1.053691280763443, "symbol": "ADA_USDT" }, { "rate": 1.0131351132624051, "symbol": "THETA_USDT" }, { "rate": 1.0131028052314173, "symbol": "ETH_USDT" }, { "rate": 1.0129123134105333, "symbol": "BNB_USDT" }, { "rate": 1.0115898575931457, "symbol": "XRP_USDT" }, { "rate": 1.0088663292695992, "symbol": "CHZ_USDT" }, { "rate": 1.0055896067354058, "symbol": "DOGE_USDT" }]
    // 涨幅榜: [{ "rate": 3.3950374163056254, "symbol": "ADA_USDT", "VolumeP": 474603172.89415 }, { "rate": 3.5133919969169494, "symbol": "BNB_USDT", "VolumeP": 781524119.90095 }, { "rate": 3.5155709342560524, "symbol": "XRP_USDT", "VolumeP": 1277376545.173805 }, { "rate": 3.861111111111118, "symbol": "CHZ_USDT", "VolumeP": 108146599.98992498 }, { "rate": 4.068501286873878, "symbol": "MATIC_USDT", "VolumeP": 460923925.20629996 }, { "rate": 4.159415245388094, "symbol": "THETA_USDT", "VolumeP": 145317661.34635 }, { "rate": 4.578168732506998, "symbol": "DOGE_USDT", "VolumeP": 679970947.8944551 }, { "rate": 4.588896308552124, "symbol": "ETH_USDT", "VolumeP": 7259844770.226775 }]
    // 成交榜: [{ "quoteQty": 3923169.689999988, "symbol": "BTC_USDT" }, { "quoteQty": 1380056.0300000014, "symbol": "ETH_USDT" }, { "quoteQty": 856356.3700000024, "symbol": "AXS_USDT" }, { "quoteQty": 839315.4369999996, "symbol": "LINK_USDT" }, { "quoteQty": 791385.6441399998, "symbol": "DOGE_USDT" }]
    let str = `✱牛蛙量化专注马丁类策略的研究,QQ方式:863368681 #32CD32
    1.币安涨幅榜,成效量榜,波动率榜数据展示...#ff0000
    2.涨幅榜选取交易额过亿的前十头部币
    3.波动榜在涨幅榜中选取波动率大的币的排名`
    // let tbl3 = {
    //     type: "table",
    //     title: "成交榜",
    //     cols: ["交易对", "成交额(USDT)"],
    //     rows: []
    // }
    // for (let i = 0; i < tradesquoteQtys.length; i++) {
    //     let e = tradesquoteQtys[i];
    //     tbl3.rows.push([e.symbol, `${_N(e.quoteQty, 2)}usdt`])
    // }


    let tbl2 = {
        type: "table",
        title: "涨幅榜",
        cols: ["交易对", "涨幅率", "成交额(USDT)"],
        rows: []
    }
    for (let i = 0; i < upDownRates.length; i++) {
        let e = upDownRates[i];
        tbl2.rows.push([e.symbol, _N(e.rate, 2), `${_N(e.VolumeP, 2)}usdt`])
    }

    let tbl1 = {
        type: "table",
        title: "波动榜",
        cols: ["交易对", "波动率"],
        rows: []
    }
    for (let i = 0; i < vixRates.length; i++) {
        let e = vixRates[i];
        tbl1.rows.push([e.symbol, _N(e.rate, 2)])
    }
    LogStatus(_D(), "\n`" + JSON.stringify([tbl1, tbl2]) + "`\n" + str)
}


function OnTick() {
    exchange.SetContractType(Ct)
    let eName = exchange.GetName()
    let patt = /Futures_/;
    if (patt.test(eName)) {
        OrgAccount = GetAccount(exchange);
        let exchangeInfo = exchange.IO("api", "GET", `/fapi/v1/exchangeInfo`, ``, ``)
        SymbolsEx = exchangeInfo.symbols;
        Log("启动=>", "交易所:", eName, "类型:", Ct, "#FF0000")
    } else {
        throw '只支持期货交易...'
    }
    while (true) {
        try {
            let rData = AutoSelectCoin()
            // let rData = { tradesquoteQty: tradesquoteQtys[0], vixRate: vixRates[0], upDownRate: upDownRates[0] }
            Log("当前推荐交易对:", rData.vixRate.symbol, "当前涨幅最大交易对:", rData.upDownRate.symbol, "#32CD32")
            Sleep(19900)
        } catch (error) {
            continue
        }

    }

}
function main() {
    LogReset(1)
    OnTick()
}

More

轻轻的云 【赞】!!!【赞】!!!【赞】!!!