First Difference and Running Sum

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #174646 quote
    JS
    Participant
    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

    Schermafbeelding-2021-08-04-om-01.08.21.jpg Schermafbeelding-2021-08-04-om-01.08.21.jpg
    #174649 quote
    robertogozzi
    Moderator
    Master

    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?

    #174661 quote
    JS
    Participant
    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)

    #174663 quote
    JS
    Participant
    Senior

    @Roberto, can you give me some help with the coding…?

    (The equitations are correct)

    #174667 quote
    robertogozzi
    Moderator
    Master

    I’ll try asap.

    JS thanked this post
    #175005 quote
    robertogozzi
    Moderator
    Master

    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.

    First-Difference-FD-non-Linear.jpg First-Difference-FD-non-Linear.jpg
    #175031 quote
    JS
    Participant
    Senior

    Thanks Roberto for the effort, I will give it a thought …

    #195487 quote
    JS
    Participant
    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).

    Schermafbeelding-2022-06-17-om-15.01.41.jpg Schermafbeelding-2022-06-17-om-15.01.41.jpg Schermafbeelding-2022-06-17-om-15.07.03.jpg Schermafbeelding-2022-06-17-om-15.07.03.jpg
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

First Difference and Running Sum


General Trading: Market Analysis & Manual Trading

New Reply
Author
author-avatar
JS @js Participant
Summary

This topic contains 7 replies,
has 2 voices, and was last updated by JS
3 years, 8 months ago.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 08/04/2021
Status: Active
Attachments: 4 files
Logo Logo
Loading...