How can I find the difference between 2 values in

2019-05-24 14:47发布

问题:

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)

回答1:

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
}


回答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!!!!