Announcement

Collapse
No announcement yet.

Ploblem with setBarFgColor()

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

  • Ploblem with setBarFgColor()

    I'm trying to change the color of a study when certain conditions are met ie. an ema changes from down to up or up to down. I've attempted to follow the example for setBarFgColor. Historic bars are colored as expected but new bars do not change colors.
    Following is the code as well as the attached file.

    Code:
    /*
    Study loads ema into the top position of an array at the begining of each bar.
    Elements 0, 1, & 2 are compared for 0 > 1 && 1 >2 or 0 < 1 or 1 < 2.
    When conditions are met the bar color is changed to blue for up or red for down.
    
    */  
    
    
    function preMain() {
    
        setPriceStudy(false);
        setStudyTitle("ema with color changes");
        setCursorLabelName("ema with colors",0);
    
    }
    
    var nCtr = null;
    var nLen = null;
    var var1 = null;
    
    var aArray = null;
    
    function main() {
    
        if (nCtr == null) nCtr = 0;
        if (var1 == null) var1 = 0; 
        if (nLen == null) nLen = 3;
        if (aArray == null) aArray = new Array(0, 0, 0);
        
        if (getBarState() == BARSTATE_NEWBAR) {
            var1 = ema(3, close());
            nLen = aArray.unshift(var1);
            var2 = aArray.pop();
    
        
    
            for (nCtr = 0; nCtr < aArray.length; nCtr++) {
           
                debugPrintln("[" + nCtr + "]:" + aArray[nCtr].toFixed(2) + "]");                    
            }
            
            debugPrintln();
            if ((aArray[0] > aArray[1]) && 
                (aArray[1] > aArray[2])) {
                    setBarFgColor(Color.blue,0);
                    debugPrintln("Change to blue");
            } else if ((aArray[0] < aArray[1]) && 
                (aArray[1] < aArray[2])) {
                    setBarFgColor(Color.red,0);
                    debugPrintln("Change to red");
            } 
        }
        return(aArray[0]);
    }
    Thanks,

    Rod
    Attached Files
    Last edited by ramckay; 04-05-2006, 08:20 AM.

  • #2
    Rod
    The formula engine already stores the historical values of the builtin studies (and/or any custom series created using the efsInternal() or efsExternal() functions) so unless you have a specific reason to use an array you don't actually need to do that yourself. All you need to do is intialize the study and then retrieve the historical values using the getValue() method as shown in the revision of your formula enclosed below.
    Alex

    PHP Code:
    function preMain(){

        
    setPriceStudy(false);
        
    setStudyTitle("ema with color changes");
        
    setCursorLabelName("ema with colors",0);
        
    setDefaultBarFgColor(Color.lightgrey);
        
    setDefaultBarThickness(2);
    }

    var 
    var1 null;

    function 
    main(){

        if(
    var1 == nullvar1 ema(3,close());
        
        if(
    var1.getValue(0) > var1.getValue(-1) && var1.getValue(-1) > var1.getValue(-2)){
            
    setBarFgColor(Color.blue);
        }
        if(
    var1.getValue(0) < var1.getValue(-1) && var1.getValue(-1) < var1.getValue(-2)){
            
    setBarFgColor(Color.red);
        }
            
        return 
    var1.getValue(0);

    Comment


    • #3
      Rod
      Adding to my prior reply. If for whatever reason you do want to use an array then I would suggest that you look into the ezArray() function described here which will simplify considerably your code
      Here is the same code as I posted earlier using the ezArray() function. Note that I retrieve the value of the ema() directly in the ezArray() function. The results will be the same as with the code posted earlier.
      Alex

      PHP Code:
      function preMain(){

          
      setPriceStudy(false);
          
      setStudyTitle("ema with color changes");
          
      setCursorLabelName("ema with colors",0);
          
      setDefaultBarFgColor(Color.lightgrey);
          
      setDefaultBarThickness(2);
      }

      var 
      Lib addLibrary("easyArray.efsLib");//call the easyArray function library

      function main(){

          var 
      aArray Lib.ezArray(ema(3,close(),0),3,"var1");//creates and maintains the array
          
          
      if(aArray[0] > aArray[1] && aArray[1] > aArray[2])
              
      setBarFgColor(Color.blue);
          if(
      aArray[0] < aArray[1] && aArray[1] < aArray[2])
              
      setBarFgColor(Color.red);   
          
          return 
      aArray[0];
      }

      //Note that the ema() function is used directly inside the ezArray() function in line 14
      //to retrieve the current value of the indicator 

      Comment


      • #4
        Alex,
        Thanks so much for the solution. It works like a champ.
        Rod

        Comment


        • #5
          Rod
          You are most welcome
          Alex

          Comment

          Working...
          X