如何在 pinescript 上为长条目和短条目添加延迟

问题描述 投票:0回答:1
// Entry and Exit Conditions with a delay of 15 minutes (3 bars on 5-minute chart)
longCondition = close < smaValue and bullCond and (highVolatility or mediumVolatility or extremeLowVolatility)
shortCondition = close > smaValue and bearCond and (highVolatility or mediumVolatility or extremeLowVolatility)
// Initialize counter

// Define a function to enter positions after a delay
enterPositionAfter(delayBars, isLong) =>
    var int counter = 0
    if (na(counter))
        counter := 0
    if (na(counter) and (isLong ? longCondition : shortCondition))
        counter := delayBars

    // Decrement counter on each bar
    counter := math.max(counter - 1, 0)

    // Buy or sell signal when counter becomes zero
    counter == 0

// Entry and Exit Conditions with a delay of 15 minutes (3 bars on 5-minute chart)
if (enterPositionAfter(3, true))
    strategy.entry("Long", strategy.long)

if (enterPositionAfter(3, false))
    strategy.entry("Short", strategy.short)
// Exit Long if close crosses above 50 SMA or if ATR-based stop loss is hit
if (close > smaValue)
    strategy.close("LongExit")

// Exit Short at market price or if ATR-based stop loss is hit
if (close < smaValue )
    strategy.close("ShortExit")

我正在根据上述条件构建pinescript策略。在出现背离并且已经过去了 3 根蜡烛后,我需要延迟执行交易。无论如何,有没有使用 pinescript 来实现它。

我尝试了不同的方法,例如barsince和bar_index,但似乎没有任何效果。

pine-script pine-script-v5 tradingview-api indicator
1个回答
0
投票

要在事件发生后执行交易,您可以使用 ta.barssince 函数。该函数接受事件条件并返回自事件发生以来已经经过的柱数。我无法理解您的代码正在执行的所有操作,因此这是一个使用 sma20 crossover 作为事件的非常简单的示例:

eventCondition = ta.crossover(close, ta.sma(close, 20))

barsSinceEvent = ta.barssince(eventCondition)

// 3 bars after event, first bar is counted as 0
if (barsSinceEvent == 2) 
    // execute trade logic here
© www.soinside.com 2019 - 2024. All rights reserved.