Announcement

Collapse
No announcement yet.

close positions at end of day

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

  • close positions at end of day

    im backtesting a program and i need to have the positions closed out at 4:00. i tried a gethour and == 1600 then cover/sell but that doesnt work anyone know what i need to do?

  • #2
    ugalee
    here is an example to close a Long position
    if((getHour()*100)+getMinute()==1600&&Strategy.isL ong()==true)
    Strategy.doSell(your parameters here);
    Alex

    Comment


    • #3
      couldn't get it to work

      i used that code and adjusted it a little and it didn't close the trade in backtesting....any idea what im doing wrong??

      else if (
      (getHour()*100)+getMinute()==1600&&Strategy.isLong ()==true)
      onAction5();


      function onAction5() {
      Strategy.doSell("", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT, 0);
      vLastAlert = 5;

      Comment


      • #4
        ugalee
        What interval is your chart? Does it have a bar time stamped 16:00?
        For example if you had a Time Template set for 9:30-16:00 the last bar of the day will be 15:59 and the command will not trigger.
        Alex

        Comment


        • #5
          ill try 15:59

          ill try that and see what happens.

          mike

          Comment


          • #6
            Here is how I handle it..

            var vStartHour = 6;
            var vStartMin = 32;
            var vEndHour = 12;//9;
            var vEndMin = 58;

            var vHour;
            var vMin;
            var vSec;
            var vDay;
            var vMonth;




            function main() {

            var vTime = new Date();
            //---------------------------------------------------
            // Get Time Variables
            vTime = getValue("Time", 0);
            vHour = vTime.getHours();
            vMin = vTime.getMinutes();
            vSec = vTime.getSeconds();
            vDay = vTime.getDate();
            vMonth = vTime.getMonth() +1;


            // Within trading times..
            if (((vHour > vStartHour) && (vHour < vEndHour)) || ((vHour == vStartHour) && (vMin >= vStartMin)) || ((vHour == vEndHour) && (vMin <= vEndMin)) ) { //Execute New Stop



            }
            // End within trading times


            //---------------------------------------------------
            // Close any OPEN trades after ENDTIME ....
            if ( ((vHour > vEndHour)) || ((vHour == vEndHour) && (vMin > vEndMin)) ) { //Close all OPEN Trades
            if (Strategy.isInTrade()) {
            if ((Strategy.isShort() == true)) {
            Strategy.doCover("Close Short EOD", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, close());
            drawShapeRelative(0, low() +1.25, Shape.UPARROW, null, Color.black, Shape.ONTOP, getValue("rawtime") + "EOD");
            OpenContracts = 0;
            }
            if ((Strategy.isLong())) {
            Strategy.doSell("Close Long EOD", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, close());
            drawShapeRelative(0, high() +1.25, Shape.DOWNARROW, null, Color.black, Shape.ONTOP, getValue("rawtime") + "EOD");
            OpenContracts = 0;
            }
            }
            } // END CLOSE OPEN TRADES


            }
            Brad Matheny
            eSignal Solution Provider since 2000

            Comment


            • #7
              thanks

              ill try that.. thanks for the help..

              Comment


              • #8
                why did you use these times?

                var vStartHour = 6;
                var vStartMin = 32;
                var vEndHour = 12;//9;
                var vEndMin = 58;

                Comment


                • #9
                  PST times..

                  These are PST times for the west coast of the US.

                  Nothing specific... Just a start time and end time.

                  Brad
                  Brad Matheny
                  eSignal Solution Provider since 2000

                  Comment

                  Working...
                  X