Pine 脚本 - 当多个条件为真时输入位置
Posted
技术标签:
【中文标题】Pine 脚本 - 当多个条件为真时输入位置【英文标题】:Pine script - Enter position when multiple conditions are true 【发布时间】:2018-07-01 07:06:05 【问题描述】:我这几天一直在尝试解决这个问题,我知道答案会很简单......
我想在多个条件为真时输入一个位置。编译脚本时没有创建错误,但图表上没有出现应有的买卖(也许代码中还有其他问题??)。我尝试了很多东西,但这是我最近的尝试。
//@version=3
strategy("Easy BTC Trading", overlay=true,pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)
Multiple = input(2, minval=0,step=0.01)
ATR1 = atr(21)*Multiple
volumeavg = (volume[2]+volume[3]+volume[4])/3
highest = highest(high,5)
lowest = lowest(low,5)
long1 = high > highest
long2 = volume[1] >= volumeavg*2
go_long = high > highest and volume[1] > volumeavg*2
exit_long = low < lowest
strategy.entry("Long",strategy.long, when = go_long)
strategy.exit("Exit long","Long", profit = 5, stop = strategy.position_avg_price - ATR1, when = exit_long)
【问题讨论】:
嗨 Ben,您是否尝试通过硬编码go_long
进行调试?我会尝试用一些 if-then 语句替换尝试的布尔 AND
操作......
【参考方案1】:
我让你的脚本工作了,我复制/粘贴了下面的代码,用 cmets 来解释:
//@version=3
strategy("Easy BTC Trading", overlay=true,pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)
Multiple = input(2, minval=0,step=0.01)
ATR1 = atr(21)*Multiple
volumeavg = (volume[2]+volume[3]+volume[4])/3
//I guess that you want to know the highest value for the last 5 bars
//if yes, in the highest & lowest functions, you need to use close[1]
//which is the previous close before the actual one
//if you just use close, your actual close can be the highest one
//so your long order would never trigger as your close would never
//be higher than the highest
highest = highest(close[1],5)
lowest = lowest(close[1],5)
long1 = close > highest
long2 = volume[1] >= (volumeavg*2)
//go_long = high > highest and volume[1] > volumeavg*2
//there was a problem with that condition above, so I wrote it a clearer way,
// with an if and it works better
go_long = 0
if (close > highest and volume[1] > volumeavg)
go_long := 1
exit_long = close < lowest
strategy.entry("Long",strategy.long, when = go_long)
strategy.exit("Exit long","Long", profit = 5, stop = strategy.position_avg_price - ATR1, when = exit_long)
【讨论】:
【参考方案2】:Vsoler 是对的。高永远不会高于最高。 当一个新的高点达到高点等于最高点时,您需要将当前蜡烛从最高点检查中取出。
改变这个:highest = highest(high,5)
致此:highest = highest(high[1],5)
这样您将传递给“highest()”的最后 5 根蜡烛,不包括当前蜡烛。
【讨论】:
【参考方案3】:你认为“high”可以大于“highest”吗?也许它可以大于最高[1]
问候 维森特·索勒
【讨论】:
以上是关于Pine 脚本 - 当多个条件为真时输入位置的主要内容,如果未能解决你的问题,请参考以下文章
Javascript:<script src=jquery...仅当条件为真时
当某个条件为真时,如何在 Eclipse 中以编程方式设置 C++ 断点?