Announcement

Collapse
No announcement yet.

Multi time frame interval

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

  • Multi time frame interval

    I am trying to return something that should be simple, but can't seem to get the language correct in my formula. I would like to color the bars on a 1 minute chart based on two conditions being met on a 3 minute chart: the close of the 3 min bar over an sma and the osc over 0. The bars on the 1 min chart or either delayed by 3 or 4 (3Min) bars or color prematurely before both conditions are met.

    Here is what I have so far:

    function preMain() {
    setPriceStudy(true);
    setStudyTitle ("3M");
    setCursorLabelName(3M");
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);
    }

    function main() {
    var vMA = sma(20,inv(3));
    var vOsc3 = osc(10,21,false,inv(3));
    var vCl3 = close(0,inv(3));

    (vMA == null ll vOsc3 == null ll vCl3 == null);


    if(getBarStateInterval(3) == BARSTATE_NEWBAR) {

    //nInterval = getInterval(3);
    //return;
    //}

    if ((vOsc3 > 0) && (vCl3 > vMA)) {
    setPriceBarColor(Color.green);
    }


    I've read the posts on intervals and getBarStateIntervals but I'm not grasping something. Any help would be so appreciated.

  • #2
    Re: Multi time frame interval

    turbotrade
    Assuming I understood correctly what you are trying to accomplish you may not need to enclose the condition and related command to paint the price bars inside a condition that checks for BARSTATE_NEWBAR of the external interval. Just check for the condition at the prior bar of the external interval and then paint the bars accordingly
    Enclosed below is your script with these changes [and some modifications to make it more efficient] together with some comments
    Alex

    PHP Code:
    function preMain() {
       
    setPriceStudy(true);
       
    setStudyTitle ("3M");
       
    setCursorLabelName("3M");
       
    setColorPriceBars(true);
       
    setDefaultPriceBarColor(Color.black);
    }

    //global variables
    var vMA null;
    var 
    vOsc3 null;
    var 
    vCl3 null;

    function 
    main() {
        
    //when the efs loads initialize the global variables
        
    if(getBarState()==BARSTATE_ALLBARS){
            
    vMA sma(20,inv(3));
            
    vOsc3 osc(10,21,false,inv(3));
            
    vCl3 close(inv(3));//changed from close(0,inv(3)) to retrieve the series
        
    }
        
    //local variables to store the values at the prior bar
        
    var nMA vMA.getValue(-1);//retrieve the value of the MA at the prior bar
        
    var nOsc3 vOsc3.getValue(-1);//retrieve the value of the MA at the prior bar
        
    var nCl3 vCl3.getValue(-1);//retrieve the value of the Close at the prior bar
        
        
    if (nMA == null || nOsc3 == null || nCl3 == null) return;//null check on the values
        //note that the vertical lines are not ll but the pipe character ie ||
        
        
    if ((nOsc3 0) && (nCl3 nMA)) {
            
    setPriceBarColor(Color.green);
        }


    Originally posted by turbotrade
    I am trying to return something that should be simple, but can't seem to get the language correct in my formula. I would like to color the bars on a 1 minute chart based on two conditions being met on a 3 minute chart: the close of the 3 min bar over an sma and the osc over 0. The bars on the 1 min chart or either delayed by 3 or 4 (3Min) bars or color prematurely before both conditions are met.

    Here is what I have so far:

    function preMain() {
    setPriceStudy(true);
    setStudyTitle ("3M");
    setCursorLabelName(3M");
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);
    }

    function main() {
    var vMA = sma(20,inv(3));
    var vOsc3 = osc(10,21,false,inv(3));
    var vCl3 = close(0,inv(3));

    (vMA == null ll vOsc3 == null ll vCl3 == null);


    if(getBarStateInterval(3) == BARSTATE_NEWBAR) {

    //nInterval = getInterval(3);
    //return;
    //}

    if ((vOsc3 > 0) && (vCl3 > vMA)) {
    setPriceBarColor(Color.green);
    }


    I've read the posts on intervals and getBarStateIntervals but I'm not grasping something. Any help would be so appreciated.

    Comment


    • #3
      Alex,
      I am so grateful. You are exactly correct & it works perfectly. I would have never figured this out - thank you!

      I have now tried to add just a few lines at the end to address the short side but am coming up with all black bars. Is my error with the "else if" statement? I removed it, but that didn't seem to work either. Thank you again in advance.


      }
      else if ((vOsc3 < 0) && (vCl3 < vMA)) {
      setPriceBarColor(Color.red));
      }
      }

      Comment


      • #4
        turbotrade
        You are most welcome
        There are a couple of errors . The first one is a double closing parenthesis in setPriceBarColor(Color.red))
        The second is that in the additional condition you are not using the variables that store the values at the prior bar ie nOsc3, nCl3 and nMA
        Alex


        Originally posted by turbotrade
        Alex,
        I am so grateful. You are exactly correct & it works perfectly. I would have never figured this out - thank you!

        I have now tried to add just a few lines at the end to address the short side but am coming up with all black bars. Is my error with the "else if" statement? I removed it, but that didn't seem to work either. Thank you again in advance.


        }
        else if ((vOsc3 < 0) && (vCl3 < vMA)) {
        setPriceBarColor(Color.red));
        }
        }

        Comment


        • #5
          OK - got it! I also had not changed the "vOsc", etc to "nOsc" etc in the last condition lines.

          Many thanks again for sharing.

          Comment


          • #6
            My pleasure
            Alex


            Originally posted by turbotrade
            OK - got it! I also had not changed the "vOsc", etc to "nOsc" etc in the last condition lines.

            Many thanks again for sharing.

            Comment

            Working...
            X