Difference in value between two moving averages

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #19703 quote
    robdav
    Participant
    Veteran

    Hi, I’m a newbie here.

    I’m attempting to code a ProOrder trading system and I need to code a condition for a position.

    The condition is where the difference between two moving average values is greater than or equal to a value, say 5 for example. So it would need to take into account at any one point the two values of two different moving averages and act where the difference between the two values is greater than 5 as an example.

    Can someone point me at or provide an example of how this should be coded please? Hopefully I have explained it well enough.

    Many thanks

    Robert

    #19705 quote
    Nicolas
    Keymaster
    Master

    This can be coded easily.

    Firstly, define your 2 moving average:

    MA1 = average[10](close)
    MA2 = average[20](close)

    Calculate their absolute differences (no need to test if the MA1 is above or below the MA2):

    difference = abs(MA1-MA2)

    Then make a boolean variable that will return “1” if the condition is true:

    test = difference >= 5*pointsize

    With the variable “test”, you can now play with to do any other things in your code like open or close a position, adding another one, etc..

    Of course these lines of codes would have been shortened but I find it is the best way to explain the coding process.

    GraHal and robdav thanked this post
    #19712 quote
    robdav
    Participant
    Veteran

    Great, thank you Nicolas.

    #19960 quote
    robdav
    Participant
    Veteran

    Hi

    In this example, I do now need to test if one moving average is above or below the other and by how much. Is there an example of how this is achieved?

    Many thanks

    Robert

    #19964 quote
    Nicolas
    Keymaster
    Master

    You already have everything to do it! You got your 2 MA values (MA1 and MA2), you only need to test if one is superior to the other one. The difference between them is already calculated with “difference”..

    #20038 quote
    robdav
    Participant
    Veteran

    Yes, of course. Thank you.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Difference in value between two moving averages


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
robdav @robdav Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by robdav
9 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/03/2017
Status: Active
Attachments: No files
Logo Logo
Loading...