Announcement

Collapse
No announcement yet.

Exit on 3rd Friday

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

  • Exit on 3rd Friday

    Hello

    Is it possible to exit a trade always on the 3rd Friday (Option expiry)?

    guterm

  • #2
    guterm,

    There isn't a checkbox in the backtester to always exit on the 3rd Friday.

    In your strategy you would have to get the current bar's date and then use the Date.getDay() method to determine the day of the week. Then use the plain getDay() method to get the day of the month to determine if it's the 3rd Friday.

    Steve

    Comment


    • #3
      Thanks Steve

      I just don't know how to determinde the 3rd Friday...

      var today = new Date();
      var weekday = getDay();

      if weekday = 6 then & ???

      Do you could give me some help? Thank

      guterm

      Comment


      • #4
        guterm,

        if (weekday == 6 && monthday >= 15 && monthday <= 21)
        {// then 3rd friday
        // do 3rd friday processing here
        }

        Steve

        Comment


        • #5
          Hello Steve,

          Wow, thanks, this is fast and simple, I like it!

          Daniel

          Comment


          • #6
            Daniel,

            You're most welcome.

            Steve

            Comment


            • #7
              Hmm, I must be a fool, I can't have a text on the Friday:

              /** draw test "Friday" on the 3rd Friday of the Month
              ************************************************** ****/



              function preMain() {

              setPriceStudy(true);
              setStudyTitle("Friday");
              }

              function Main() {

              var weekday = new Day();
              var monthday = new Date();

              if (weekday == 6 ) {
              // if (weekday == 6 && monthday >=15 && monthday <= 21)
              // then 3rd friday
              drawTextAbsolute( 0, AboveBar1, "Friday" );
              }
              return;
              }

              Why?

              Comment


              • #8
                Daniel
                There are several errors in the script
                1. function Main() should be function main()

                2. Day() is not an existing object. To create a Date Object based on bar time you need to use getValue("time") (see the link to the article in the EFS KnowledgeBase for the description and syntax of the function). To correct this replace
                var weekday = new Day();
                with
                var weekday = getValue("time");

                3. In the condition if (weekday == 6) you omitted to use the method .getDay() to retrieve the day of the week from the object (for the description and syntax of the method see this article in the EFS KnowledgeBase on the Date Object). Also you are evaluating if the weekday is equal to 6 which corresponds to Saturday and not Friday. To correct this replace
                if (weekday == 6 ) {
                with
                if (weekday.getDay() == 5 ) {

                4. The line drawTextAbsolute( 0, AboveBar1, "Friday" ); is incorrect or incomplete depending on which function you intended to use (see this or this article on drawText() or drawTextRelative() functions). To correct this replace the line with either
                drawText("Friday", AboveBar1);
                or
                drawTextRelative(0, AboveBar1, "Friday", Color.blue, null, Text.PRESET, "Arial", 10, "TextA"+rawtime(0));
                If you use the first option you may also want to add a setDefaultFont() statement in the preMain() function to set the font and its properties

                Once you make the above corrections the formula should write Friday on each bar that is time stamped as Friday
                Alex

                Originally posted by guterm
                Hmm, I must be a fool, I can't have a text on the Friday:

                /** draw test "Friday" on the 3rd Friday of the Month
                ************************************************** ****/



                function preMain() {

                setPriceStudy(true);
                setStudyTitle("Friday");
                }

                function Main() {

                var weekday = new Day();
                var monthday = new Date();

                if (weekday == 6 ) {
                // if (weekday == 6 && monthday >=15 && monthday <= 21)
                // then 3rd friday
                drawTextAbsolute( 0, AboveBar1, "Friday" );
                }
                return;
                }

                Why?

                Comment


                • #9
                  Daniel
                  Adding to my reply. Once you have made the changes I suggested you will also need to modify the following line (which is currently commented out)
                  // if (weekday == 6 && monthday >=15 && monthday <= 21)
                  with the following to get it to work
                  if (weekday.getDay() == 5 && weekday.getDate() >=15 && weekday.getDate() <= 21)
                  Note that in the second and third condition I use the .getDate() method of the Date Object which returns the date of the month. At this point the text should print only on the third Friday of each month
                  Then you can comment out (or delete) the following line of code as it is not required in the context of your script
                  var monthday = new Date();
                  Hope this helps
                  Alex

                  Comment


                  • #10
                    Alex

                    Thank you very much for your kindly help and explanations.

                    But I still get no text on the chart with the following:
                    function preMain() {

                    setPriceStudy(true);
                    setStudyTitle("Friday");
                    }

                    function main() {

                    var weekday = getValue("time");

                    if (weekday.getDay() == 5 && weekday.getDate() >=15 && weekday.getDate() <= 21 ) {
                    // the 3rd friday
                    drawTextRelative( 0, AboveBar1, "Friday", Color.blue, null, Text.PRESENT, "Arial", 10, "TextA"+rawtime(0));
                    }
                    return;
                    }

                    Where did I go wrong? Does it work with your chart?

                    Daniel

                    Comment


                    • #11
                      Daniel
                      That is because you have Text.PRESENT instead of Text.PRESET in the drawTextRelative() function
                      Once you correct that the text should display.
                      Alex


                      Originally posted by guterm
                      Alex

                      Thank you very much for your kindly help and explanations.

                      But I still get no text on the chart with the following:
                      function preMain() {

                      setPriceStudy(true);
                      setStudyTitle("Friday");
                      }

                      function main() {

                      var weekday = getValue("time");

                      if (weekday.getDay() == 5 && weekday.getDate() >=15 && weekday.getDate() <= 21 ) {
                      // the 3rd friday
                      drawTextRelative( 0, AboveBar1, "Friday", Color.blue, null, Text.PRESENT, "Arial", 10, "TextA"+rawtime(0));
                      }
                      return;
                      }

                      Where did I go wrong? Does it work with your chart?

                      Daniel

                      Comment


                      • #12
                        Wow, great, thanks.
                        Daniel

                        Comment


                        • #13
                          Daniel
                          You are most welcome
                          Alex


                          Originally posted by guterm
                          Wow, great, thanks. Daniel

                          Comment

                          Working...
                          X