NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 01-12-2010, 10:07 AM   #1
rperez
Member
 
Join Date: Aug 2008
Posts: 62
Thanks: 0
Thanked 0 times in 0 posts
Default Need Help in Calculating Slope to Degrees

Hi,

I found the following formula to calculate the slope of a line:


public int CalcSlope(IDataSeries line)
{
double radToDegrees = 180/Math.PI;
return (int)(radToDegrees*(Math.Atan((line[0]-(line[1]+line[2])/2) /1.5/TickSize))) ;
}
I want to enable the calculation of the slope in degrees given any number of bars, from 1 to n. I don't understand why the line is divided by 1.5.

Would the following formula be valid? Say I want to calculate the slope for the last 3 bars:

return (int)(radToDegrees*(Math.Atan((line[0]-(line[1]+line[2]+line[3])/3) /1.5/TickSize)))

If not, what is the formula?

Thanks.

rpq
rperez is offline  
Reply With Quote
Old 01-12-2010, 10:37 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,411
Thanks: 252
Thanked 976 times in 959 posts
Default

rpg, you could also check into our Slope() method - http://www.ninjatrader-support.com/H...eV6/Slope.html

This will give you a rise over run calculation between two points, you would then need to add logic for converting to degrees...

The cited logic for converting to degress is valid, not sure why the 1.5 divisor is needed - best is to print / plot the result and then verify by hand / visuals.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 01-18-2010, 02:22 PM   #3
MJT
Senior Member
 
Join Date: Jun 2006
Posts: 111
Thanks: 0
Thanked 1 time in 1 post
Default

It looks as if (line[1]+line[2])/2) is calculating the value of 'line' at a point 1/2 way between 2bars ago and 1bar ago ~ that is 1 1/2 bars ago from line[0]. Hence the use of 1.5 which is normalizing the slope to give a rise value for one bar.

To get the angle in degrees for 3 bars, I think this will work, but not tested:-
return (int)(radToDegrees*(Math.Atan((line[0]-line[3])/3/TickSize)));
MJT is offline  
Reply With Quote
Old 01-19-2010, 08:04 PM   #4
rperez
Member
 
Join Date: Aug 2008
Posts: 62
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks MJT..... I'll try it out to see if it works. I tried with the Slope() function, but it doesn't seem to give correct results.

Regards,

RPQ
rperez is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating fair value? Bogan7 NinjaScript File Sharing Discussion 4 04-30-2012 01:46 PM
Calculating P&L by day? leonchan1204 Strategy Development 1 06-08-2009 06:14 AM
Calculating a limit entry kevinh786 Strategy Development 5 12-08-2008 05:35 AM
Calculating on Bar Close... kjkraus Strategy Analyzer 1 09-07-2007 07:28 PM
Calculating slope & percentage... funk101 General Programming 2 04-13-2007 03:01 AM


All times are GMT -6. The time now is 09:01 AM.