首页/量化教学/Pine量化入门/ Pine快速入门教程 06:初识回测功能 快速识别策略优劣
Pine快速入门教程 06:初识回测功能 快速识别策略优劣
2024年06月28日


代码:


//@version=5
strategy("Trend QQE AV2", "TQA", overlay = true, format=format.price, precision=2, pyramiding = 1, default_qty_value = 2, initial_capital = 10000)
//super trend input---------------------------------------------------------------------------
Periods = input.int(title="ATR Period", defval=9, group = "Trend")
src = input(hl2, title="Source", group = "Trend")
Multiplier = input.float(title="ATR Multiplier", step=0.1, defval=3.9, group = "Trend")
changeATR= input.bool(title="Change ATR Calculation Method ?", defval=true, group = "Trend")
showsignals = input.bool(title="Show Buy/Sell Signals ?", defval=false, group = "Trend")
highlighting = input.bool(title="Highlighter On/Off ?", defval=false, group = "Trend")

//QQE MOD input---------------------------------------------------------------------------
RSI_Period = input(6, title='RSI Length', group = "QQE")
SF = input(5, title='RSI Smoothing', group = "QQE")
QQE = input(3, title='Fast QQE Factor')
ThreshHold = input(3, title="Thresh-hold", group = "QQE")
RSI_Period2 = input(6, title='RSI Length', group = "QQE")
SF2 = input(5, title='RSI Smoothing', group = "QQE")
QQE2 = input(1.61, title='Fast QQE2 Factor', group = "QQE")
ThreshHold2 = input(3, title="Thresh-hold", group = "QQE")
src2 = input(close, title="RSI Source", group = "QQE")
length = input.int(50, minval=1, title="Bollinger Length", group = "QQE")
mult = input.float(0.35, minval=0.001, maxval=5, step=0.1, title="BB Multiplier", group = "QQE")
//-----------------------------------------------------------------------------------

//AV2  input---------------------------------------------------------------------------
ma_type = input.string(title="MA Type", defval="EMA", options=["EMA", "SMA", "SWMA", "VWMA", "WMA"], group = "AV2")
ma_period = input.int(title="MA Period (Length)", defval=52, minval=1, group = "AV2")
ma_period_smoothing = input.int(title="MA Period smoothing (Length)", defval=10, minval=1, group = "AV2")
//-------------------------------------------------------------------------------------------------------------------

atr2 = ta.sma(ta.tr, Periods)
atr= changeATR ? ta.atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? math.max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)

sellSignal = trend == -1 and trend[1] == 1
buySignal = trend == 1 and trend[1] == -1

plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(sellSignal ? dn : na, title="DnTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))

//--------------------------------------------------------------------------------------------------

//QQE MOD-------------------------------------------------------------------------------------------
Wilders_Period = RSI_Period * 2 - 1

Rsi = ta.rsi(src, RSI_Period)
RsiMa = ta.ema(Rsi, SF)
AtrRsi = math.abs(RsiMa[1] - RsiMa)
MaAtrRsi = ta.ema(AtrRsi, Wilders_Period)
dar = ta.ema(MaAtrRsi, Wilders_Period) * QQE

longband = 0.0
shortband = 0.0
QQEtrend = 0

DeltaFastAtrRsi = dar
RSIndex = RsiMa
newshortband = RSIndex + DeltaFastAtrRsi
newlongband = RSIndex - DeltaFastAtrRsi
longband := RSIndex[1] > longband[1] and RSIndex > longband[1] ?
   math.max(longband[1], newlongband) : newlongband
shortband := RSIndex[1] < shortband[1] and RSIndex < shortband[1] ?
   math.min(shortband[1], newshortband) : newshortband
cross_1 = ta.cross(longband[1], RSIndex)
QQEtrend:= ta.cross(RSIndex, shortband[1]) ? 1 : cross_1 ? -1 : nz(QQEtrend[1], 1)
FastAtrRsiTL = QQEtrend== 1 ? longband : shortband
////////////////////
basis = ta.sma(FastAtrRsiTL - 50, length)
dev = mult * ta.stdev(FastAtrRsiTL - 50, length)
upper = basis + dev
lower = basis - dev
color_bar = RsiMa - 50 > upper ? #00c3ff : RsiMa - 50 < lower ? #ff0062 : color.gray

// Zero cross
QQEzlong = 0
QQEzlong := nz(QQEzlong[1])
QQEzshort = 0
QQEzshort := nz(QQEzshort[1])
QQEzlong := RSIndex >= 50 ? QQEzlong + 1 : 0
QQEzshort := RSIndex < 50 ? QQEzshort + 1 : 0
//  
//Zero = hline(0, color=color.white, linestyle=hline.style_dotted, linewidth=1)
////////////////////////////////////////////////////////////////
//
Wilders_Period2 = RSI_Period2 * 2 - 1

Rsi2 = ta.rsi(src, RSI_Period2)
RsiMa2 = ta.ema(Rsi2, SF2)
AtrRsi2 = math.abs(RsiMa2[1] - RsiMa2)
MaAtrRsi2 = ta.ema(AtrRsi2, Wilders_Period2)
dar2 = ta.ema(MaAtrRsi2, Wilders_Period2) * QQE2
longband2 = 0.0
shortband2 = 0.0
trend2 = 0

DeltaFastAtrRsi2 = dar2
RSIndex2 = RsiMa2
newshortband2 = RSIndex2 + DeltaFastAtrRsi2
newlongband2 = RSIndex2 - DeltaFastAtrRsi2
longband2 := RSIndex2[1] > longband2[1] and RSIndex2 > longband2[1] ?
   math.max(longband2[1], newlongband2) : newlongband2
shortband2 := RSIndex2[1] < shortband2[1] and RSIndex2 < shortband2[1] ?
   math.min(shortband2[1], newshortband2) : newshortband2
cross_2 = ta.cross(longband2[1], RSIndex2)
trend2 := ta.cross(RSIndex2, shortband2[1]) ? 1 : cross_2 ? -1 : nz(trend2[1], 1)
FastAtrRsi2TL = trend2 == 1 ? longband2 : shortband2
//
// Zero cross
QQE2zlong = 0
QQE2zlong := nz(QQE2zlong[1])
QQE2zshort = 0
QQE2zshort := nz(QQE2zshort[1])
QQE2zlong := RSIndex2 >= 50 ? QQE2zlong + 1 : 0
QQE2zshort := RSIndex2 < 50 ? QQE2zshort + 1 : 0//  

hcolor2 = RsiMa2 - 50 > ThreshHold2 ? color.silver :
   RsiMa2 - 50 < 0 - ThreshHold2 ? color.silver : na
//plot(FastAtrRsi2TL - 50, title='QQE Line', color=color.new(color.white, 0), linewidth=2)
//plot(RsiMa2 - 50, color=color.new(hcolor2, 50), title='Histo2', style=plot.style_columns)
//
Greenbar1 = RsiMa2 - 50 > ThreshHold2
Greenbar2 = RsiMa - 50 > upper

Redbar1 = RsiMa2 - 50 < 0 - ThreshHold2
Redbar2 = RsiMa - 50 < lower
//plot(Greenbar1 and Greenbar2 == true ? RsiMa2 - 50 : na, title="QQE Up", style=plot.style_columns, color=color.new(#00c3ff, 0))
//plot(Redbar1 and Redbar2 == true ? RsiMa2 - 50 : na, title="QQE Down", style=plot.style_columns, color=color.new(#ff0062, 0))

//—AV2———————————————————————————————————————————————————————————————————————————————
// I.2. Settings, Function definition — — — — — — — — — — — — — — — — — — — — — —
//————————————————————————————————————————————————————————————————————————————————

f_ma_type(input_ma_type, input_source, input_ma_period) =>
    result = float(na)

    if input_ma_type == "EMA"
        result := ta.ema(input_source, input_ma_period)
        result
    if input_ma_type == "SMA"
        result := ta.sma(input_source, input_ma_period)
        result
    if input_ma_type == "SWMA"
        result := ta.swma(input_source)
        result
    if input_ma_type == "VWMA"
        result := ta.vwma(input_source, input_ma_period)
        result
    if input_ma_type == "WMA"
        result := ta.wma(input_source, input_ma_period)
        result

    result

//————————————————————————————————————————————————————————————————————————————————
// II.1. Calculations, MA — — — — — — — — — — — — — — — — — — — — — — — — — — — —
//————————————————————————————————————————————————————————————————————————————————

o = f_ma_type(ma_type, open, ma_period)
c = f_ma_type(ma_type, close, ma_period)
h = f_ma_type(ma_type, high, ma_period)
l = f_ma_type(ma_type, low, ma_period)

//————————————————————————————————————————————————————————————————————————————————
// II.2. Calculations, Heikin Ashi — — — — — — — — — — — — — — — — — — — — — — — —
//————————————————————————————————————————————————————————————————————————————————

ha = ticker.heikinashi(syminfo.tickerid)

ha_o = request.security(ha, timeframe.period, o)
ha_c = request.security(ha, timeframe.period, c)
ha_h = request.security(ha, timeframe.period, h)
ha_l = request.security(ha, timeframe.period, l)

//————————————————————————————————————————————————————————————————————————————————
// II.3. Calculations, MA (Smoothing) — — — — — — — — — — — — — — — — — — — — — —
//————————————————————————————————————————————————————————————————————————————————

ha_o_smooth = f_ma_type(ma_type, ha_o, ma_period_smoothing)
ha_c_smooth = f_ma_type(ma_type, ha_c, ma_period_smoothing)
ha_h_smooth = f_ma_type(ma_type, ha_h, ma_period_smoothing)
ha_l_smooth = f_ma_type(ma_type, ha_l, ma_period_smoothing)

AV2trend = ha_c_smooth >= ha_o_smooth

//————————————————————————————————————————————————————————————————————————————————
// III.2. Display, Plotting & Filling — — — — — — — — — — — — — — — — — — — — — —
//————————————————————————————————————————————————————————————————————————————————

o_line = plot(ha_o_smooth, color=color.new(#26A69A, 100), title="Open line")
c_line = plot(ha_c_smooth, color=color.new(#EF5350, 100), title="Close line")

h_line = plot(ha_h_smooth, color=color.new(#26A69A, 100), title="High line")
l_line = plot(ha_l_smooth, color=color.new(#EF5350, 100), title="Low line")

fill(o_line, c_line, color = AV2trend ? color.new(#26A69A, 50) : color.new(#EF5350, 50), title="Open & Close Trendcloud")
fill(h_line, l_line, color= color.new(#808080, 80), title="High & Low Trendcloud")



//————————————————————————————————————————————————————————————————————————————————
//————————————————————————————————————————————————————————————————————————————————
// 下单 — — — — — — — — — — — — — — — — — — — — — —
//————————————————————————————————————————————————————————————————————————————————
if buySignal and Greenbar1 and Greenbar2 and AV2trend
    strategy.entry("多单", strategy.long)
if sellSignal and Redbar1 and Redbar2 and AV2trend == false
    strategy.entry("空单", strategy.short)
if ta.crossunder(low, ha_l_smooth)
    strategy.close("多单", comment = "平多", alert_message = "多单平仓。")
if ta.crossover(high, ha_h_smooth)
    strategy.close("空单", comment = "平空", alert_message = "空单平仓。")


免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。
要开始Pine量化创作,
请微信扫码,加入我们: