Announcement

Collapse
No announcement yet.

Time of day - is this the best way?

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

  • Time of day - is this the best way?

    Hi,

    I'm just trying to work out a standard way to see if I should trade, related to the time of day. i.e. I don't want to open any trades too soon after the open, at lunchtime, or too close to the end of the day, and I want to close off at the end of day. This is the way I am doing it at the moment, but I was just wondering if anyone else has a simpler or more efficient method.

    Thanks

    mmillar


    In this example I can trade between 9.00am and 12.00noon, then 2.00pm and 4.00pm. Any open positions are closed at 4.30pm.

    ...
    // Setup the trading hours
    var TRADE_TIME_FLAG = "OFF";
    var TRADE_TIME_HOUR_AM_ON = 9;
    var TRADE_TIMS_MINS_AM_ON = 0;
    var TRADE_TIME_HOUR_AM_OFF = 12;
    var TRADE_TIME_MINS_AM_OFF = 0;
    var TRADE_TIME_HOUR_PM_ON = 14;
    var TRADE_TIME_MINS_PM_ON = 0;
    var TRADE_TIME_HOUR_PM_OFF = 16;
    var TRADE_TIME_MINS_PM_OFF = 0;
    var TRADE_TIME_HOUR_CLOSE = 16;
    var TRADE_TIME_MINS_CLOSE = 30;
    ...

    function main() {
    ...
    var vTime = getValue("Time");
    if (vTime == null) return;

    var sHour = vTime.getHours();
    if (sHour == null) return;

    var sMins = vTime.getMinutes();
    if (sMins == null) return;
    ...

    // Determine if we are in allowed trading hours. Set the trading flag.
    if (((sHour >= TRADE_TIME_HOUR_AM_ON) && (sHour <= TRADE_TIME_HOUR_AM_OFF))
    || ((sHour >=TRADE_TIME_HOUR_PM_ON) && (sHour <= TRADE_TIME_HOUR_PM_OFF))) {
    TRADE_TIME_FLAG = "ON";
    } else
    TRADE_TIME_FLAG = "OFF";

    // If trade flag is ON and other conditions are met you can trade.
    ...

    // End of Day - make sure all positions are closed
    if (sHour == TRADE_TIME_HOUR_CLOSE && sMins == TRADE_TIME_MINS_CLOSE)
    if (Strategy.isShort())
    Strategy.doCover("EOD Close", Strategy.CLOSE, Strategy.THISBAR);
    else if (Strategy.isLong())
    Strategy.doSell("EOD Close", Strategy.CLOSE, Strategy.THISBAR);

    ...

    }

  • #2
    OK, the last formula didn't actually work. So this is my new one. Again, if anyone knows a better or more efficient way I would very grateful.


    ...
    // Setup the trading hours - read '(14 * 60) + 45' as 2.45pm
    var vTRADE_TIME_FLAG = false;
    var vTRADE_TIME_AM_ON = (14 * 60) + 55;
    var vTRADE_TIME_AM_OFF = (17 * 60) + 10;
    var vTRADE_TIME_PM_ON = (19 * 60) + 30;
    var vTRADE_TIME_PM_OFF = (21 * 60) + 00;
    var vTRADE_TIME_CLOSE = (21 * 60) + 05;


    function main() {

    //Get the current time - hours and minutes
    var vHour = getHour();
    if (vHour == null) return;
    var vMins = getMinute();
    if (vMins == null) return;

    var vBarTime = (vHour * 60) + vMins;


    //Work out whether we are within a valid trading time window - set the vTRADE_TIME_FLAG to true or false
    if (((vBarTime >= vTRADE_TIME_AM_ON ) && (vBarTime <= vTRADE_TIME_AM_OFF )) ||
    ((vBarTime >= vTRADE_TIME_PM_ON ) && (vBarTime <= vTRADE_TIME_PM_OFF )))
    vTRADE_TIME_FLAG = true;
    else
    vTRADE_TIME_FLAG = false;

    ...
    // If trade flag is true and other conditions are met you can trade.
    ...


    // End of Day - make sure all positions are closed
    if (Strategy.isInTrade() && (vBarTime >= vTRADE_TIME_CLOSE)) {
    vTRADE_TIME_FLAG = false;
    if (Strategy.isShort()) {
    Strategy.doCover("EOD Close", Strategy.MARKET, Strategy.NEXTBAR);
    }
    if (Strategy.isLong()) {
    Strategy.doSell("EOD Close", Strategy.MARKET, Strategy.NEXTBAR);
    }
    }


    ...
    }
    Last edited by mmillar; 05-10-2003, 04:31 AM.

    Comment


    • #3
      Hi,
      I use these lines as a simple way of identifying key zones throughout the day. At least it can give you something to build upon, dependant on your specific requirements.

      Paul
      Attached Files

      Comment


      • #4
        Hmmm, this is a nifty study. Except I have my charts changed to Pacific West Coast time and then I edited the study to PST as well. The first set of bars, however, do not show up until 8:15AM.

        function preMain(){
        setPriceStudy(true);
        setStudyTitle("Reversal Time Zones");

        setComputeOnClose();

        }

        function main(line1time, line2time, line3time, line4time, line5time, line6time, line7time,line8time, line9time, line10time, line11time, line12time, line13time, line14time){


        if(line1time==null){
        line1time=0650;
        }

        if(line2time==null){
        line2time=0710;
        }

        if(line3time==null){
        line3time=0725;
        }

        if(line4time==null){
        line4time=0735;
        }

        if(line5time==null){
        line5time=815;
        }

        if(line6time==null){
        line6time=830;
        }

        if(line7time==null){
        line7time=900;
        }

        if(line8time==null){
        line8time=915;
        }

        if(line9time==null){
        line9time=1015;
        }

        if(line10time==null){
        line10time=1030;
        }

        if(line11time==null){
        line11time=1115;
        }

        if(line12time==null){
        line12time=1130;
        }

        if(line13time==null){
        line13time=1200;
        }

        if(line14time==null){
        line14time=1230;
        }



        BarHr = getHour();
        BarMin = getMinute();
        cBarTimeInt =BarHr*100+BarMin*1;


        if(cBarTimeInt==line1time){
        setBarBgColor( Color.red );
        setBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line2time){
        setBarBgColor( Color.red );
        setBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line3time){
        setBarBgColor( Color.red );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line4time){
        setBarBgColor( Color.red );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line5time){
        setBarBgColor( Color.RGB(254,197,24) );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line6time){
        setBarBgColor( Color.RGB(254,197,24) );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line7time){
        setBarBgColor( Color.lime );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line8time){
        setBarBgColor( Color.lime );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line9time){
        setBarBgColor( Color.red );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line10time){
        setBarBgColor( Color.red );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line11time){
        setBarBgColor( Color.red );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line12time){
        setBarBgColor( Color.red );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line13time){
        setBarBgColor( Color.red );
        setDefaultBarStyle(PS_DASH);
        }

        if(cBarTimeInt==line14time){
        setBarBgColor( Color.red );
        setDefaultBarStyle(PS_DASH);
        }


        return null
        }

        Comment


        • #5
          JT
          Remove the leading 0 in 0650, 0710, 0725 and 0735. Notice in fact that 815 which is the first line that plots does not have a leading 0.
          Alex

          Comment


          • #6
            Alexis, thanks for the reply. Actually the 0 was put in there while I was attempting to, albeit lamely, troubleshoot the problem. It still occurs with or without the zeros.

            Comment


            • #7
              JT
              Are you positive about this? Here are two images of the same chart one run with the formula having the leading 0s and the other with the 0s taken out.
              What interval are you using in your charts? If other than 1 or 5 minutes there could be the possibility that the bars don't plot on those specific times.
              Alex



              Last edited by ACM; 05-17-2003, 10:05 PM.

              Comment


              • #8
                Hmm, now we're getting somewhere. Changed it back w/o the 0s and then switched to a different time interval. I'm using a 3min and it doesn't look like that it likes it, thus the ommission of the first few bars. 2 min and 5 min works. Any way to make it work with a 3min? Thx and good catch!

                Comment


                • #9
                  JT
                  The lines don't show with the 3 minute chart because there are no bars with a time stamp of :10, :25, :35 or :50 which are the conditions of the first 4 lines.
                  All the other bars show because they are set for :15, :30 and :60 minute marks which are divisible by 3.
                  Hence, if you want to show the bar at 6:50 on a 3 min chart you will need to change the time in the formula to either 6:48 or 6:51 ie the closest 3 minute marks.
                  Alex

                  Comment

                  Working...
                  X