Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

  • Forums
  • Pinescript
  • Placing Buy Limit Orders on Bollinger Bands' Lower Band in Pine Script 5

Placing Buy Limit Orders on Bollinger Bands' Lower Band in Pine Script 5

Jul 24, 2023 - 3:25 PM

Viewed 404 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/1780430 COPY
  • Hello everyone,

    I'm trying to implement the following in Pine Script 5, but unfortunately, it's not working: I want to place a Buy Limit Order on the lower band of the Bollinger Bands. Whenever the price touches the lower band, a limited buy order should be triggered. Unfortunately, I can't seem to get it right. Here's my test code and an image for clarification. How can I make sure that a Buy Limit Order is placed for every bar that is above the lower band?

    //@version=5
    strategy("Bollinger Bands Limit Order", overlay = true)
    
    length = input.int(20, minval=1)
    mult = input.float(2.0)
    src = close
    basis = ta.sma(src, length)
    dev = mult * ta.stdev(src, length)
    lowerBand = basis - dev
    upperBand = basis + dev
    
    plot(basis, "Basis", color=color.blue)
    p1 = plot(lowerBand, "Lower Band", color=color.red)
    p2 = plot(upperBand, "Upper Band", color=color.green)
    fill(p1, p2, color=color.new(color.purple, 80))
    
    if (close < lowerBand or open < lowerBand)
        strategy.entry("Buy", strategy.long, limit=lowerBand)
        label.new(bar_index, low, text = "Buy", color = color.green, style = label.style_label_down, yloc = yloc.belowbar)
    

    1.png

    0
  • Hi James

    How can I make sure that a Buy Limit Order is placed for every bar that is above the lower band?

    Here your code test if the candle open and close is below the lower band (not testing if it's above)

    if (close < lowerBand or open < lowerBand)

    2/ More importantly, a strategy needs either a buy and sell conditions or a buy or sell condition with some exits (SL and/or TP) conditions
    Because, a trade is made of an entry or exit

    Probably your strategy doesn't take any trade because you didn't configure the exit

    You can use the strategy.exit or strategy.close functions for that

    0
CONTENTS