Loading ...

基于回归幅度的反转交易策略

Author: Hukybo, Date: 2018-01-20 14:43:42
Tags: Trend Commodity-futures


/*backtest
start: 2010-01-11 09:00:00
end: 2018-01-03 15:00:00
period: 1h
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES","minfee":10,"fee":[0,0]}]
*/

function main() {
    var contractType_Dic = {};
    var contractType_Array1 = contractType.split(",");
    var contractType_Array2 = [];
    for (var i = 0; i < contractType_Array1.length; i++) {
        contractType_Array2.push(contractType_Array1[i].split('/')[1]);
    }
    contractType_Array2.toString();
    for (var key in contractType_Array2) {
        contractType_Dic[contractType_Array2[key]] = {
            falsePosition: 0
        }
    }
    $.CTA(contractType, function(st) {
        var bars = st.records; //获取K线数组
        var j = bars[bars.length - 2].Close;
        //计算N日内的最高价
        var high = TA.Highest(bars, cycleLength, 'High');
        
        //计算N日内的最低价
        var low = TA.Lowest(bars, cycleLength, 'Low');

        //计算N日内的最高价的回撤1%的值
        var highBack = high * (1 - backRatio / 100);

        //计算N日内的最低价的回撤1%的值
        var lowBack = low * (1 + backRatio / 100);
        
        if (!bars || bars.length < cycleLength + 1) {
            return;
        }
        
        
        //多头平仓
        if (contractType_Dic[st.symbol].falsePosition > 0 && j < (lowBack + highBack) / 2) {
            contractType_Dic[st.symbol].falsePosition = 0;
            return -unit;
        }
        
        //空头平仓
        if (contractType_Dic[st.symbol].falsePosition < 0 && j > (lowBack + highBack) / 2) {
            contractType_Dic[st.symbol].falsePosition = 0;
            return unit;
        }
        
        //多头开仓
        if (contractType_Dic[st.symbol].falsePosition == 0 && j > lowBack && j > highBack) {
            contractType_Dic[st.symbol].falsePosition = 1;
            return unit;
        }
        
        //空头开仓
        if (contractType_Dic[st.symbol].falsePosition == 0 && j < lowBack && j < highBack) {
            contractType_Dic[st.symbol].falsePosition = -1;
            return -unit;
        }
        
    });
}

Related

More