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

Replacing ‘Time[0].Day’ by something as ‘Time[0].dayWednesday to start on Wednesdays

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

    Replacing ‘Time[0].Day’ by something as ‘Time[0].dayWednesday to start on Wednesdays

    Hello

    I actually have an indicator that starts making its process with the first bar of each day with ‘ActualDay = Time[0].Day;’. For now without declaring any DateTime “variable” previously on the top of the code.

    But now I need the indicator starts its process on each Wednesday and I’m trying to change this line of code for other where be specified the Wednesday but I’m not getting the correct syntax to do that.

    All the examples I found here in the forum was about if-statements like ‘if(Time[0].DayOfWeek == DayOfWeek.Monday’, and I couldn’t find anything different than comparison processes about this topic, so as I couldn’t find in the Forum any similar example like the one I need, then I tried with different ways like:


    Code:
    [FONT=Arial]Time[0] == DayOfWeek.Wednesday;[/FONT]
    Or
    Code:
    [FONT=Arial]ActualDay = Time[0]. DayOfWeek.Wednesday[/FONT]
    Or
    Code:
    [FONT=Arial]…
    public class CustomIndicator : Indicator
     {
        ...
        private DateTime dayWednesday;
        …
       protected override void OnBarUpdate()
        {
           dayWednesday = Time[0]
          Time[0] == DayOfWeek.Wednesday
          …[/FONT]
    And some more variations, but all them getting errors in the Editor.

    Could you please show me what would be the way to get this working?

    And another question would be, is there a way to maybe specify a DateTime (private DateTime dayWednesday) in the variables list and then just do something like ‘ActualDay = Time[0].dayWednesday’ and in this way only replace ‘Time[0].Day’ by ‘Time[0].dayWednesday’ to be able to switch from one to another when needed in a quick way.

    Thank you

    #2
    Hello

    The code you provided is correct, you just need to change Monday to Wednesday:
    Code:
    if(Time[0].DayOfWeek == DayOfWeek.Monday)
    Code:
    if(Time[0].DayOfWeek == DayOfWeek.Wednesday)
    You could also use a variable for this, it would be a DayOfWeek type variable.

    Code:
    private DayOfWeek myDay = DayOfWeek.Wednesday;
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you for your response Jesse

      Originally posted by NinjaTrader_Jesse View Post
      The code you provided is correct, you just need to change Monday to Wednesday:
      Code:
      if(Time[0].DayOfWeek == DayOfWeek.Monday)
      Code:
      if(Time[0].DayOfWeek == DayOfWeek.Wednesday)
      Yes of course, I just put '.Monday' to make an exact reference to an example found here in the Forum, however this way is not working in my case because I'm not making a comparison procces in this exact case and what I need is to make an assignment, so if possible what I need would be to be able to just replace the extension '.Day' in 'Time[0].Day' by something like 'Time[0].myDay' because now I'm observing the case better, I will need some scenarios depending on the situation, so my final idea with this would be that the current 'Time[0].Day' that is working, maybe could be replaced by something like 'Time[0].customTime' where the user can specify what exactly is that '.customTime' depending on what is needed in a specific situation, i.e. the extension in 'Time[0].Day' could be '.myDay' (Wednesday) or simply '.Day' and so on with any other custom DateTime extension.


      You could also use a variable for this, it would be a DayOfWeek type variable.

      Code:
      private DayOfWeek myDay = DayOfWeek.Wednesday;
      https://docs.microsoft.com/en-us/dot...tframework-4.8
      Thank you for the Microsoft reference. Yesterday I was to those Microsoft sites but no way to get what I need yet.

      I tried with your example here with 'private DayOfWeek myDay = DayOfWeek.Wednesday;' but when I try to use 'myDay' as extension in the way: 'Time[0].myDay' then the Editor shows the next kind of errors:

      'System.DateTime' does not contain a definition for 'myDay' and no extension method 'myDay' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly

      And I was thinking that as 'myDay' is not a default extension, then when you declare 'private DayOfWeek myDay = DayOfWeek.Wednesday;' that in that way you could use the created "custom DateTime" as extension that is exactly what I'm needing

      Btw, It would great if there could be an example with a similar scenario that provides the guide to obtain what I'm looking for. Something similar or approximate.

      Thank you!

      Comment


        #4
        Hello,

        I tried with your example here with 'private DayOfWeek myDay = DayOfWeek.Wednesday;' but when I try to use 'myDay' as extension in the way: 'Time[0].myDay' then the Editor shows the next kind of errors:
        You would not use Time[0] with the variable like that, you would use it like a variable comparison with the DayOfWeek property:

        Code:
        if(Time[0].DayOfWeek == myDay)
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello

          The code you provided is correct, you just need to change Monday to Wednesday:
          Code:
          if(Time[0].DayOfWeek == DayOfWeek.Monday)
          Code:
          if(Time[0].DayOfWeek == DayOfWeek.Wednesday)
          You could also use a variable for this, it would be a DayOfWeek type variable.

          Code:
          private DayOfWeek myDay = DayOfWeek.Wednesday;
          https://docs.microsoft.com/en-us/dot...tframework-4.8


          Hello and thank you for your previous reply.

          Ok, I’m using the next to evaluate some separate conditions every Wednesday and every Day as, more or less as I previously posted:


          Code:
          [FONT=Arial]…
          if(Time[0].DayOfWeek == DayOfWeek.Wednesday)
          …[/FONT]
          Code:
          [FONT=Arial]…
          …ActualDay = Time[0].Day      //This is in a separate place from the previous line
          …[/FONT]

          And now I need an extra step that would be to evaluate some extra conditions ONLY every 3 Wednesdays and/or every 2 days. I tried with examples like:

          Code:
          [FONT=Arial]…
          if(Time[0].DayOfWeek == DayOfWeek.Wednesday *3)    //The idea is the action from this part of the code runs 1 Wed yes and 2 Wed no, 1 Wed yes and 2 Wed no, and so on...
          …[/FONT]
          But of course with this I got errors in the NinjaScript Editor

          Could you please show me how I can handle the situations like this where I need to work with DayOfWeek/DateTime and at the same time with something like an “int” that indicates I need a process run every certain number of times.

          Thank you
          Last edited by futurenow; 01-13-2022, 02:28 PM.

          Comment


            #6
            Hello futurenow,

            You would need to use a variable and logic for that tye of use. An int could be used and then you could count a number of times and reset the variable. You could increment a variable like the following

            Code:
            private int myInt; 
            
            protected override void OnBarUpdate()
            {
                 if(aCondition)
                 {
                       myInt++; // increment the value
                  }
                 if(myInt >= 3)
                 {
                    //do something because the 3rd instance happened
                    myInt = 0;
                 }
            
            }
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DanielTynera, Today, 01:14 AM
            0 responses
            2 views
            0 likes
            Last Post DanielTynera  
            Started by yertle, 04-18-2024, 08:38 AM
            9 responses
            40 views
            0 likes
            Last Post yertle
            by yertle
             
            Started by techgetgame, Yesterday, 11:42 PM
            0 responses
            11 views
            0 likes
            Last Post techgetgame  
            Started by sephichapdson, Yesterday, 11:36 PM
            0 responses
            2 views
            0 likes
            Last Post sephichapdson  
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,615 views
            0 likes
            Last Post aligator  
            Working...
            X