First Difference and Running Sum
08/04/2021 at 7:27 AM #174646
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.
//Calculation of the First Difference
For i = 1 to N-1
y = Close[i] – Close[i-1]
//Calculation of the Running Sum
For i = 1 to N-1
y = Close[i] + y[i-1]
Return y08/04/2021 at 7:47 AM #174649
The first FOR…NEXT is useless, since Y will be assigned only the last value of all iterations, the same as:1y = Close[N-1] – Close[N-2]
Did you mean:123For i = 1 to N-1y = Close[i] – y[i+1]Next
Moreover, is it correct N-1 as last iteration?08/04/2021 at 11:34 AM #17466108/04/2021 at 1:56 PM #17466308/04/2021 at 2:46 PM #17466708/10/2021 at 11:28 AM #175005
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:123//Calculation of the First Differencey = Close – CloseReturn 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.08/10/2021 at 4:41 PM #17503106/17/2022 at 2:12 PM #195487First Difference12345678910111213141516DefParam DrawOnLastBarOnly = TrueUnSet($y)$y=0DrawHLine(0) Coloured(0,0,255)N=2000For 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])NextReturnRunning Sum1234567891011121314151617181920DefParam DrawOnLastBarOnly = TrueUnSet($y)$y=0UnSet($x)$x=0N=2000For i = 1 to N-1$x[LastSet($x)+1] = Close[i] - Close[i-1]NextFor 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])NextReturn
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).