Fixing stoploss and takeprofit after strategy.entry

58 Views Asked by At

I'm trying to get this code to work. The strategy consists in a MACD/HMA/BB combination for entry condition. When all conditions where verified, set stoploss in the lowest low and take-profit of 1.5*(entry price - lowest low). The same logic is applied for short positions. My problem is that my stoploss and take profit are not fixed after the verification of all entry conditions. This leads to a variation of take profit and stoploss "in trade". Can someone help please?

strategy("Custom Strategy", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=1.5)
// Input parameters
lengthBB = 200
multBB1 = 1.2
multBB2 = 2.0
lengthHMA = 22
lengthMACD = 12
signalMACD = 26
lengthATR = 14
riskToRewardRatio = 1.5
maxBars = 10

// Bollinger Bands
basisBB1 = ta.sma(close, lengthBB)
upperBB1 = basisBB1 + multBB1 * ta.stdev(close, lengthBB)
lowerBB1 = basisBB1 - multBB1 * ta.stdev(close, lengthBB)
basisBB2 = ta.sma(close, lengthBB)
upperBB2 = basisBB2 + multBB2 * ta.stdev(close, lengthBB)
lowerBB2 = basisBB2 - multBB2 * ta.stdev(close, lengthBB)

// Hull Moving Average (HMA)
hmaClose = ta.hma(close, 22)

// MACD
[macdLine, signalLine, _] = ta.macd(close, lengthMACD, signalMACD, 9)

// ATR
atrValue = ta.atr(lengthATR)

// Entry conditions
longCondition = (close <= lowerBB1 or close >= upperBB2) and ta.crossover(macdLine, signalLine) and close >= hmaClose
shortCondition = (close > upperBB1 or close < lowerBB2) and ta.crossunder(macdLine, signalLine) and close <= hmaClose

// Calculate position size based on 1.5% risk per trade
riskPerTrade = strategy.equity * 0.015
positionSize = riskPerTrade / atrValue  

// Highest high and lowest low values over the last 14 bars
stoplossshort = ta.highest(high, 7)
stoplosslong  = ta.lowest(low, 7)
takeProfitLevelLong = close + riskToRewardRatio * (close - ta.lowest(low, 7))
takeProfitLevelShort = close - riskToRewardRatio * (ta.highest(high, 7) - close)

// Stop Loss and Take Profit levels at entry
var float stoplossshort1 = na
var float stoplosslong1 = na
var float takeProfitLevelLong1 = na
var float takeProfitLevelShort1= na

if (longCondition == false and shortCondition == false)
    stoplossshort1 := stoplossshort
    stoplosslong1 := stoplosslong
    takeProfitLevelLong1 := takeProfitLevelLong
    takeProfitLevelShort1 := takeProfitLevelShort

strategy.entry("Long", strategy.long, qty = positionSize, when = longCondition)
strategy.entry("Short", strategy.short, qty = positionSize, when = shortCondition)

if (close <= stoplosslong1 or close >= takeProfitLevelLong1)
    strategy.close("Long")

if (close >= stoplossshort1 or close <= takeProfitLevelShort1)
    strategy.close("Short")

// Plotting Bollinger Bands
plot(upperBB1, color=color.red)
plot(lowerBB1, color=color.green)
plot(upperBB2, color=color.red)
plot(lowerBB2, color=color.green)

// Plotting HMA
plot(hmaClose, color=color.blue)

// Plotting Stop Loss and Take Profit levels
plot(stoplosslong1, color=color.red, linewidth=2, title="Stop Loss (Long)")
plot(stoplossshort1, color=color.green, linewidth=2, title="Stop Loss (Short)")
plot(takeProfitLevelLong1, color=color.rgb(175, 76, 76), linewidth=2, title="Take Profit (Long)")
plot(takeProfitLevelShort1, color=color.rgb(82, 255, 111), linewidth=2, title="Take Profit (Short)")


I tried to set a intrade boolean based on strategy.position but it didnt work. PineScript is new to me so i might get something messed up in the code.
1

There are 1 best solutions below

0
On

I think your problem will be solved by editing this line:

Source:
if (longCondition == false and shortCondition == false)
    stoplossshort1 := stoplossshort
    stoplosslong1 := stoplosslong
    takeProfitLevelLong1 := takeProfitLevelLong
    takeProfitLevelShort1 := takeProfitLevelShort

New edited code:
if (longCondition or shortCondition)
    stoplossshort1 := stoplossshort
    stoplosslong1 := stoplosslong
    takeProfitLevelLong1 := takeProfitLevelLong
    takeProfitLevelShort1 := takeProfitLevelShort

Seems other parts of your code work correctly, by changing the above condition, Long and short TP/SLs just update when a new entry condition is triggered, else the previous TP/SL's don't change. Your problem was varying of TP/SL and you can see the new result in the below picture:

enter image description here