Max
Max

Reputation: 45

Pine Script how to store the high/low of my entry condition?

I want to test a simple strategy. If my Condition "Test_Bar" is happening, I want to store the Candle high ("Bar_high) of this bar. If after some candles the price reaches the "Bar-high", I want to execute a Buy limit order at this price. Stop Loss and Take profit are calculated.

(Here an example https://cdn.discordapp.com/attachments/669368497767448596/960639771183570974/unknown.png)

But the script doesn't work =( Can anybody help me pls?

strategy("Test-Bar", overlay=true)

number=input(10)

Test_Bar = high>high[2] and low<low[2] 

if (Test_Bar==true)
    Bar_high := high
    Bar_low := low

//Stoploss + Take profit
SL = input(0.5)  
TP = input(2.5)  

longstop = (Bar_high - Bar_low)*SL + Bar_low  //Stop-Loss calculated
longprofit = longstop * TP  + Bar_high      //Take Profit Calculated

//Position entry + exit
strategy.entry("My Long Entry Id", strategy.long, limit=Bar_high)

if strategy.position_avg_price >0
    strategy.exit(stop=longstop or stop=longprofit)


  [1]: https://i.sstatic.net/LWy3F.png
  [2]: https://i.sstatic.net/bseQi.png

Upvotes: 1

Views: 1137

Answers (1)

vitruvius
vitruvius

Reputation: 21294

You can use the var keyword to keep a variable's value the same for each execution.

So, you can do the following:

var float my_high = na
var float my_low = na

if (Test_Bar)
    my_high := high
    my_low := low

Upvotes: 1

Related Questions