Announcement

Collapse
No announcement yet.

Change color of line for different slopes

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

  • #16
    Alex

    Thank you again, still much to learn on my side.

    How can I plot the MA based on High a few pips higher than the actual line should be without using Percentage or Standard Deviations. In other words how can I add for instance 5 pips to the High and then do the calculation on the new value?

    Regards,

    Comment


    • #17
      Kobus
      Just add the 5 pips to the value of the average eg
      var AdjustedAvg = vMA.getValue(MAStudy.MA) +0.0005;
      and then return AdjustedAvg in lieu of vMA.getValue(MAStudy.MA)
      The result will be the same as what you would get by first adding 5 pips to the High and then calculating an average of that
      Alex

      Comment


      • #18
        Kobus
        Enclosed below is a script that illustrates what I said in my prior reply ie that you will get the same result whether you first add 5 pips to the High and then calculate the average or if you calculate the average of the High and then add 5 pips. Comments are included in the script
        Alex

        PHP Code:
        function preMain(){
            
        setPriceStudy(true);
        }

        var 
        Study1 null;
        var 
        Study2 null;
            
        function 
        main(){

            if(
        Study1==nullStudy1 sma(5,efsInternal("adjustedHigh"));//calculates the average of the adjusted high
            
        if(Study2==nullStudy2 sma(5,high());// calculates the average of the high
            
            
        if(Study1.getValue(0)==null||Study2.getValue(0)==null) return;//null check
            
            
        var adjustedStudy2 Study2.getValue(0)+0.0005;//adds 0.0005 to the average of the high
            
            
        return new Array (Study1.getValue(0), adjustedStudy2);//these will return the same values
        }

        function 
        adjustedHigh(){
            return 
        high(0)+0.0005;//adjust the high by adding 0.0005

        Comment


        • #19
          Alex

          Thank you very much, I have implemented as per your post, works perfectly.

          I am posting a picture where I indicated bars between the 2 MA's.

          How can the number of bars between the averages been counted?

          The idea is to count the bars and then gives a signal/alert if the number of bars between the MA's reach a specified number.

          Thank you very much for your wise instruction so far, it is much appreciated.

          Regards
          Attached Files

          Comment


          • #20
            Kobus
            The enclosed sample script shows you how to count the bars that are contained within a channel. Comments and explanations of the logic used are included in the script.
            Note that the script contains both the logic to trigger an alert only on BARSTATE_NEWBAR and to paint the background in real time each using the variable Counter under slightly different conditions.
            Alex

            PHP Code:
            function preMain(){
                
            setPriceStudy(true);
            }
             
            var 
            Avg null;//global variable
            var Counter 0;//global variable
             
            function main(){
                    
                if(
            Avg==nullAvg sma(20);//initialize study
                
            if(Avg.getValue(0)==null) return;//null check 
                
                
            var CurrentState 0;//local variable
                
                
            if(getBarState()==BARSTATE_NEWBAR){//at every first tick of a new bar
                    //if the prior bar High is less than or equal to the prior bar upper channel
                    //AND the prior bar Low is greater than or equal to the prior bar lower channel
                    //add 1 to the variable Counter
                    
            if(high(-1) <= (Avg.getValue(-1)+1.5) && low(-1) >= (Avg.getValue(-1)-1.5)){
                        
            Counter += 1;
                    }else{
            //any other condition set the variable Counter to 0
                        
            Counter 0;
                    }
                    
            //if the Counter is equal to 5 play a sound
                    
            if(Counter==5){
                        
            Alert.playSound("ding.wav");
                    }
                }
                
            //if the current bar High is less than or equal to the current bar upper channel
                //AND the current bar Low is greater than or equal to the current bar lower channel
                //set the variable CurrentState to 1
                
            if(high(0) <= (Avg.getValue(0)+1.5) && low(0) >= (Avg.getValue(0)-1.5)){
                    
            CurrentState 1;
                }
                
            //if the current bar is equal to 1 ie the current bar is inside the channel
                
            if(CurrentState == 1){
                    
            //paint the background between the lower and upper channel in yellow
                    
            setBarBgColor(Color.yellow,0,Avg.getValue(0)+1.5Avg.getValue(0)-1.5);
                    
            //if the sum of CurrentState plus Counter is equal to 5 ie it is the 5th bar in the channel
                    
            if((CurrentState+Counter) == 5){
                        
            //paint the background between the lower and upper channel in lime 
                        
            setBarBgColor(Color.lime,0,Avg.getValue(0)+1.5Avg.getValue(0)-1.5);
                    }
                }
                
                return new Array ((
            Avg.getValue(0)+1.5), (Avg.getValue(0)-1.5))


            Comment


            • #21
              Alex

              Thank you for the code. I implemented the code and it works fine but I can not get the 5th bar to change color lime. When I load your code and adjust it for forex it works fine, but incorporated in my code I encounter the problem.

              I must be overlooking something simple, but I can not find it, your inspection will be appreciated.

              I attach my code for inspection, I will also post an image.

              Regards
              Attached Files

              Comment


              • #22
                Alex

                Here is my code as stated in the previous post.

                Regards
                Attached Files

                Comment


                • #23
                  Kobus
                  There are several errors in the script. Going down from the top
                  • In line 120 you included Counter as a parameter of the main definition ie
                  function main(Length, Offset,Source,..,..,..,Counter)
                  You need to remove Counter as it is not a parameter but a global variable.
                  • In the example I posted I used EFS2 functions which cannot be copied verbatim to your script as this is written using EFS1 functions. To fix this issue you need to replace in lines 128, 129, 131 and 132 all instances of vMA.getValue(0) or vMA1.getValue(0) with vMA.getValue(MAStudy.MA,0) and vMA1.getValue(MAStudy.MA,0).
                  • The condition in line 174 ie. if(high(-1) <= (adjustedvMA) && low(-1) >= (adjustedvMA1)){ is incorrect
                  If you look at the same condition in my example you will see that I reference the average at the prior bar ie Avg.getValue(-1). In your condition instead you are comparing the High and Low at the prior bar to the adjusted averages at the current bar. To resolve this you need to write the condition as follows
                  PHP Code:
                  if(high(-1)<=(vMA.getValue(MAStudy.MA,-1)+AdjustmentU) && low(-1)>=(vMA1.getValue(MAStudy.MA,-1)+AdjustmentD)){ 
                  • The syntax for the command in line 195 ie setBarBgColor(Color.lime,0,vMA.adjustedvMA, adjustedvMA1); is incorrect and should be setBarBgColor(Color.lime,0,adjustedvMA, adjustedvMA1);
                  • Lastly you may want to remove if (getBarState () == BARSTATE_NEWBAR) in line 148 and vFlag = false; in line 150 as they are unused (note that they do not cause any errors)
                  Once you implement these fixes you should see the script work as intended.
                  Alex

                  Comment


                  • #24
                    Alex

                    My sincere thanks for all your patience. I implemented the changes and added a few things.

                    I attached the final code
                    Attached Files

                    Comment


                    • #25
                      Alex

                      I added 2 extra MA's that can act as extra bands.
                      Attached Files

                      Comment


                      • #26
                        Alex

                        Or the extra bands can be used for cross over purposes.

                        I incuded a short explanation in the code of how to use the study.

                        I also change the counter so that different values can be set through the Edit studies window.

                        My thanks again for the help without which this would not have been possible

                        Regards
                        Attached Files

                        Comment


                        • #27
                          Kobus
                          You are most welcome.
                          At this point if you are interested there are some small changes that could be implemented in your code to make it slightly more efficient.
                          • The following section of code (lines 214-217) is called on every execution of main (ie on every tick) but could be instead run only once when the efs is first loaded (or is reloaded).
                          PHP Code:
                          setDefaultBarThicknessThick);
                          setDefaultBarThicknessThick);
                          setDefaultBarThicknessThick12);
                          setDefaultBarThicknessThick1); 
                          In order to run it only once we need to add a global variable (called for example bInitialized) that we set initially to false. Then in main() we check for bInitialized to be equal to false, execute all the commands we want to run only one time and set bInitialized to true. This will prevent the section of code from being executed at every subsequent iteration of the efs. Here is how it would look like
                          PHP Code:
                          var bInitialized false;//global variable
                           
                          function main(MALength1,Offset1,Source1,...,...,...,Thick1){
                           
                              if(
                          bInitialized==false){//check for bInitialized to be false
                                  //execute all the required commands
                                  
                          setDefaultBarThicknessThick);
                                  
                          setDefaultBarThicknessThick);
                                  
                          setDefaultBarThicknessThick12);
                                  
                          setDefaultBarThicknessThick1);
                                  
                          bInitialized true;//set bInitalized to true
                              
                          }
                          //rest of your code 
                          • While we are doing this we might as well include the following section of code (lines 222-225) inside the bInitialized conditional statement.
                          PHP Code:
                          if (vMA1 == nullvMA1 = new MAStudy(MALength1Offset1Source1, eval(Type1));
                          if (
                          vMA2 == nullvMA2 = new MAStudy(MALength2Offset2Source2, eval(Type2));
                          if (
                          vMA3 == nullvMA3 = new MAStudy(MALength3Offset3Source3, eval(Type3));
                          if (
                          vMA4 == nullvMA4 = new MAStudy(MALength4Offset4Source4, eval(Type4)); 
                          Note that you are already running each of those lines once only because you are checking for those functions to be null ie if(vMA1 == null) vMA1 = new MAStudy(...); which will happen only the first time so this change will not improve the efficiency per se but only make the layout of the code a bit cleaner.
                          PHP Code:
                          var bInitialized false;
                           
                          function 
                          main(MALength1,Offset1,Source1,..,...,..,..,Thick1) {
                             
                              if(
                          bInitialized==false){
                                  
                          setDefaultBarThicknessThick);
                                  
                          setDefaultBarThicknessThick);
                                  
                          setDefaultBarThicknessThick12);
                                  
                          setDefaultBarThicknessThick1);
                                  
                          vMA1 = new MAStudy(MALength1Offset1Source1, eval(Type1));
                                  
                          vMA2 = new MAStudy(MALength2Offset2Source2, eval(Type2));
                                  
                          vMA3 = new MAStudy(MALength3Offset3Source3, eval(Type3));
                                  
                          vMA4 = new MAStudy(MALength4Offset4Source4, eval(Type4));
                                  
                          bInitialized true;
                              } 
                          Notice also that I removed from each line the condition if(vMA == null) as that is no longer necessary.
                          • Lines 219 and 220 also are executed on every tick which is unnecessary since you are not modifying those colors based on any condition. We can move those statements to preMain() and change them to setDefaultBarFgColor(...) where they will be executed only once.
                          vMA1.getValue(...) and vMA2.getValue(...) are used multiple times throughout the script. Each time we use those methods we are instructing the efs to retrieve the values from the corresponding functions. Depending on how often this is done it can become somewhat inefficient. A better solution is to assign those values to a local variable and re-use the variable.
                          So, immediately after the bInitialized conditional statement we would add the following
                          PHP Code:
                          var nMA1 vMA1.getValue(MAStudy.MA,0);
                          var 
                          nMA2 vMA2.getValue(MAStudy.MA,0);
                          var 
                          nMA3 vMA3.getValue(MAStudy.MA,0);
                          var 
                          nMA4 vMA4.getValue(MAStudy.MA,0);
                          var 
                          nMA1_1 vMA1.getValue(MAStudy.MA,-1);
                          var 
                          nMA2_1 vMA2.getValue(MAStudy.MA,-1); 
                          Once we have done that we will use nMA1, nMA2, etc in place of vMA1.getValue(...), vMA2.getValue(...), etc
                          Attached to this message is a revision of your efs that includes all these changes
                          Alex
                          Attached Files

                          Comment


                          • #28
                            Alex

                            Thank you for the additional improvements, I have implemented and compared with your updated file, realy great.

                            On behalf of all of us who are benefitting from your clear and patient instruction I would like to express my appreciation.

                            Regards

                            Comment

                            Working...
                            X