Announcement

Collapse
No announcement yet.

restricting trading hours

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

  • restricting trading hours

    Jason,

    I'd like to understand how to add code to existing studies to restrict the hours they are active. This would be used for backtesting futures. I don't think creating a time template for the specific hours would work if I am using any kind of moving average . Is that simple enough to demonstrate in this forum?

    thanks,
    Peter
    /\/\/\/\/\/\/\/\

  • #2
    Hello Peter,

    The following formula will give you a basic example of how to restrict trading to a specific range of time using getHour(). You can access the time of a bar through the following Data Access Functions:

    getYear()
    getMonth()
    getDay()
    getHour()
    getMinute()
    getSecond()

    This formula colors the background light grey to show you the bars that would allow your code to execute. The formula is set by default to restrict trades (or code execution) to the hours of 8 to 12. These start and end hours are also setup so you can change them via the "Edit Studies" option (nStartHour and nEndHour).

    PHP Code:
    /*****************************************************************
    Provided By : eSignal. (c) Copyright 2003
    *****************************************************************/


    function preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Restrict Trading Hours ");
        
    setShowCursorLabel(false);
    }

    function 
    main(nStartHournEndHour) {
        if (
    nStartHour == nullnStartHour 8;
        if (
    nEndHour == nullnEndHour 11;
        
        var 
    BarHour getHour();
        if (
    BarHour >= nStartHour && BarHour <= nEndHour) {
            
    setBarBgColor(Color.lightgrey);
            
    // add back testing code here;
            // return new Array(bla, bla, bla);
        
    } else {
            return;
        }    

    RestrictTradingHours.efs

    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


    • #3
      Jason,

      How would you get it to restrict between 6:30 and 13:00?
      Thanks.
      Michael

      Comment


      • #4
        Here's an easy way to do time calculations:

        var vTime = (getHour() * 100) + getMinutes();

        if (vTime >= 630 && vTime <= 1300) {
        // add time-restricted code here
        }

        Comment


        • #5
          thank you...so simple. i unfortunately can't do the programming thing. thanks to all the human 'compilers' out there.
          Michael

          Comment


          • #6
            Looking for assistance making restricted trading hours values adjustable via Edit Studies. So far I have:

            (in pre-main)
            var fp18 = new FunctionParameter("n1TimeStart", FunctionParameter.NUMBER);
            fp18.setName("1st Time Start");
            fp18.setLowerLimit(0);
            fp18.setDefault(930);

            var fp19 = new FunctionParameter("n1TimeEnd", FunctionParameter.NUMBER);
            fp19.setName("1st Time End");
            fp19.setLowerLimit(0);
            fp19.setDefault(1100);

            var fp20 = new FunctionParameter("n2TimeStart", FunctionParameter.NUMBER);
            fp20.setName("2nd Time Start");
            fp20.setLowerLimit(0);
            fp20.setDefault(1400);

            var fp21 = new FunctionParameter("n2TimeEnd", FunctionParameter.NUMBER);
            fp21.setName("2nd Time End");
            fp21.setLowerLimit(0);
            fp21.setDefault(1600);

            (var declarations in global variables before main)
            var n1TimeStart;
            var n1TimeEnd;
            var n2TimeStart;
            var n2TimeEnd;

            (in main)
            function main([the fp1 - fp17 names are here, then], n1TimeStart,n1TimeEnd,n2TimeStart,n2TimeEnd) {

            (later in main)
            if((getHour()*100)+getMinute()>= n1TimeStart &&(getHour()*100)+getMinute()< n1TimeEnd ||
            (getHour()*100)+getMinute()>= n2TimeStart &&(getHour()*100)+getMinute()< n2TimeEnd){
            var vTime = 1;
            }

            The problem is that these trading-hours function parameters (fp18 - fp21) are not appearing in Edit Studies, and so the values for the variables are returned as undefined. In this formula, there are 17 other function parameters (fp1 - fp17) that all appear and function as they should in Edit Studies. Possibly, is 17 the limit for function parameters in Edit Studies? If not, why aren't fp18 - fp21 appearing in Edit Studies?

            Comment


            • #7
              Hi Lancer,

              Do not declare any of the variables in your main ( function main([the fp1 - fp17 names are here, then], n1TimeStart,n1TimeEnd,n2TimeStart,n2TimeEnd) {
              ) statement as global variables. If they need to be globals such that they are available outside of main, create different global variable names (e.g. new1, new2, etc) and make them equal to the variable names in the main statement within main as follows:

              PHP Code:
              if (getBarState() == BARSTATE_ALLBARS) {//true once during initial load and subsequent reload

              new1 n1TimeStart;
              new2 n1TimeEnd;
              new3 n2TimeStart;
              new4 n2TimeEnd;


              Comment


              • #8
                Thanks Steve, got it working.

                Comment


                • #9
                  Hi Lancer,

                  You are very welcome.

                  Comment


                  • #10
                    Hi,

                    I dont want to sound dumb but i dont understand how to apply the restricted trading hour formula to the chart.

                    What i want to do is have a chart only for 9.30ET to 11.45ET. From that, i then want to put on an indicator that shows the high and low in that period.

                    Could someone explain to me how to do this with all the steps explained.

                    Kind regards,

                    David Dunne

                    Comment


                    • #11
                      David

                      ...i dont understand how to apply the restricted trading hour formula to the chart

                      As is the efs only paints in grey the background for the time period set by the user defined parameters nStartHour and nEndHour. You need to insert in the space indicated by the comments the logic to generate the signals you want to use either in back testing or for real time

                      What i want to do is have a chart only for 9.30ET to 11.45ET

                      If you want to plot a chart only for that specific time period you will need to use a Time Template. For more information on Time Templates see this article in the eSignal KnowledgeBase and view the related video available in the eSignal University
                      Alex

                      Comment

                      Working...
                      X