JSParticipant
Senior
In DSP (Digital Signal Processing) there are processes that can change signals in ways that resemble differentiation and integration.
Only the terms differentiation and integration refers to actions on continuous signals.
For actions on discreet signals these terms are given other names:
First Derivative (continuous signals) = First Difference (discreet signals)
Integration (continuous signals) = Running Sum (discreet signals)
The relation (equation) for the First Difference is: y[n] = x[n] – x[n-1]
The relation(equation) for the Running Sum is: y[n] = x[n] + y[n-1]
This kind of equation is also called, Difference Equation or Recursion Equation.
In code:
//Calculation of the First Difference
y=0
For i = 1 to N-1
y = Close[i] – Close[i-1]
Next
Return y
//Calculation of the Running Sum
y=Close
For i = 1 to N-1
y = Close[i] + y[i-1]
Next
Return y
The first FOR…NEXT is useless, since Y will be assigned only the last value of all iterations, the same as:
y = Close[N-1] – Close[N-2]
Did you mean:
For i = 1 to N-1
y = Close[i] – y[i+1]
Next
?
Moreover, is it correct N-1 as last iteration?
JSParticipant
Senior
Thanks Roberto,
Indeed sloppy coding, properly I must use arrays.
The last iteration must be N instead of N-1
(when i start at 0 then i=0 to N-1)
JSParticipant
Senior
@Roberto, can you give me some help with the coding…?
(The equitations are correct)
I think the First Difference should be (see attached pic):
y[n] = x[n + 1] – x[n]
If this is the case, then the code to get Ys is:
//Calculation of the First Difference
y = Close – Close[1]
Return y
FOR…NEXT is useless, as you only return the value calculated by the LAST iteration.
What exactly do you want to calculate?
As you have put it, it’s just a momentum indicator over the last 2 candles.
JSParticipant
Senior
Thanks Roberto for the effort, I will give it a thought …
JSParticipant
Senior
DefParam DrawOnLastBarOnly = True
UnSet($y)
$y[0]=0
DrawHLine(0) Coloured(0,0,255)
N=2000
For i = 1 to N-1
$y[lastset($y)+1] = close[i] - close[i-1]
DrawPoint(BarIndex[i],$y[i],2)
DrawSegment(BarIndex[i-1],$y[i-1],BarIndex[i],$y[i])
Next
Return
DefParam DrawOnLastBarOnly = True
UnSet($y)
$y[0]=0
UnSet($x)
$x[0]=0
N=2000
For i = 1 to N-1
$x[LastSet($x)+1] = Close[i] - Close[i-1]
Next
For i = 1 to N-1
$y[lastset($y)+1] = $y[i-1] + $x[i]
DrawPoint(BarIndex[i],$y[i],2)
DrawSegment(BarIndex[i-1],$y[i-1],BarIndex[i],$y[i])
Next
Return
I’ve become a little wiser 😉 partly thanks to the forum…
The theory of the “first difference” and the “running sum” is correct but the execution was not correct, I had to use arrays…
The “first difference” is the discrete version of the first derivative or the slope of the input signal (close).
The “running sum” is the discrete version of the integral or when you integrate the first derivative you get the original signal (close).