Pine Script 如何存储我的输入条件的high/low?

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

我想测试一个简单的策略。 如果我的条件“Test_Bar”正在发生,我想存储此柱的蜡烛高点(Bar_high)。如果在一些蜡烛之后价格达到“柱高”,我想执行以此价格买入限价订单。计算止损和止盈。

(这里有一个例子https://cdn.discordapp.com/attachments/669368497767448596/960639771183570974/unknown.png

但是脚本不起作用=( 有人可以帮我吗?

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.stack.imgur.com/LWy3F.png
  [2]: https://i.stack.imgur.com/bseQi.png

您可以使用 var 关键字使变量的值在每次执行时保持相同。

因此,您可以执行以下操作:

var float my_high = na
var float my_low = na

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