Search code examples
pine-scriptpine-script-v5

How to reverse entry signals


I have my strategy, everything is working fine, but I am a complete beginner in Pine Script so I am absolutely not able to figure out how I can do this. I have my Entry Conditions and I applied the Flip Signals-Options which I want to enable and disable.

Logically it should work like this:

  • If "Flip Buy" is disabled, do everything like it is doing now
  • If "Flip Sell" is disabled, do everything like it is doing now
  • If "Flip Buy" is enabled, enter a Short Position when "LongEntrySignal" is detected insted of entering a Long Position
  • If "Flip Sell" is enabled, enter a Long Position when"ShortEntrySignal" is detected instead of entering a Short Position.
//1: In the Inputs Section of the Strategy I want to enable/disable a:
//"Flip Buy Signal to Short Entry" and
//"Flip Sell Signal to Long Entry"

//*** FLIP ENTRY SIGNALS
flipLongSignal = input(title='Flip Buy Signal to Short Entry?', defval=false, group="Flip Entry Signals")
flipShortSignal = input(title="Flip Sell Signal to Long Entry?", defval=false, group="Flip Entry Signals")


// 2:
// Detect Entry Signals
LongEntrySignal =
     volBuy and didiBuy and
     open > baseLine and 
     close > baseLine and 
     not na(atr3)
     and barstate.isconfirmed
ShortEntrySignal = volSell and didiSell and
     open < baseLine and
     close < baseLine and
     not na(atr3)
     and barstate.isconfirmed


// Draw Data to Chart: Long & Short Entry Signal
plotshape(LongEntrySignal, style=shape.labelup, color=color.rgb(34, 255, 5), location=location.belowbar, text="Buy", title="confirmed Buy Signal")
plotshape(ShortEntrySignal, style=shape.labeldown, color=color.rgb(255, 59, 59), location=location.abovebar, text="Sell", title="confirmed Sell Signal")


// 3:
// Calculated Stops and Targets


// 4: Enter Buy and Sell Orders
// Enter Buy Orders
if LongEntrySignal and strategy.position_size == 0
     // a little bit of code for stop, target, position size
     strategy.entry(id="Long", direction=strategy.long, qty=positionSize)

// Enter Sell Orders
if ShortEntrySignal and strategy.position_size == 0 
     // a little bit of code for stop, target, position size
    strategy.entry(id="Short",direction=strategy.short, qty=positionSize)


// 5: Exit
// Manage Exit Orders (TP & SL)
strategy.exit(id="Long Exit", from_entry="Long", limit=t_target, stop=t_stop, when=strategy.position_size > 0)
strategy.exit(id="Short Exit", from_entry="Short", limit=t_target, stop=t_stop, when=strategy.position_size < 0)

Solution

  • You should have 2 blocks for entering Buy order :

    if LongEntrySignal and strategy.position_size == 0 and not(flipLongSignal)
        # Enter a Long
    

    and

    if LongEntrySignal and strategy.position_size == 0 and flipLongSignal
        # Enter a Short
    

    and 2 blocks for entering Short order :

    if ShortEntrySignal and strategy.position_size == 0 and not(flipShortSignal)
         # Enter a Short
    

    and

    if ShortEntrySignal and strategy.position_size == 0 and flipShortSignal
         # Enter a Long