Announcement

Collapse
No announcement yet.

Adding time as a factor

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

  • Adding time as a factor

    Im a simple chartist, volume, price, pivots and MAs. So anything I look at obviously I want to be based on these simple fundimental trading basics.

    I want to take trades out of a MA cross strategy im using from 11am to 2pm, when the range of the last 20 minutes isnt 7 points or more (I trade the nq eminis mostly). Once this is written I can play with both #s easily and maximize. I know if you can eliminate the mid day churn from your trading your %s go up big time.

    I need this to say "NO NEW ENTRIES" but its ok to exit a trade you are already in.

    Appreciate any help I can get as I learn code. The trading part I have down for the most

    tx

  • #2
    Re: Reply to post 'Adding time as a factor'

    uboinc
    One way to do this would be first to create a time flag variable such as for
    example
    var vTimeFlag = 0;
    then set the times and the flag for no trades
    if (getHour()*100 > 1100 && getHour()*100<1400)
    vTimeFlag=1

    then in the conditions for the trade you would add the vTimeFlag which has
    to be 0 for the trade to be taken
    if(Strategy.isInTrade==false && vTimeFlag==0)
    Strategy.do...

    Alex

    Comment


    • #3
      adding time as a factor

      Alex,

      Here is the script with the time factors you posted. It went from 446 trades to none in the same time span so I know Im doing something wrong. I appreciate your help.

      Heres the script (couldnt figure out how o get it in that script window ive seen you use):


      var study = new MAStudy(13, 0, "Close", MAStudy.EXPONENTIAL);
      var vTimeFlag = 0;


      function preMain() {
      setPriceStudy(true);
      }

      function main() {
      var v = study.getValue(MAStudy.MA);

      if (getHour()*100 > 1100 && getHour()*100<1400)
      vTimeFlag=1


      if(v == null)
      return;


      if(close() >= v) {
      if(!Strategy.isLong()) {
      if(Strategy.isInTrade==false && vTimeFlag==0)
      Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.NEXTBAR);
      }

      } else {
      if(!Strategy.isShort()) {

      if(Strategy.isInTrade==false && vTimeFlag==0)
      Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.NEXTBAR);
      }
      }

      if(Strategy.isLong()) {
      setBarBgColor(Color.green);
      } else if(Strategy.isShort()) {
      // setBarBgColor(Color.red);
      }


      return v;
      }

      Comment


      • #4
        uboinc
        The enclosed script should do what you want (see image)
        Alex

        PS. To post the script in a window and with the appropriate formatting use the tags [ P H P ] and [ / P H P ] (without the spaces) at the beginning and end of the code



        PHP Code:
        var vSMA13 = new MAStudy(130"Close"MAStudy.EXPONENTIAL);

        function 
        preMain() {
            
        setPriceStudy(true);
            
        setDefaultBarBgColor(Color.white);
        }

        function 
        main() {

        if(
        getHour() >= 11 && getHour()<14)
        var 
        vTimeFlag=1;

            if (
        close() > vSMA13.getValue(MAStudy.MA)  
            && 
        Strategy.isLong()==false
            
        &&vTimeFlag!=1)
            
        Strategy.doLong("Crossing Up"Strategy.MARKETStrategy.NEXTBAR);

            if (
        close() < vSMA13.getValue(MAStudy.MA)   
            &&
        Strategy.isShort()==false
            
        &&vTimeFlag!=1)
            
        Strategy.doShort("Crossing Down"Strategy.MARKETStrategy.NEXTBAR);

            if (
        Strategy.isLong()==true
            
        &&close()<vSMA13.getValue(MAStudy.MA)
            &&
        vTimeFlag==1)
            
        Strategy.doSell("Close Long",Strategy.CLOSE,Strategy.THISBAR);

            if(
        Strategy.isShort()==true
            
        &&close()>vSMA13.getValue(MAStudy.MA)
            &&
        vTimeFlag==1)
            
        Strategy.doCover("Cover Short",Strategy.CLOSE,Strategy.THISBAR);

            if(
        Strategy.isLong()==true
                
        setBarBgColor(Color.green);
            if(
        Strategy.isShort()==true
                 
        setBarBgColor(Color.red);

        return 
        vSMA13.getValue(MAStudy.MA);

        Comment


        • #5
          Alex,

          Thanks for the help, I really do appreciate it a ton, being code illiterate( but Im working on that). Next ill start optimizing the times.

          One thing, in my initial request, was to eliminate trades from 11am-2pm where the range for the last 20 minutes was less than 7 points only, and in that time period with low range only exits would be allowed. Your script only factors in the time variable. If it isnt that much work could you include the range variable also?

          Thanks a ton,
          UBOINC

          Comment


          • #6
            Hi,

            Try this:

            PHP Code:
            var vSMA13 = new MAStudy(130"Close"MAStudy.EXPONENTIAL);

            var 
            nLastMin null;
            var 
            nLastHour null;
            var 
            nNoGo false;
            var 
            bOKFlag false;

            function 
            preMain() {
                
            setPriceStudy(true);
                
            setDefaultBarBgColor(Color.white);
            }

            function 
            main() {

                var 
            i;

                if(
            getHour() >= 11 && getHour()<14){
                    var 
            vTimeFlag=1;
                } else {
                    
            bOKFlag false;
                }


                var 
            nIndex getCurrentBarIndex();
                var 
            nState getBarState();
                if (
            nState == BARSTATE_NEWBAR || nState == BARSTATE_ALLBARS){
                    var 
            nInterval getInterval();
                    if ( 
            isMonthly(nInterval) || isWeekly(nInterval) || isDaily(nInterval) || isTick(nInterval) ){ // Minutes only
                        
            drawTextRelative(-600"Intervals in Minutes Only"Color.maroonColor.lightgrey,  Text.FRAME Text.ONTOP Text.RELATIVETORIGHT Text.RELATIVETOTOPnull20gID());    
                        
            nNoGo true;
                        return;
                    }
                }
                if (
            vTimeFlag == 1){
                    var 
            nRange Math.round20/(parseInt(nInterval)) );
                    
            debugPrintln("nIndex = " nIndex " nRange = " nRange);
                    if (
            nRange 1){
                        var 
            nHi high(0,-nRange);
                        var 
            nLo low(0,-nRange);
                        var 
            nHH nHi[0];
                        var 
            nLL nLo[0];
                        for (
            i=1;i<nRange;i++){
                            if (
            nHi[i] > nHH)
                                
            nHH nHi[i];
                            if (
            nLo[i] < nLL)
                                
            nLL nLo[i];
                        } 
                    } else {
                        
            nHH high();
                        
            nLL low();
                    }
                        
                    if ((
            nHH-nLL) > 7){
                        
            bOKFlagtrue;
                        
            debugPrintln("nIndex = " nIndex " HH-LL > 7");
                    }
                }

                if (
            nNoGo == true)
                    return;
                    
                if (
            close() > vSMA13.getValue(MAStudy.MA)  
                && 
            Strategy.isLong()==false
                
            &&(vTimeFlag!=|| bOKFlag == true) )
                
            Strategy.doLong("Crossing Up"Strategy.MARKETStrategy.NEXTBAR);

                if (
            close() < vSMA13.getValue(MAStudy.MA)   
                &&
            Strategy.isShort()==false
                
            &&(vTimeFlag!=|| bOKFlag == true) )
                
            Strategy.doShort("Crossing Down"Strategy.MARKETStrategy.NEXTBAR);

                if (
            Strategy.isLong()==true
                
            &&close()<vSMA13.getValue(MAStudy.MA)
                &&
            vTimeFlag==1)
                
            Strategy.doSell("Close Long",Strategy.CLOSE,Strategy.THISBAR);

                if(
            Strategy.isShort()==true
                
            &&close()>vSMA13.getValue(MAStudy.MA)
                &&
            vTimeFlag==1)
                
            Strategy.doCover("Cover Short",Strategy.CLOSE,Strategy.THISBAR);

                if(
            Strategy.isLong()==true
                    
            setBarBgColor(Color.green);
                if(
            Strategy.isShort()==true
                     
            setBarBgColor(Color.red);

            return 
            vSMA13.getValue(MAStudy.MA);

            Garth

            Comment


            • #7
              Tx GSpiker, took me a while just to chop thru it and see where you were headed, a little over my head programming
              wise....ok a lot appreciate the work.

              im still not getting anywhere near the results I want. Since my style of trading relies on volume, angle of dmi+/- in relation to
              eachother, and most importantly trendlines and pivots, I feel it would be impossible to duplicate. Especially the trendline
              part. i will sometimes draw 2 or 3 tls from the same starting point before deciding which
              has the most relavance in a specific situation, using wicks, bodies etc. imo its not up to ur beliefs, but whomever is taking
              the most aggressive roll in the market you are trading at that specific moment. If GSCO
              is reallocating a billion into the es, and he uses bodies, you better see what he sees. so I decided to try and get this
              ma system which ive used mainly as a tool to keep in the market and ride
              trends, with great success over the years as a fund manager, automated and backtested.


              I know it has merit, and taking the trades out of the middle of the day helped raise the %s and $/winner and
              lower the $/loser, but no where near enough. I may have to rethink the entry strategy, or the entire thing.

              13ema, 10 min results, trading from 8.15am-4.15pm
              always in the market 455 trades %wins Ave wins/loses$ $1598/($986) profit factor .30
              out the market 11am-2pm 336 trades 17.56%wins wins/loses$ $1835/($1084) profit factor .36
              out 11-2+gspikers range 370 trades 16.21%wins wins/loses$ $1813/($1020) profit factor .34


              Any thoughts or insite would be appreciated.

              UBOINC
              Last edited by uboinc; 06-12-2003, 10:20 AM.

              Comment


              • #8
                Gspiker,
                I just noticed positions are held overnight. can you adjust the code so all positions are closed out at 4.15pm est tx a ton

                Comment


                • #9
                  What you want to do is possible to do in efs, but you are right some of it isn't trivial.

                  For the trend lines, you might try using Chris Kryza's Auto Trendline and Support Resistance tool. I think it is like 50.00 and for me it was well worth spending the $ rather than sitting down and coding it. It has a lot of options on how it will determine T/Ls and S/R, and it does seem to use any of H/L/O/C to calculate. Of course it may be impossible for it to duplicate exactly what you want, but I think it is a great tool.

                  There are people out there (sorry forget who) who did some work on dmi and the angle of the lines (If I remember correctly). It may pay to do a search of this BB and see if you can find a formula that provides a good starting point.

                  As for the MA formula - I can add exit's on close. I will look at it tonight...

                  G
                  Garth

                  Comment


                  • #10
                    thanks for the on close exits.

                    as far as some efs for tls, that is something that cant be scripted imo, because i dont always draw them the same way. no code can predict which one i will use, if one, and some of my techniques r very unorthodox. example is the current 10 daily chart on the Es eminis. starting at the 3/12 wick lows, i have 2 tls that have been in play. one hits the 5/1 wick lows to 5/28 wick highs. the other is the major tl, from the same 3/12 wick lows, to the 5/21-5/22 wick lows. so its not as much which way i draw them, but more which tl is in control of the trend and the chart, and that can change a few times. so im not as interested in coding my tl theories, which could take me days 2 write about in english. as for the dmi and its angles, i have done a few searches and so far have yet to come up with anything but ill keep looking tx. and i appreciate ur help on the closing trades

                    ubo
                    ps i read ur article last night on stoch from 8/2000. interesting, even though i dont use indicators (dmi is just a ma thingy used for stuff im not trading like indecies). when u said u only trade in the direction on the 5/13 ma cross, were u talking about id or overnigh trades?

                    Comment


                    • #11
                      Try this, I think it will do what you want with exit on close. Note you will have to change the variable nCloseHour to reflect your time zone.

                      G

                      PHP Code:
                      var vSMA13 = new MAStudy(130"Close"MAStudy.EXPONENTIAL);

                      var 
                      nLastMin null;
                      var 
                      nLastHour null;
                      var 
                      nNoGo false;
                      var 
                      bOKFlag false;
                      var 
                      nCloseHour 13// Change to reflect your TZ
                      var nCloseMinute 15;

                      function 
                      preMain() {
                          
                      setPriceStudy(true);
                          
                      setDefaultBarBgColor(Color.white);
                      }

                      function 
                      main() {

                          var 
                      i;

                          if(
                      getHour() >= 11 && getHour()<14){
                              var 
                      vTimeFlag=1;
                          } else {
                              
                      bOKFlag false;
                          }


                          var 
                      nIndex getCurrentBarIndex();
                          var 
                      nState getBarState();
                          if (
                      nState == BARSTATE_NEWBAR || nState == BARSTATE_ALLBARS){
                              var 
                      nInterval getInterval();
                              if ( 
                      isMonthly(nInterval) || isWeekly(nInterval) || isDaily(nInterval) || isTick(nInterval) ){ // Minutes only
                                  
                      drawTextRelative(-600"Intervals in Minutes Only"Color.maroonColor.lightgrey,  Text.FRAME Text.ONTOP Text.RELATIVETORIGHT Text.RELATIVETOTOPnull20"L-10");    
                                  
                      nNoGo true;
                                  return;
                              }
                          }
                          if (
                      vTimeFlag == 1){
                              var 
                      nRange Math.round20/(parseInt(nInterval)) );
                              
                      // debugPrintln("nIndex = " + nIndex + " nRange = " + nRange);
                              
                      if (nRange 1){
                                  var 
                      nHi high(0,-nRange);
                                  var 
                      nLo low(0,-nRange);
                                  var 
                      nHH nHi[0];
                                  var 
                      nLL nLo[0];
                                  for (
                      i=1;i<nRange;i++){
                                      if (
                      nHi[i] > nHH)
                                          
                      nHH nHi[i];
                                      if (
                      nLo[i] < nLL)
                                          
                      nLL nLo[i];
                                  } 
                              } else {
                                  
                      nHH high();
                                  
                      nLL low();
                              }
                                  
                              if ((
                      nHH-nLL) > 7){
                                  
                      bOKFlagtrue;
                                  
                      // debugPrintln("nIndex = " + nIndex + " HH-LL > 7");
                              
                      }
                          }

                          if (
                      nNoGo == true)
                              return;
                              
                          if (
                      close() > vSMA13.getValue(MAStudy.MA)  
                          && 
                      Strategy.isLong()==false
                          
                      &&(vTimeFlag!=|| bOKFlag == true) )
                              
                      Strategy.doLong("Crossing Up"Strategy.MARKETStrategy.NEXTBAR);

                          if (
                      close() < vSMA13.getValue(MAStudy.MA)   
                          &&
                      Strategy.isShort()==false
                          
                      &&(vTimeFlag!=|| bOKFlag == true) )
                              
                      Strategy.doShort("Crossing Down"Strategy.MARKETStrategy.NEXTBAR);

                          if ( (
                      Strategy.isLong()==true) &&
                          ( (
                      close()<vSMA13.getValue(MAStudy.MA) &&
                          
                      vTimeFlag==1) || (getHour() > nCloseHour && getMinute() > nCloseMinute) ) )
                              
                      Strategy.doSell("Close Long",Strategy.CLOSE,Strategy.THISBAR);

                          if ( (
                      Strategy.isShort()==true) &&
                          ( (
                      close()>vSMA13.getValue(MAStudy.MA) &&
                          
                      vTimeFlag==1) || (getHour() > nCloseHour && getMinute() > nCloseMinute) ) )
                              
                      Strategy.doCover("Cover Short",Strategy.CLOSE,Strategy.THISBAR);

                          if(
                      Strategy.isLong()==true
                              
                      setBarBgColor(Color.green);
                          if(
                      Strategy.isShort()==true
                               
                      setBarBgColor(Color.red);

                      return 
                      vSMA13.getValue(MAStudy.MA);

                      Garth

                      Comment


                      • #12
                        G,

                        Still have trades held over. changed the closing time to 16.15, only in 1 place right?

                        changed the sma to ema but still have hold overs - any idea?

                        tia

                        Comment


                        • #13
                          Yes, in only one place.
                          Not sure why...I'll take a look a bit later.

                          G
                          Garth

                          Comment


                          • #14
                            G,

                            You said, "This means if our 5-DMA is
                            above our 13-DMA we will only accept long signals, and if the 5-DMA
                            is below the 13-DMA we will accept only short plays"

                            was this for swing trades or id trades also?

                            ubo

                            Comment


                            • #15
                              Hi,

                              There was indeed a problem with my logic on the close at EOD, but I also added preventing new longs and shorts after EOD.

                              Try this:

                              PHP Code:
                              var vSMA13 = new MAStudy(130"Close"MAStudy.EXPONENTIAL);

                              var 
                              nLastMin null;
                              var 
                              nLastHour null;
                              var 
                              nNoGo false;
                              var 
                              bOKFlag false;
                              var 
                              nCloseHour 13// Change to reflect your TZ
                              var nCloseMinute 15;

                              function 
                              preMain() {
                                  
                              setPriceStudy(true);
                                  
                              setDefaultBarBgColor(Color.white);
                              }

                              function 
                              main() {

                                  var 
                              i;

                                  if(
                              getHour() >= 11 && getHour()<14){
                                      var 
                              vTimeFlag=1;
                                  } else {
                                      
                              bOKFlag false;
                                  }


                                  var 
                              nIndex getCurrentBarIndex();
                                  var 
                              nState getBarState();
                                  if (
                              nState == BARSTATE_NEWBAR || nState == BARSTATE_ALLBARS){
                                      var 
                              nInterval getInterval();
                                      if ( 
                              isMonthly(nInterval) || isWeekly(nInterval) || isDaily(nInterval) || isTick(nInterval) ){ // Minutes only
                                          
                              drawTextRelative(-600"Intervals in Minutes Only"Color.maroonColor.lightgrey,  Text.FRAME Text.ONTOP Text.RELATIVETORIGHT Text.RELATIVETOTOPnull20"L-10");    
                                          
                              nNoGo true;
                                          return;
                                      }
                                  }
                                  if (
                              vTimeFlag == 1){
                                      var 
                              nRange Math.round20/(parseInt(nInterval)) );
                                      
                              // debugPrintln("nIndex = " + nIndex + " nRange = " + nRange);
                                      
                              if (nRange 1){
                                          var 
                              nHi high(0,-nRange);
                                          var 
                              nLo low(0,-nRange);
                                          var 
                              nHH nHi[0];
                                          var 
                              nLL nLo[0];
                                          for (
                              i=1;i<nRange;i++){
                                              if (
                              nHi[i] > nHH)
                                                  
                              nHH nHi[i];
                                              if (
                              nLo[i] < nLL)
                                                  
                              nLL nLo[i];
                                          } 
                                      } else {
                                          
                              nHH high();
                                          
                              nLL low();
                                      }
                                          
                                      if ((
                              nHH-nLL) > 7){
                                          
                              bOKFlagtrue;
                                          
                              // debugPrintln("nIndex = " + nIndex + " HH-LL > 7");
                                      
                              }
                                  }

                                  if (
                              nNoGo == true)
                                      return;
                                      
                                  if (
                              close() > vSMA13.getValue(MAStudy.MA)  
                                    && 
                              Strategy.isLong()==false
                                    
                              && (vTimeFlag!=|| bOKFlag == true)
                                    && ( !(
                              getHour() == nCloseHour && getMinute() >= nCloseMinute) || (getHour() > nCloseHour) ) )
                                      
                              Strategy.doLong("Crossing Up"Strategy.MARKETStrategy.NEXTBAR);

                                  if (
                              close() < vSMA13.getValue(MAStudy.MA)   
                                    && 
                              Strategy.isShort()==false
                                    
                              && (vTimeFlag!=|| bOKFlag == true
                                    && ( !(
                              getHour() == nCloseHour && getMinute() >= nCloseMinute) || (getHour() > nCloseHour) ) )
                                      
                              Strategy.doShort("Crossing Down"Strategy.MARKETStrategy.NEXTBAR);

                                  if ( (
                              Strategy.isLong()==true) &&
                                    ( (
                              close()<vSMA13.getValue(MAStudy.MA) && vTimeFlag==1) ||
                                    ( (
                              getHour() == nCloseHour && getMinute() >= nCloseMinute) || (getHour() > nCloseHour) ) ) )
                                      
                              Strategy.doSell("Close Long",Strategy.CLOSE,Strategy.THISBAR);

                                  if ( (
                              Strategy.isShort()==true) &&
                                    ( (
                              close()>vSMA13.getValue(MAStudy.MA) && vTimeFlag==1) || 
                                    ( (
                              getHour() == nCloseHour && getMinute() >= nCloseMinute) || (getHour() > nCloseHour) ) ) )
                                      
                              Strategy.doCover("Cover Short",Strategy.CLOSE,Strategy.THISBAR);

                                  if(
                              Strategy.isLong()==true
                                      
                              setBarBgColor(Color.green);
                                  if(
                              Strategy.isShort()==true
                                       
                              setBarBgColor(Color.red);

                              return 
                              vSMA13.getValue(MAStudy.MA);

                              Garth

                              Comment

                              Working...
                              X