Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using Time periods in Strategy Builder

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

    Using Time periods in Strategy Builder

    I am trying to use the trading times of day in the strategy Builder and have entered the code as below in set one.

    The strategy only works when there are one set of start and stop times per set, when I try and have 2 start and stop times the strategy no longer works

    is there an example how this would work in the strategy builder?


    if ((xxxxxxxxxxxxxxxTrend[0] == -1)
    && (VOLMA1[0] >= Volma)
    && (Times[0][0].TimeOfDay >= StartAM.TimeOfDay)
    && (Times[0][0].TimeOfDay <= StopAM.TimeOfDay)
    && (Times[0][0].TimeOfDay >= StartPM.TimeOfDay)
    && (Times[0][0].TimeOfDay <= StopPM.TimeOfDay))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    Many thanks
    Last edited by fredfred; 04-01-2019, 08:39 AM.

    #2
    Hello fredfred,

    Thank you for the question.

    I believe in this case this is because of how you formed the condition. Right now this is saying if the time is between the Start/StopAM and is also between Start/StopPM. You are asking that the time is two times at once which cant happen.

    I believe if you wanted to make this happen in either the AM or PM you need to split this into two separate conditions or use condition groups. In a first condition, you could check the AM part, in a second condition check the PM part. You can form this as one condition as well as using condition groups. In a first group check that the AM conditions are all true, in a second group check that the PM conditions are true. As the primary condition, check if Any of the conditions are true meaning if either the AM group or PM group are true.

    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      OK, Ive split them into 2 groups Morning and afternoon as below

      // Condition group 1
      && ((Times[0][0].TimeOfDay >= StartAM.TimeOfDay)
      || (Times[0][0].TimeOfDay <= StopAM.TimeOfDay))
      // Condition group 2
      && ((Times[0][0].TimeOfDay >= StartPM.TimeOfDay)
      || (Times[0][0].TimeOfDay <= StopPM.TimeOfDay)))
      {
      EnterShort(Convert.ToInt32(DefaultQuantity), "");
      }

      and it is still not working ?

      Comment


        #4
        Hello fredfred,

        This is still requiring both conditions to become true, I see where you added OR however that will only affect the time conditions directly.It may help if you say out loud what the condition is asking, here is an example:

        And Time greater equal to StartAM or Time Less equal than StopAm And Time greater equal to StartPM or time less equal to StopPM.
        The OR statement should instead be between the two AM and PM time conditions. You want to ensure the time is between your two AM and PM times first, so the AM part of the condition would need to read as

        time greater equal to StartAM AND time less equal to StartPM
        The same goes for the PM:
        time greater equal to StartPM AND time less equal to StartPM
        The OR statement would need to go between these two blocks:

        time greater equal to StartAM AND time less equal to StartPM

        OR

        time greater equal to StartPM AND time less equal to StartPM

        enter long
        Here is a simplified if condition using OR between the two time blocks:

        Code:
        if(
        Times[0][0].TimeOfDay >= StartAM.TimeOfDay [B]&&[/B] Times[0][0].TimeOfDay <= StopAM.TimeOfDay
        [B]||[/B]
        Times[0][0].TimeOfDay >= StartPM.TimeOfDay [B]&&[/B] Times[0][0].TimeOfDay <= StopPM.TimeOfDay
        )
        {
        EnterShort(Convert.ToInt32(DefaultQuantity), "");
        }

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks Jesse, Got it, but the only way the Strategy builder will write that in Conditions and actions with "If Any" If I put it in another set without an Action
          if (
          // Condition group 1
          ((Times[0][0].TimeOfDay >= StartAM.TimeOfDay)
          && (Times[0][0].TimeOfDay <= StopAM.TimeOfDay))
          // Condition group 2
          || ((Times[0][0].TimeOfDay >= StartPM.TimeOfDay)
          && (Times[0][0].TimeOfDay <= StopPM.TimeOfDay)))
          {
          Your code has Enter Short is in here ? but I need the second condition below as well ? not to go short on the time of day?
          }

          Then I need to go short with "If All " instructions in another set ?

          if ((xxxxxxxxxxxxxxxTrend[0] == -1)
          && (VOLMA1[0] >= Volma)
          {
          EnterShort(Convert.ToInt32(DefaultQuantity), "");
          }

          In all, I have this

          // Set 1
          if (
          // Condition group 1
          ((Times[0][0].TimeOfDay >= StartAM.TimeOfDay)
          && (Times[0][0].TimeOfDay <= StopAM.TimeOfDay))
          // Condition group 2
          || ((Times[0][0].TimeOfDay >= StartPM.TimeOfDay)
          && (Times[0][0].TimeOfDay <= StopPM.TimeOfDay)))
          {
          }

          // Set 2
          if ((xxxxxxxxxxxxxxxxxx.Trend[0] == 1)
          && (VOLMA1[0] >= Volma))
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), "");
          }

          I understand your clear explanation, but the Strategy Builder doesn't allow me to mix "If All "and "If Any"

          Many thanks

          Comment


            #6
            Hello fredfred,

            If you wanted to join both into a single condition as I had described, it would be as I had shown however this depends on your overall condition if you can do that or not. If the conditions you are creating require all of the conditions to become true, you will likely need to form a second set for the PM section of logic instead of doing an OR statement and one set. The AM and PM logic should not become true at the same time, so having separate sets shouldn't run the risk of entering twice or having an entry ignored.

            In a case where the overall condition can be variable, the logic like I had shown could be used. This type of logic could also be used to set a user variable to true/false instead of directly entering into a position so that it can drive other sets of logic. An example would be making a single condition which checks if the time is am/pm and ok to trade, then sets a user variable to true. In a second set, simply check if the variable is true in order to enter.



            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by helpwanted, Today, 03:06 AM
            1 response
            10 views
            0 likes
            Last Post sarafuenonly123  
            Started by Brevo, Today, 01:45 AM
            0 responses
            9 views
            0 likes
            Last Post Brevo
            by Brevo
             
            Started by aussugardefender, Today, 01:07 AM
            0 responses
            5 views
            0 likes
            Last Post aussugardefender  
            Started by pvincent, 06-23-2022, 12:53 PM
            14 responses
            242 views
            0 likes
            Last Post Nyman
            by Nyman
             
            Started by TraderG23, 12-08-2023, 07:56 AM
            9 responses
            387 views
            1 like
            Last Post Gavini
            by Gavini
             
            Working...
            X