Announcement

Collapse
No announcement yet.

setBarBgColor will not work

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

  • setBarBgColor will not work

    I have a code and want to have the colors shown on the chart if it is Long or Short (and have it work so I can back test it). Near the end of the code (colored purple) this line will not work,

    Strategy.doLong("Long Signal", Strategy.CLOSE, Strategy.THISBAR);

    can any one tell me why?

    Here is the code (the part related to the function):

    Code:
    function Calc_VWEMA(Length) {
        var nRes = 0;
        var nRes1 = 0;
        var nMAVolPrice = 0;
        var nMAVolPrice1 = 0;
        var nMAVol = 0;
        var nMAVol1 = 0;
        
        if (!bSecondInit) {
            xMAVolPrice = ema(Length, efsInternal("CalcVolPrice"));
            xMAVol = ema(Length, volume());
            bSecondInit = true;
        }
        
        nMAVolPrice = xMAVolPrice.getValue(0);
        nMAVol = xMAVol.getValue(0);
        nMAVolPrice1 = xMAVolPrice.getValue(-1 );
        nMAVol1 = xMAVol.getValue(-1);
        if (nMAVolPrice == null || nMAVol == null || nMAVol == 0) return;
        nRes = nMAVolPrice / nMAVol;
        nRes1 = nMAVolPrice1 / nMAVol1; 
            
        if(nRes > nRes1) {
            if(!Strategy.isLong()) {
                debugPrintln(1,nRes,"  ",nRes1);  //This line works, why does the next two lines not work?
                [color=purple]Strategy.doLong("Long Signal", Strategy.CLOSE, Strategy.THISBAR);
                setBarBgColor(Color.RGB(222,254,223))[/color]; // Light Green
            }
        } else {
            if(!Strategy.isShort()) {
                [color=purple]Strategy.doShort("Short Signal", Strategy.CLOSE, Strategy.THISBAR);
                setBarBgColor(Color.RGB(255,222,221))[/color]; // Light Red
            } 
        } 
        
        return nRes;
    }

  • #2
    Re: setBarBgColor will not work

    WantToLearn
    The reason why it will not work is [briefly] explained in this post by JasonK
    The way to resolve this is to add the strategy code logic in the main function of the script you are using [ie which has the efsInternal() call to the Calc_VWEMA function]. Specifically you could add it after line 63 of that script eg
    PHP Code:
    var nVWEMA1 xVWEMA.getValue(-1);
        if (
    nVWEMA1 == null ) return;
        
        if(
    nVWEMA nVWEMA1) {
            if(!
    Strategy.isLong()) {
                
    //debugPrintln(1,nVWEMA,"  ",nVWEMA1);  //This line works, why does the next two lines not work?
                
    Strategy.doLong("Long Signal"Strategy.CLOSEStrategy.THISBAR);
                
    setBarBgColor(Color.RGB(222,254,223)); // Light Green
            
    }
        } else {
            if(!
    Strategy.isShort()) {
                
    Strategy.doShort("Short Signal"Strategy.CLOSEStrategy.THISBAR);
                
    setBarBgColor(Color.RGB(255,222,221)); // Light Red
            

        } 
    Note that I changed the variable names to account for those used in the main function of that script
    Alex


    Originally posted by WantToLearn
    I have a code and want to have the colors shown on the chart if it is Long or Short (and have it work so I can back test it). Near the end of the code (colored purple) this line will not work,

    Strategy.doLong("Long Signal", Strategy.CLOSE, Strategy.THISBAR);

    can any one tell me why?

    Here is the code (the part related to the function):

    Code:
    function Calc_VWEMA(Length) {
        var nRes = 0;
        var nRes1 = 0;
        var nMAVolPrice = 0;
        var nMAVolPrice1 = 0;
        var nMAVol = 0;
        var nMAVol1 = 0;
        
        if (!bSecondInit) {
            xMAVolPrice = ema(Length, efsInternal("CalcVolPrice"));
            xMAVol = ema(Length, volume());
            bSecondInit = true;
        }
        
        nMAVolPrice = xMAVolPrice.getValue(0);
        nMAVol = xMAVol.getValue(0);
        nMAVolPrice1 = xMAVolPrice.getValue(-1 );
        nMAVol1 = xMAVol.getValue(-1);
        if (nMAVolPrice == null || nMAVol == null || nMAVol == 0) return;
        nRes = nMAVolPrice / nMAVol;
        nRes1 = nMAVolPrice1 / nMAVol1; 
            
        if(nRes > nRes1) {
            if(!Strategy.isLong()) {
                debugPrintln(1,nRes,"  ",nRes1);  //This line works, why does the next two lines not work?
                [color=purple]Strategy.doLong("Long Signal", Strategy.CLOSE, Strategy.THISBAR);
                setBarBgColor(Color.RGB(222,254,223))[/color]; // Light Green
            }
        } else {
            if(!Strategy.isShort()) {
                [color=purple]Strategy.doShort("Short Signal", Strategy.CLOSE, Strategy.THISBAR);
                setBarBgColor(Color.RGB(255,222,221))[/color]; // Light Red
            } 
        } 
        
        return nRes;
    }

    Comment


    • #3
      That solved the problem. I appreciae the explanation and the other source to go to for even more info.

      Thank you Alex for being so thorough. I am learning and like the indepth info you gave.

      Comment


      • #4
        WantToLearn
        You are welcome
        Alex


        Originally posted by WantToLearn
        That solved the problem. I appreciae the explanation and the other source to go to for even more info.

        Thank you Alex for being so thorough. I am learning and like the indepth info you gave.

        Comment

        Working...
        X