Announcement

Collapse
No announcement yet.

Correct usage of creating new studyies or and anonomly

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

  • Correct usage of creating new studyies or and anonomly

    I have written some code that is expereiencing some weird behavior.

    First, when you assign variable to be a default study, like RSI
    i.e. var rs = new RSIStudy(16, "HLC/3");

    is it best to do this outside of main, and premain, so it have global scope? or is it better to do this within main.


    My understanding is if you do it once, outside of main and premain, then it is only created once(I take it, it's updated for each bar automatically) so this would have the best performance as it's not creating a new rsi each time the code is called.

    Well I did this for a package I am writing, and found that when this is declared this way, and I am set to computeonclose, my main is being executed with each tick. When I move this study/variable definition within main this problem goes away. Another anomoly that is also caused by this definition being outside of main, is when I draw lines on the chart, with each new tick the lines move to the left. I did some debuging and found that my internal tracking for the pviots of these lines are not being updated, as designed, so it must have something to do with esignal's internal code that is causing this anomoly. Any ideas?

    I moved the definition within main and all seems to work, except I do notice the performance is not as fast when I run the routine on all the bars on the chart(I actually filter and only do it for the last 200) but it still takes a lot longer to execute than with the definition outside of main.

    thoughts?
    Last edited by philli4093; 04-23-2003, 10:27 AM.

  • #2
    is it best to do this outside of main, and premain, so it have global scope? or is it better to do this within main.
    Ouside of a function (like main() or PreMain()) is best, for a number of reasons.

    My understanding is if you do it once, outside of main and premain, then it is only created once(I take it, it's updated for each bar automatically) so this would have the best performance as it's not creating a new rsi each time the code is called.
    You can do it in main and avoid this by checking for a
    getBarState() return of BARSTATE_ALLBARS and settting up your builtin only when this is true.

    Well I did this for a package I am writing, and found that when this is declared this way, and I am set to computeonclose, my main is being executed with each tick.
    I have done similar things and never seen this behavior.

    Another anomoly that is also caused by this definition being outside of main, is when I draw lines on the chart, with each new tick the lines move to the left. I did some debuging and found that my internal tracking for the pviots of these lines are not being updated, as designed, so it must have something to do with esignal's internal code that is causing this anomoly. Any ideas?
    This is almost always a bug in the efs code. I have fixed this for people maybe a dozen times now...if you want post up the code and I will take a look.
    Garth

    Comment


    • #3
      Hello philli4093,

      If you var a study inside main(), the data for the study is calculated for every bar in your chart every time main() is called. This isn't necessary and will slow down the performance of the formula dramatically. Always var the studies outside main(). The study objects only need to be established once.

      As for your lines shifting left and not updating, please post your code here and I will investigate the problem.
      Jason K.
      Project Manager
      eSignal - an Interactive Data company

      EFS KnowledgeBase
      JavaScript for EFS Video Series
      EFS Beginner Tutorial Series
      EFS Glossary
      Custom EFS Development Policy

      New User Orientation

      Comment


      • #4
        Thanks

        Unforuanately, I can't post the code. It's a port of some code I've written on another platform and has some Intectualy property I can't release. I am just trying to port from one trading system to another. I like what I see in esignal.

        I do not think it's my code, as I've gone ahead an put a statement in the code that would cause the entire main routine to not execute with each tick

        if (getBarState()==BARSTATE_CURRENTBAR)
        {return;}

        I've also included debug statement that would print if the code for some reason got past here..and it doesn't show those debug.
        only think I can think is that the lines that I've plotted on the chart are affected by the global studies I've initialized.

        I'll try to recreate the outline of the issue with some generic code to see if I can reproduce and send to you.

        Thanks

        t

        Comment


        • #5
          Unforuanately, I can't post the code. It's a port of some code I've written on another platform and has some Intectualy property I can't release.
          Well, I can understand that. I have similar issues with some of my codes...

          do not think it's my code, as I've gone ahead an put a statement in the code that would cause the entire main routine to not execute with each tick
          I'm not sure which of the problems you think this proves isn't your code. The fragment you posted:

          if (getBarState()==BARSTATE_CURRENTBAR)
          {return;}
          Will never allow your main() to execute ever. Your EFS is only called on each tick of data...and even a NEWBAR is generated with a tick...

          I'm sure you have some logic, I'm just not following you. Is this for the setComputeOnClose(true)?

          I'll try to recreate the outline of the issue with some generic code to see if I can reproduce and send to you.
          This is what I often end up doing. If your like me, 90% of the time when you are trying to create a minimal case to show someone, you will figure out what your doing wrong ;-).

          But if you can reporduce it with a minimal case post it up and I will look at it.

          G
          Garth

          Comment


          • #6
            thk

            Thanks,

            Actually I think I might have found the issue.
            I change the variable names of the variable I was declaring outside of main, and that seemed to do it, still testing though.

            My point about the

            if (getBarState()==BARSTATE_CURRENTBAR)
            {return;}

            was that I do not want the code to execute on a tick, only a new bar, so by default if bar state = barstate_newbar, then it executes, which in fact is what it does. I was using this to explain that there was no way my code was moving the bars becuase that part of the code was never getting executed on BARSTATE_CURRENTBAR

            I think the varilable names, might have confused the javascript interpreter or something can't tell.. first real weird situation I'v found. but it looks to be working so far..no more moving line.s

            thanks for the help..

            Comment


            • #7
              Great! I'll keep my fingers crossed that it is fixed.
              Garth

              Comment


              • #8
                Hello philli4093,

                Looks like you have found a solution. I put together a little exercise anyway that may better explain why your lines were moving left. By the way, I'm not using the Compute On Close feature in the EFS Settings. Your line isn't moving or shifting to the left, the value of the line on the current bar is just being erased with a null value. Take a look at line 17 in the following code.

                PHP Code:
                function preMain() {
                    
                setPriceStudy(true);
                    
                //setComputeOnClose(true);
                    
                setStudyTitle("test");
                    
                //debugClear();
                }

                var 
                study = new MAStudy(100"Close"MAStudy.SIMPLE);
                var 
                vMA null;
                var 
                TickCntr 0;

                function 
                main() {
                    if (
                getBarState() == BARSTATE_NEWBAR) {
                        
                TickCntr += 1;
                        
                debugPrintln("NEWBAR ============");
                    }
                    if (
                getBarState() == BARSTATE_CURRENTBAR) return;     LINE 17
                    
                //if (getBarState() == BARSTATE_CURRENTBAR) return vMA;  LINE 18
                    
                    
                vMA study.getValue(MAStudy.MA);
                    
                    if (
                getCurrentBarIndex() == 0debugPrintln("Tick ... " TickCntr);
                    return 
                vMA;

                When the if statement on line 17 is true, the return statement returns null to the chart, which erases the value of the MA line. I think this is the event you are experiencing. The solution is to make vMA a global variable and use line 18 instead. That way, when the bar state is CURRENTBAR, you simply return the last value calculated for vMA so that the line doesn't get overwritten with the null value.
                Jason K.
                Project Manager
                eSignal - an Interactive Data company

                EFS KnowledgeBase
                JavaScript for EFS Video Series
                EFS Beginner Tutorial Series
                EFS Glossary
                Custom EFS Development Policy

                New User Orientation

                Comment


                • #9
                  oh my..

                  Wow!

                  That might be it.

                  I had a conditional statement in there that would only execute my code if the currentbarnumber was more than -200, for performance sake. if that failed it didn't return a value.
                  This could very well be the problem.

                  But now I can't understand how my changing of the variable names solved it as well..

                  Good idea to make those values that are returned global.
                  I'll do that.

                  Really Thanks for everyones' help!

                  Comment


                  • #10
                    apon further investigation

                    I actually wasn't plotting a line as a return, but actually drawing a line using drawRelative. These were the lines that were moving to the left when the code wasn't being run on each tick.
                    but for some reason when I changed the variable names which I used to declare for the studies, it started working again.
                    Going to file this one under "Mysterious"
                    btw, I did have several returns without parameters being returned, so thanks for pointing that out.

                    to

                    Comment

                    Working...
                    X