[Solved] Calculating angles with atan2()

Have a QScript to share, or need help with programming? Post your comment here.
Post Reply
sbank
Posts: 188
Joined: Tue Jul 21, 2015 9:35 pm
Contact:

[Solved] Calculating angles with atan2()

Post by sbank » Thu Oct 24, 2024 1:51 pm

Solved:
I'll leave this here if anyone else is searching. But I made a silly mistake. I was using the bar numbers and prices as is when I fed it to atan2(). Since I wanted the angle that *is on the screen* I needed to use bar_to_pixel() and price_to_pixel(). I am embarrassed that it took me a couple hours this morning to figure out my mistake. :roll:

~~~

Hi All,

I am trying to calculate the angle of a trendline via Qscript. For the life of me I cannot find my mistake. :D

Here is a picture for reference. I have the tops and bottoms labeled in red with the variable names I used for the bar and price values. In teal I print the angle. And as you can see the angle is nowhere close to what the protractor is saying it should be.
2024-10-24_09-44-08.png
2024-10-24_09-44-08.png (33.24 KiB) Viewed 12612 times
Here is a snippet of my QScript (mround() is a simple function I wrote to just truncate. Similar to the Excel variant.):

Code: Select all

tl = trendline(lastTopBar, lastTop, newBottomBar, newBottom, red);
#trendline(bar1, price1, bar2, price2, color)
#atan2(bar2-bar1,price2-price1)
redAngle = 270-mround(atan2(newBottomBar-lastTopBar,lastTop-newBottom),1);                 
I'll probably try just atan() instead of atan2(), but I am not hopeful... Would love some thoughts.

thanks :lol:
Steven
Last edited by sbank on Thu Oct 24, 2024 3:20 pm, edited 1 time in total.

sbank
Posts: 188
Joined: Tue Jul 21, 2015 9:35 pm
Contact:

Re: Calculating angles with atan2()

Post by sbank » Thu Oct 24, 2024 2:01 pm

Edit:

I removed mround() and adjusted the "y" variables to atan2(). Still doesn't come close to what I would expect. I figured I would have to add or subtract 90 or 270 or something like that. But still doesn't seem plausible with the number I get from atan2().
2024-10-24_09-58-37.png
2024-10-24_09-58-37.png (36.48 KiB) Viewed 12611 times

Code: Select all

tl = trendline(lastTopBar, lastTop, newBottomBar, newBottom, red);
#trendline(bar1, price1, bar2, price2, color)
#atan2(bar2-bar1,price2-price1)                          
redAngle = atan2(newBottomBar-lastTopBar,newBottom-lastTop); 
Second Edit:
Here is the code I am using to print the text on the chart:

Code: Select all

if (barnum > newBottomBar) text(lastTopBar,lastTop+10,"lastTopBar/lastTop",red,tx_center,10);
if (barnum > newBottomBar) text(newBottomBar,newBottom-10,"newBottomBar/newBottom",red,tx_center,10);
if (barnum > newBottomBar) text(lastTopbar,lastTop+20,redAngle,teal,tx_center,10);

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests