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
  • Find last Green (close above the open) candle or last Red candle (close below the open)

Find last Green (close above the open) candle or last Red candle (close below the open)

Mar 21, 2022 - 9:54 PM

Viewed 1403 times

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

    This seems pretty straightforward but I've been looking for days for a solution with nothing to show.

    I am looking for Pinescript code to find the last (most recent) green ( close above open) or red (close below open) candle from the current candle. I want to return the opening price in either scenario.
    Thank you for any help!

    0
  • Hi

    I'd use the barstate.islast function for that

    green_candle = open[1] > close[1]
    red_candle = open[1] < close[1]
    
    if barstate.islast
    
       open_price_green = ta.valuewhen(green_candle, 0, open)
       open_price_red = ta.valuewhen(red_candle, 0, open)
    
    

    I haven't tested :) but it should work

    0
CONTENTS