Search code examples
pine-scriptpine-script-v5

take profit is not working but stop loss is pinescript


hello i'm trying to set a specific take profit amount in pine script code but it is skipping take profit amount and just hitting stop loss amount

//@version=5
strategy("Golden cross","GC", overlay = true)

ma9 = ta.sma(close,9)
ma50 = ta.sma(close,50)
prevoiusma50 = ma50[1]< ma50

//plotting Moving averages on chart
plot(ma9)
plot(ma50,color =color.white)

// when 50 ma crosses 200 ma upwards and previous ma level smaller new ma level 
crossover = ta.crossover(ma9,ma50) and prevoiusma50
plotshape(crossover,"co", color = color.green, text = "buy")

//if statment stating strategy set to buy when ma9 crosses ma50
if crossover == true 
    strategy.entry("Enter Long", strategy.long)
    strategy.exit("Exit Long", from_entry="Enter Long", profit=600)
    strategy.exit("Exit Long", from_entry="Enter Long", loss=250)

the script runs fine having no errors so i don't know what to try to fix it

example on chart


Solution

  • That's because your second strategy.exit() modifies the first exit order. You can actually combine (and you should) those two together.

    if crossover == true 
        strategy.entry("Enter Long", strategy.long)
        strategy.exit("Exit Long", from_entry="Enter Long", profit=600, loss=250)