Reputation: 13
I'm trying to have a static stop loss based on the swing low at the entry time. but when i tried the following, the stop loss kept changing with every bar as there are new lowest lows
SwingLowBars=20
longStop = lowest(low, SwingLowBars)[1]
longTake = strategy.position_avg_price + ((strategy.position_avg_price-longStop)*3)
i want a function that keeps adding +1 to the SwingLowBars variable with every new candle in the position so that the longTake stays static and doesn't change when the lowest bar is more than 20 bars away
Upvotes: 1
Views: 533
Reputation: 1694
You can memorize the SL value in "var" variable:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov
//@version=4
strategy("My Strategy", overlay=true)
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
SwingLowBars=20
longStop = lowest(low, SwingLowBars)[1]
var sl = float(na)
if strategy.position_size !=0 and strategy.position_size[1] == 0
sl := strategy.position_avg_price - ((strategy.position_avg_price-longStop)*3)
strategy.exit("x", stop = sl)
Upvotes: 2