Announcement

Collapse
No announcement yet.

Dead Zone Trading

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

  • Dead Zone Trading

    I'd like to modify my exising study so that if the time is between 11:30 and 14:00 (deadzone) that it will pass on taking any trades. Any ideas? Thx!!

  • #2
    Geoff
    Here is one way to do it. First create a variable called (for example) AllowTrading and set it initially to true.
    Then create a variable called Time which will return the bar time in an easy to use format
    PHP Code:
    var Time = (hour(0)*100)+minute(0);
    //Time returns a value expressed as 930, 1045, 1420, etc 
    Once you have this you can check for the value of Time to be within your conditions and if it is you set the variable AllowTrading to false
    PHP Code:
    if(Time >=1130 && Time <1400){
            
    AllowTrading false;

    At this point all you need to do is enclose your code inside a conditional statement that checks if AllowTrading is equal to true.
    Shown below is the complete example. For the purpose of illustrating how this works I have included a command to paint the background in yellow when the conditions do allow trading.
    Alex

    PHP Code:
    function main(){
     
        var 
    AllowTrading true;
        var 
    Time = (hour(0)*100)+minute(0);
     
        if(
    Time >=1130 && Time <1400){
            
    AllowTrading false;
        }
        
        if(
    AllowTrading){
            
    //your trading code here
            
    setBarBgColor(Color.yellow);//used for example
        
    }
        
        return;

    Comment


    • #3
      Skipping DZ Trades

      A,

      This is what I ended up with (workin!). Its bascially a hack up of a few stuidies. If there is anything nasty that jumps out (besides the overal poor structure layout) then please yell. I'm running it on bar replay now and it seems to be working!!

      It was not working at first but I had to use { } around my other if statements inside of if(AllowTrading). Here is the code if anyone wants to snag it:

      PHP Code:
      /*****************************************
      Description    : 10 EMA/10 MA
      Crossover Strategy 
      Modifications: Will exclude trades in deadzone (1130-2PM)
      *****************************************/




      var ma = new MAStudy(100"Close"MAStudy.SIMPLE);



      function 
      preMain() {

          
      setPriceStudy(true);

          
      setStudyTitle("DZ No Trading"); 

          
      setCursorLabelName("MA 10",0);

          
      setDefaultBarFgColor(Color.red,0);

          
      setCursorLabelName("EMA 10 (MA 10)",1);

          
      setDefaultBarFgColor(Color.blue,1);

          
      setColorPriceBars(true);

          
      setDefaultPriceBarColor(Color.grey);



      }

      var 
      EMA_1 0;

      function 
      main() {

          var 
      / (10 1);

              var 
      EMA ma.getValue(MAStudy.MA) + (K) * EMA_1;

              var 
      AllowTrading true;  

              var 
      Time = (hour(0)*100)+minute(0);

        if(
      Time >=1130 && Time <1400){   

              
      AllowTrading false;  }    


          if(
      AllowTrading){ 

          if (
      getBarState() == BARSTATE_NEWBAR)

              
      EMA_1 EMA;

          if(
      ma.getValue(MAStudy.MA) > EMA && !Strategy.isLong()) {

                  
      Strategy.doLong("Long"Strategy.MARKETStrategy.THISBAR);
                              
      Alert.addToList(getSymbol(), "Skyscraper @ "+close(), Color.RGB(0,0,0), Color.blue); }  

          if(
      ma.getValue(MAStudy.MA) < EMA && !Strategy.isShort()) {

                  
      Strategy.doShort("Short"Strategy.MARKETStrategy.THISBAR);
                              
      Alert.addToList(getSymbol(), "Midget @ "+close(), Color.RGB(0,0,0), Color.red);   }

              
      setBarBgColor(Color.green); } // YELLOW SCARES ME

          
      if(Strategy.isLong())

                     
      setPriceBarColor(Color.blue);

          if(
      Strategy.isShort())

                  
      setPriceBarColor(Color.red);

                  

          return new Array(
      ma.getValue(MAStudy.MA),EMA);


      Whoo hoo

      Comment


      • #4
        Geoff,

        Just wanted to thank you for posting that code. Found it very interesting and actually never ran across that particular moving average idea before. Hope you don't mind my butting in.

        I have a program I've been working on which I am attaching in the hope that you may find it useful and maybe save you some time and hope I don't mess you up with the post. BTW I don't take credit for any of it, it was "stolen" from other examples in this forum or documentation

        The portions that are relevent here are related to time processing. I actual check for start time and end time, and when the study is loaded or back tested you get prompted and can pass the time periods as parameters which prevents you from having to change the EFS code each time.

        I have and end of day time also specified for closing out trades, which can be different from the end time. You may want to initiate a trade before 11 for example and keep it until EOD.

        I am going to try, and I'm not a programmer and merge your time check with mine, basically looking to trade during the prime time market hours excluding the lunchtime period.

        Hope you find it useful and feel free to improve upon my code enough to make it profitable and give me a shout.

        There is also alot of money mgmt code included, profit targets, trailing stops which may or may not be on your agenda, also stolen from here and there which you may find useful.

        Thanks again....there are SO FEW pieces of code published out of the thousands posted here that show any profit potential whatsoever...that I had to thank you and clumsiley try to return the favor.

        glen [email protected]

        ps feel free to share anymore ideas because I could use it.



        Attached Files
        Last edited by demarcog; 08-28-2006, 03:09 PM.
        Glen Demarco
        [email protected]

        Comment


        • #5
          Geoff,

          I hope someone at esignal has made you aware that MARKET.THISBAR really means OPEN.THISBAR and I've been told to use MARKET.NEXTBAR, which is really the open of the next bar.
          That killed the profit on alot of shorter term 1-5 minute systems.

          It's a major issue for me, and cannot imagine why this situation has not been corrected.



          One more question.

          Just curious is the formula you are applying to the SMA to get the EMA different then using the esignal EMA function?


          var vEMA10 = new MAStudy(10, 0, "Close", MAStudy.EXPONENTIAL);
          Last edited by demarcog; 08-28-2006, 05:05 PM.
          Glen Demarco
          [email protected]

          Comment


          • #6
            If you are working with an intraday strategy back testing WITHOUT LOOK INSIDE BAR is worthless! You are just wasting your time, IMHO.

            If you are trading a swing strategy (daily/weeky) then you can use 1 minute bars.

            Most people don't understand how to backtest.



            Originally posted by demarcog
            Geoff,

            I hope someone at esignal has made you aware that MARKET.THISBAR really means OPEN.THISBAR and I've been told to use MARKET.NEXTBAR, which is really the open of the next bar.
            That killed the profit on alot of shorter term 1-5 minute systems.

            It's a major issue for me, and cannot imagine why this situation has not been corrected.



            One more question.

            Just curious is the formula you are applying to the SMA to get the EMA different then using the esignal EMA function?


            var vEMA10 = new MAStudy(10, 0, "Close", MAStudy.EXPONENTIAL);

            Comment


            • #7
              Originally posted by buzzhorton
              If you are working with an intraday strategy back testing WITHOUT LOOK INSIDE BAR is worthless! You are just wasting your time, IMHO.

              If you are trading a swing strategy (daily/weeky) then you can use 1 minute bars.

              Most people don't understand how to backtest.
              "LOOK INSIDE BAR" if I recall was an option specified while testing an ezlanguage strategy in Tradestation. This would in effect break a bar of greater then 1 minute into 1 minute bars during the backtest to enable "trades" to occur at the 1 minute interval time period, instead of waiting for the 5 minute bar, for example to be close.

              If you know a way of doing that in esignal please share.

              I agree that backtesting certainly has it's problems and limitations which is why I'm in this post right now trying to resolve.

              The irony of you using IMHO I'm sure didn't get past you Avery LOL
              Glen Demarco
              [email protected]

              Comment


              • #8
                Thanks for the input buzz and demarcog. I hate to admit it but your right. I just started back testing and have no idea what I'm doing. I see huge profits and am getting all giddy. I changed to the NEXTBAR strategy and now most of the studies show negative PNL. Ahh my mood has declined by 5%.

                I need to add to more things to that 'no deadzone' trade script. Wanted it to close out any trade that was open at 3:58pm and I wanted it to close or place a stop on any trade that was open going in to deadzone. Just been very busy and haven't had time to look in to that part.

                Do you use NEXTBAR or do you compensate by upping the slippage amount in the analyzer settings? I'm thinking about getting some sleep. Ttyl!

                Comment


                • #9
                  Geoff
                  There are a few errors in the code you posted. If you run the script in Tick Replay you should see the following:
                  a) during the cutoff time (ie the time in which trades are not allowed) the EMA is not updating correctly (see following image)



                  This is because the section of code that transfers the value of EMA to EMA_1 at BARSTATE_NEWBAR is enclosed inside the AllowTrading conditional statement hence never executes when AllowTrading is false.
                  b) during the times in which trading is allowed the EMA is also calculating incorrectly albeit in a less obvious way. To verify this run the script for a while then Pause the Tick Replay and Reload the efs and you will see the plot change. This is because the transfer of values between EMA and EMA_1 occurrs at BARSTATE_NEWBAR after the EMA has updated its current value whereas it should happen before. What this means is that on a new bar the value that gets transferred is the value at the first tick of a new bar instead of the last one of the bar that just closed. The EMA also needs to be declared as a global variable instead of a local variable so that it retains its value during each iteration of the efs.

                  Also, Strategy.MARKET should be changed to Strategy.CLOSE (or LIMIT or STOP) if the trade is to be executed on the same bar that triggers it.
                  Strategy.MARKET corresponds to the Open and should be used in conjunction with Strategy.THISBAR only when the conditions are based on the prior bar eg. "IF the Close of the prior bar was above the value of the moving average at the prior bar THEN Buy at Market (ie Open) of This bar".
                  If instead the conditions are based on the current bar then Strategy.MARKET should be used with Strategy.NEXTBAR eg "IF the Close of this bar is above the value of the moving average on this bar THEN Buy at Market (ie Open) of the Next bar".
                  Using Strategy.MARKET on the same bar that triggers the trade will return unrealistic results.
                  Lastly the Strategy Object should be used only for back testing and not for real time. For the latter you should create your own variables with which you track the status of the strategy.

                  Enclosed below is a revision of your code (complete with comments on the changes made) that corrects the errors described above.
                  Alex

                  PHP Code:
                  var ma = new MAStudy(100"Close"MAStudy.SIMPLE);
                   
                  function 
                  preMain() {
                   
                      
                  setPriceStudy(true);
                      
                  setStudyTitle("DZ No Trading"); 
                      
                  setCursorLabelName("MA 10",0);
                      
                  setDefaultBarFgColor(Color.red,0);
                      
                  setCursorLabelName("EMA 10 (MA 10)",1);
                      
                  setDefaultBarFgColor(Color.blue,1);
                      
                  setColorPriceBars(true);
                      
                  setDefaultPriceBarColor(Color.grey);
                  }
                   
                  var 
                  EMA   0;//variable needs to be global
                  var EMA_1 0;
                   
                  function 
                  main() {
                   
                      var 
                  / (10 1);
                      
                      
                  //the following needs to happen before current value of EMA is calculated
                      
                  if (getBarState() == BARSTATE_NEWBAR){//moved from original location
                          
                  EMA_1 EMA;//moved from original location
                      
                  }
                      
                      
                  EMA ma.getValue(MAStudy.MA) + (K) * EMA_1;//removed var and declared variable globally
                   
                      
                  var AllowTrading true;  
                      var 
                  Time = (hour(0)*100)+minute(0);
                   
                      if(
                  Time >=1130 && Time <1400){   
                          
                  AllowTrading false;  
                      }    
                   
                      if(
                  AllowTrading){ 
                      
                          
                  //if (getBarState() == BARSTATE_NEWBAR)//moved to new location
                              //EMA_1 = EMA;//moved to new location
                   
                          
                  if(ma.getValue(MAStudy.MA) > EMA && !Strategy.isLong()) {
                              
                  Strategy.doLong("Long"Strategy.CLOSEStrategy.THISBAR);//replaced MARKET with CLOSE
                              
                  Alert.addToList(getSymbol(), "Skyscraper @ "+close(), Color.RGB(0,0,0), Color.blue); 
                          }  
                   
                          if(
                  ma.getValue(MAStudy.MA) < EMA && !Strategy.isShort()) {
                              
                  Strategy.doShort("Short"Strategy.CLOSEStrategy.THISBAR);//replaced MARKET with CLOSE
                              
                  Alert.addToList(getSymbol(), "Midget @ "+close(), Color.RGB(0,0,0), Color.red);   
                          }
                   
                          
                  setBarBgColor(Color.cyan);
                      }
                      
                      if(
                  Strategy.isLong()){
                          
                  setPriceBarColor(Color.blue);
                      }
                      if(
                  Strategy.isShort()){
                          
                  setPriceBarColor(Color.red);
                      }
                      
                      return new Array(
                  ma.getValue(MAStudy.MA),EMA);

                  Comment


                  • #10
                    Hello demarcog/glay,

                    Originally posted by demarcog
                    Geoff,

                    I hope someone at esignal has made you aware that MARKET.THISBAR really means OPEN.THISBAR and I've been told to use MARKET.NEXTBAR, which is really the open of the next bar.
                    That killed the profit on alot of shorter term 1-5 minute systems.

                    It's a major issue for me, and cannot imagine why this situation has not been corrected.
                    Which fill type and fill bar constants to use in back testing is dependant upon the logic used for the script. If your conditions evaluate the prior bar then you can use MARKET/THISBAR. If your conditions evaluate the current bar, then you can use MARKET/NEXTBAR. All the possible combinations and recommended logic are outlined in section 1.3 of our Back Testing tutorial, Introduction to the Strategy Object.
                    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


                    • #11
                      Jason,


                      As always thanks for the valuable assistance and the link to the useful doc, looks like a brand new or recently updated doc.
                      Last thing I want to do is give you grief about MARKET.THISBAR
                      as you and Alexis have been invaluable and hope you don't take it personally.
                      Glen Demarco
                      [email protected]

                      Comment


                      • #12
                        Gr8

                        Thanks for all the comments/updates Alexis/Jason. I would of never caught that EMA issue!!

                        Comment

                        Working...
                        X