比如,当adx指标小于50时,K线图上取第一次adx小于50时的收盘价画一条水平线,直到adx指标不满足小于50的条件时直线结束
4条回答 默认 最新
关注引自免费微信小程序:皆我百晓生
在 Pine 语言中,你可以通过以下步骤实现这个功能:
- 计算 ADX 指标。
- 使用
plot()函数绘制水平线。 - 使用
if语句判断 ADX 是否满足条件,并在满足条件时重新绘制水平线。
首先,确保你已经导入了必要的数据和计算 ADX 所需的函数。如果你还没有安装TA-Lib库,你需要先安装它。在 TradingView 中,可以在研究窗口顶部点击 "Install Studies",然后搜索 "Talib" 并安装。
//@version=4 study("ADX Horizontal Line", overlay=true) // Import the TA-Lib library for ADX calculation import ta // Define input parameters length = input(title="ADX Length", type=input.integer, defval=14) src = input(close, title="Source") // Calculate ADX adx = ta.ADX(high=high, low=low, close=close, length=length) // Draw a horizontal line only when ADX is less than 50 lineColor = adx < 50 ? color.red : na lineStyle = adx < 50 ? line.style_dashed : na lineWidth = adx < 50 ? 2 : na // Find the first occurrence of ADX being less than 50 and store its index firstOccurrenceIndex = 0 for i = 1 to bar_index if adx[i] < 50 and (i == 1 or adx[i - 1] >= 50) firstOccurrenceIndex := i break // Get the price at the first occurrence index priceAtFirstOccurrence = close[firstOccurrenceIndex] // Draw the line line hl = line.new(x1=firstOccurrenceIndex, y1=priceAtFirstOccurrence, x2=bar_index, y2=priceAtFirstOccurrence, color=lineColor, style=lineStyle, width=lineWidth) // Update the line position on every bar hl.x2 := bar_index这段代码会计算 ADX 指标,并在 ADX 小于 50 的情况下,在 K 线图上画出一条红色虚线。这条线会从第一个 ADX 小于 50 的价格开始,一直延伸到当前的 bar(除非 ADX 不再小于 50)。
注意:Pine 语言中的索引是从 0 开始的,因此我们使用
firstOccurrenceIndex + 1来获取对应的日期。另外,bar_index是一个特殊的变量,表示当前的 bar 数量。解决 无用评论 打赏 举报 编辑记录