How to plot a line with 2 colour

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #196922 quote
    Jiacky
    Participant
    Junior

    This is the code I made from Average Filter Regression in this library:

    // K = 500 ({1...2000})
    
    Series = (Open + High + Low + Close) / 4
    
    ONCE Pred = Series
    
    IF BarIndex = 0 THEN
    AFR = Series
    ELSE
    Smooth = Pred + (Series - Pred) * SQRT((K / 10000) * 2)
    Velo = Velo + ((K / 10000) * (Series - Pred))
    Pred = Smooth + Velo
    AFR = Pred
    ENDIF
    
    RETURN AFR

    It looks like the first screenshot. How can I make it look like the second screenshot: If Velo > 0 then the line is green, If Velo <= 0 then the line is red?

    Thank you

    #196929 quote
    druby
    Participant
    New

    Hi..      I think this follows you description.

    The ‘if’ statement follows your logical criteria of ‘ Velo’ which set the value’s for ‘r’ and ‘g’. Then the addition of the ‘coloured’ modifier uses them to set the RGB colour of your ‘AFR’  line.

    Additional lines 16-22,   modified line 24.

    // K = 500 ({1...2000})
    
    Series = (Open + High + Low + Close) / 4
    
    ONCE Pred = Series
    
    IF BarIndex = 0 THEN
    AFR = Series
    ELSE
    Smooth = Pred + (Series - Pred) * SQRT((K / 10000) * 2)
    Velo = Velo + ((K / 10000) * (Series - Pred))
    Pred = Smooth + Velo
    AFR = Pred
    ENDIF
    
    if velo > 0 then
    g = 255
    r = 0
    else
    g=0
    r=255
    endif
    
    RETURN AFR coloured(r,g,0)
    Nicolas thanked this post
    #196939 quote
    Jiacky
    Participant
    Junior

    Thanks, It works. You are a legend.

    druby thanked this post
    #196982 quote
    druby
    Participant
    New

    Thanks for you kind comment.

    I recall a saying – If it’s not broken don’t try and fix it!     Bare that in mind.

    Since I’m on a steep learning curve trying to get in to PRT and the coding side, I decided to analyse the code you posted as an exercise. If this is beyond or distracting to what your doing, feel free to ignore. Just sharing.

    The code here is just main code but the .itf file has a lot more comments in it, why I changed and/or added things.

    I’ve learned a lot doing this and wouldn’t have done it if it wasn’t for your post and well laid out request/spec.

    In analyzing I have, replaced/altered/created some variables with stock/new, replaced original ‘if’ block and added a value guard, error message and logic control  to the ‘K’ variable if invalid.

    Additionally, it made me figure out a couple of technique when checking that code changers give same outcome’s. These such as creating alternate, by a numbered index, variable’s to calculate a kind of before and after type scenario and by subtracting one from the other it should result in zero if there the same

    Also by having a indicator panel with original  and a new file with the original copied in it, as changes are made you can see if expected result are obtained. Also have a original in price window. A plan is better than no plan.

    Not possibly rocket science or ground breaking but, kept me focused on the task for a while. As always do your own testing.

    All the best.

    // include dynamic variable 
    // 'K'   decimal/integer, default 500
    
    ONCE AFR = totalPrice
    
    // Input Guard - Limits 'K' input range +(1 to 2000) @ 1 d.p.
    validK = round(max(min(K,2000), 1),1)    
    
    // Calculate CONSTANT
    CONSTANT = validK/10000 
    
    // Input gaurd error code - if 'K' exceeds set limit's give warning!
    if K < 1 or K > 2000 then
    drawtext("ERROR! ~ K = #K# Out of Range  (1-2000)",0,8,Dialog,Bold,15)Anchor(bottom)coloured("RED")
    else
    // else run the number cruncher - calculate new AFR
    Smooth = AFR + (totalPrice - AFR) * SQRT(CONSTANT * 2)
    Velo = Velo + CONSTANT * (totalPrice - AFR)
    AFR = Smooth + Velo
     
    // Set colour value's for 'AFR' line, 'green' velo > 0, 'red' velo <= 0
    if Velo > 0 then
    g = 255
    r = 0
    else
    g=0
    r=255
    endif
    endif
    
    RETURN AFR coloured(r,g,0)  // new AFR value set to required colour's
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

How to plot a line with 2 colour


ProBuilder support

New Reply
Author
author-avatar
Jiacky @jiacky Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by druby
3 years, 6 months ago.

Topic Details
Forum: ProBuilder support
Language: English
Started: 07/06/2022
Status: Active
Attachments: 3 files
Logo Logo
Loading...