Announcement

Collapse
No announcement yet.

Argh! Can't get this to work

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

  • Argh! Can't get this to work

    Hi,
    I am at my wits end trying to figure this out. I am having trouble creating a custom series and then applying a technical analysis study to it.

    In the attached efs, I am trying to calculate 2 ROC series using different calculation periods (one 11 days, the other 14 days), add them together, and then create a weighted MA of the sum. There are no errors from the code when loading on to a chart, but nothing is plotted.

    I must be missing something simple...

    Any and all help appreciated,

    Jonathan
    Attached Files

  • #2
    Jonathan
    There are too many errors in your script to try and fix it. I would suggest re-writing the script from scratch starting with the most basic construct. Then you can then add whatever user adjustable parameters, etc you may want or need.
    Here are a couple of things to keep in mind. When you apply some math to a series the result is not a series but a value. This means that the sum of the two ROCs in your script is not a series and therefore cannot be used as the source for the wma() function. You need to perform your calculations in a separate function or efs and then call that function or efs from main() using efsInternal() or efsExternal(). This will create the series that you can then pass to the wma() function.
    The getSeries() function is only used when you need to retrieve an already existing series that has been cached and not to create a series.
    That said here is how the efs needs to be written. First declare the required global variables outside of main() or preMain(). Note that you could also declare these variables locally but it is more effcient to do it this way.
    PHP Code:
    var xSmoothPrice null;
    var 
    xROC1 null;
    var 
    xROC2 null;
    var 
    xSumROCnull;
    var 
    xAverage null
    Then create a function called sumROC in which you calculate the two ROCs used in your equation.
    PHP Code:
    function sumROC(){

        if(
    xSmoothPrice==nullxSmoothPrice sma(10);
        if(
    xROC1==nullxROC1 roc(11,xSmoothPrice);
        if(
    xROC2==nullxROC2 roc(14,xSmoothPrice);

        if(
    xROC1.getValue(0)==null || xROC2.getValue(0)==null) return;//null check
        
        
    return ((xROC1.getValue(0)+xROC2.getValue(0))-2)*100;//add the values of the two ROCs

    At this point in the main() function you use the efsInternal() function to call the separate function and create the series which you can then use as a source for the wma() function
    PHP Code:
    function main(){
        
        if(
    xSumROC==nullxSumROC efsInternal("sumROC");//call separate function 
        
    if(xAverage==nullxAverage wma(10xSumROC);//calculate weighted average using the sum of ROC series as source
        
        
    if(xSumROC.getValue(0)==null || xAverage.getValue(0)==null) return;//null check
        
        
    return new Array (xSumROC.getValue(0), xAverage.getValue(0));//return values

    Following are the completed efs with some comments and a screenshot of the resulting plot.
    Alex

    PHP Code:
    //declare global variables
    var xSmoothPrice null;
    var 
    xROC1 null;
    var 
    xROC2 null;
    var 
    xSumROC null;
    var 
    xAverage null;

    function 
    main(){
        
        if(
    xSumROC==nullxSumROC efsInternal("sumROC");//call separate function 
        
    if(xAverage==nullxAverage wma(10xSumROC);//calculate weighted average using the sum of ROC series as source
        
        
    if(xSumROC.getValue(0)==null || xAverage.getValue(0)==null) return;//null check
        
        
    return new Array (xSumROC.getValue(0), xAverage.getValue(0));//return values
    }

    //separate function to calculate and add two ROCs
    function sumROC(){

        if(
    xSmoothPrice==nullxSmoothPrice sma(10);//smooth price using simple average
        
    if(xROC1==nullxROC1 roc(11,xSmoothPrice);//calculate first ROC study using smoothed price series as source
        
    if(xROC2==nullxROC2 roc(14,xSmoothPrice);//calculate second ROC study using smoothed price series as source

        
    if(xROC1.getValue(0)==null || xROC2.getValue(0)==null) return;//null check
        
        
    return ((xROC1.getValue(0)+xROC2.getValue(0))-2)*100;//add the values of the two ROCs

    Comment


    • #3
      Alex,
      THank you! THank you! That is exactly what I was looking for. It works now with no problem. Just one thing: when I select Weekly interval trying to override the chart's daily interval, it isn't charting the way I expect. Rather than one value for the whole week, it charts a value everyday.

      Any idea how to change this?
      Attached Files

      Comment


      • #4
        Jonathan
        You are most welcome.
        The sample script I provided was not set up for use with external intervals so I would not expect it to calcualte the indicator based on weekly data
        If you have modified the script then please post your version so that I or someone else can see why it is not working and help you in fixing it.
        Alex

        Comment


        • #5
          Edited script

          Hi Alex,
          Posting the script would be a good idea. Here it is. Hopefully with less errors than the otherone. I created two new internal functions to create some other series I need. I hope they are done correctly.

          Thanks again for all your help,

          Jonathan
          Attached Files

          Comment


          • #6
            Jonathan
            In order for efsInternal() or efsExternal() to be able to control the context (ie the interval and/or symbol) in which the called function or efs are going to run you need to pass the interval and/or symbol using the inv() or sym() functions (see the respective links for descriptions and syntax). Also, as explained in the KB articles for efsInternal() and efsExternal() these must be the last parameter being passed to the function or efs.
            In order for your script to work you need to change lines 103, 107 and 121 to the following
            PHP Code:
            xMOM1 =  efsInternal("sumROC",Smoothing,sym(vSymbol));//line 103

            vRatio efsInternal("CreateRatio"sym(vSymbol),sym(vSymComp));//line 107

            return close(Sym1) / close(Sym2);//line 121 
            Having said this you could also simplify your script and rather than calculating the ratio using a function you could calculate the ratio directly as a symbol and pass that symbol to the sumROCCustom function.
            To do this you would remove line 107 and the CreateRatio function and modify line 108 as follows
            PHP Code:
            xMOM1 =  efsInternal("sumROCCustom",Smoothing,sym(vSymComp)); 
            Then in Edit Studies you would enter your symbol as a ratio in the CompareSymbol box. For example if the main symbol was IBM you would enter IBM / $SPX in the CompareSymbol box. The result should be the same as calculating the ratio yourself. You could also automate the process of creating the composite symbol if you want.
            Alex

            Comment


            • #7
              Alex, As always thanks again for your help. I will make these changes and get it working.
              JOnathan

              Comment


              • #8
                Jonathan
                You are most welcome
                Alex

                Comment

                Working...
                X