Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angle of Line in Pine Script

Tags:

pine-script

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

like image 559
Phillip Avatar asked Oct 04 '18 15:10

Phillip


People also ask

How do you calculate an angle in a pine script?

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.

How do you measure angles in TradingView?

To use: Add any moving average indicator to the chart Click that indicator's More > Add Indicator on (MA) Select the Cosmic Angle indicator Adjust the Cosmic Angle 's Price To Bar Ratio value to reflect that of your chart's Adjust...

What is ATR in Pine script?

An important trading skill is to interpret price volatility and include this measure of risk in our trading plan. One way to measure price volatility is with the Average True Range (ATR).


2 Answers

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: enter image description here

like image 158
vitruvius Avatar answered Dec 30 '22 15:12

vitruvius


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/

like image 21
galki Avatar answered Dec 30 '22 17:12

galki