在 Pine Script 中设置 'flags' 买入并持有
Setting 'flags' in Pine Script to buy and hold
我很难理解这门语言。
我想设置一些不会改变每个柱线的变量,以便我以后可以在 if/elses 语句中更改它们。
例如:
f(x) => math.ceil(x / 500) * 500
g(x) => math.floor(x / 500) * 500
if bar_index == 0
start_price = g(close / 1.15)
end_price = f(close)
else:
if close <= start_price
strategy.entry('Long', strategy.long, 100)
strategy.exit('Long', when=end_price)
start_price = g(close - 500)
end_price = f(start_price*1.15)
但是我当然不能那样做,因为 start_price 和 end_price 是未声明的。但是如果我在 if 语句之外声明它们,它们将更改每个新柱并且永远不会满足条件 close <= start_price
您需要在声明它们时使用 var
关键字,以避免它们在每个柱上都发生变化。
请阅读Variable declarations chapter in the Pine Script 5 User Manual。
另请查看 What’s the difference between ==, = and :=? at PineCoders.
这会让事情更清楚。
您修改后的示例:
//@version=5
strategy("My Script", overlay=false)
var float start_price = na
var float end_price = na
f(x) => math.ceil(x / 500) * 500
g(x) => math.floor(x / 500) * 500
if bar_index == 0
start_price := g(close / 1.15)
end_price := f(close)
else
if close <= start_price
strategy.entry('Long', strategy.long, 100)
strategy.exit('Long', limit=end_price)
start_price := g(close - 500)
end_price := f(start_price*1.15)
plot(na)
我很难理解这门语言。
我想设置一些不会改变每个柱线的变量,以便我以后可以在 if/elses 语句中更改它们。
例如:
f(x) => math.ceil(x / 500) * 500
g(x) => math.floor(x / 500) * 500
if bar_index == 0
start_price = g(close / 1.15)
end_price = f(close)
else:
if close <= start_price
strategy.entry('Long', strategy.long, 100)
strategy.exit('Long', when=end_price)
start_price = g(close - 500)
end_price = f(start_price*1.15)
但是我当然不能那样做,因为 start_price 和 end_price 是未声明的。但是如果我在 if 语句之外声明它们,它们将更改每个新柱并且永远不会满足条件 close <= start_price
您需要在声明它们时使用 var
关键字,以避免它们在每个柱上都发生变化。
请阅读Variable declarations chapter in the Pine Script 5 User Manual。
另请查看 What’s the difference between ==, = and :=? at PineCoders.
这会让事情更清楚。
您修改后的示例:
//@version=5
strategy("My Script", overlay=false)
var float start_price = na
var float end_price = na
f(x) => math.ceil(x / 500) * 500
g(x) => math.floor(x / 500) * 500
if bar_index == 0
start_price := g(close / 1.15)
end_price := f(close)
else
if close <= start_price
strategy.entry('Long', strategy.long, 100)
strategy.exit('Long', limit=end_price)
start_price := g(close - 500)
end_price := f(start_price*1.15)
plot(na)