Announcement

Collapse
No announcement yet.

Help with EMA(); Series Needed?

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

  • Help with EMA(); Series Needed?

    What I want to accomplish is the following:
    1- Run the calculation in Line 10 (That works); then,
    2- Calculate an EMA of line 10, in line 11; then,
    3- Calculate another EMA (double smoothing) in line 12 of line 11; then,
    4- Return the results in vEMAsN.

    CODE SAMPLE:
    1- function preMain() {
    2- setPriceStudy(false);
    3- setStudyTitle("Array");
    4- setCursorLabelName("Array", 0);
    5- setDefaultBarStyle(PS_SOLID, 0);
    6- setDefaultBarFgColor(Color.navy, 0);
    7- setDefaultBarThickness(1, 0);
    8- setPlotType(PLOTTYPE_HISTOGRAM, 0);
    }

    9- function main() {

    10- var vSMq = (getValue("close") - (.5 * (getValue("high") + getValue("low"))));
    11- var vEMArN = new ema(20, vSMq);
    12- var vEMAsN = new ema(20, vEMArN);

    13- return vEMAsN;

    }

    The problem I have is line 12. When I run the code, the following error comes up:

    Line 12; Parameter No. 2 of function ema is invalid

    For some reason the EMA does not look at vEMArN and use it to calculate the final result. I believe it may be because vSMq is not in a Series, but I am not sure. If that is the case, how do I get the vSMq into a Series so an EMA() will correctly use it. Or, can I just put my math in line 10 direclty into the first EMA()? I trued that but it does not appear to work.

    If anyone has any thoughts, please let me know.

    Regards,
    ZM

  • #2
    Re: Help with EMA(); Series Needed?

    zmendel
    The reason you are getting that error message is because your variable vSMq is not a series as required by the ema() function but a value.
    In order to create a series that can be used as a source for the ema() function you need to calculate your expression
    (getValue("close") - (.5 * (getValue("high") + getValue("low"))));
    in a separate function that you then call from the main function using the efsInternal() function (see the link for the description an syntax)
    So as the first step create a separate function called [for example] myCalc
    PHP Code:
    function myCalc(){
        return (
    getValue("Close") - (.5 * (getValue("High") + getValue("Low"))));

    At this point in main you use the efsInternal() function to call myCalc and you assign that to your variable vSMq
    PHP Code:
    var vSMq efsInternal("myCalc"); 
    Once you have done that vSMq will be a series which you can then use as the source for the ema() function.
    If you search these forums for the keyword efsInternal* you will find many examples of how to use this function to create custom series
    Hope this helps
    Alex


    Originally posted by zmendel
    What I want to accomplish is the following:
    1- Run the calculation in Line 10 (That works); then,
    2- Calculate an EMA of line 10, in line 11; then,
    3- Calculate another EMA (double smoothing) in line 12 of line 11; then,
    4- Return the results in vEMAsN.

    CODE SAMPLE:
    1- function preMain() {
    2- setPriceStudy(false);
    3- setStudyTitle("Array");
    4- setCursorLabelName("Array", 0);
    5- setDefaultBarStyle(PS_SOLID, 0);
    6- setDefaultBarFgColor(Color.navy, 0);
    7- setDefaultBarThickness(1, 0);
    8- setPlotType(PLOTTYPE_HISTOGRAM, 0);
    }

    9- function main() {

    10- var vSMq = (getValue("close") - (.5 * (getValue("high") + getValue("low"))));
    11- var vEMArN = new ema(20, vSMq);
    12- var vEMAsN = new ema(20, vEMArN);

    13- return vEMAsN;

    }

    The problem I have is line 12. When I run the code, the following error comes up:

    Line 12; Parameter No. 2 of function ema is invalid

    For some reason the EMA does not look at vEMArN and use it to calculate the final result. I believe it may be because vSMq is not in a Series, but I am not sure. If that is the case, how do I get the vSMq into a Series so an EMA() will correctly use it. Or, can I just put my math in line 10 direclty into the first EMA()? I trued that but it does not appear to work.

    If anyone has any thoughts, please let me know.

    Regards,
    ZM

    Comment


    • #3
      zmendel
      Adding to my prior reply.
      You do not need to use the new operator with the efs2 functions. In your code replace
      var vEMArN = new ema(20, vSMq);
      var vEMAsN = new ema(20, vEMArN);

      in lines 11 and 12 with the following
      var vEMArN = ema(20, vSMq);
      var vEMAsN = ema(20, vEMArN);

      Hope this helps
      Alex

      Comment

      Working...
      X