'Why the script doesn't work after the first trade?

This is a dummy strat.

Long = Macd Fast > Macd Slow

Stop Loss = Previous Williams fractal

Take Profit = 1.5 R/R ratio from Stop Loss.

After the first trade, the script is not working. I don't see where is the mistake. Anyone can help before I hit the screen? :/

//@version=4
strategy("BT@|MACD & Moving Averages", "MACD & MA'S", true) 

h = input(2)
piv_low  = pivotlow(h, h)       //orange - long stop loss
plotshape(piv_low,  color=color.orange, location=location.belowbar, offset=-2)

// MACD

fastlen = input(12, "Fast Length", group="MACD")
slowlen = input(26, "Slow Length", group="MACD")
siglen  = input(9, "Signal Length", group="MACD")
src     = input(close, "Source", group="MACD")
[fast, slow, hist] = macd(src, fastlen, slowlen, siglen)


// Long Stop loss

l_entry_sl = strategy.position_size > 0 ? piv_low : na
l_track_entry_sl = valuewhen(strategy.position_size > strategy.position_size[1], l_entry_sl, 0)
l_dont_track_sl = strategy.position_size > 0 ? l_track_entry_sl : na

// Long Take Profit

l_entry_tp = (strategy.position_avg_price - l_track_entry_sl) * 1.5 + strategy.position_avg_price
plot(l_entry_tp, color=color.white)
l_track_entry_tp = valuewhen(strategy.position_size > strategy.position_size[1], l_entry_tp, 0)
plot(l_track_entry_tp, color=color.purple)
l_dont_track_tp = strategy.position_size > 0 ? l_track_entry_tp : na
plot(l_dont_track_tp, color=color.green)

long  = fast > slow
if long
    strategy.entry("Long", true)
    strategy.exit("P|or|L", "Long", limit=l_dont_track_tp, stop=l_dont_track_sl )


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source