0

I've been trying to get backtest results for a Long-only strategy without success. Also, I can't seem to set a "Take Profit" / Stop loss for some reason. I'm trading second-based intervals so I want a take profit of 0.01% and am using :

strategy.exit("Exit Long", from_entry="Long", stop=close * 1.00001)"

(I've also tried limit=close instead of "stop") But playing up and down with the value doesn't seem to do anything to the net profit results. It did work in 15mn candles for some reasons. I can't seem to set a stop-loss whatever I try tho.

If you know of a good "input" style TP and SL that I could change directly in the indicator's settings, instead of having to type it manually in the Pine editor, that would be awesome too.

Here's my code. Bear with me as I'm completely beginner level in any kind of programing. I tried removing everything that could mention short, adding the "strategy.direction.long" line that I found in the doc. The parts with // are the ones I've tried but didn't solve anything, but kept for later tests.

// Only trade from the long side
strategy.risk.allow_entry_in(strategy.direction.long)


// Submit orders
//if buySignal
    //strategy.entry(id="EL", long=true)

//if sellSignal
    //strategy.entry(id="ES", long=false)

strategy.entry('Long', strategy.long, 1, when=buySignal)
strategy.close('Long', when=sellSignal)
strategy.cancel('Short', when=sellSignal)


//strategy.exit("exit", "long", profit = 50, loss = 20)

strategy.exit("Exit Long", from_entry="Long", stop=close * 1.000015)

//strategy.exit("Exit Long", from_entry="Long", stop=low * 1.007)

strategy.risk.allow_entry_in(strategy.direction.long)

If anything is outdated for Pine V5, do tell.

Inerti4
  • 5
  • 3

1 Answers1

0

If you call this line each time without any condition, your old exit order will be deleted and a new one will be created with the new value of close.

strategy.exit("Exit Long", from_entry="Long", stop=close * 1.000015)

That's certainly the reason why your stop didn't execute in the 1 second timeframe.
You should execute this line only one time after your strategy.entry.
Try :

if strategy.opentrades == 0 // No open order
    strategy.entry('Long', strategy.long, 1, when=buySignal)
    strategy.exit("Exit Long", from_entry="Long", stop=close * 1.000015)
G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • Thank you sir Lebret ! What line are you referring to, if you don't mind precising ? And do you have any clue about the other matter (backtest long only without any short entries) – Inerti4 Dec 07 '22 at 18:22
  • Awesome, I just saw your edit ! Thanks for taking the time, I respect your altruism. I solved my "Long" only issue, and ended up using a pre-made code for TP-SL with a trailing stop-loss so close to target that makes the TP irrelevant. It's looking great. Now I have to open a new thread for a different question entirely, but I'll still ask it here in case I'm allowed to abuse your kindness :D – Inerti4 Dec 08 '22 at 14:21
  • Once my Trailing Stop-Loss is activated, what would be the easiest way to instantly re-open a Long position if the conditions for strategy.entry('Long' .....) are still being met? The buy signal happens once => Long Entry => Trailing Stop Loss exits the position => Last signal is still Buy but I'm missing the most part of some big trending moves as the Buy-signal doesn't reactivate. A Sell signal must appear first for the next Buy to"pop". – Inerti4 Dec 08 '22 at 14:29
  • Done, thanks for the heads up ! New post can be found here https://stackoverflow.com/questions/74732860/how-to-force-a-buy-signal-to-reactivate-asap-after-a-trailing-stop-loss-closes – Inerti4 Dec 08 '22 at 16:11