ATR-RSI combined strategy

Author: A knife, Date: 2022-02-13 17:17:17
Tags:

The Atr Index

Average True Range, abbreviated as ATR, is a measure of the intensity of market volatility and indicator of market volatility, but does not reflect the direction and stability of the price trend. The higher the value of the indicator, the greater the probability of a trend change, and the smaller the probability of a trend change.

Method of calculation

The average real range is calculated based on both the real range and the real range over the past N days. The single-day real range is calculated from the mean of the three sets of results, the maximum price difference between the highest price of the day and the lowest price of the day, the highest price of the day and the closing price of yesterday.

The Rsi indicator

The Relative Strength Index (RSI) is a technical indicator of future market trends by comparing the strength and weakness of the buying and selling forces of the two sides over a period of time.

Method of calculation

RSI = 100 - (100/(1+RS)); RS = sum of closing margin and closing loss on n days; Generally, the RSI is 50 as the midline, greater than 50 is considered to be a multi-head market, and less than 50 is considered to be a short-head market; An RSI greater than 70 is considered to be overbought, and a subsequent market pullback or reversal is likely, while less than 30 is considered to be oversold, and a subsequent uptrend is likely.

The Principles of Strategy

ATR is used to filter, when ATR>ATRMa (mean ATR over the past N days) indicates that market volatility has started to increase and the trend is strengthening.


/*backtest
start: 2021-02-11 00:00:00
end: 2022-02-10 23:59:00
period: 15m
basePeriod: 5m
exchanges: [{"eid":"Huobi","currency":"BCH_USDT"}]
args: [["rsi_period",12],["atrma_period",18]]
*/

/*
* rsi_period: 强弱指标计算周期
* atr_period: 平均真实波幅计算周期
* atrma_period: 平均真实波幅均值计算呢周期
* tick_interval: 时间间隔
* slide_price: 下单滑动值
*/

// RSI指示操作状态
var RSI_NONE = 0;
var RSI_BUY = 1;
var RSI_SELL = 2;

var last_rsi_staus;

// ATR活跃信号判断
function isAtrActive(records) {
    let atr = TA.ATR(records, atr_period);
    let atrma = atr[atr.length - 1];
    if (atr.length > atrma_period) {
        let tmp_atr = 0;
        for (let i = atr.length - atrma_period; i < atr.length; i++) {
            tmp_atr += atr[i];
        }
        atrma = tmp_atr / atr_period;
    }
    else {
        atrma = aval(atr.join("+")) / atr.length;
    }
    return atr[atr.length - 1] > atrma;
}

// 获取RSI操作状态
function getRsiStatus(records) {
    let rsi = TA.RSI(records, rsi_period)[records.length - 1];
    if (rsi < 30) {
        return RSI_BUY;
    }
    else if (rsi > 70) {
        return RSI_SELL;
    }
    else {
        return RSI_NONE;
    }
}

// 取消未成交下单
function canelPendingOrders() {
    while (true) {
        let orders = _C(exchange.GetOrders);
        if (orders.length == 0) {
            break;
        }
        for (let i = 0; i < orders.length; i++) {
            exchange.CancelOrder(orders[i].Id);
        }
    }
}

function onTick() {
    let records = _C(exchange.GetRecords, PERIOD_M15);
    let ticker = _C(exchange.GetTicker);
    if (records == null ||
        ticker == null ||
        records.length < rsi_period ||
        records.length < atr_period) {
        return;
    }

    if (isAtrActive(records)) {
        let rsi = getRsiStatus(records);
        if (rsi != RSI_NONE) {
            let account = _C(exchange.GetAccount);
            if (rsi == RSI_BUY && last_rsi_staus != RSI_BUY) {
                Log("买入信号");
                last_rsi_staus = RSI_BUY;
                canelPendingOrders();
                if(account.Balance>0){
                    let price = ticker.Last + slide_price;
                    let amount = account.Balance / price * 0.99;
                    exchange.Buy(price, amount);
                }
            } else if (rsi == RSI_SELL && last_rsi_staus != RSI_SELL) {
                Log("卖出信号");
                last_rsi_staus = RSI_SELL;
                canelPendingOrders();
                if (account.Stocks > 0) {
                    let price = ticker.Last - slide_price;
                    exchange.Sell(price, account.Stocks);
                }
            }
        }
    }
    last_records = records;
}

function main() {
    while (true) {
        onTick();
        Sleep(tick_interval * 1000);
    }
}

More

AllyRuntimeError: abort ((undefined) at Error at jsStackTrace, this is crazy

A knifeIs there any other information? Let me see.