简体   繁体   English

Pine Script 中的线条角度

[英]Angle of Line in Pine Script

I would like to find price trend for last 200 bars in TradingView Pine Script language.我想在 TradingView Pine Script 语言中找到最近 200 根柱线的价格趋势。

I want to draw a line from latest bar (close) to 200 bars older one.我想从最新的柱线(收盘价)到 200 柱线的旧柱线之间画一条线。 Then I want to calculate the angle of the line in degrees to see how much bullish or bearish the trend.然后我想以度为单位计算线的角度,以查看趋势看涨或看跌的程度。

I can do this by Regression Trend tool in TradingView drawing screen easily.我可以通过 TradingView 绘图屏幕中的回归趋势工具轻松完成此操作。 I want to do the same thing programmatically.我想以编程方式做同样的事情。

I guess the angle can be found by this formula (Java):我想可以通过这个公式(Java)找到角度:

double rads = Math.Atan((line.Y2 - line.Y1) / (line.X2 - line.X1));
double degrees = rads * (180f / Math.PI);

Can you give me an example?你能给我一个例子吗?

Thanks谢谢

You can access the historical values of a series type with the history referencing operator [] .您可以使用历史引用运算符[]访问系列类型的历史值。 So, for example;所以,例如; close[1] will give you yesterday's close price, which is also a series. close[1]会给你昨天的收盘价,也是一个系列。

Your formula to find the angle is correct.你找到角度的公式是正确的。 Your y2 - y1 is close - close[200] and your x2 - x1 is 200 - 0 .你的y2 - y1close - close[200]而你的x2 - x1200 - 0 So, what you need to calculate is atan((close - close[200]) / 200) .所以,你需要计算的是atan((close - close[200]) / 200)

Here is an indicator that colors the background depending on the value of the angle in radians.这是一个指示器,它根据以弧度为单位的角度值为背景着色。 You can play with the input to try out different ranges.您可以使用输入来尝试不同的范围。

//@version=3
study(title="Angle Bg", overlay=true)
x = input(title="Range", minval=1, defval=5)
y = close - nz(close[x])
angle = atan(y/x) // radians
color = angle < 0 ? green : red
bgcolor(color, transp=70)

Below piece of code is for debugging purposes.下面的一段代码用于调试目的。 It plots the angle in radians.它以弧度绘制角度。

//@version=3
study(title="Angle", overlay=false)
x = input(title="Range", minval=1, defval=5)
y = close - nz(close[x])
angle = atan(y/x) // radians
plot(angle, title="Angle", linewidth=4)
hline(0, color=gray, linestyle=dotted, linewidth=3)

Below code is also for debugging purposes.下面的代码也用于调试目的。 It plots the current close price and close[x].它绘制了当前收盘价和 close[x]。 So you don't need to go back and forth while calculating the angle manually :)因此,您无需在手动计算角度时来回走动:)

//@version=3
study("Close")
range = input(title="Range", type=integer, minval=1, defval=5)
plot(close, title="close", linewidth=4, color=orange)
plot(nz(close[range]), title="close[]", linewidth=4, color=green)

Note: I found using radians more useful than degrees.注意:我发现使用弧度比使用度数更有用。 But if you want to use degrees in your indicator, you might as well apply your formula to angle variable.但是,如果您想在指标中使用度数,您不妨将公式应用于angle变量。 Please note that pine-script does NOT have any built-in variables for pi .请注意pine-script没有任何内置变量pi So, you are gonna have to type that manually.所以,你将不得不手动输入。

If you add those three indicators to your chart, you should get something similar to this:如果您将这三个指标添加到您的图表中,您应该会得到类似于以下内容的信息: 在此处输入图片说明

You can create an "angle" oscillator to measure line angles.您可以创建一个“角度”振荡器来测量线角度。

//@version=4
study("Angle Oscillator", overlay=false)

src = input(title="Source", type=input.source, defval=close)
price2bar_ratio = input(title="Price To Bar Ratio", type=input.float, defval=5.0)

get_degrees(src, price2bar_ratio) => (180.0 / (22.0 / 7.0)) * atan(change(src) / price2bar_ratio)

plot(get_degrees(src, price2bar_ratio))

The price2bar_ratio is the value from Chart settings > Scales > Lock Price To Bar Ratio . price2bar_ratio是来自Chart settings > Scales > Lock Price To Bar Ratio


The ratio itself is up to you since you're the one that decides what is a "steep" or "flat" angle.比率本身取决于您,因为您是决定什么是“陡峭”或“平坦”角度的人。 The catch is that to compare angles effectively (the price chart with the angle indicator) you'll have to use the same price to bars ratio for that symbol/timeframe for both chart and indicator.问题在于,要有效地比较角度(价格图表与角度指标),您必须对图表和指标的该交易品种/时间范围使用相同的价格柱比。

So if your chart's price scale is set to Auto scaling, you'll get a different chart angle for the same price with every change in zoom (the indicator angle values won't be affected).因此,如果您的图表的价格比例设置为自动缩放,您将在每次缩放时为相同价格获得不同的图表角度(指标角度值不会受到影响)。 To get the same chart angle no matter how much you zoom in or out, right click on the scale and make sure Lock Price To Bar Ratio is checked.无论放大或缩小多少,要获得相同的图表角度,请右键单击刻度并确保选中锁定价格与柱线比率

To use:使用:

  1. save the above angle oscillator so it appears in Indicators > My scripts保存上述角度振荡器,使其出现在Indicators > My scripts
  2. add a MA indicator to the chart向图表添加 MA 指标
  3. click that indicator's More > Add Indicator on (MA)单击该指标的More > Add Indicator on (MA)
  4. select the angle oscillator from My scriptsMy scripts选择角度振荡器
  5. adjust the angle oscillator's Price To Bar Ratio value调整角度振荡器的Price To Bar Ratio

For a more advanced version see https://www.tradingview.com/script/D8RA0UqC-Cosmic-Angle/有关更高级的版本,请参阅https://www.tradingview.com/script/D8RA0UqC-Cosmic-Angle/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM