Announcement

Collapse
No announcement yet.

source question

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

  • source question

    Hello,
    I added some function parameters to this EFS and would like to be able to use high + low + (2*close)/4 as one of the sources for the CCI, if not the only choice, but I don't seem to be able to get it to work, it comes up blank.
    Also when I try to add direction color to the line it goes blank as well.
    Any help would be greatly appreciated.

    PHP Code:
    /*********************************************************
    By Alexis C. Montenegro December 2004                     
    Use and/or modify this code freely. If you redistribute it
    please include this and/or any other comment blocks and a 
    description of any changes you make.                      
    **********************************************************/
    var fpArray = new Array();
    function 
    preMain() {
        
    setPriceStudy(false);
        
    setStudyTitle("CCI adj");
        
    setCursorLabelName("CCI adj"0);
        
    setDefaultBarFgColor(Color.yellow0);
        
    setPlotType(PLOTTYPE_LINE,0);
        
    setDefaultBarThickness(1,0);
        
    setStudyMin(-220);
        
    setStudyMax(220);
        
    addBand(200PS_DOT2Color.grey,"200");
        
    addBand(100PS_DOT2Color.green,"100");
        
    addBand(0PS_DASH2Color.grey,"zero");
        
    addBand(-100PS_DOT2Color.green,"-100");
        
    addBand(-200PS_DOT2Color.grey,"-200"); 
        
    askForInput();
        var 
    x=0;
        
    fpArray[x] = new FunctionParameter("Length"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(1);        
            
    setDefault(20);
        }
        
    fpArray[x] = new FunctionParameter("Source"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("open"); 
            
    addOption("high");
            
    addOption("low");
            
    addOption("close");
            
    addOption("hl2");
            
    addOption("hlc3");
            
    addOption("ohlc4");
            
    //Is there any way to add HLCC option here ?
            //addOption("hlcc4");
            
    setDefault("hlc3"); 
        }
        
    fpArray[x] = new FunctionParameter("Type"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("sma");
            
    addOption("ema");
            
    addOption("wma");
            
    addOption("vwma");
            
    setDefault("sma");
        }
        
    fpArray[x] = new FunctionParameter("Multiplier"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(0.001);        
            
    setDefault(0.015);
        }
    }
    var 
    bInit false;
    var 
    xPrice null;
    var 
    xAvgPrice null;
    var 
    xMulti null;
    var 
    xCCI null;
    //var xHLCC = null;
    function main(LengthSourceTypeMultiplier){
        if(
    bInit == false){ 
        
    //Tried to add new source here
        //xHLCC = (high(0)+low(0)+(2*close(0)))/4; 
        //xPrice = xHLCC;
        
    xPrice = eval(Source)();
        
    xAvgPrice = eval(Type)(Length,xPrice);
        
    xMulti Multiplier;
        
    bInit true;
        }
        if(
    xPrice.getValue(-Length)==null||xAvgPrice.getValue(-Length)==null) return;
        var 
    Sum 0;
        for (var 
    i=0i<Lengthi++){
            
    Sum += Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0));
        }
        var 
    MeanDevPrice Sum/Length;
        var 
    xCCI = (xPrice.getValue(0)-xAvgPrice.getValue(0))/(MeanDevPrice*xMulti);
        
    //CCI color change with direction change
        /*if(xCCI.getValue(0) > xCCI.getValue(-1)) {
            setBarFgColor(Color.green,0);
                } else setBarFgColor (Color.red,0);*/
        
    return xCCI;    


  • #2
    Re: source question

    rainwater88
    The reason your script is not working is because hlcc4 is not a valid source as there is no such function.
    In order to do what you want you will need to calculate your custom source in a separate function or efs and then call that function or efs from inside main() using either the efsInternal() or efsExternal() functions (see the links to the corresponding article in the EFS KnowledgeBase for the descripotion and syntax of these functions). This will create the series which you can then use as a source for the moving average functions used to calculate xAvgPrice
    For a more detailed explanation and solution see this post in reply to a similar question. In that reply you will also find links to other examples on creating studies on studies or custom variables using efs2 functions
    Alex


    Originally posted by rainwater88
    Hello,
    I added some function parameters to this EFS and would like to be able to use high + low + (2*close)/4 as one of the sources for the CCI, if not the only choice, but I don't seem to be able to get it to work, it comes up blank.
    Also when I try to add direction color to the line it goes blank as well.
    Any help would be greatly appreciated.

    PHP Code:
    /*********************************************************
    By Alexis C. Montenegro December 2004                     
    Use and/or modify this code freely. If you redistribute it
    please include this and/or any other comment blocks and a 
    description of any changes you make.                      
    **********************************************************/
    var fpArray = new Array();
    function 
    preMain() {
        
    setPriceStudy(false);
        
    setStudyTitle("CCI adj");
        
    setCursorLabelName("CCI adj"0);
        
    setDefaultBarFgColor(Color.yellow0);
        
    setPlotType(PLOTTYPE_LINE,0);
        
    setDefaultBarThickness(1,0);
        
    setStudyMin(-220);
        
    setStudyMax(220);
        
    addBand(200PS_DOT2Color.grey,"200");
        
    addBand(100PS_DOT2Color.green,"100");
        
    addBand(0PS_DASH2Color.grey,"zero");
        
    addBand(-100PS_DOT2Color.green,"-100");
        
    addBand(-200PS_DOT2Color.grey,"-200"); 
        
    askForInput();
        var 
    x=0;
        
    fpArray[x] = new FunctionParameter("Length"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(1);        
            
    setDefault(20);
        }
        
    fpArray[x] = new FunctionParameter("Source"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("open"); 
            
    addOption("high");
            
    addOption("low");
            
    addOption("close");
            
    addOption("hl2");
            
    addOption("hlc3");
            
    addOption("ohlc4");
            
    //Is there any way to add HLCC option here ?
            //addOption("hlcc4");
            
    setDefault("hlc3"); 
        }
        
    fpArray[x] = new FunctionParameter("Type"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("sma");
            
    addOption("ema");
            
    addOption("wma");
            
    addOption("vwma");
            
    setDefault("sma");
        }
        
    fpArray[x] = new FunctionParameter("Multiplier"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(0.001);        
            
    setDefault(0.015);
        }
    }
    var 
    bInit false;
    var 
    xPrice null;
    var 
    xAvgPrice null;
    var 
    xMulti null;
    var 
    xCCI null;
    //var xHLCC = null;
    function main(LengthSourceTypeMultiplier){
        if(
    bInit == false){ 
        
    //Tried to add new source here
        //xHLCC = (high(0)+low(0)+(2*close(0)))/4; 
        //xPrice = xHLCC;
        
    xPrice = eval(Source)();
        
    xAvgPrice = eval(Type)(Length,xPrice);
        
    xMulti Multiplier;
        
    bInit true;
        }
        if(
    xPrice.getValue(-Length)==null||xAvgPrice.getValue(-Length)==null) return;
        var 
    Sum 0;
        for (var 
    i=0i<Lengthi++){
            
    Sum += Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0));
        }
        var 
    MeanDevPrice Sum/Length;
        var 
    xCCI = (xPrice.getValue(0)-xAvgPrice.getValue(0))/(MeanDevPrice*xMulti);
        
    //CCI color change with direction change
        /*if(xCCI.getValue(0) > xCCI.getValue(-1)) {
            setBarFgColor(Color.green,0);
                } else setBarFgColor (Color.red,0);*/
        
    return xCCI;    

    Comment


    • #3
      Thank you Alexis

      I used the EFSInternal

      xPrice = EFSInternal("xHLLCC")
      xHLCC function() {
      return((high(0)+low(0)+(2*close(0)))/4;
      }
      works good.

      With the formula as is below when I try to add direction color to the line, (bottom blocked out code), it goes blank, any suggestions ?

      Thanks again for you help.

      Comment


      • #4
        rainwater88
        The reason why the script is not working when you uncomment that section of code is because in the condition to color the plot ie
        if(xCCI.getValue(0) > xCCI.getValue(-1)) {
        you are trying to retrieve the current and previous values of xCCI using the .getValue() method which is invalid since xCCI is not a series but a single value. In this case the simplest solution is to use one of methods suggested in this post.
        Alex


        Originally posted by rainwater88
        Thank you Alexis

        I used the EFSInternal

        xPrice = EFSInternal("xHLLCC")
        xHLCC function() {
        return((high(0)+low(0)+(2*close(0)))/4;
        }
        works good.

        With the formula as is below when I try to add direction color to the line, (bottom blocked out code), it goes blank, any suggestions ?

        Thanks again for you help.

        Comment


        • #5
          Thanks Alex, I managed to get the line red but no green though. I still must have something wrong ?

          PHP Code:
          var bInit false;
          var 
          xPrice null;
          var 
          xAvgPrice null;
          var 
          xMulti null;
          var 
          xCCI null;
          var 
          xCCI_1 null;
          function 
          main(LengthSourceTypeMultiplier){
              if (
          getBarState() == BARSTATE_NEWBAR) {
              
          xCCI_1 xCCI;
              }
              if(
          bInit == false){    
              
          xPrice = eval(Source)();
              
          xAvgPrice = eval(Type)(Length,xPrice);
              
          xMulti Multiplier;
              
          bInit true;
              }
              if(
          xPrice.getValue(-Length)==null||xAvgPrice.getValue(-Length)==null) return;
              var 
          Sum 0;
              for (var 
          i=0i<Lengthi++){
                  
          Sum += Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0));
              }
              var 
          MeanDevPrice Sum/Length;
              var 
          xCCI = (xPrice.getValue(0)-xAvgPrice.getValue(0))/(MeanDevPrice*xMulti);
              
              if(
          xCCI >= xCCI_1) {
                  
          setBarFgColor(Color.green,0);
                      } else 
          setBarFgColor(Color.red,0);
              
              return 
          xCCI;    

          Comment


          • #6
            rainwater88
            Remove the var from the line
            var xCCI = (xPrice.getValue(0)-xAvgPrice.getValue(0))/(MeanDevPrice*xMulti);
            and it should work.
            Alex



            Originally posted by rainwater88
            Thanks Alex, I managed to get the line red but no green though. I still must have something wrong ?

            PHP Code:
            var bInit false;
            var 
            xPrice null;
            var 
            xAvgPrice null;
            var 
            xMulti null;
            var 
            xCCI null;
            var 
            xCCI_1 null;
            function 
            main(LengthSourceTypeMultiplier){
                if (
            getBarState() == BARSTATE_NEWBAR) {
                
            xCCI_1 xCCI;
                }
                if(
            bInit == false){    
                
            xPrice = eval(Source)();
                
            xAvgPrice = eval(Type)(Length,xPrice);
                
            xMulti Multiplier;
                
            bInit true;
                }
                if(
            xPrice.getValue(-Length)==null||xAvgPrice.getValue(-Length)==null) return;
                var 
            Sum 0;
                for (var 
            i=0i<Lengthi++){
                    
            Sum += Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0));
                }
                var 
            MeanDevPrice Sum/Length;
                var 
            xCCI = (xPrice.getValue(0)-xAvgPrice.getValue(0))/(MeanDevPrice*xMulti);
                
                if(
            xCCI >= xCCI_1) {
                    
            setBarFgColor(Color.green,0);
                        } else 
            setBarFgColor(Color.red,0);
                
                return 
            xCCI;    

            Comment


            • #7
              Thank you Alex,
              your very kind, I never would of figured that one out, but I am advancing my knowledge of how to work with EFS, little by little. If I may ask what is the reason for the removal ?
              Here is the finished EFS
              Thanks Alex
              Attached Files

              Comment


              • #8
                rainwater88
                You are most welcome.
                Removing the var is required because the variable xCCI needs to be declared globally (rather than locally as was the case) so that its value persists between iterations of the efs and you can transfer it to xCCI_1. If it is declared locally it will instead be undefined at the following iteration of the efs until it gets declared and a value is assigned to it
                Hope this helps
                Alex


                Originally posted by rainwater88
                Thank you Alex,
                your very kind, I never would of figured that one out, but I am advancing my knowledge of how to work with EFS, little by little. If I may ask what is the reason for the removal ?
                Here is the finished EFS
                Thanks Alex

                Comment


                • #9
                  rainwater88
                  To illustrate what I explained in my previous reply run the following efs on a chart and open the Formula Output Window.
                  PHP Code:
                  //global variables
                  var null;
                  var 
                  null;

                  function 
                  main(){

                      
                  debugPrintln("in line 6 x is = "+x);

                      if(
                  getBarState()==BARSTATE_NEWBAR){
                          
                  x;//value of x is transferred to y at every first tick of new bar
                      
                  }
                          
                      var 
                  close(0);//declaring locally overrides the global scope
                      
                      
                  debugPrintln("in line 15 x is = "+x);

                  You will see that on each iteration of the efs the variable x at line 6 will return undefined hence y will also be set to undefined. This is what was happening in your script prior to the correction.



                  Now remove the var from line 15, save the efs and reload it.



                  You will now see that on each iteration of the efs the variable x at line 6 will return value of x at line 15 of the prior iteration hence that value will be transferred to the variable y
                  Alex

                  Comment


                  • #10
                    Alex, Thank you for the example, I understand the difference in variable calls better now.

                    From the CCI efs example below could you translate this piece of code for me. I see this in many formulas, I understand the results, but don't understand the use of "i" or how the code is working.

                    var Sum = 0;

                    for (var i=0; i<Length; i++){

                    Sum += Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0));

                    }

                    Thank you,
                    rainwater

                    Comment


                    • #11
                      rainwater
                      That is a for loop. In the example the variable i will hold a value that starts at 0 and keeps on incrementing by 1 on each iteration of the loop as long as i is less than the value of Length. When the value of i is equal to the value of Length the loop terminates. On each iteration of the loop it adds to the local variable Sum the result of the equation Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0))
                      For a complete description of the for loop including examples on how to use it you may want to review this article of the Core JavaScript Guide which is in the EFS KnowledgeBase. The for loop is also explained in the JavaScript for EFS video series
                      Alex


                      Originally posted by rainwater88
                      Alex, Thank you for the example, I understand the difference in variable calls better now.

                      From the CCI efs example below could you translate this piece of code for me. I see this in many formulas, I understand the results, but don't understand the use of "i" or how the code is working.

                      var Sum = 0;

                      for (var i=0; i<Length; i++){

                      Sum += Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0));

                      }

                      Thank you,
                      rainwater

                      Comment


                      • #12
                        Very good Alex, you have been most helpful.
                        This has been a great exercise to further my knowledge of EFS coding.
                        Sometimes it becomes a little challenging to be an affective trader and learn EFS programming at the same time. The EFS work is usually put off until later.
                        You make it much easier to learn the code and how it is used.
                        Thank you for your time and assistance.
                        rainwater

                        Comment


                        • #13
                          rainwater88
                          You are most welcome
                          Alex


                          Originally posted by rainwater88
                          Very good Alex, you have been most helpful.
                          This has been a great exercise to further my knowledge of EFS coding.
                          Sometimes it becomes a little challenging to be an affective trader and learn EFS programming at the same time. The EFS work is usually put off until later.
                          You make it much easier to learn the code and how it is used.
                          Thank you for your time and assistance.
                          rainwater

                          Comment

                          Working...
                          X