I tried to code a TP with bollinger bands. The logic behind is that the TP will change regarding the current close. If the close has recently crossed the lower line then the TP will be triggered if the price crosses the middle line (basis). If not the TP will be when the price crosses the upper line. I want this to locate the price inside the bollinger bands and therefore let it decide the safest TP. Like a 2 step TP zone.
But instead I do not get any TP. The strategy exits every entry instantly. I came up with some code but I am still a beginenr so please advise me better. Open for any suggestions!
Here is the code: TP1 is for long TP2 is for short
basisCOND = ta.cross(close, lower)
basisCross = ta.cross(close, basisSL)
upperCross = ta.cross(close, upper)
lookbackcross = input(4, title = "Lookback")
crossLookback = ta.barssince(basisCOND) < lookbackcross
TP1 = upper
TP2 = lower
if crossLookback
TP1 := basisSL
else
TP1 := upper
if crossLookback
TP2 := basisSL
else
TP2 := lower
strategy.exit("LONG EXIT", from_entry = "BUY", stop = TP1 )
strategy.exit("SHORT EXIT", from_entry = "SELL", stop = TP2 )
CHANGES:
lowerCross = ta.cross(close, lower)
basisCross = ta.cross(close, basisSL)
upperCross = ta.cross(close, upper)
lookbackcross = input(4, title = "Lookback")
crossLookbackL = ta.barssince(lowerCross) < lookbackcross
crossLookbackS = ta.barssince(upperCross) < lookbackcross
if crossLookbackL
strategy.close(id = "BUY", when = basisCross)
if not crossLookbackL
strategy.close (id="BUY", when = upperCross)
if crossLookbackS
strategy.close(id= "SELL", when = basisCross)
if not crossLookbackS
strategy.close(id="SELL", when = lowerCross)
The stop
argument is used for stop loss. You need to use the limit
argument for take profit. This will place an exit order at the bollinger bar on bar close. So, it will exit based on some price (value of the bollinger band).
If you want to exit based on a condition (e.g. whenever price crosses and closes above the band), you should use the strategy.close()
function.