Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Struggling with Time Windows

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

    Struggling with Time Windows

    Maybe I'm just dumb or burned out, but I'm trying to prevent my strategy from entering orders after 2:45 PM (CST) but allow them after 5 pm CST. The ES Emini trading hours are 5 pm to 4pm. I want to trade between 5 pm CST and 2:45 pm of the following day - something that makes sense for Emini. What is the easiest way to implement this?

    The code below isn't working as intended.

    // Set 1
    if (
    // Crossover Current Period
    ((CrossAbove(LinReg1, Bollinger1.Middle, 1))
    && (Bollinger1.Upper[0] < Bollinger2.Middle[0])
    && (ChaikinMoneyFlow1[1] > ChaikinMoneyFlow1[2])
    && (Times[0][0].TimeOfDay >= new TimeSpan(17, 0, 0))
    && (Times[0][0].TimeOfDay <= new TimeSpan(14, 45, 0)))

    // Crossover Current Period2
    || ((CrossAbove(LinReg1, Bollinger1.Middle, 1))
    && (Slope(ChaikinMoneyFlow1, 3, 1) > 0.5)
    && (MarketPosition.Short != Position.MarketPosition)
    && (Times[0][0].TimeOfDay >= new TimeSpan(17, 0, 0))
    && (Times[0][0].TimeOfDay <= new TimeSpan(14, 45, 0))))

    {
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), Typical[0], "");
    }

    #2
    Have you studied this sample?

    Comment


      #3
      Hello dniedrauer,

      Thanks for your post.

      You would need two timespans, with an or condition, to permissibly cover the time period of interest. So from 17,0,0 to 23,59,59 OR time from 0,0,0 to 14,45,0

      The reason is to fit the logic operations based on the numerical values, IE: 170000 <= 235959 is true but 170000<=000000 is not true





      Paul H.NinjaTrader Customer Service

      Comment


        #4
        still not behaving as expected after using the example

        // Set 1
        if (
        // Crossover Current Period
        ((CrossAbove(LinReg1, Bollinger1.Middle, 1))
        && (Bollinger1.Upper[0] < Bollinger2.Middle[0])
        && (ChaikinMoneyFlow1[1] > ChaikinMoneyFlow1[2])
        && (((ToTime(Time[0]) >= 170000 && ToTime(Time[0]) <=235900) || (ToTime(Time[0]) >= 000000 && ToTime(Time[0]) < 144900)))


        )
        // Crossover Current Period2
        || ((CrossAbove(LinReg1, Bollinger1.Middle, 1))
        && (Slope(ChaikinMoneyFlow1, 3, 1) > 0.5)
        && (MarketPosition.Short != Position.MarketPosition))
        && (((ToTime(Time[0]) >= 170000 && ToTime(Time[0]) <=235900) || (ToTime(Time[0]) >= 000000 && ToTime(Time[0]) < 144900))))
        {
        EnterLongLimit(Convert.ToInt32(DefaultQuantity), Typical[0], "");
        }

        It thinks anytime after 1700 it can enter any position with no other conditions.

        Comment


          #5
          Originally posted by dniedrauer View Post
          still not behaving as expected after using the example

          // Set 1
          if (
          // Crossover Current Period
          ((CrossAbove(LinReg1, Bollinger1.Middle, 1))
          && (Bollinger1.Upper[0] < Bollinger2.Middle[0])
          && (ChaikinMoneyFlow1[1] > ChaikinMoneyFlow1[2])
          && (((ToTime(Time[0]) >= 170000 && ToTime(Time[0]) <=235900) || (ToTime(Time[0]) >= 000000 && ToTime(Time[0]) < 144900)))


          )
          // Crossover Current Period2
          || ((CrossAbove(LinReg1, Bollinger1.Middle, 1))
          && (Slope(ChaikinMoneyFlow1, 3, 1) > 0.5)
          && (MarketPosition.Short != Position.MarketPosition))
          && (((ToTime(Time[0]) >= 170000 && ToTime(Time[0]) <=235900) || (ToTime(Time[0]) >= 000000 && ToTime(Time[0]) < 144900))))
          {
          EnterLongLimit(Convert.ToInt32(DefaultQuantity), Typical[0], "");
          }

          It thinks anytime after 1700 it can enter any position with no other conditions.
          What is the purpose of the code in red?
          Seems like that code actually allows an entry after 1700, right?

          Well, if the time is after 1700, and you're getting an entry, the rest of
          the conditions must also have been true, right?

          Comment


            #6
            Hello dniedrauer,

            Thanks for your reply.

            Here is a screenshot where I've created the time condition and added the action to color the background. As you can see the only area that is not colored is the time period 2:45pm to 5:00 pm which indicators the time filter works as expected.

            Click image for larger version

Name:	dniedrauer-1.PNG
Views:	339
Size:	73.6 KB
ID:	1162876

            Using drawing or draw objects when a condition set is true is a good way to verify functionality.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by bltdavid View Post

              What is the purpose of the code in red?
              Seems like that code actually allows an entry after 1700, right?

              Well, if the time is after 1700, and you're getting an entry, the rest of
              the conditions must also have been true, right?
              No. Actually, it's entering trades with no entry signal that aren't entered in other iterations of the strategy. I'm wondering if perhaps the time window is interfering with certain indicators like CMF or linear regression lines.

              When I simply cloned each set and put one condition in A (the 000 to 1447) and the other in B (1700 to 2359), it behaved mostly as expected, although in a few instances, trades at 5 pm were not entered on market replay due, I suspect, to interrupted calculations of indicators. In any case, it seems the time window decreases - rather than increases - performance on market replay. So I'm probably going to drop it for this strategy.

              Comment


                #8
                Can you attach your script?

                Comment


                  #9
                  Since the Strategy Builder doesnt have the ToTime method, how are we supposed to allow a time span to enable trading and deny all other times?
                  I could just paste this in:
                  Code:
                  if  (ToTime(Time[0]) >= 83500 && ToTime(Time[0]) <= 144500)
                  but it wont work since my code is locked.

                  Also I wrote that for central time. Will it not work right since it should be on GMT or Eastern?

                  Comment


                    #10
                    Hello ezrollin,

                    Please see our Conditions Examples for how to create time filters in the Strategy Builder.



                    Time[0] (the bar's timestamp) will be translated to the time zone entered in the Control Center's Tools > Options > General menu.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      I think I've got it figured out.
                      Just trying to avoid that 8:30am central opening bell

                      //ConditionSet1 to enter a trade:
                      .....................
                      && (Times[0][0].TimeOfDay > new TimeSpan(8, 40, 0))
                      && (Times[0][0].TimeOfDay < new TimeSpan(8, 25, 0)))

                      Comment


                        #12
                        Hello ezrollin,

                        Thanks for your reply.

                        Those conditions:

                        && (Times[0][0].TimeOfDay > new TimeSpan(8, 40, 0))
                        && (Times[0][0].TimeOfDay < new TimeSpan(8, 25, 0)))
                        will not work. In order for those two lines to resolve to true the bar time has to be both less than 8:25 and greater than 8:40, at the same moment.

                        I've created an educational example strategy that demonstrates using a bool variable that is set true when it is okay to trade. I used the times of 7 to 8:25 and 8:40 to 2:00 PM. As you can see the exclusion period is 8:25 to 8:40, as well as before 7:00 AM and after2:00 PM See the screenshot:

                        Click image for larger version

Name:	ezrollin-1.PNG
Views:	291
Size:	41.7 KB
ID:	1178894

                        The strategy uses a bool variable called TimeOkToTrade.

                        In Set #1 the bool is set to false with no conditions. This is important that this be the first set. The reason this is done is that a bool will retain its state when set and if you only set it true when a condition occurs, it does not automatically change to false when the condition is not true. So the first set makes the bool false each time the code is executed.

                        In set #2 are two groups of time filters. Note that the "set" is configured to "If any" meaning if either of the groups are true then the set will be true and the action will be performed. The action is set #2 is to set the bool TimeOkToTrade to true when either of the time filters is true. If the time if between 8:20 and 8:40 the bool is not set to true and as it is set to false by set 1, the bool will proceed as false.

                        Set #3 is used to verify the time filter functionality by drawing the background of the chart when the bool is true. You can see that the background color on the chart matches the two time filters.

                        [ATTACH]n1178895[/ATTACH]

                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks I'll try it!
                          The problem is that after you already have a strategy built, you cant just move sets to wherever you want. It'll automatically rearrange them the way it wants them.

                          Comment


                            #14
                            Ok, I did it and it worked!
                            However it didnt like the Midnight overlap. So it wouldnt draw the 2nd group/half at all if it was overlapped
                            What do I do about the midnight 1 minute loss its gonna close my trade on?


                            Code:
                             // Set 2
                            if (
                            // MidnightTO0825am
                            ((Times[0][0].TimeOfDay > new TimeSpan(0, 0, 0))
                            && (Times[0][0].TimeOfDay < new TimeSpan(8, 25, 0)))
                            // 9amTOmidnight
                            || ((Times[0][0].TimeOfDay > new TimeSpan(9, 0, 0))
                            && (Times[0][0].TimeOfDay < new TimeSpan(23, 59, 0))))     [B]//      <----  doesnt like 000  (24:00 hrs midnight)[/B]
                            {
                            TimeOkToTrade = true;
                            }
                            Thanks!

                            Comment


                              #15
                              Hello ezrollin,

                              Thanks for your replies.

                              "The problem is that after you already have a strategy built, you cant just move sets to wherever you want. It'll automatically rearrange them the way it wants them." You can rearrange the tabs by clicking and dragging them to resequence as you wish. You would need to compile after doing so to keep them in that order.

                              "What do I do about the midnight 1 minute loss its gonna close my trade on?" The value of having a sim101 account is the ability to test and answer questions. No, the bool being false for 1 minute would mean that no new trades would be made in that 1 minute period, assuming you are using the bool only for the entry conditions.
                              Paul H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Haiasi, 04-25-2024, 06:53 PM
                              2 responses
                              16 views
                              0 likes
                              Last Post Massinisa  
                              Started by Creamers, Today, 05:32 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post Creamers  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              12 responses
                              1,785 views
                              0 likes
                              Last Post Leafcutter  
                              Started by poplagelu, Today, 05:00 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post poplagelu  
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,407 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Working...
                              X