Announcement

Collapse
No announcement yet.

Colour bar study

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Colour bar study

    I would like to colour the price bars (candles preferably) blue if the following conditions apply:

    Moving average 1 crosses Moving average 2.

    The colour change to blue is only to apply to the first bar that meets the condition. Subsequent bars to follow normal convention, e.g. red for down, green for up, until the next MA crossover.

    The basicmax2(colourbar).efs changes all the bars following a MA crossover, and I don't understand how to change it. Thanks for your help. David

  • #2
    Re: Colour bar study

    David
    Following is the basic logic [with comments] required to do what you are trying to accomplish.
    As a first step we retrieve the values of the averages on the current bar and at the prior bar and assign them to some variables which we will then use in the condition. The value at the prior bar is used in this example to define when the averages are crossing each other.
    PHP Code:
    var Avg1_0 sma(10,0);//value of Avg1 at current bar
    var Avg1_1 sma(10,-1);//value of Avg1 at prior bar
    var Avg2_0 sma(20,0);//value of Avg2 at current bar
    var Avg2_1 sma(20,-1);//value of Avg2 at prior bar 
    At this point we can write the initial condition that evaluates when the averages are crossing.
    The condition checks if at the prior bar one average was below (or above) the other and at the current bar it is above (or below) the other. When this condition evaluates to true we paint the price bar in blue.
    PHP Code:
    if(Avg1_1 Avg2_1 && Avg1_0 Avg2_0 || Avg1_1 Avg2_1 && Avg1_0 Avg2_0){//IF averages crossed up OR down THEN
        
    setPriceBarColor(Color.blue);//paint price bar in blue

    The above condition will paint only the bar in which the crossover occurs.
    However in efs if you paint one bar then you need to also paint all the other bars else they get painted in the default color assigned by the script (or by the formula engine if no default is set). So at this point we need to add some conditions and subsequent commands to color the up and down (and/or neutral) bars.
    PHP Code:
    else if(close(0) > open(0)){//ELSE IF current close is greater than current open THEN
        
    setPriceBarColor(Color.green);//paint price bar in green
    }
    else if(
    close(0) < open(0)){//ELSE IF current close is less than current open
        
    setPriceBarColor(Color.red);//paint price bar in red
    } else {//ELSE
        
    setPriceBarColor(Color.black);//paint price bar in black

    You can use this logic as the blueprint for your script replacing the names of the variables and/or functions and methods used to retrieve the values of the averages with those used in your script.
    Hope this helps
    Alex



    Originally posted by david1212
    I would like to colour the price bars (candles preferably) blue if the following conditions apply:

    Moving average 1 crosses Moving average 2.

    The colour change to blue is only to apply to the first bar that meets the condition. Subsequent bars to follow normal convention, e.g. red for down, green for up, until the next MA crossover.

    The basicmax2(colourbar).efs changes all the bars following a MA crossover, and I don't understand how to change it. Thanks for your help. David

    Comment


    • #3
      Colourbar script

      Alex,

      Thanks for your help with this. I have attached my (failed) effort at writing the study, but am getting a lot of syntax errors. Normally I use the Formula wizard to write studies, but I don't know how to bring your script into the wizard. Can you fix it for me ?

      David
      Attached Files

      Comment


      • #4
        David
        The Formula Wizard can only read code that was written using the Formula Wizard.
        If you normally use the Formula Wizard then you can use that tool to implement the same logic I described in my previous reply.
        Here is a basic example (with some illustrations) of what you would need to do.
        First create the two averages that you will be using. Then in Set1 set the conditions that will evaluate when vSMA10 crosses over vSMA20. This condition checks if vSMA10 at the prior bar (Offset -1) is less than vSMA20 at the prior bar and if vSMA10 is above vSMA20 on the current bar



        Repeat the logic in Set2 reversing the operators and applying the same color (ie blue) to the price bars.
        Then in Set3 you set the condition for an up bar (ie where the Close is greater than the Open)



        Repeat the logic in Set4 reversing the operator (ie Close is less than Open) to check for a down bar and paint the price bar in red.
        Then add one last Set which will be Set5 in which you apply a condition that will always be true by definition (such as for example 1 == 1) and paint the price bars in black. This condition will be evaluated only if all the preceeding ones evaluate to false ie the averages are not crossing each other and the Close is equal to the Open.
        Hope this helps
        Alex



        Originally posted by david1212
        Alex,

        Thanks for your help with this. I have attached my (failed) effort at writing the study, but am getting a lot of syntax errors. Normally I use the Formula wizard to write studies, but I don't know how to bring your script into the wizard. Can you fix it for me ?

        David

        Comment

        Working...
        X