Moving average colour change

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #182055 quote
    Stocks008Stocks008
    Participant
    Average

    Hi there,

    I would like to code a moving average(10 SMA) but change the colour when it points up or down. is there a way to do it in the code so I do not have to do it manually?I did search for it but didn’t find anything.

    thanks in advance, 🙂

    #182066 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    A colour change is when the current candle is:

    • ascending and the previous one was descending;
    • descending and the previous one was ascending.

    Here is the code:

    MySma   = average[20,0](close)
    Rising  = MySma > MySma[1] AND MySma[1] < MySma[2]
    Falling = MySma < MySma[1] AND MySma[1] > MySma[2]
    Stocks008 thanked this post
    #182239 quote
    Stocks008Stocks008
    Participant
    Average

    Okay, just struggling to make it work in the code. What do I have to return in the code to make the average appear on chart and still have two separate colours? I know it might be a dumb question, just not too great at coding. Usually to make the average a standard colour we return the average and also the colour, but more than that I’m struggling with.

    #182245 quote
    NicolasNicolas
    Keymaster
    Legend

    Just a way to do it (many other options to color based on conditions)

    MySma   = average[20,0](close)
    Rising  = MySma > MySma[1] AND MySma[1] < MySma[2]
    Falling = MySma < MySma[1] AND MySma[1] > MySma[2]
    
    if rising then 
     r = 0
     g = 255
    elsif falling then 
     r = 255
     g = 0
    endif 
    
    return mysma coloured(r,g,0)
    
    Stocks008 and robertogozzi thanked this post
    #182258 quote
    Stocks008Stocks008
    Participant
    Average

    Thank you.very simple and I just didn’t think of that. There isn’t maybe a way to do colourzones as well in a code?

    #182348 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    You can use the indicator’s properties (settings) to add colour zones.

    This is the code that mimicks the properties, but is not as good:

    MySma   = average[20,0](close)
    Rising  = MySma > MySma[1] AND MySma[1] < MySma[2]
    Falling = MySma < MySma[1] AND MySma[1] > MySma[2]
    if rising then
       r = 0
       g = 255
    elsif falling then
       r = 255
       g = 0
    endif
    IF close > MySma THEN
       OO = close
       HH = close
       LL = MySma
       CC = MySma
       DrawCandle(OO,HH,LL,CC) coloured(0,128,0,16)
    ELSIF close < MySma THEN
       OO = MySma
       HH = MySma
       LL = close
       CC = close
       DrawCandle(OO,HH,LL,CC) coloured(255,0,0,16)
    ENDIF
    return mysma coloured(r,g,0)
    Stocks008 thanked this post
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

Moving average colour change


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Stocks008 @jd008 Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by robertogozzirobertogozzi
4 years, 9 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 11/22/2021
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...