4

嘿,我正在尝试将 strategy.entry 时的收盘价保存到一个变量中,以便以后可以使用它来退出。

if condition
    strategy.entry("long", true)
    buyprice=close
(strategy.exit("exit","long", when = close>buyprice*1.1) 

我得到错误:Undeclared identifier 'buyprice'。据我了解,这意味着该变量在 if 语句之外无效。有没有办法改变这个?在此先感谢您的帮助

4

2 回答 2

2

是我可以让它工作的唯一方法。

基本上,您在满足多头条件时设置先前的价格,然后在下一阶段从全局变量中检索该值。

//@version=2
...
buyprice=buyprice[1]


golong=...

if golong
    buyprice := close

goshort=... or close<=buyprice*0.95

strategy.entry("Long", long=true, when=golong)
strategy.close("Long", when=goshort)

希望这可以帮助!

于 2018-05-05T13:26:36.993 回答
0

将此 AFTER 添加到您用于输入头寸的代码中:

bought = strategy.position_size[0] > strategy.position_size[1]
entry_price = valuewhen(bought, open, 0)
于 2020-12-06T04:07:39.300 回答