Imagina a sine wave oscillating about a zero line. My task is to calculate the slope at several random points along the wave using a fairly coarse x axis scale. (yes this has a real application)
When the wave is in +ve terrirtory (above the zero line) slope can be calculated from:
Slope = (y(n) / y(n-1)) - 1
This yeilds +ve slope vlaues heading up and -ve heading down.
The problem is that this must be switched when we are in -ve territory and then two more expressions are required when one of the vlaues is zero for a total of four expressions that must be chosen programatically with a conditional statements.
I would like to find ONE expression that covers all four condtions as this is at the center of a heavily travelled algorithm and clks count!
I am sure this would be a trivial solution for a math genius, but to these tired eyes, it eludes me...
Added:
The "sine wave" is actually an MACD indicator that is derived from (random) price action of financial markets. an example would be here:
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_average_conve
The slope (of the thick black line in the lower graph for example) is what I need to calculate here defined simply as up or down (where heading up is +ve)
The problem is that both +ve and -ve slope can occur above and below zero. A slope calcualtion may also occur using increments that cross the zero line and at the zero line.
It would be nice to find a solution that not involve a ton of IF statements... like for example, shifing all the y values by a fixed amount so they become +ve and then calculating slope in the +ve region. I would need to pick a number that historically, y has never been below, like a couple of orders of magnitude for example (99) and then I could perform the offest and one slope calculation?