0

I'm building a second-based strategy (currently on 5sec candles to limit the amount of alerts), it's working great in backtesting, but I think it can be optimized even further.

Let's say I work with an indicator which triggers a simple buySignal and sellSignal (one must follow the other without repetition), I've set my strategy.entry/close based on those signals (duh.) and added a Trailing Stop-Loss useTrailStop so tight that it often Exits (in profit 80% of the time) one, two, maybe max three candles after Entry (1sec or 5 sec).

Ok, cool, I'm making good profit and it's a steady growth over time, but then I'm missing-out on every big price swings because the profit-taking happened way too soon, and the next buySignal can only happen AFTER the following sellSignal. So...

Is there any way to reactivate my buySignal to trigger as-soon-as-possible after Exit if the conditions for my initial Long Entry are still being met ?

Here comes the code, stripped down to the basics.

// Risk management inputs (settings) :
//If one of you knows how to translate this part in percentages, something about input.float I think, I'd love to know.

inpTrailStop    = input(defval = 200, title = "Trailing Stop Loss", minval = 0)
inpTrailOffset  = input(defval = 0, title = "Trailing Stop Loss Offset", minval = 0)

// === RISK MANAGEMENT Back-end ===
// if an input is less than 1, assuming not wanted so we assign 'na' value to disable it.

useTrailStop    = inpTrailStop   >= 1 ? inpTrailStop   : na
useTrailOffset  = inpTrailOffset >= 1 ? inpTrailOffset : na

// Strategy backtesting signals Buy / Close :

strategy.entry("Long", strategy.long, 1, when=buySignal)
strategy.close("Long", when=sellSignal)

// === STRATEGY RISK MANAGEMENT EXECUTION ===

strategy.exit("Exit Long", from_entry = "Long", trail_points = useTrailStop, trail_offset = useTrailOffset )
Dharman
  • 30,962
  • 25
  • 85
  • 135
Inerti4
  • 5
  • 3

1 Answers1

0

The idea is to monitor for the last trade being closed with strategy.exit (comment of the closed trade will be "Trail" in that case), and check if position has changed, and then enter Long.

// Strategy backtesting signals Buy / Close :


if (buySignal) or (ta.change(strategy.closedtrades) and strategy.closedtrades.exit_comment(strategy.closedtrades -1) == "Trail")
    strategy.entry("Long", strategy.long, 1)

if sellSignal
    strategy.close("Long")

// === STRATEGY RISK MANAGEMENT EXECUTION ===

strategy.exit("Exit Long", from_entry = "Long", trail_points = useTrailStop, trail_offset = useTrailOffset, comment = "Trail")

Starr Lucky
  • 1,578
  • 1
  • 7
  • 8
  • but side note - the entry still will be at bar close / or next tick with calc_on_every_tick activated – Starr Lucky Dec 09 '22 at 14:19
  • 1
    My friend, I don't think you realise what you've just done for me. Profit has been catapulted x10 with this simple tweak, and I was already on the bar-close calculation + magnifier to make sure there wasn't multiple signals on a singe candle. Leave me your contact, if there is any way I can give back to you once I manage to set this from virtual to online, I will. – Inerti4 Dec 09 '22 at 14:56