首页 > 解决方案 > Pine 脚本 - 当多个条件为真时输入位置

问题描述

几天来我一直在尝试解决这个问题,我知道答案会很简单......

我想在多个条件为真时输入一个位置。编译脚本时不会产生任何错误,但图表上没有出现应有的买卖(也许代码中还有其他问题??)。我尝试了很多东西,但这是我最近的尝试。

//@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)

标签: pine-script

解决方案


我使您的脚本正常工作,我复制/粘贴了下面的代码,并附有注释来解释:

//@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)

推荐阅读