[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Slope Differentiation



PureBytes Links

Trading Reference Links

I can't give you TS-code, becaue I don't have TS, but I can give you
the math. formulars as Perl-code.

This one is without computing the x-values (Faster & easier).
The x-values are assumed to 0,1,2,3 while 0 is the last bar,
1= one before, 2=2 before ...: the form of a straight line: y = mx +b;
'#' indicates a comment-line in Perl
So hand over a list of just y-values with the latest at the beginning

sub slope { #: calculate  slope $sl as of Origin (0,0)
	# my @_ = @_; #: ARRAY of y-values, x-values are assumed to be (0,1,2,3, ..)
	# y = $sl*x + b; # $sl = slope ;	
	# b = Su(yi)/n -m(Su(xi)/n) # Error² = (1/(n-1))* Sum(( yi - m*xi+b)²)
	# defining some variables:
	my ($a,$b,$c,$d,$i,$n) = 
	    (0,0,(@_*(@_-1))/2,((@_-1)*@_*(2*(@_-1)+1))/6,(0+@x),(0+@x));
	while ($i--) { $a += $_[$i]*$i; $b += $_[$i]; }
	$b =$b/ $n;
	# return just the slope $m if only 1 value is requested
	return (-($a-($b*$c))/($d - ($c**2)/$n)) if (!wantarray);

	#now compute $b the y-axe part of the staright line fromular:
	my $sl = -($a-($b*$c))/($d - ($c**2)/$n); # slope =same as behind return
	my $ab = $b - $sl*$c/$n;   # y-Axe part;
	$i = $n; $a = 0;
	while ($i--) {
		$a += ($_[$i]-$sl*$i+$ab)**2;
	}
	# return both slope 'm' and 'b' = y(x=0)
	return ($sl,sqrt($a/($n-1))); 
}

I added the math formulars as pdf-file. But it is in german.
If you need to know the exact translation of one word into english please use
	http://dict.leo.org/
and put yopur word in the field below 'Suchbegriff' and press 'Suchen'

On Friday 28 February 2003 05:50, zaitech.llc@xxxxxxxxxxx wrote:
> I need a small bit of help. I know how to code the slope of a moving
> average by direction
> up or down. i.e.. through the following:
>
> Slope=X-X[1];
>
> If X > X[1] then Slope=1;
> If X < X[1] then Slope=-1;
>
> What I would like to know is how do I make further differentiation in
> the slope change, such that if the slope is greater or less than a
> specific angle (for example  say 30 degrees ) than that Slope change
> will define my slope direction?
>
> I have tried the following but  think there might be a more efficient
> and precise way to do this.
>
> If Slope > PctgUp then Slope=1;
> If Slope < PctgDn then Slope=-1;
> If Slope < PctgUp and Slope> PctgDn then Slope=0;
>
> Thanks in advance.

Attachment:
slope.pdf

Attachment: Description: "Description: Adobe PDF document"