Difference in value between two moving averages

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #19703 quote
    robdavrobdav
    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
    NicolasNicolas
    Keymaster
    Legend

    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
    robdavrobdav
    Participant
    Veteran

    Great, thank you Nicolas.

    #19960 quote
    robdavrobdav
    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
    NicolasNicolas
    Keymaster
    Legend

    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
    robdavrobdav
    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.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

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 robdavrobdav
9 years, 8 months ago.

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