生生不息

生生不息

pine多指标选择策略

2025-07-18
pine多指标选择策略

背景

pine三指标转换为策略

代码

pine脚本记录

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=6
strategy(title="my RSI", overlay = true)

// Inputs
i_stratSelect = input.string(defval = "rsi", title = "Select Strategy", options = ["rsi", "macd", "bb"])
i_bbsrc = input.source(defval = close, title = "BB strategy source")

f_strategySelector(_strat) =>
    if _strat == "rsi"
        // RSI
        rsi = ta.rsi(close, 14)
        // Rsi strategy
        risbuyCondition = ta.crossover(rsi, 30)
        rsisellCondition = ta.crossunder(rsi, 70)
        [risbuyCondition, rsisellCondition]
    else if _strat == "macd"
        // MACD
        [macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
        crossOverSignal = ta.crossover(macdLine, signalLine)
        crossUnderSignal = ta.crossunder(macdLine, signalLine)
        macdbuyCondition = histLine > 0 and crossOverSignal
        macdsellCondition = histLine < 0 and crossUnderSignal
        [macdbuyCondition, macdsellCondition]
    else if _strat == "bb"  
        // BB
        [middle, upper, lower] = ta.bb(close, 10, 2)
        bbbuyCondition = i_bbsrc < lower
        bbsellCondition = i_bbsrc > upper
        [bbbuyCondition, bbsellCondition]
    else
        na

[buy, sell] = f_strategySelector(i_stratSelect)
plotshape(buy ? low : na, text = "buy", style = shape.arrowup, location = location.belowbar, color = color.green, textcolor = color.green, size = size.large)
plotshape(sell ? high : na, text = "sell", style = shape.arrowdown, location = location.abovebar, color = color.red, textcolor = color.red, size = size.large)

if buy
    strategy.entry("golang", strategy.long)

if sell
    strategy.entry("goshort", strategy.short)

将指标设置进新的函数f_strategySelector。最后提供了买入卖出的策略代码。这样就可以看到回测结果。

注意

回测可以设置初始资金,单一方向订单数目,手续费,滑点等。