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

Specific Time Intervals?

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

    Specific Time Intervals?

    Hello, I have created a strategy that only runs from 2:00 AM - 12:00 PM (ES). I used the sample time filter code that you guys provided and it works. However, after doing some analysis on the backtest I would like to make the time in which the strategy is enabled more specific. For example, I would like to be able to run my strategy from

    7am-9am
    10am-1pm
    2pm-3pm

    I cannot seem to figure out how to go about this in code, I have tried using multiple if statements to check if the time was for example later than 7am but earlier than 9am. It didn't work in the backtest however, and the strategy kept taking positions during times when it wasn't supposed to. Any help would be appreciated. Thanks

    #2
    Hello spetsnaz,

    Thank you for the post.

    I wanted to check, can you provide what you have now so I can provide a matching sample?

    It is very likely that you need to only use one condition but make multiple ways it can equate to true.

    Using the OR operator || will let you make a single condition that has multiple logical paths.

    Here is one simple example of two times using ToTime:

    Code:
    if ((ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= 84500 ) || (ToTime(Time[0]) >= 94500 && ToTime(Time[0]) <= 104500))
    {
        // logic goes here
    }

    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 09-20-2018, 03:09 PM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello spetsnaz,

      Thank you for the post.

      I wanted to check, can you provide what you have now so I can provide a matching sample?

      It is very likely that you need to only use one condition but make multiple ways it can equate to true.

      Using the OR operator || will let you make a single condition that has multiple logically paths.

      Here is one simple example of two times using ToTime:

      Code:
      if ((ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= 84500 ) || (ToTime(Time[0]) >= 94500 && ToTime(Time[0]) <= 104500))
      {
          // logic goes here
      }

      I look forward to being of further assistance.
      Hey Jesse, I basically tried that exact same if statement yesterday. I used some || statements to basically check if it was between 7-9am OR 10-1pm, etc. It was still placing orders after 4pm for some reason. I will post what code I currently have.

      Code:
      if (CrossBelow(SMA1, Close, 1) && (ToTime(Time[0]) >= 2000 && ToTime(Time[0]) < 120000) && (DM(14)[0] > 27 || DM(14)[0] < 24))  //Checks for proper time and checks value of ADX before placing trade
      			{
      				Print("The current ADX Value is: " + DM(14)[0] + ". Now entering Long Position");
      				EnterLong(Convert.ToInt32(DefaultQuantity), @"enterLongPosition");
      				
      			}
      			
      			 // Set 2 (SHORT)
      			if (CrossAbove(SMA1, Close, 1) && (ToTime(Time[0]) >= 2000 && ToTime(Time[0]) < 120000) && (DM(14)[0] > 27 || DM(14)[0] < 24))  //Checks for proper time and checks value of ADX before placing trade
      			{
      				Print("The current ADX Value is: " + DM(14)[0] + ". Now entering Short Position");
      				EnterShort(Convert.ToInt32(DefaultQuantity), @"enterShortPosition");
      				
      			}

      Comment


        #4
        Hello

        Thank you for the reply.

        I think part of the problem are the values being used. the integer values you have used don't seem to relate to the times you mentioned.

        For example, 7 am would be 70000. or 9am would be 90000.


        Here is a sample using the times you mentioned:

        Code:
        if ((ToTime(Time[0]) >= 70000 && ToTime(Time[0]) <= 90000 ) || (ToTime(Time[0]) >= 100000 && ToTime(Time[0]) <= 130000))
        {
             // logic goes here
        }
        Which produces the attached result in magenta.



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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by TradeSaber, Today, 07:18 AM
        0 responses
        4 views
        0 likes
        Last Post TradeSaber  
        Started by PaulMohn, Today, 05:00 AM
        0 responses
        9 views
        0 likes
        Last Post PaulMohn  
        Started by ZenCortexAuCost, Today, 04:24 AM
        0 responses
        6 views
        0 likes
        Last Post ZenCortexAuCost  
        Started by ZenCortexAuCost, Today, 04:22 AM
        0 responses
        3 views
        0 likes
        Last Post ZenCortexAuCost  
        Started by SantoshXX, Today, 03:09 AM
        0 responses
        17 views
        0 likes
        Last Post SantoshXX  
        Working...
        X