TradingView
adolgov
8 ספט׳ 2020 08:58

Two level TP + TrailStp exits example 

Apple Inc.NASDAQ

תיאור

Multi level exist example.
When you need take profit 50% of position size and trailing stop for remain part.
The different ids names for exits is important. Same id needs when we want to modify exit's parameters.
תגובות
JGTR
@adolgov, I would be really interested to know how you create a strategy alert for both exits? Currently, I have the same 2 step exit strategy but my alert only reads the first exit and takes 100% profit.
kiceros
Does it repaint?
somasekhar4d6
@adolgov It is rewriting when using trail_point and trail_offset. It is showing different in alert and strategy entries.
tweakerID
Hello Adolgov. Your scripts are very useful, thanks so much. Can you help me to figure out how to have 2 exits (Take Profit and Trailing Stop Loss) in two different strategy.exit orders? The way I have it now only allows me to execute the first coded strategy.exit order and the other one gets lost. I already tried changing the close_entries_rule to "ANY" but It didn't have the desired effect. Thanks!
adolgov
@tweakerID, can I ask why? Do you need pass different `alert_message` for them?
tweakerID
@adolgov, here is the code:
---

LSL=lowest(5)
TPRRR=input(defval=1.5, step=0.5)

if whateversignal
strategy.entry("long", true)

//exit
var LSLSet = false
if strategy.position_size > 0
TP = strategy.position_avg_price + (strategy.position_avg_price - LSL)*TPRRR
strategy.exit("TP", "long", limit=TP, stop=LSL, when=not LSLSet)
LSLSet := true

else
LSLSet := false

----
The exit part permits me to have a reward ratio take profit exit calculated on the stop loss value (in this case, lowest(5)). this is thanks to the var, "freezing" the TP value inside the "if". The problem is that the LSL value also gets frozen and I cannot seem to find the way to use it independently, for example with a second strategy.exit where I could use the LSL as a trailing stop. Is there someway to avoid this?
adolgov
@tweakerID, If I understand your needs correctly, you can change values of TP and LSL variables independently and call strategy.exit at each bar (or when you change TP or LSL values)
tweakerID
@adolgov, yes, that's what normally would happen because they're both series that change on each bar. With the var LSLSet I'm preventing that to happen so i can get a fixed TP number based on a LSL value everytime strategy.position_size > 0. But because of this "var" I also fix the LSL, which I'd like to keep as a constantly changing value for a trailing stop exit. I tried adding another exit outside of the "var" but it completely overwrites the exit inside the "var". Does this make sense?
adolgov
@tweakerID, If you use same "exit id" ("TP" in your case) in several `strategy.exit` this is treated as modify previous exit.
tweakerID
@adolgov, ah maybe it's that. will give it a try. thanks!
עוד