開發Pine語言的主要目標之一是為用戶提供盡可能多的有用工具。這些工具可能具有各種不同的用途,並且透過某些操作,某些指標和圖表類型使您可以從未來的K線或交易(相對於當前處理的K線)中提取數據。由於交易者無法在實際交易中接收此數據,因此圍繞該數據構建的策略在回測時可能會產生不切實際的獲利結果,而在即時交易中,這些交易會造成損失。在策略中使用未來資訊的錯誤也被稱為前瞻性偏見。
一些TradingView用戶出於無知或出於惡意,傾向於利用此功能來建立想法和腳本發布。 TradingView無法刪除功能本身,因為它在某些情況下可能有用,但是同時,我們致力於警告用戶這種行為。
使用日式K線的策略
這種行為的一個很常見的原因是對日式圖表(Renko,Kagi等)進行策略回測。問題來自於策略回測引擎將每個K線視為4筆交易,其價格為開盤價、最高價、最低價和收盤價(常規K線圖就是這種情況)。因此,在Renko圖表上,策略回測引擎可以用實際上不存在的價格進入/退出倉位。此外,如果您將箱子尺寸(Box Size)的值設置為小於mintick,則可以在回測引擎處理實際價格之前,檢查下一個價格是否高於或低於當前價格,並提前進入/退出倉位。
//@version=4
strategy("My Strategy", overlay=true)
if close < close[1] strategy.entry("ShortEntryId", strategy.short)
strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)
strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])//@version=4strategy("My Strategy", overlay=true)if close < close[1] strategy.entry("ShortEntryId", strategy.short)strategy.close("ShortEntryId", when = close > close[1])if close > close[1] strategy.entry("LongEntryId", strategy.long)strategy.close("LongEntryId", when = close < close[1])
JavaScript//@version=4
strategy("CalcOnOrderFillsStrategy", overlay=true, calc_on_order_fills=true)// a variable is used to prevent double entry on the same bar
var lastTimeEntry = 0 longCondition = close > sma(close, 14) and lastTimeEntry != time
if longCondition strategy.entry("LongEntryId", strategy.long) strategy.exit("exitId", "LongEntryId", limit=high)
lastTimeEntry := time
JavaScript//@version=4
strategy("My Strategy", overlay=true)
dayStart = security(syminfo.tickerid, "1D", time, lookahead=barmerge.lookahead_on)
dayHigh = security(syminfo.tickerid, "1D", high, lookahead=barmerge.lookahead_on)
dayLow = security(syminfo.tickerid, "1D", low, lookahead=barmerge.lookahead_on)// entry at first bar of a day
if time == dayStart // distance to daily high is further, so we can earn more if abs(open - dayHigh) > abs(open - dayLow) strategy.entry("LongEntryId", strategy.long) strategy.exit("exitLongId", "LongEntryId", limit=dayHigh) else strategy.entry("ShortEntryId", strategy.short) strategy.exit("exitShortId", "ShortEntryId", limit=dayLow) plot(dayHigh)
plot(dayLow)
JavaScript