Announcement

Collapse
No announcement yet.

MOMofMA to MADIFF

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

  • MOMofMA to MADIFF

    Hello, I am using the MOMofMA efs, as suggested in others threads, to get the tick difference in a 20EMA (using 10 as the lookback period). I would like to show the number, with 1 decimal point, in the bottom right of the price pane, not as an indicator line. Number does show in cursor window now.
    My crude fix only gives me a "NaN" display at placement.
    Using on YM seems ok, but on AB or NQ doesn't give tick increment, only number difference, do I need a divider on these?
    I also want to calculate the difference of a 20LSMA (using 5 as the lookback), can I use this same template? I will place it just above the MADIFF number.
    Can you please help?
    Here is the code:

    var fpArray = new Array();
    function preMain() {
    //changed the name of EFS and took off indicator line
    setPriceStudy(true);
    setStudyTitle("MADIFF");
    setCursorLabelName("MADIFF", 0);
    //setDefaultBarStyle(PS_SOLID, 0);
    //setDefaultBarFgColor(Color.blue, 0);
    //setDefaultBarThickness(1, 0);
    //setPlotType(PLOTTYPE_LINE, 0);
    askForInput();

    var x=0;
    //changed a few of the defaults
    fpArray[x] = new FunctionParameter("DIFFLength", FunctionParameter.NUMBER);
    with(fpArray[x++]){
    setLowerLimit(1);
    setDefault(10);
    }
    fpArray[x] = new FunctionParameter("MAType", FunctionParameter.STRING);
    with(fpArray[x++]){
    addOption("sma");
    addOption("ema");
    addOption("wma");
    setDefault("ema");
    }
    fpArray[x] = new FunctionParameter("MALength", 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");
    setDefault("hlc3");
    }
    fpArray[x] = new FunctionParameter("Symbol", FunctionParameter.STRING);
    with(fpArray[x++]){
    setDefault();
    }
    fpArray[x] = new FunctionParameter("Interval", FunctionParameter.STRING);
    with(fpArray[x++]){
    setDefault();
    }
    fpArray[x] = new FunctionParameter("Params", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
    setName("Show Parameters");
    setDefault(false);
    }
    }

    var bInit = false;
    //changed name of variable
    var xDIFFofMA = null;
    function main(DIFFLength,MAType,MALength,Source,Symbol,Inte rval,Params)

    {
    if(bInit == false){
    if(Symbol == null) Symbol = getSymbol();
    if(Interval == null) Interval = getInterval();
    var vSymbol = Symbol+","+Interval;
    //change name of variable
    xDIFFofMA = mom(DIFFLength,eval(MAType)(MALength,eval(Source)( sym(vSymbol))));
    setShowTitleParameters(eval(Params));
    bInit = true;
    }

    //tried to add text placement
    drawTextAbsolute(5,0,xDIFFofMA.toFixed(0),null,nul l,Text.RELATIVETOBOTTOM | Text.BOLD|Text.VCENTER,"Arial",20,"MADIFF");
    return getSeries(xDIFFofMA);
    }

    Any help would be greatly appreciated.
    Thank you

  • #2
    rainwater88
    The NaN (which means Not A Number) is caused by the fact that you are applying the .toFixed(0) method to a series object and not to a specific value.
    To correct this replace xDIFFofMA.toFixed(0) with xDIFFofMA.getValue(0).toFixed(0) in the following line of code of your script
    PHP Code:
    drawTextAbsolute(5,0,xDIFFofMA.toFixed(0),null,null,
                         
    Text.RELATIVETOBOTTOM Text.BOLD|Text.VCENTER,"Arial",20,"MADIFF"); 
    If you now run the script you will get an error that says that xMADIFFofMA.getValue(0) has no properties. This is because while the study is priming it returns null so you need to run a null check on that value prior to applying the .toFixed(0) method.
    To do this insert the following line of code just before the drawTextAbsolute(...) command
    PHP Code:
    if(xDIFFofMA.getValue(0)==null) return; 
    The next thing you need to do is to remove or comment out (as shown below) the return statement ie
    PHP Code:
    //return getSeries(xDIFFofMA); 
    This way the script will not be returning any values to the chart.
    Once you implement these changes you should get a display similar to the one shown in the enclosed screenshot
    Alex

    Comment


    • #3
      Thank you Alexis, your correction worked great and your explanation's make it easier to understand how the EFS works. I took that template and replaced with the LSMA (using 5 lookback). The number shows up fine at placement but rarely is correct calculation, usually off by halfish but that's not constant. Am I missing something in the math because it is using the LSMA, or do other things need to be changed? I took out the "MAType" statement as that was not in the LSMA efs. Thank you again for help
      PHP Code:
      var fpArray = new Array();
      function 
      preMain() {
      //changed the name of EFS and took out indicator line
          
      setPriceStudy(true);
          
      setStudyTitle("LSMADIFF");
          
      setCursorLabelName("LSMADIFF"0);
          
      //setDefaultBarStyle(PS_SOLID, 0);
          //setDefaultBarFgColor(Color.blue, 0);    
          //setDefaultBarThickness(1, 0);
          //setPlotType(PLOTTYPE_LINE, 0);
          
      askForInput();
          var 
      x=0;
      //changed a few of the defaults
          
      fpArray[x] = new FunctionParameter("DIFFLength"FunctionParameter.NUMBER);
          
      with(fpArray[x++]){
              
      setLowerLimit(1);        
              
      setDefault(5);
          }
          
      //took out MAType
          //fpArray[x] = new FunctionParameter("MAType", FunctionParameter.STRING);
          //with(fpArray[x++]){
              //addOption("sma");
              //addOption("ema");
              //addOption("wma");
              //setDefault("ema");
          //}
          
      fpArray[x] = new FunctionParameter("MALength"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"); 
              
      setDefault("hlc3"); 
          }
          
      fpArray[x] = new FunctionParameter("Symbol"FunctionParameter.STRING);
          
      with(fpArray[x++]){
              
      setDefault();
          }
          
      fpArray[x] = new FunctionParameter("Interval"FunctionParameter.STRING);
          
      with(fpArray[x++]){
              
      setDefault();
          }
          
      fpArray[x] = new FunctionParameter("Params"FunctionParameter.BOOLEAN);
          
      with(fpArray[x++]){
              
      setName("Show Parameters");
              
      setDefault(false);
          }
      }
      //added LSMA variable
      var amLib addLibrary("amStudies.efsLib");
      var 
      bInit false;
      var 
      xLSMA null;
      //took out DIFFofMA
      //var xDIFFofMA = null;
      //took out MAType
      function main(DIFFLength,MALength,Source,Symbol,Interval,Params) {
          if(
      bInit == false){
              if(
      Symbol == nullSymbol getSymbol();
              if(
      Interval == nullInterval getInterval();
              var 
      vSymbol Symbol+","+Interval;
      //change name of variable, took out MAType    
              
      xLSMA mom(DIFFLength,(MALength,eval(Source)(sym(vSymbol))));
              
      setShowTitleParameters(eval(Params));
              
      bInit true;
          }  
      //statement added as instructed
          
      if(xLSMA.getValue(0)==null) return;
      //statement changed as instructed, added 1 decimal point and set placement above MADIFF number    
          
      drawTextAbsolute(5,30,xLSMA.getValue(0).toFixed(1),null,null,Text.RELATIVETOBOTTOM Text.BOLD|Text.VCENTER,"Arial",16,"LSMADIFF");
              
      //statement taken off as instructed
              //return getSeries(xDIFFofMA);

      Comment


      • #4
        rainwater88
        The problem is in the following line of your script (line 71)
        PHP Code:
        xLSMA mom(DIFFLength,(MALength, eval(Source)(sym(vSymbol)))); 
        in which you omitted to add the amLSMA() function. Replace it with the following
        PHP Code:
        xLSMA mom(DIFFLength,amLib.amLSMA(MALength,eval(Source)(sym(vSymbol)))); 
        This should fix the problem you are seeing
        Alex

        Comment


        • #5
          Thank you Alex, both programs work perfect, just what I was looking for. Sure appreciate your time and prompt responses. Here is the finished MADIFF efs and will post again with the LSMADIFF efs.
          Attached Files

          Comment


          • #6
            Here is the LSMADIFF efs. Thank you again Alex, great job.
            Attached Files

            Comment


            • #7
              rainwater88
              You are most welcome
              Alex

              Comment

              Working...
              X