0
votes

I'm trying to put together a pine script for tradingview and I'm almost done with it, but i'm not sure how I can get it to close the trade at either the end of the day (11:59PM) OR when the current price is equal to the previous day candle high (high[1] + X pips on day candles).

Here's what I have so far:

//@version=2
strategy("Previous Day High and Low Breakout Strategy", overlay=true)
D_High = security(tickerid, 'D', high[1]) 
D_Low = security(tickerid, 'D', low[1]) 
D_Close =  security(tickerid, 'D', close[1]) 
D_Open =  security(tickerid, 'D', open[1]) 

// Go Long - if prev day high is broken and stop loss prev day low
// Go Short - if prev day low is broken and stop loss prev day high
plot(isintraday ? D_High : na, title="Daily High",style=line, color=blue,linewidth=2) 
plot(isintraday ? D_Low : na, title="Daily Low",style=line, color=red,linewidth=2) 

signal =crossover(high,D_High) ? true : crossover(D_Low,low) ? false : signal[1]

longCondition = signal
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = signal != true
if (shortCondition)
    strategy.entry("Short", strategy.short)
2

2 Answers

0
votes

Exit with previous high looks like that:

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

DESIRED_PROFIT_TICKS = 5

exitPrice = 0.0
exitPrice := exitPrice[1]

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)
    exitPrice := (high[1] - close) / syminfo.mintick + DESIRED_PROFIT_TICKS

strategy.exit("ExitId", "My Long Entry Id", profit = exitPrice)

But I doubt there's a way to exit at the end of a day, because you can't be sure this trade is last or opposite - we skip these trades and hope to exit after a while, but there a chance, that there will no trades to the end of the day.

0
votes

I think you can do it like this, because I did it like this, anyways if there is a problem comment and let me know.

tomorrow=0
tomorrow:=strategy.opentrades==1 ? time : 0
strategy.close("long", when=tomorrow>0)