Announcement

Collapse
No announcement yet.

Closing postion EOD troubles

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

  • Closing postion EOD troubles

    My backtesting works as expected until I try to close my postions at the end of the day. I am using the east coast time template.

    I am trying to close my positions at the end of the day for the close price. When I add the following code, the backtest will only take trades on the first day of the chart. None of the rest of the days show up. How do you close at the close price for the current day and keep processing the other days?

    Thanks

    nEOD = close(-1);
    /*
    if (hour(0) > hour(1)){
    if (Strategy.isLong()){
    Strategy.doSell("End of Day", Strategy.STOP, Strategy.THISBAR, Strategy.ALL,nEOD);
    }

    if (Strategy.isShort()){
    Strategy.doCover("End of Day", Strategy.STOP, Strategy.THISBAR,Strategy.ALL, nEOD);
    }
    }
    */

    if (day(0)!=day(1)) {
    if (Strategy.isLong()){
    Strategy.doSell("End of Day", Strategy.STOP, Strategy.THISBAR, Strategy.ALL,nEOD);
    }

    if (Strategy.isShort()){
    Strategy.doCover("End of Day", Strategy.STOP, Strategy.THISBAR,Strategy.ALL, nEOD);
    }
    }

  • #2
    Just a quick look...
    Use a negative number when referring to bar indexes so change hour(1) and day(1) to hour(-1) and day(-1).

    Wayne

    Comment


    • #3
      That still would only allow for running the code for one day.

      I added :
      if (day(0)> day(-1) && hour(0)<8 ){

      So now it will not take trades before the 9 o'clock hour.

      Comment


      • #4
        the code:
        PHP Code:
        if (day(0)!=day(-1)){//this will execute on every new day for loaded bars.  If only 2 days are loaded then it executes once.
            //make sure you have enough bars/days loaded on the chart.  
            //If it only executes one day try scrolling the chart backwards until you add more days then reload the script.
            //you can also set the # of days to load in the time template window
            //code inside this block will only run each day on the first tick of the new day's first bar
            //so on a dynamic time template that would be the first bar after midnight
            //on east coast and similar time templates it will run each day on the first tick of the first bar of the new day

        "if (day(0)> day(-1)" is always true since today is always one day later than yesterday
        "hour(0)<8"" restricts execution to from midnight up until 7:59:59.
        If you use the east coast time template you don't need to restrict the hours since it by default starts at 9:30 and stops at 16:00 (which is the close of 15:57).

        try this test code:
        PHP Code:
        debugClear();
        function 
        main(){
            if (
        day(0)!=day(-1)) {
                
        debugPrintln(day(-1)+","+(hour(-1)+":"+minute(-1)+":"+second(-1))+", close-1: "+close(-1));//on a 3 min int chart
                //on a dynamic time template, line 4 outputs the close at 23:57:00 for the previous day
                //on a RTH East Coast time template, line 4 outputs the close at 15:57:00 for the previous day
                
        debugPrintln(day(0)+","+(hour(0)+":"+minute(0)+":"+second(0))+", close0: "+close(0));//on a 3 min int chart
                //on a dynamic time template, line 7 outputs the close at 00:00:00 for the current day
                //on a RTH East Coast time template, line 7 outputs the close at 9:30:00 for the current day
            
        }

        Do an efsKB search for "strategy".
        See: http://kb.esignalcentral.com/display...n=1&docid=8104
        for explanation of the Strategy object

        PHP Code:
            //Strategy.STOP will "If long, issue a stop sell order if high of the current bar >= StopOrLimit".
            //so try Strategy.CLOSE
            //try this, I'll leave the testing to you
            
        if ((hour(0)*100)+minute(0)>=1557) { //for East Coast time template 
            //using an interval that includes bar time 1557 (i.e., a 60 min interval bar time starting at 930 will stop at 1530 
            //when using an East Coast time template so it won't execute the code block,
            //but a 3 minute interval includes bar time 1557 and will execute the code.
            //if ((hour(0)*100)+minute(0)>=1600) { //for dynamic time template
                
        if (Strategy.isLong()){
                    
        Strategy.doSell("End of Day"Strategy.CLOSEStrategy.THISBARStrategy.ALL); 
                    
                }

                if (
        Strategy.isShort()){
                    
        Strategy.doCover("End of Day"Strategy.CLOSEStrategy.THISBAR,Strategy.ALLnEOD);
                }
            } 
        Wayne
        Last edited by waynecd; 05-16-2013, 03:23 PM.

        Comment


        • #5
          Just in case it is not clear:

          PHP Code:
          if (day(0)!=day(-1)){

          executes only once per change from one day to the next and only on the first tick of the new day. So adding additional time filters/conditionals after it would only be evaluated on the first tick of the new day when the above condition is true. When using a dynamic time template, that would be the first tick after midnight. When using a user defined time template, that would be on the first tick of the day at whatever start time was defined in the time template.

          Wayne
          Wayne

          Comment


          • #6
            FWIW contrary to what waynecd says that statement executes on every tick of the first bar of the day on intraday intervals and on every tick on a daily interval
            Alex


            Originally posted by waynecd View Post
            Just in case it is not clear:
            PHP Code:
            if (day(0)!=day(-1)){

            executes only once per change from one day to the next and only on the first tick of the new day. So adding additional time filters/conditionals after it would only be evaluated on the first tick of the new day when the above condition is true<SNIP>

            Comment


            • #7
              I appreciate the correction.

              Wayne

              Comment


              • #8
                To clarify:
                This correction applies to real time (bar currently being processed whether intraday or daily).
                For historical bars like those used in backtesting there are no ticks within each bar, just the OHLC so there is only one returned value, not repeated for each tick in the processed bar.

                PHP Code:
                debugClear();
                function 
                main(){
                    if (
                day(0)!=day(-1)) {
                        
                debugPrintln();
                        
                debugPrintln(day(-1)+"/"+month(-1)+"/"+year(0)+","+(hour(-1)+":"+minute(-1)+":"+second(-1))+", close-1: "+close(-1));//on a 3 min int chart
                        //on a dynamic time template, line 4 outputs the close at 23:57:00 for the previous day
                        //on a RTH East Coast time template, line 4 outputs the close at 15:57:00 for the previous day
                        
                debugPrintln(day(0)+"/"+month(0)+"/"+year(0)+","+(hour(0)+":"+minute(0)+":"+second(0))+", close0: "+close(0));//on a 3 min int chart
                        //on a dynamic time template, line 7 outputs the close at 00:00:00 for the current day
                        //on a RTH East Coast time template, line 7 outputs the close at 9:30:00 for the current day
                    
                }//the above outputs only once pre day.
                    
                if(hour(0)*100+minute(0)==330){//change the 330 time to the time of your choice using a 2400 hour format
                        //debugPrintln("13: "+(hour(0)*100+minute(0)));
                        
                debugPrintln("14: "+day(0)+"/"+month(0)+"/"+year(0)+","+(hour(0)+":"+minute(0)+":"+second(0))+", close0: "+close(0));//outputs on every tick of the bar at 3:30 am
                    
                }
                }  
                //In real time it outputs on every tick within the bar that meets the conditional regardless of interval
                //In backtesting historical bars in executes only once on the bar that meets the conditional. 
                Wayne
                Last edited by waynecd; 07-16-2013, 01:39 AM.

                Comment

                Working...
                X