pine-script - 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)
解决方案
我使您的脚本正常工作,我复制/粘贴了下面的代码,并附有注释来解释:
//@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)
推荐阅读
- php - download from s3 bucket ajax php
- android - 在 SIP 关闭时发送空键盘信息
- python-3.x - 使用 python3 访问 https 页面
- angular - 尝试将值添加到 Angular 5 中的类数组变量时出错
- javascript - 侦听 nodejs express 应用程序对 EC2 上端口 3000 的 HTTPS 请求
- python - 用 Django 脆表单隐藏字段
- java - 并行执行 - Cucumber-JVM v4.0.0
- python - 特征向量歧义 - 如何强制执行特定的符号约定
- django - Django以与输入不同的顺序打印多对多的孩子
- java - Spring Boot 应用程序启动时没有正确的应用程序名称