"x" 天后平仓(pine editor - tradingview)

Close a position after a "x" days (pine editor - tradingview)

我是新手,我已经制定了交易策略,但我无法将操作平仓到一定数量的蜡烛。 我使用表达式 "barssince" 但它在满足条件后才算数;有时它会发出信号,然后 returns 满足条件,并再次重新计算收盘蜡烛。 当然,如果有人可以帮助我。谢谢。

我写了一个简单的策略,在 20 天内关闭。我认为它显示了如何在 N 天内平仓。

//@version=4
strategy("My Strategy", overlay=true)

entryTime = 0.0
entryTime := entryTime[1]

if not na(strategy.position_size) and strategy.position_size > 0 and na(entryTime)
    entryTime := time

longCondition = dayofweek == dayofweek.monday
if longCondition
    strategy.entry("buy", true)

MILLIS_IN_DAY = 24 * 60 * 60 * 1000
CLOSE_AFTER_DAYS = 20
if not na(entryTime) and time - entryTime >= CLOSE_AFTER_DAYS * MILLIS_IN_DAY
    strategy.close("buy")
    entryTime := na

如果在 N 根柱后收盘:

//@version=4
strategy("My Strategy", overlay=true)

enterIndex = 0.0
enterIndex := enterIndex[1]

inPosition = not na(strategy.position_size) and strategy.position_size > 0
if inPosition and na(enterIndex)
    enterIndex := bar_index

longCondition = dayofweek == dayofweek.monday
if longCondition
    strategy.entry("buy", true)

CLOSE_AFTER_BARS = 20
if not na(enterIndex) and bar_index - enterIndex + 1 >= CLOSE_AFTER_BARS
    strategy.close("buy")
    enterIndex := na

Ver.3: 更改了 OP 的脚本

//@version=3
strategy("StoJor", overlay=true)

// enterIndex is index of the bar where we've entered in the position. It's empty at the begin

// put here 0.0 just to give to Pine-Script compiller understanding
// what the type of enterIndex is (float)
enterIndex = 0.0 
enterIndex := enterIndex[1] // here it's becoming 'na' till we've entered to a position

// check that we are not in the position. As an order is filled strategy.position_size becomes a number
inPosition = not na(strategy.position_size) and strategy.position_size != 0
if inPosition and na(enterIndex)
    enterIndex := n // preserve an index of the bar where we entered to current position

// the function checks if we've reached N bars in position
itNthBarInPos(closeAfterBars) => not na(enterIndex) and n - enterIndex + 1 >= closeAfterBars


len = input(25, minval=1, title="Tiempo Stochastic") 
smoothK = input(1, minval=1, title="SmoothK Stochastic")
smoothD = input(1, minval=1, title="SmoothD Stochastic")
k = sma(stoch(close, high, low, len), smoothK)
d = sma(k, smoothD)
min = input(6, minval=1, title="Min")
max = input(96, minval=1, title="Max")

// position's direction. constants for easier using of it:
NONE = 0
SHORT = -1
LONG = 1

direction = NONE
direction := direction[1]

// I don't know Spanish, but suppose the minutos_cierre input meand close after 'minutos_cierre' bars
minutos_cierre = input(3, title='Minutos cierre', minval=1)

STOLONG = crossunder(k, min)
STOSHORT = crossover(k, max)
if STOLONG
    strategy.entry("STOLONG", strategy.long)
    direction := LONG

if itNthBarInPos(minutos_cierre) and direction == LONG
    strategy.close(id = "STOLONG")
    enterIndex := na // set na to enterIndex to mark that we've exited from the position
    direction := NONE

if STOSHORT
    strategy.entry("EMASHORT", strategy.short)
    direction := SHORT

if itNthBarInPos(minutos_cierre) and direction == SHORT
    strategy.close(id = "EMASHORT")
    enterIndex := na // set na to enterIndex to mark that we've exited from the position
    direction := NONE

感谢米歇尔的回复。 代码是:

//@version=3
strategy("StoJor", overlay=true)

len = input(25, minval=1, title="Tiempo Stochastic") 
smoothK = input(1, minval=1, title="SmoothK Stochastic")
smoothD = input(1, minval=1, title="SmoothD Stochastic")
k = sma(stoch(close, high, low, len), smoothK)
d = sma(k, smoothD)
min = input(6, minval=1, title="Min")
max = input(96, minval=1, title="Max")
minutos_cierre = input(3, title='Minutos cierre', minval=1)
STOLONG = crossunder(k, min)
STOSHORT = crossover(k, max)
strategy.entry("STOLONG", strategy.long, when=STOLONG)
strategy.close(id = "STOLONG", when = barssince(STOLONG) >= minutos_cierre)
strategy.entry("EMASHORT", strategy.short, when=STOSHORT)
strategy.close(id = "EMASHORT", when = barssince(STOSHORT) >= minutos_cierre)

我需要在下一根蜡烛图 4 号收盘。 我很欣赏你的代码,但由于我只是在学习,我还不能完全理解它