Announcement

Collapse
No announcement yet.

Add'l Help With Count Bars, Please?

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

  • Add'l Help With Count Bars, Please?

    Hello,
    I've slightly modified this T3 study to prove to myself that I am starting to understand efs a little bit. However, I still have a terrible time with counting bars. In any directional movement, I would like the 1st bar to be labeled #1, the 2nd, #2, etc. for a total of 13 bars. If the move has only ,say, 7 bars, the numbering would stop on #7. If there are more than 13 bars in the movement, the numbering would stop on #13, & there would be no more #s for the rest of the bars.When a new direction is established, the numbering would start again. There would be no #s for the bars with the yellow circles.

    function preMain(){
    setPriceStudy(true);
    setCursorLabelName("Upper",0);
    setCursorLabelName("T3Avg",1);
    setCursorLabelName("Lower",2);
    setDefaultBarFgColor(Color.khaki,0)
    setDefaultBarFgColor(Color.blue,1);
    setDefaultBarFgColor(Color.khaki,2);
    setDefaultBarThickness(2,0);
    setDefaultBarThickness(2,1);
    setDefaultBarThickness(2,2);
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);
    }
    var e1_1=0.0;
    var e2_1=0.0;
    var e3_1=0.0;
    var e4_1=0.0;
    var e5_1=0.0;
    var e6_1=0.0;

    function main(Price,Periods){
    if(Price==null)Price="Close";
    if(Periods==null)Periods=6;
    var vPrice=getValue(Price,0);
    var b=0.7;
    var b2=b*b;
    var b3=b*b*b;
    var c1=-b3;
    var c2=3*b2+3*b3;
    var c3=-6*b2-3*b-3*b3;
    var c4=1+3*b+b3+3*b2;
    var f1=2/(Periods+1);
    var f2=1-f1;
    var e1=f1*vPrice+f2*e1_1;
    var e2=f1*e1+f2*e2_1;
    var e3=f1*e2+f2*e3_1;
    var e4=f1*e3+f2*e4_1;
    var e5=f1*e4+f2*e5_1;
    var e6=f1*e5+f2*e6_1;

    if(getBarState()==BARSTATE_NEWBAR){
    e1_1=e1;
    e2_1=e2;
    e3_1=e3;
    e4_1=e4;
    e5_1=e5;
    e6_1=e6;
    }
    var T3Average=c1*e6+c2*e5+c3*e4+c4*e3;
    var vUpper=T3Average*(1+.07/100));
    var vLower=T3Average*(1-.07/100));

    if(vPrice>=vUpper){
    drawShapeRelative(0,vPrice,Shape.UPTRIANGLE,null,C olor.green,null);
    drawTextRelative(0,vPrice,"1",Color.magenta,null,T ext.CENTER|Text.BOTTOM,"Arial",15);
    }
    if(VPrice<=vLower){
    drawShapeRelative(0,vPrice,Shape.DOWNTRIANGLE,null ,Color.red,null);
    drawTextRelative(0,vPrice,"2",Color.aqua,null,Text .CENTER|Text.BOTTOM,"Arial"15);
    }
    if(vPrice>vLower&&(vPrice<vUpper)){
    drawShapeRelative(0,vPrice,Shape.CIRCLE,null,Color .yellow,null);
    }
    return new Array(vUpper,T3Average,vLower);

    }

    I guess I just don't understand how what I would like to accomplish can be simplified to fit in a small place in a text line. Thanks in advance for your help.
    Diane

  • #2
    Diane
    Before doing anything you need to run a Syntax Check on your script as there are a few errors
    As to creating a counter the first thing you need to do is define for each one of the directional movements a unique condition - ie a change of state - from which to start a count.
    This is because none of the conditions you have at this time are unique. Take for example your first condition ie if(Price>=vUpper) and you will see that it is true on every bar in which Price is greater or equal to vUpper hence you have no way to anchor a count.
    The simplest solution in this case would be to use a flag that has three states, one for each directional movement for example 1 if Price greater or equal to vUpper, 0 if Price between vUpper and vLower and -1 if Price less or equal to vLower. This flag need to be declared as a global variable (ie outside of function main) as follows

    var vFlag = 2;

    I am using 2 as the intial value because it is different from any of the three states it will have ie 1, 0 or -1.
    You also need to declare globally your counter variable.

    var vCounter = 0;

    The next step is to have the counter increase by 1 at every new bar. Since you already have the necessary condition to define a new bar (in line 45 ie if(getBarState()==BARSTATE_NEWBAR)) you can simply insert the logic to increment the counter inside that conditional statement as follows

    PHP Code:
    if(getBarState()==BARSTATE_NEWBAR){//on every new bar
          
    if(vCounter<14vCounter++;//if counter is less than 14 increment its value by 1
          
    e1_1=e1;
          
    e2_1=e2;
          
    e3_1=e3;
          
    e4_1=e4;
          
    e5_1=e5;
          
    e6_1=e6;
        } 
    The reason for if(vCounter<14) is to stop the count at 13 regardless
    Then inside each of your conditions you add a further condition that checks the state of vFlag, resets vCounter to 1 (so as to begin a new count) and at the same time changes the state of vFlag so the condition will no longer be true. This way you create a starting point for the counter.
    For purpose of this example I will use the last of your conditions ie if(vPrice>vLower && vPrice<vUpper)
    In the following example you see what you would need to do

    PHP Code:
    if(vPrice>vLower&&vPrice<vUpper){
        
    drawShapeRelative(0,vPrice,Shape.CIRCLE,null,Color.yellow,null);
        if(
    vFlag!=0vCounter=1;
        
    vFlag 0;
        if(
    vCounter<14drawTextRelative(0,AboveBar1,vCounter,Color.red,null,Text.PRESET|Text.CENTER|Text.BOTTOM,"Arial",12);

    The first two lines are the same as you had originally written them
    Then you have if(vFlag!=0) vCounter=1; This checks that vFlag is different than 0 and if true it resets vCounter to 1.
    In the next line vFlag is set to 0 which means that at the next iteration of the efs the condition right above it will no longer be true. We have therefore created a unique condition from which to start our counter.
    In the following line you then have the command to draw the text on the chart. Notice that the command is preceeded by the condition if(vCounter<14) so that the numbers are no longer drawn on the chart past 13.

    For the other conditions you will need to apply a similar logic to the one shown above. the only thing that will change is the state of vFlag. In the first of your conditions you will write

    if(vFlag!=1) vCounter = 1;
    vFlag = 1;


    and in the second it will be

    if(vFlag!= -1) vCounter= 1;
    vFlag = -1;

    In the image below you see the results of the suggested modifications (in this case applied only to the third condition). Notice that every time a yellow circle is drawn the counter starts at 1 and only goes up to 13. Past the 13th bar it no longer shows the number.
    Hope this helps
    Alex

    Comment


    • #3
      Hi Alex,
      Happy Saturday to you! Thank you so much for all this wonderful & valuable information! It may take me a bit to digest it, but I am so grateful to you for providing it. Once I really get my teeth into it, I should be able to do any text. Thank you, again, for your patience & valuable time.
      Diane

      Comment


      • #4
        Diane
        You are most welcome.
        Here are some further comments/explanations on the sample code I posted in my prior reply

        PHP Code:
        if(vPrice>vLower&&vPrice<vUpper){
            
        drawShapeRelative(0,vPrice,Shape.CIRCLE,null,Color.yellow,null);
            if(
        vFlag!=0vCounter=1;
            
        vFlag 0;
            if(
        vCounter<14drawTextRelative(0,AboveBar1,vCounter,Color.red,null,Text.PRESET|Text.CENTER|Text.BOTTOM,"Arial",12);

        The reason why you check for the condition if(vFlag!=0) is because the first time that vPrice>vLower&&vPrice<vUpper that flag will be either 1 if vPrice was previously above vUpper or -1 if vPrice was previously lower then vLower hence the condition is true. Once this new condition is determined the counter is set to 1 and vFlag is set to 0 ie the state of the current directional movement (that is price is inside the envelope).
        Once vFlag is set to 0 then the condition if(vFlag!=0) is no longer true and vCounter will no longer be reset to 1. It will only be incremented by 1 (up to a maximum of 13) at every new bar.
        Should the condition change ie vPrice>=vUpper OR vPrice<=vLower then vFlag will be set to its new state by the corresponding conditions vFlag = 1 or vFlag = -1 which will also reset the counter to 1 and start a new counting cycle
        Alex

        Comment


        • #5
          Alex,
          Thanks for the additional information. WOW! How methodically detailed all of this is. Terrific! OK- so let's see if I'm on track:
          1) The global vFlag indicates that something other than the normal conditions will be happening.
          2) Because there are 3 things I want to do, I need 3 separate flags(,0,1,&-1).
          3) The global VCounter is "resting" on 0 until something happens.
          4) If we want the bar count to stop at 13, we tell the vCounter<14. What do the e1-e6 represent?
          5) If there's a vFlag!=0, the must be a vFlag=0. It means that there will be a different condition next time & so the vFlag # has to be different.
          6) We put the vCounter on 1 because something is going to happen.
          7) Because of the 3 flags, we have 3 things that will be special instead of the plain ol' conditions.
          8) The 2 flag statements & the vCounter <14 line tell the VCounter in the TextRelative line what to do.
          Am I close?
          Diane

          Comment


          • #6
            Diane
            1) vFlag is used to indicate which state (ie condition) we are in. I have suggested the following
            a) if vPrice is greater than vUpper then vFlag will be set to 1
            b) if vPrice is lesser than vLower then vFlag will be set to -1
            c) if vPrice is in between vUpper and vLower then vFlag will be set to 0
            This flag helps us determine the very first time we enter in any one of the three conditions. This is because in that first time we are looking for vFlag to be different (ie not equal to) from the state we have just entered at which poiint we reset vCounter to 1 and we set vFlag to match our state. If we did not do that then the counter would get reset at every bar and we would not be able to perform a count.

            2) Only one vFlag is used which has three conditions

            3) vCounter is declared once globally and set to 0. This will happen only the very first time. From that moment onwards vCounter is increasing its value by 1 on every new bar (up to a maximum count of 13) regardless of the condition we are in. Then at every first change of condition vCounter is reset to 1 at which point it begins counting again up to 13

            4) e1-e6 are the variables required by the indicator to perform its calculations.They are of no concern to us

            5) Yes there is a vFlag = 0 when vPrice is in between vUpper and vlower. But what we want to know is not when it is equal to but when it is not equal to because that is the one time we can create a unique condition in which we perform the reset of vCounter

            6) We set (actually reset) vCounter to 1 because we need to start counting. vCounter is not like vFlag but it is the counter ie it keeps count of the number of bars that have gone by.

            7) There is only 1 flag with three states (or conditions)

            8) drawTextRelative() is used only to write the count number on the chart. The condition vCounter<14 that preceeds it is used to prevent it from writing 13 on every bar after the 13th bar.
            Alex

            Comment


            • #7
              Alex,
              Thank you for the recap. I'll keep studying. I can't tell you how grateful I am for all the help you've been to me. Hope you have a great weekend!
              Diane

              Comment


              • #8
                Diane
                My pleasure and a great weekend to you too
                Alex

                Comment


                • #9
                  Alex,
                  How can I send you a picture of my chart so you can see how smart you made me?
                  Diane

                  Comment


                  • #10
                    Diane
                    Glad to have been of help.
                    You can post the image in this thread if you wish. Just attach it to your reply.
                    Alex

                    Comment


                    • #11
                      Sorry, Alex. I don't know how to do that. I found some instructions in File Share. I have the image in a folder in "my Documents", but I don't know how to get it to you. I tried to send it ,but I was told I am not a moderator.
                      Diane
                      Last edited by Diane P; 08-13-2005, 05:37 PM.

                      Comment


                      • #12
                        Diane
                        In the same page in which you compose your messages there is an Attach file section (see image below). Click on the Browse button, navigate to your My Documents folder, select the image to attach and click the Open button in the dialog box. That will attach the file to your post
                        Alex

                        Comment


                        • #13
                          Alex,
                          Did anything come through?
                          Diane

                          Comment


                          • #14
                            Alex- FYI
                            Diane
                            Attached Files

                            Comment


                            • #15
                              OH MY GOODNESS!! Is this technology great stuff or what?!
                              Well, Alex , you sure have been educating me today. I think you should get a raise! Thank you, again.
                              Diane

                              Comment

                              Working...
                              X