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 )
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.