rr1050
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: January 11th, 2023

Strategy entry on exact price

Dear All,

This is my first question on forum. I am facing an issue while strategy testing because I want to enter in Trade only if next candle is high to previous .

I used a possible ways to sort out but it is not a precise solution, so pls guide.

Order is not placing at Buy_true value i.e. Buyprice, it is always on Close. My requirement is Entry on next Bar when High is above previous High, and entry price should Buyprice. (High + 2% I added formula).

I trying to add picture here in forum, but not showing option to browse and attached picture.
so uploaded in external link.

https://ibb.co/58dxT20

Code: Select all

// Next Bar after buySignal check for High greater than earlier high              DO NOT DELETE 
Buy_true = ta.valuewhen (buySignal,Buyprice,0)
Barhigh = high >= high [1]
Bar_Entry = (high >= high [1]) and (buySignal [1] )
plot (Buy_true)

if Bar_Entry
    strategy.entry(id="Long", direction=strategy.long, limit = Buy_true, stop = Buy_true)
Thanks

purplemint22
Pine Script Rookie
Pine Script Rookie
Posts: 17
Joined: October 14th, 2022

Re: Strategy entry on exact price

I would suggest playing around with the barstate commands when assigning the variable.

You could try something like

if barstate.isnew and high > high[1]
buyprice = high + 2%

Although, the problem with this is that it is subject to repainting. TradingView suggests using barstate.isconfirmed as a way to avoid repainting within strategy scripts.

So, you really want the bar to close first, which means there might be somewhere that you can assign the high value instead of the close value, but I'm not sure where that would be because I can't see how you are building the Buyprice variable.

But, for example, if you wanted to use the high instead of the close for an ema, you would say

ema = ta.ema(high, 20)

instead of

ema = ta.ema(close, 20)

This way the barstate will be confirmed, but you will be saving the high value for the bar to use in your variable.

rr1050
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: January 11th, 2023

Re: Strategy entry on exact price

Hi, Thanks for reply.

barstate.isnew is better and now at least I can see the entry on High means Blue line exact.

Many Thanks for your help.

Code: Select all

/ Next Bar after buySignal check for High greater than earlier high              DO NOT DELETE 
Buy_true = ta.valuewhen (buySignal,Buyprice,0)
Barhigh = barstate.isnew and high >= high [1]
Bar_Entry = Barhigh and (buySignal [1])
plot (Buy_true)

Return to “Pine Script Q&A”