Moving average colour change

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #182055 quote
    Stocks008
    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
    robertogozzi
    Moderator
    Master

    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
    Stocks008
    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
    Nicolas
    Keymaster
    Master

    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
    Stocks008
    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
    robertogozzi
    Moderator
    Master

    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.

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 robertogozzi
4 years, 2 months ago.

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