'I'm using the ta.lowest function to calculate a stop loss and take profit. Though I want these to become a static fixed number when trade is executed
I'm using the ta.lowest and ta.highest function to calculate a stop loss and take profit. Though I want these to become a static fixed number when trade is executed. So on a long trade, im trying to put my stop at the low of the past 10 days, and the take profit a multiple of that. Though when the trade executes, these numbers continue to calculate off the past bars and move with price (as they should). Is there a way I can make these a static number when the trade has been executed?
high10 = ta.highestbars(10)
low10 = ta.lowest(low, 10)
takeProfit = ((close - low10) * 2) + close
longCondition = ta.crossover(EMA_fast, EMA_slow)
longExitCondition = ta.crossunder(EMA_fast, EMA_slow)
longExitCondition1 = takeProfit
if longCondition
strategy.entry("long", strategy.long)
stopLoss = low10
strategy.exit("exit", "long", loss=stopLoss)
if high >= longExitCondition1
strategy.close(id="long")
Solution 1:[1]
we can use var to prevent recalculation of values from bar to bar. Variables declared with var initialize on the first bar at runtime, and hold their values, unless we dictate another value. New values can be assigned to a variable later in the script using the := operator. Here I have taken your script and added a stop and a limit declaring them with var. They are assigned under the entry condition. I also plotted them so you can see them and included notes for descriptions of the changes.
Cheers and best of luck with your coding and trading
EMA_fast = ta.ema(close, 9)
EMA_slow = ta.ema(close, 21)
// declare 2 variables with `var` which sets them to 0 on first bar and will not recalculate until we "set" them
var stopLoss = 0.0
var takeProfit = 0.0
long = strategy.position_size > 0 // a variable to check if we are in a long position
high10 = ta.highestbars(10)
low10 = ta.lowest(low, 10)
takeProfitCalc = ((close - low10) * 2) + close
longCondition = ta.crossover(EMA_fast, EMA_slow)
longExitCondition = ta.crossunder(EMA_fast, EMA_slow)
// we need to check that we are not already long otherwise we may "set" our limits again mid-trade if the condition were to occur before the trade is through
if longCondition and not long
strategy.entry("long", strategy.long)
stopLoss := low10
takeProfit := takeProfitCalc // Here we use `:=` to "set" the variables under the entry condition
strategy.exit("exit", "long", stop = stopLoss, limit = takeProfit) // we use the `stop` and `limit` args to enter our levels
drawOrders = long or longCondition // condition to check when to draw lines. we only want them when in, or entering a position
plot(drawOrders or drawOrders[1] ? stopLoss : na, "Stop Loss", color.red ,style = plot.style_linebr) // we use the draworders cond to draw lines on the exit bar too
plot(drawOrders or drawOrders[1] ? takeProfit : na, "Take Profit", color.green ,style = plot.style_linebr) // note the plot style. this allows breaks in the lines when not in pos.
// optional close if cross back down
// if longExitCondition
// strategy.close(id="long")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bjorgum |
