pine-scriptpine-script-v5tradingview-apitradingalgorithmic-trading

TradingView Pine Script Breakout Strategy - Problem same bar close


I build a breakout strategy in Pine Script:

So if it hits the take profit of a long position it closes and waits for a short signal. If it doesn't hit the take profit the trade is reversed in the event of a short signal. Vice versa for a short trade. It works well, except when the strategy is entered and closed at the same bar due to a take profit. It then opens a new trade in the same direction if the long/short condition is valid (see screenshot below).

I cannot figure out how to avoid this. I've tried several combinations with strategy.opentrades.*, *profit, bar_index variables etc. trying to check if the last bar was a take profit or how many bars are between the last take profit and the new entry, but I cannot figure out the right combination. Or maybe there's an easier way.

Edit: If it helps, it's a futures trading strategy. So if you want to check yourself you could use ES1! or NQ1! on a 30 minutes chart for example.

Here's the code:

//@version=5

strategy(title="Breakout", overlay = true, process_orders_on_close = false)
periodhh = input(100)
periodll = input(50)
reverse = input(false, title="Trade reverse")
hh = ta.highest(periodhh)
ll = ta.lowest(periodll)

pos=0
pos :=  close > hh[1] ? 1 : close < ll[1] ? -1 : nz(pos[1], 0)

if (pos==1 and pos[2]!=1)
    strategy.entry("Long", strategy.long)
    strategy.exit("Take Profit", profit=25)
    
if (pos==-1 and pos[2]!=-1)
    strategy.entry("Short", strategy.short)   
    strategy.exit("Take Profit", profit=25)

enter image description here


Solution

  • In my example above, 25 ticks is very little for BTC

    for debug profit, try this:

    if strategy.position_size > 0 and strategy.position_size[1] <= 0
        profitLevel := open + syminfo.mintick * 25
    if strategy.position_size < 0 and strategy.position_size[1] >= 0
        profitLevel := open - syminfo.mintick * 25
    
    plot (
       profitLevel,
      'profitLevel',
      linewidth      = 2,
      color          = color.new(color.red, 0),
      style          = plot.style_steplinebr)
    

    test

    full code:

    //@version=5
    
    strategy(title="Breakout", overlay = true, process_orders_on_close = false)
    periodhh = input(100)
    periodll = input(50)
    reverse = input(false, title="Trade reverse")
    hh = ta.highest(periodhh)
    ll = ta.lowest(periodll)
    
    pos=0
    pos :=  close > hh[1] ? 1 : close < ll[1] ? -1 : nz(pos[1], 0)
    
    var float profitLvl = na
    
    if (pos==1 and pos[1]!=1)
        strategy.entry("Long", strategy.long)
    
    if (pos==-1 and pos[1]!=-1)
        strategy.entry("Short", strategy.short)   
    
    strategy.exit("Take Profit", profit=25)
    
    plot (
       hh,
      'hh',
      linewidth      = 2,
      color          = color.new(color.gray, 0),
      style          = plot.style_line)
    plot (
       ll,
      'll',
      linewidth      = 2,
      color          = color.new(color.silver, 0),
      style          = plot.style_line)
    plot(
       pos,
      "pos",
      editable = false, display = display.data_window)
    
    if strategy.position_size > 0 and strategy.position_size[1] <= 0
        profitLvl := open + syminfo.mintick * 25
    if strategy.position_size < 0 and strategy.position_size[1] >= 0
        profitLvl := open - syminfo.mintick * 25
    
    plot (
       profitLvl,
      'profitLvl',
      linewidth      = 2,
      color          = color.new(color.red, 0),
      style          = plot.style_steplinebr)