Announcement

Collapse
No announcement yet.

Change Color If Above or Below last bar value

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

  • Change Color If Above or Below last bar value

    Hello... im trying to do a simple color change on an indicator, but i was unable to do it. Seams i can't retrive data from my .getValue (-).

    can anyone help me please. What i would like to do is Plot Red if indicator falling, and plot green is rising.

    ---- EFS ---
    function preMain() {

    setPriceStudy(true);
    setStudyTitle("MP");
    setCursorLabelName("50%", 0);
    setDefaultBarStyle(PS_DOT, 0);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarThickness(2, 0);
    setPlotType(PLOTTYPE_LINE, 0);
    //}}EFSWizard_PreMain 29264

    }

    function main(vBars, vOffset) {
    //Create Edit Functions to Change LookBack and Offset
    if (vBars == null) {
    vBars = 20;

    }

    if (vOffset == null) {
    vOffset = 2;

    }
    //Create New Indicator to Get Highest High and Lowest Low

    var vDonchian = new DonchianStudy(vBars+2, vOffset);

    var vDochUp = (vDonchian.getValue(DonchianStudy.UPPER));
    var vDochDown = (vDonchian.getValue(DonchianStudy.LOWER));

    //Calc MidPoint of the Range

    var vDochMidpoint = (vDochUp + vDochDown)

    //Change Color

    // if( vDochMidpoint > vDochMidpoint.getSeries(-1)) {
    // setPriceBarColor(Color.green);}
    debugPrintln(vDochMidpoint/2)
    return (vDochMidpoint/2);

  • #2
    garaujo
    You don't actually need to calculate the midpoint of the Donchian Channel as that is already available through one of the methods of the DonchianStudy() function eg
    PHP Code:
    xxx.getValue(DonchianStudy.BASIS,0)//current bar's value of the midpoint
    xxx.getValue(DonchianStudy.BASIS,-1)//prior bar's value of the midpoint
    xxx.getValue(DonchianStudy.BASIS,-2)//value of the midpoint two bars ago
    //etc 
    To accomplish what you want remove the following lines of code from your script
    PHP Code:
    var vDochUp = (vDonchian.getValue(DonchianStudy.UPPER));
    var 
    vDochDown = (vDonchian.getValue(DonchianStudy.LOWER));

    //Calc MidPoint of the Range

    var vDochMidpoint = (vDochUp vDochDown)

    //Change Color

     //   if( vDochMidpoint > vDochMidpoint.getSeries(-1)) {
       //     setPriceBarColor(Color.green);}
    debugPrintln(vDochMidpoint/2
    and replace them with the following
    PHP Code:
    var MidPointCurrent vDonchian.getValue(DonchianStudy.BASIS,0);
    var 
    MidPointPreviousBar vDonchian.getValue(DonchianStudy.BASIS,-1);
    //then run a null check on the oldest value being retrieved
    if(MidPointPreviousBar==null) return;
    //then set your conditions
    if(MidPointCurrent MidPointPreviousBar){
        
    //your commands to be executed if current value is greater than previous value (ie rising)
    }else if(MidPointCurrent MidPointPreviousBar){
        
    //your commands to be executed if current value is less than previous value (ie falling)
    } else {
        
    //your commands to be executed if neither of the above condition is true

    Then replace the return statement ie return (vDochMidpoint/2); with return MidPointCurrent;
    Alex

    Comment

    Working...
    X