如何在TradingView的Pine Script策略中设置回测起止日期
Pine Script新手问题解决方案
一、设置回测起止日期的两种方法
方法1:界面直接设置(无需改代码)
- 打开策略回测界面,点击右上角齿轮形状的「设置」图标
- 切换到「输入」标签页,拉到最底部找到「回测日期范围」选项
- 勾选「使用自定义日期范围」,选择你需要的起止日期后确认即可
方法2:修改代码固定日期范围(适合需要锁定回测区间的场景)
你当前的代码是指标脚本(使用study()函数),本身不带回测配置。改成策略脚本并添加日期过滤的步骤如下:
- 把开头的
study()替换为strategy(),启用策略回测功能 - 加入日期过滤逻辑,只在指定区间内触发交易信号
下面是修改后的完整代码,你可以自行调整startDate和endDate的数值:
//@version=3 //////////////////////////////////////////////////////////// // Copyright by HPotter v1.0 19/06/2019 // This is combo strategies for get a cumulative signal. // // First strategy // This System was created from the Book "How I Tripled My Money In The // Futures Market" by Ulf Jensen, Page 183. This is reverse type of strategies. // The strategy buys at market, if close price is higher than the previous close // during 2 days and the meaning of 9-days Stochastic Slow Oscillator is lower than 50. // The strategy sells at market, if close price is lower than the previous close price // during 2 days and the meaning of 9-days Stochastic Fast Oscillator is higher than 50. // // Second strategy // This indicator plots the oscillator as a histogram where blue denotes // periods suited for buying and red . for selling. If the current value // of AO (Awesome Oscillator) is above previous, the period is considered // suited for buying and the period is marked blue. If the AO value is not // above previous, the period is considered suited for selling and the // indicator marks it as red. // You can make changes in the property for set calculating strategy MA, EMA, WMA // // WARNING: // - For purpose educate only // - This script to change bars colors. //////////////////////////////////////////////////////////// // 自定义回测日期范围:格式为timestamp(年, 月, 日, 时, 分) startDate = timestamp(2020, 1, 1, 0, 0) // 起始日期 endDate = timestamp(2024, 12, 31, 23, 59) // 结束日期 inDateRange = time >= startDate and time <= endDate Reversal123(Length, KSmoothing, DLength, Level) => vFast = sma(stoch(close, high, low, Length), KSmoothing) vSlow = sma(vFast, DLength) pos = 0.0 pos := iff(close[2] < close[1] and close > close[1] and vFast < vSlow and vFast > Level, 1, iff(close[2] > close[1] and close < close[1] and vFast > vSlow and vFast < Level, -1, nz(pos[1], 0))) pos BillWilliamsAC(nLengthSlow, nLengthFast,nLengthMA, nLengthEMA, nLengthWMA, bShowWMA, bShowMA, bShowEMA) => pos = 0 xSMA1_hl2 = sma(hl2, nLengthFast) xSMA2_hl2 = sma(hl2, nLengthSlow) xSMA1_SMA2 = xSMA1_hl2 - xSMA2_hl2 xSMA_hl2 = sma(xSMA1_SMA2, nLengthFast) nRes = xSMA1_SMA2 - xSMA_hl2 xResWMA = wma(nRes, nLengthWMA) xResMA = sma(nRes, nLengthMA) xResEMA = ema(nRes, nLengthEMA) xSignalSeries = iff(bShowWMA, xResWMA, iff(bShowMA, xResMA, iff(bShowEMA, xResEMA, na))) cClr = nRes > nRes[1] ? blue : red pos := iff(xSignalSeries[2] < 0 and xSignalSeries[1] > 0, 1, iff(xSignalSeries[2] > 0 and xSignalSeries[1] < 0, -1, nz(pos[1], 0))) pos // 将study改为strategy,启用策略回测功能 strategy(title="Combo Strategy 123 Reversal & Bill Williams. Awesome Oscillator (AC) with Signal Line", shorttitle="Combo", overlay = true) Length = input(14, minval=1) KSmoothing = input(1, minval=1) DLength = input(3, minval=1) Level = input(50, minval=1) //------------------------- nLengthSlow = input(34, minval=1, title="Length Slow") nLengthFast = input(5, minval=1, title="Length Fast") nLengthMA = input(15, minval=1, title="MA") nLengthEMA = input(15, minval=1, title="EMA") nLengthWMA = input(15, minval=1, title="WMA") bShowWMA = input(type=bool, defval=true, title="Show and trading WMA") bShowMA = input(type=bool, defval=false, title="Show and trading MA") bShowEMA = input(type=bool, defval=false, title="Show and trading EMA") posReversal123 = Reversal123(Length, KSmoothing, DLength, Level) posBillWilliamsAC = BillWilliamsAC(nLengthSlow, nLengthFast,nLengthMA, nLengthEMA, nLengthWMA, bShowWMA, bShowMA, bShowEMA) pos = iff(posReversal123 == 1 and posBillWilliamsAC == 1 , 1, iff(posReversal123 == -1 and posBillWilliamsAC == -1, -1, 0)) // 仅在指定日期范围内执行交易信号 if (inDateRange) if (pos == 1) strategy.entry("Long", strategy.long) if (pos == -1) strategy.entry("Short", strategy.short) barcolor(pos == -1 ? red: pos == 1 ? green : blue )
二、从零开始学Pine Script的渠道
- TradingView内置官方手册:在代码编辑器右上角点击问号图标即可打开,从基础语法、内置函数到策略编写都有详细讲解,步骤清晰,完全适配新手
- TradingView公共脚本库:编辑器左侧「公共脚本」栏目里有大量免费的策略和指标示例,直接拿源码修改,边实践边学效率更高
- TradingView帮助中心教程:帮助中心内有分阶段的系列教程,从入门到进阶覆盖全面,包含回测设置、资金管理等核心内容
内容的提问来源于stack exchange,提问作者mona
相关产品推荐
相关产品推荐

