Announcement

Collapse
No announcement yet.

Stuck on Coding 2X Stochastic

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

  • Stuck on Coding 2X Stochastic

    I've tried everything to debug the code for a 2X Stochastic. I think the logic and math are right, but I cannot get it to display. It keeps telling me the return new Array is an invalid return. Please help. Thanks.
    Attached Files

  • #2
    FiveString
    There are a couple of errors in the script. The first one is in this line of code which is invalid
    PHP Code:
    var KFullStudy ema(3KFast0); 
    This is because KFast is a value and not a series as required by the ema() function. To make KFast a series you need to calculate it in a separate function (called for example calcKFast) and then call that function into main using efsInternal()
    The separate function would look like this
    PHP Code:
    function calcKFast(){
     
        var 
    LL lowest(7hl2());
        var 
    HH highest(7hl2());
        
        if ((
    HH LL) == 0)  {
            var 
    KFast=0;
        }else{
            var 
    KFast = (100 * ((((high(0)+low(0))/2) - LL) / (HH-LL)));
        }  
        
        return 
    KFast;

    In main() you would then call that function using efsInternal()
    PHP Code:
    var myStudy efsInternal("calcKFast"); 
    At this point myStudy is a series which you can use as a source for the ema() function ie
    PHP Code:
    var KFullStudy ema(3myStudy); 
    You could also simplify the two lines of code and accomplish the same result in one step ie
    PHP Code:
    var KFullStudy ema(3efsInternal("calcKFast")); 
    The next error is in this line of code and is similar to the first one ie you are using a value instead of a series as the source for the second ema() function.
    PHP Code:
    var DFullStudy ema(3KFull0); 
    This should instead be written as follows
    PHP Code:
    var DFullStudy ema(3KFullStudy); 
    Once you implement these changes you should see the script work correctly
    Having said all the above I think you are taking an unnecessarily complex route to calculate your study.
    In fact the same study can be written in its most basic form as follows
    PHP Code:
    function main(){
     
        var 
    KFullStudy ema(3stochK(7,1,1hl2()));
        var 
    DFullStudy ema(3,KFullStudy);
        
        return new Array(
    KFullStudy.getValue(0),DFullStudy.getValue(0));

    This will return the same results as your script once that is corrected of the errors.
    All you need to do is add the preMain() function with the required statements, user defined parameters (if any), etc
    Alex

    Comment


    • #3
      FiveString
      In addition to the information on efsInternal() that is available at the link I provided in my previous reply you can find several more examples on the use of that function in this and this thread
      Alex

      Comment


      • #4
        Wow! Thanks once more for your help. I've been able to get it going with your simplified version, but want to work on allowing variable inputs and cleaning it up.

        I'll also try to learn from the other info you posted. What a wonderful resource you are.

        Thanks again.

        Richard

        Comment


        • #5
          Richard
          You are most welcome and thank you for the kind words
          Alex

          Comment


          • #6
            More Issues

            Alex, I'm glad you have so much patience. I've cut and pasted from a number of places to get the attached the version I got working, but it does have some problems.

            The efs file attached has "commented out" a section that I had to add to get it to work. The comments describe the problems.

            The program does not ask for or allow user input for the variables and does not recognize the default values for those variables, so without the additional section, the program does not work.

            I expect it's something simple, but I can't find it.

            Thanks.

            Richard
            Attached Files

            Comment


            • #7
              Richard
              The script is actually correct. The problem is caused by the word main() which is contained in one of the comments. Delete the word main() (or at least remove the parenthesis) and the script will function properly
              Alex

              Comment


              • #8
                That works -- but I thought all comments were ignored during execution??? Live and learn.

                Now I still have a problem getting the OverBought and OverSold variables to work with the addBands. Says they are not defined if I use them instead of the 90 and 10.

                Comment


                • #9
                  Richard
                  As far as I know all comments are ignored except for the specific word main()
                  The addBand() functions need to be inside the main() function if you want them to be user adjustable.
                  When you move them into main() you will also need to add a tagID that is unique to each addBand() function
                  Alex

                  Comment


                  • #10
                    Alex,

                    Have now got it going, thank you.

                    Next step is tracing through the levels of called functions to see if there are any differences with the formula I tried to do.

                    It appears that the stochK called uses a value of Close(0) rather than High(0)+Low(0)/2. Am I wrong? If not, is there a simple way to change this or do I need to create more functions?

                    My latest is attached.
                    Attached Files
                    Last edited by FiveString; 10-27-2006, 01:12 PM.

                    Comment


                    • #11
                      Richard
                      In the efs you posted the Stochastic study is set to use hl2() [ie (High+Low)/2] as the source.
                      Alex

                      Comment


                      • #12
                        Alex,

                        I see on line 86 of my code where we call StochK with the parameter hl2.

                        But I can't see in StochK that there is an input for that parameter or where it uses the hl2. This also particularly shows in line 53 within calcPercentK which starts with close(0). I can't see where this is modified to become hl2.

                        What am I missing?

                        Richard

                        Comment


                        • #13
                          Richard
                          For the syntax of the Stochastic function(s) see this article in the EFS KnowledgeBase.
                          I would also suggest that you review the links I provided to you in another thread with regards to the syntax of the built-in studies.
                          Alex

                          Comment


                          • #14
                            Alex,

                            Thank you for your infinite patience.

                            I understand the syntax of passing parameters to the stochK function. What I don't see is how those parameters are used when I look at the coding within that function. It does not define the series parameter coming in nor seem to use it. And the equation in line 53 does not adjust to use the series parameter passed in.

                            Richard

                            Comment


                            • #15
                              Richard
                              I am not sure I understand what equation you are referring to. In line 53 of the script you posted I see the following
                              fpArray[x] = new FunctionParameter("OverBought", FunctionParameter.NUMBER);
                              Also I am not sure how you can see the coding of the built-in stochK() function since that is hard-coded into the application
                              Alex

                              Comment

                              Working...
                              X