Search code examples
pine-scriptpine-script-v5

Pine Script: Updating Entry Orders


My strategy sets a limit order at a moving average when a set of conditions are meant

Sometimes this can be considerably above / below the current price

I would like to have the limit price update at the end of every bar to the new MA price

Does anybody know if this is doable with pine-script v5?

Here is the code. I'm only focused on longs at the moment.

Appreciate the help

Edited to include the full code

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)

//-------------------------------------------------------------------------------------------------------------------------------------
//SQUEEZE CODE WITHOUT PLOTS
length = input.int(20, "TTM Squeeze Length")
//-------------------------------------------------------------------------------------------------------------------------------------
//BOLLINGER BANDS
BB_mult = input.float(2.0, "Bollinger Band STD Multiplier")
BB_basis = ta.sma(close, length)
dev = BB_mult * ta.stdev(close, length)
BB_upper = BB_basis + dev
BB_lower = BB_basis - dev
//-------------------------------------------------------------------------------------------------------------------------------------
//DEFINE EMA's
ema1 = ta.ema(close, 8)
ema2 = ta.ema(close, 21)
ema3 = ta.ema(close, 34)
ema4 = ta.ema(close, 55)
ema5 = ta.ema(close, 89)
ema6 = ta.ema(close, 250)
//-------------------------------------------------------------------------------------------------------------------------------------
//KELTNER CHANNELS
KC_mult_high = input.float(1.0, "Keltner Channel #1")
KC_mult_mid = input.float(1.5, "Keltner Channel #2")
KC_mult_low = input.float(2.0, "Keltner Channel #3")
KC_basis = ta.sma(close, length)
devKC = ta.sma(ta.tr, length)
KC_upper_high = KC_basis + devKC * KC_mult_high
KC_lower_high = KC_basis - devKC * KC_mult_high
KC_upper_mid = KC_basis + devKC * KC_mult_mid
KC_lower_mid = KC_basis - devKC * KC_mult_mid
KC_upper_low = KC_basis + devKC * KC_mult_low
KC_lower_low = KC_basis - devKC * KC_mult_low
//-------------------------------------------------------------------------------------------------------------------------------------
//SQUEEZE CONDITIONS
NoSqz = BB_lower < KC_lower_low or BB_upper > KC_upper_low //NO SQUEEZE: GREEN
LowSqz = BB_lower >= KC_lower_low or BB_upper <= KC_upper_low //LOW COMPRESSION: BLACK
MidSqz = BB_lower >= KC_lower_mid or BB_upper <= KC_upper_mid //MID COMPRESSION: RED
HighSqz = BB_lower >= KC_lower_high or BB_upper <= KC_upper_high //HIGH COMPRESSION: ORANGE
//-------------------------------------------------------------------------------------------------------------------------------------
//SETUP
setup = BB_lower >= KC_lower_mid or BB_upper <= KC_upper_mid ? 1 : 0
setupSum = math.sum(setup, 8)
//-------------------------------------------------------------------------------------------------------------------------------------
//BREAKOUT
breakOut = BB_lower < KC_lower_low or BB_upper > KC_upper_low ? 1 : 0
breakOutSum = math.sum(breakOut, 2)
//-------------------------------------------------------------------------------------------------------------------------------------
//TRADING CONDITIONS
condition1 = setupSum >= 2 and breakOutSum >= 2
//-------------------------------------------------------------------------------------------------------------------------------------
if (condition1 == true and close > ema4 and strategy.position_size == 0)
    strategy.entry("Long", strategy.long, limit = ema3)

Solution

  • You can update your pending order if you call strategy.entry() with the same id ("Long" in your case).

    strategy.entry

    It is a command to enter market position. If an order with the same ID is already pending, it is possible to modify the order. If there is no order with the specified ID, a new order is placed.

    Or you can use the strategy.order() function.

    strategy.order

    It is a command to place order. If an order with the same ID is already pending, it is possible to modify the order. If there is no order with the specified ID, a new order is placed.

    If you want to modify your order after your set of conditions are true, I would suggest you use a var flag to see if the order is placed.

    var order_placed = false
    
    if (condition1 == true and close > ema4 and strategy.position_size == 0)
        strategy.entry("Long", strategy.long, limit = ema3)
        order_placed := true
    
    if (order_placed)
        strategy.entry("Long", strategy.long, limit = ema3)     // Update the limit price to the ema value
    

    Don't forget to reset this flag (probably when you enter a trade).