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

Set Defaults based on a condition

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

    Set Defaults based on a condition

    Is there a better way to set the defaults of variables on a strategy?

    I am trying to set the values of variables based on the day of the week, I tried doing this in OnBarUpdate. It does work however this step runs all the time and it creates a slower strategy.

    Is there a better way to do this?

    #2
    Hello felixolmo,

    Thanks for your post.

    Are you wanting to set the defaults based on the historical data? For example, if you have 5 days of historical data starting with a Monday, do you want to set the default values for that historical Monday and then change when the script processes Tuesday, etc. up to the current real time day?

    Or are you only wanting to set defaults based on the real time day?

    If you want to set the defaults based on the real time day then you could add your condition in OnStatechange() under State.Realtime so it is only executed once. To avoid processing those defaults on your historical data, in OnBarUpdate() you can skip processing the historical data with: if (State == State.Historical) return;

    If you want your defaults to be set for each historical and real-time day then you have to add your check in the OnBarUpdate() for the Day of the week and then setting of your defaults. This could be a simple check of Time[0] and time[1] to see if the Day of the week is different and then call a private void where you change your defaults. Should not be a significant slowness to your strategy.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you, Paul.

      I am trying to set the defaults at the beginning of each day and run calculations based on those defaults. even though it might sound like it won't take too much for it to run every single time in the OnBarUpdate, I would like to keep it lean if I can.

      As an example, I set targets, stops, auto trails based on days of the week. Doesn't sound too much, I do have other things running this is just a clean and easy example. So I am trying to set those defaults at the beginning of the day and that is it, I wouldn't want to keep setting every time we have an update on the bar.

      Is this possible? Any type of suggestions?

      Comment


        #4
        Hello felixolmo,

        Thanks for your reply.

        If I understand correctly then you don't care about your historical values and are only considering "today's" values.

        In the OnStateChange() State.RealTime you could check for DateTime.Now.DayOfWeek . for example

        if (DateTime.Now.DayOfWeek== DayOfWeek.Tuesday)
        {
        // set variables here
        }
        else if DateTime.Now.DayOfWeek == DayOfWeek.Wednesday)
        {
        // set variables here
        }
        .
        .
        .

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          I tried that, however, when I am running backtesting I get errors. Is that what you consider historical data?

          Comment


            #6
            Hello felixolmo,

            Thanks for your reply.

            Yes, that is historical data and the OnStateChnage() method would never reach State.Realtime.

            Any bars on a chart that are not moving are historical data.

            So now that we know you are backtesting you should use the other approach;

            In OnBarUpdate()

            if (Time[0].DayOfWeek != Time[1].DayOfweek) // Yes this gets checked on every bar, and is only true once per day (midnight)
            {
            set your variables based on// get set only once per day.
            if (Time[0].dayOfWeek == DayOfWeek.Monday)
            {
            // set v ars here
            }
            else if (Time[0].dayOfWeek == DayOfWeek.Monday)
            {
            // etc. etc.
            }
            }

            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Ok that sounds good. This is how I have it set up.

              One last question, utilizing Switch vs If statements. I have read that switch is faster...anything into that? and forum post that you might find useful for someone that have never utilized Switch before?

              Comment


                #8
                Hello felixolmo,

                Thanks for your reply.

                I doubt that you would notice any sort of change in functional speed in the size of what you are referring to, but it is good that speed is a concern to you in your programming.

                This is more of a C# question and we are here to support Ninjascript questions.

                You can find plenty of internet C# resources, so try a search for C# switch.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you Paul.

                  A question just came to mind, so for Backtesting I need to place the code inside OnBarUpdate. So, can I use the same strategy in Real-Time Trading? Or would I need to change the code from OnBarUpdate to State.Realtime to be able to use it in actual trading?

                  Would I need it on both sides if I want them to use the same strategy to do both backtesting and real-time?

                  Comment


                    #10
                    Hello felixolmo,

                    Thanks for your reply.

                    You should be good to go for both real time and historical testing.

                    The OnBarUpdate() is called for each historical and real time bar.

                    This means your strategy's historical trades would be based on the correct day settings as well as your real-time trades.
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by pechtri, 06-22-2023, 02:31 AM
                    9 responses
                    122 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by frankthearm, 04-18-2024, 09:08 AM
                    16 responses
                    66 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by habeebft, Today, 01:18 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by benmarkal, Today, 12:52 PM
                    2 responses
                    14 views
                    0 likes
                    Last Post benmarkal  
                    Started by f.saeidi, Today, 01:38 PM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Working...
                    X