Cristian Machado
Cristian Machado

Reputation: 33

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

I am new to this, I have developed a trading strategy and I am unable to close the operation to a certain number of candles. I use the expression "barssince" but it counts since the condition is met; and it happens that sometimes it gives a signal, and then it returns to fulfill the condition, and recalculates the closing candle again. If anyone can help me, of course. Thank you.

Upvotes: 1

Views: 6508

Answers (2)

Michel_T.
Michel_T.

Reputation: 2821

I wrote a simple strategy with closes in 20 days. I think it shows how to close position in N days.

//@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

In case close after N bars:

//@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: changed the OP's script

//@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

Upvotes: 5

Cristian Machado
Cristian Machado

Reputation: 33

Thanks Michelle for the reply. The code is:

//@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)

and I need closure on the 4th next candle. I appreciate your code, but since I'm just learning, I can't fully understand it yet

Upvotes: 0

Related Questions