Announcement

Collapse
No announcement yet.

Cleaner code?

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

  • Cleaner code?

    I am looking for the cleanest way to impliment this code. So, I have come to you more experienced coders for more clearity. Below is a sample code which I am trying to make more effecient.
    PHP Code:
    function preMain() {
    setPriceStudy(true);

    }

    function 
    main() {

    var 
    wT1 My Study Parameters 1;
    var 
    XT1 My Study Parameters 2;
    var 
    YT1 My Study Parameters 3;
    var 
    zT1 My Study Parameters 4;

    if(
    wT1 Yt1 && Xt1 Zt1){
        
    Then do the following...
    }

    if(
    wT1 Yt1 && Xt1 Zt1){
        
    Then do the following...
    }


    Is there an easier and more efficient way to declare this part of the code into "var", "Flag" or "Condition"?

    PHP Code:
        (wT1 Yt1 && Xt1 Zt1)

        (
    wT1 Yt1 && Xt1 Zt1
    For Example:
    PHP Code:
     vFlag = (wT1 Yt1 && Xt1 Zt1); 
    or
    PHP Code:
     var cOMB1 = (wT1 Yt1 && Xt1 Zt1); 
    I am aware that both of these example are more than likely worng.

    This way I can perform the code like the following:

    PHP Code:
    if vFlag == true){
        
    Then do the following...

    or

    PHP Code:
    if cOMB1 == true){
        
    Then do the following...

    Any help would be greatly appreciated.

    Thanks,
    Excellent book on JavaScript for beginners

  • #2
    Hi Dan,

    Yes, you are actually very close with what you published. I have similar conditionals used in the attached efs to this post. In that example I had set array variables equal to the conditionals then maintained their values in history, etc, but the basic premise is the same.

    I do not believe this really makes the code more efficient, but it makes it cleaner to set up multiple conditionals, at least in my opinion.

    The conditional is evaluated and becomes a boolean value, true or false. When you get into complex conditionals, my recommendation is to ensure you have the different tests contained within parenthesis. This will ensure there is no confusion when the compiler does it thing.

    This example may evaluate ok, but you are depending on how the compiler and the order in which the compiler enterprets the variables.
    PHP Code:
    vFlag = (wT1 Yt1 && Xt1 Zt1); 
    To make sure the compiler evaluates them like you want, ensure the conditionals are fully evaluated (and become booleans) prior to performing boolean logic on them. For example, 'Yt1 && Xt1' evaluate as 'true' if they are non-zero, as a result, you have to be careful how you set them up.
    For example, I would re-write your example like this:
    PHP Code:
    vFlag = ((wT1 Yt1) && (Xt1 Zt1)); 
    This is only a minor change, but ensures the '>' tests are evaluated and subsequently treated as booleans when evaluating the boolean '&&' test.

    I hope this helps.

    Comment


    • #3
      When I run this EFS in real time I get a certain set of signals. If at the end of the day I reload the EFs the signals change. I thought that when using getValue to retrieve a value from a higher time frame that the higher time frame will only return 1 value.
      Did I miss something or should I be using getBarState to fix this problem? Or could another problem be that I am using the higher interval of "11" to get my value and I may be running on a 3 or 5 minute chart, thus leading to some of the values coming in the middle of a processing bar?

      PHP Code:
      //lFlag = 0;
      //sFlag = 0;

      function preMain() {
      setPriceStudy(true);
      setComputeOnClose(true);
      setDefaultBarFgColor(Color.green,0);
      setDefaultBarFgColor(Color.magenta,1);
      setDefaultBarFgColor(Color.green,2);
      setDefaultBarFgColor(Color.magenta,3);
      }

      function 
      main() {

      var 
      lMA ema(2low (inv(11)));
      var 
      hMA ema(2high (inv(11)));

      var 
      GV1 lMA.getValue (-1);
      var 
      GV2 hMA.getValue (-1);


      if((
      getHour()*100)+getMinute() == 1557){
      if(
      Strategy.isLong()==true){
      Strategy.doSell("Closeout Long(s)",Strategy.MARKET,Strategy.THISBAR,Strategy.DEFAULT,0);
      }
      if((
      getHour()*100)+getMinute() == 1557){
      if(
      Strategy.isShort()==true){
      Strategy.doCover("Closeout Short(s)",Strategy.MARKET,Strategy.THISBAR,Strategy.DEFAULT,0); 
      }}}




      if((
      lMA GV1) && (hMA GV2) && !Strategy.isLong()){
          
      Strategy.doLong("Long"Strategy.MARKETStrategy.THISBAR);
          
      buyMarketgetSymbol(), ); 
          
      //setBarBgColor(Color.RGB(192,220,192));
          //lflag = 1;
      }

      if((
      hMA GV2) && Strategy.isLong()){
          
      Strategy.doSell("Sell"Strategy.MARKETStrategy.THISBAR);
          
      sellMarketgetSymbol(), );
          
      //lflag = 0;
      }

      if((
      lMA GV1) && (hMA GV2) && !Strategy.isShort()){ 
          
      Strategy.doShort("Short"Strategy.MARKETStrategy.THISBAR);
          
      sellShortMarketgetSymbol(), );
          
      //setBarBgColor(Color.RGB(255,160,160));
          //sFlag = 1;
      }

      if((
      lMA GV1) && Strategy.isShort()){
          
      Strategy.doCover("Cover"Strategy.MARKETStrategy.THISBAR);
          
      closePositiongetSymbol(), );
          
      //sflag = 0;
      }

      if(
      Strategy.isLong()) setBarBgColor(Color.RGB(192,220,192));
      if(
      Strategy.isShort()) setBarBgColor(Color.RGB(255,160,160));

      return new Array( 
      lMAhMA );


      Last edited by FibbGann; 10-27-2006, 07:58 PM.
      Excellent book on JavaScript for beginners

      Comment


      • #4
        Fibgann,

        Not sure if this is the same problem you are having:

        Basically when you do an end of day reload and or a backtest, the signals after the fact, or strategy analyzer reports, ie., equity curve etc, look ....rather promising?

        Then when you run it realtime or in tick replay it's a different story?

        There are some "forward looking" aspects to using multiple time intervals, particularily when loading up higher time frames in a lower time frame chart. The backtester or when reloading at EOD because the bars are already completed records the values for longer time periods based on the closing value for the indicator.

        If you search on multiple time periods Alexis and Jason have some extensive threads with many examples of what occurs and some resynchorinization techniques using getSeries() and some other coding tricks.

        I had strategies that were catching every turn in the market when "reloaded" or in a backtest and fell apart realtime. I'm not a programmer and couldn't really get the multiperiod to work correctly and kind of put it aside for now.

        BTW I like your strategy it's pretty cool, if you every figure out how to get the backtested results to match realtime please let me know..

        One other thing. You are probably aware but you realize that the strategy objects and methods, such as StrategyIsLong etc, only work when running a backtest. In realtime you will have to uncomment those flags when running realtime and keep track yourself which I'm sure you were aware.

        There is a few good examples in the knowledge base, the Stocks & Commodities Magazine section where they use the same EFS for backtesting and realtime.

        By looking for:
        PHP Code:

         
        if(getCurrentBarIndex() == && getBarState() == BARSTATE_NEWBAR) { bRealtime true}; 
        (But I noticed you are running setComputeOnClose() which never returns getCurrentBarIndex() == 0. In that case you look for a barindex of -1.

        It's been kind of suggested to me in general not to use setComputeOnClose() unless you have a good reason and rather check for getBarState()==BARSTATE_NEWBAR).


        Anyway, then check the bRealtime flag and if true do the generic broker buymarket, and if off do the Strategy.doLong statements.

        I think it's useful to have one EFS for backtesting and realtime. In case you make a "minor" change you can backtest it again prior to running it realtime.

        Are you doing any automated trading yet?

        Hope this helps, Good luck with those futures...

        glen
        Last edited by demarcog; 10-27-2006, 10:02 PM.
        Glen Demarco
        [email protected]

        Comment


        • #5
          Thanks Demarcog!

          But, I would still like to know the answer from Steve, Alexis, J to set what I may be doing wrong In coralation with this code?
          Once I get a final answer with this question I would be glad to share my progress.

          P.S. I have been doing automated Trading for the last 2 years.
          Last edited by FibbGann; 10-27-2006, 10:51 PM.
          Excellent book on JavaScript for beginners

          Comment


          • #6
            After reading more and more. I hope the current code change should resolve the issue I had, but I will have to run the old and new code in real-time to see if it does the job.

            PHP Code:
            //lFlag = 0;
            //sFlag = 0;

            function preMain() {
            setPriceStudy(true);
            //setComputeOnClose(true);
            setDefaultBarFgColor(Color.green,0);
            setDefaultBarFgColor(Color.magenta,1);
            setDefaultBarFgColor(Color.green,2);
            setDefaultBarFgColor(Color.magenta,3);
            }

            function 
            main() {

            var 
            lMA ema(2low (inv(11)));
            var 
            hMA ema(2high (inv(11)));

            var 
            GV1 lMA.getValue (-1);
            var 
            GV2 hMA.getValue (-1);


            if((
            getHour()*100)+getMinute() == 1557){
            if(
            Strategy.isLong()==true){
            Strategy.doSell("Closeout Long(s)",Strategy.MARKET,Strategy.THISBAR,Strategy.DEFAULT,0);
            }
            if((
            getHour()*100)+getMinute() == 1557){
            if(
            Strategy.isShort()==true){
            Strategy.doCover("Closeout Short(s)",Strategy.MARKET,Strategy.THISBAR,Strategy.DEFAULT,0); 
            }}}


            if (
            getBarStateInterval("11") == BARSTATE_NEWBAR) {

            if((
            lMA GV1) && (hMA GV2) && !Strategy.isLong()){
                
            Strategy.doLong("Long"Strategy.MARKETStrategy.THISBAR);
                
            buyMarketgetSymbol(), ); 
                
            //setBarBgColor(Color.RGB(192,220,192));
                //lflag = 1;
            }

            if((
            hMA GV2) && Strategy.isLong()){
                
            Strategy.doSell("Sell"Strategy.MARKETStrategy.THISBAR);
                
            sellMarketgetSymbol(), );
                
            //lflag = 0;
            }

            if((
            lMA GV1) && (hMA GV2) && !Strategy.isShort()){ 
                
            Strategy.doShort("Short"Strategy.MARKETStrategy.THISBAR);
                
            sellShortMarketgetSymbol(), );
                
            //setBarBgColor(Color.RGB(255,160,160));
                //sFlag = 1;
            }

            if((
            lMA GV1) && Strategy.isShort()){
                
            Strategy.doCover("Cover"Strategy.MARKETStrategy.THISBAR);
                
            closePositiongetSymbol(), );
                
            //sflag = 0;
            }
            }

            if(
            Strategy.isLong()) setBarBgColor(Color.RGB(192,220,192));
            if(
            Strategy.isShort()) setBarBgColor(Color.RGB(255,160,160));

            return new Array( 
            lMAhMA );


            Excellent book on JavaScript for beginners

            Comment


            • #7
              Fibbgann,

              So basically you removed setComputeOnClose() and wait until the 11 period interval ends. At the time I was working on multi time intervals I saw similiar suggestions made by Jason I think. I was using 5, 15, and 60-120 and felt like waiting for a 60-120 minute interval to expire was defeating then whole purpose of trading off a 5minute chart.

              Based on the changes you made, what I wondered about at the time was this: isn't this basically the same as running the strategy on a 11 minute chart?



              To back up a second what I was trying to do is basically use the "conventional trading wisdom":

              regardless of your favorite indicator, check higher time frames first, then when all the ducks line up, trade off the smallest time frame.

              Take a simple stochastic crossover for example. If the daily has given a but, the 6o minute has given a buy, the 15 minute, and now our 5 minute chart we are using to trade...take all buys while confirmed by the higher time.



              I've sense questioned that methodology somewhat: first because my backtesting of multiple time intervals didn't demonstrate a higher level of profitabiltiy (assuming I did it correctly and can trust the results).

              But also, if I'm waiting for a 60 minute stochastics signal to confirm my 5 minute stochastic, I end up missing many profitable trades. If a price moved up 10 handles in the S&P as confirmed and tracked by out example stochastic crossover do I really care if this move wasn't confirmed by the higher stochastic intervals?

              I guess the question is, is the percentage profitabiltiy of a given short term signal, like a stochastic crossover, or any signal for that matter increased when that same signal is confirmed by a larget time period?

              Anyway, good luck. When I responded to you initial post it wasn't my intention or desire to prevent more capable minds (Alex, Jason,Steve,etc) from responding. I just was trying to point you to a few posts that I thought would be helpful, as it was Friday afternoon and I thought those better minds wouldn't be getting back to you until Monday.

              Let me know how you multitime testing pans out.
              Last edited by demarcog; 10-29-2006, 07:19 PM.
              Glen Demarco
              [email protected]

              Comment


              • #8
                demarcog,

                Thanks for all the replies... I understood your intentions of your post.

                Also, the code posted is just a fraction of the code. I was attempting to get the values set for that criteria without making the question complicating with other code.

                Thanks,
                Excellent book on JavaScript for beginners

                Comment


                • #9
                  Hello FibbGann,

                  The first problem that I see, which demarcog also pointed out, is that you are trying to use the Strategy object for real time analysis. The Strategy object is for back testing only. It's methods and functionality will not be able to process real time data as you are expecting. You need to replace all instances of the Strategy object functions with custom variables to track the position state. Make these changes and test it again.

                  You're on the right track with the usage of getBarStateInterval() for the higher time frame logic. In this post, which you may have already read, I gave a more detailed explanation of how to back test multiple time frames.
                  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


                  • #10
                    Jay, Thank you... demarcog, Sorry I totally missed your "Strategy" point.

                    When my signals come in and the bars bg is supposed to be painted it appears to only paint the beggining of a new "11" interval bar. I thought this is only set to happen if my

                    PHP Code:
                    if(lFlag == 1setBarBgColor(Color.RGB(192,220,192));
                    if(
                    sFlag == 1setBarBgColor(Color.RGB(255,160,160)); 
                    statement was bracketed in the

                    PHP Code:
                    if (getBarStateInterval("11") == BARSTATE_NEWBAR) {


                    This is what appears though.

                    ???
                    Attached Files
                    Last edited by FibbGann; 10-30-2006, 04:14 PM.
                    Excellent book on JavaScript for beginners

                    Comment


                    • #11
                      Here is the full file.
                      Attached Files
                      Excellent book on JavaScript for beginners

                      Comment


                      • #12
                        FibbGann
                        What you are seeing is happening because sFlag is equal to 1 only on the first tick of a new higher interval bar. Add a debug statement to check the state of sFlag and you should see that.
                        To resolve this change setBarBgColor(...) to setDefaultBarBgColor(...)
                        Alex

                        Comment

                        Working...
                        X