Angle of Line in Pine Script

99封情书 提交于 2021-02-07 03:08:40

问题


I would like to find price trend for last 200 bars in TradingView Pine Script language.

I want to draw a line from latest bar (close) to 200 bars older one. 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. I want to do the same thing programmatically.

I guess the angle can be found by this formula (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


回答1:


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.

Your formula to find the angle is correct. Your y2 - y1 is close - close[200] and your x2 - x1 is 200 - 0. So, what you need to calculate is 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]. 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. Please note that pine-script does NOT have any built-in variables for 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:




回答2:


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.


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
  2. add a MA indicator to the chart
  3. click that indicator's More > Add Indicator on (MA)
  4. select the angle oscillator from My scripts
  5. adjust the angle oscillator's Price To Bar Ratio value

For a more advanced version see https://www.tradingview.com/script/D8RA0UqC-Cosmic-Angle/



来源:https://stackoverflow.com/questions/52650047/angle-of-line-in-pine-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!