2

I am working with an Oscillator that fluctuates between 10.000000 and -10.000000

The value changes say every 5 minutes. I want to find the difference between the current value and the value of 5 minutes ago. Here is my logic.

1 bar ago (1BA)= -.2
Current bar (CB) = .3

Wouldn't I get a value of 1 if I did something like:

Abs(CB) - Abs(1BA) = .3 - .2 = 1

Whereas:

Abs(CB- 1BA) = .3 - -.2 = 5

I want to simply calculate the difference between the move of the oscillator from one time frame to another. Am I applying the Abs with the right logic in mind?

Here is my actual code, please assume my method being called is correct:

if (Oscillator(PoFast, PoSlow, PoSmooth)[0] > 
           Oscillator(PoFast, PoSlow, PoSmooth)[3]
    && Math.Abs(Oscillator(PoFast, PoSlow, PoSmooth)[0] - 
           Oscillator(PoFast, PoSlow, PoSmooth)[3]) > .25)
  • 4
    This isn't an answer to your question, but stashing the result of Oscillator(PoFast, PoSlow, PoSmooth) into a variable so you don't call it four times will make your life easier. – Matt K May 27 '09 at 15:19
  • Your range values lie between `-10` and `+10` so you are very safe from one of [the trickiest errors](https://stackoverflow.com/q/38702478/465053) with `Math.Abs` function which one might want to be aware of. – RBT Mar 13 '18 at 00:10

2 Answers2

6

Of the two, Abs(CB-1BA) would be correct, since there was a change of .5 between the two readings.

EDIT: To make it more readable, and assuming you're getting double values, you'd do something like this, but with error checking, and probably making the .25 a variable.

double[] values = Oscillator(PoFast, PoSlow, PoSmooth);
double currentBar = values[0];
double oneBarAgo = values[3];

if (currentBar > oneBarAgo && Math.Abs(currentBar - oneBarAgo) > .25)
{
    // do something
}
Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
2

Your logic, Abs(CB-1BA) is correct.

As an aside, if you're not a developer do you really think you should be writing C# code to trade futures contracts? They're risky enough to trade at the best of times, never mind when you're writing code to do it and you're not a developer!!!!

Sean
  • 60,939
  • 11
  • 97
  • 136
  • 1
    Agree. @r2kTrader: please tell us what company you work for, so we can be sure not to buy futures contracts from you. – John Saunders May 27 '09 at 15:19
  • 2
    Wait, would you trust trading software written by a programmer who knows nothing about the market? – Bill the Lizard May 27 '09 at 15:24
  • As an developer working on a trading floor, I can tell you that traders write some "basic" excel applications for their needs. But yeah, most of the time, it is awful when we have to maintain their code ^^' –  May 27 '09 at 16:11