Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Printing only last value

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

    Printing only last value

    Ancient thread, however the topic is precise.

    Using NT8 and Strategy Builder I put together a simple tool to identify the time when the highest high and the lowest low were set during a time window determined by the user (historical only).

    User inputs are: Time_Start and Time_End.

    Price reaching a new high assigns High[0] to a variable named Price_High.
    Likewise, price reaching a new low assigns Low[0[ to a variable named Price_Low.

    I need to capture only the date and time -- Times[0][0] value -- when the final Price_High was set during the time window (highest high).
    Again likewise, I need to capture only the final -- Times[0][0] value -- when Price_Low was set during the time window (lowest low).


    Using the Price_High example, the following Conditions, Actions (Print statement), the output window displays ALL occurrences, not just the last one.

    Conditions
    Times[Default input][0].TimeOfDay >= Time_Start.TimeOfDay
    Times[Default input][0].TimeOfDay <= Time_Stop.TimeOfDay
    High[0] > Price_High


    Actions
    Price_High = High[0]
    Print(@"High: "(Convert.ToString(Times[0][0]));


    OUTPUT
    ~
    5/19/2023 8:30:00 AM
    5/19/2023 8:35:00 AM
    5/19/2023 8:38:00 AM
    5/19/2023 8:39:00 AM
    5/19/2023 8:43:00 AM
    5/19/2023 8:55:00 AM << This is the only value required


    What can be added to the Print statement string builder to either eliminate all but the final value OR specifically select the final value only?
    I've looked for and tried various values without success.

    Thanks



    #2
    Hello culpepper,

    I went ahead and moved this post to the correct sub forum, in the future please ensure to post NT8 questions to the NinjaTrader desktop sub forums even if there is an older post that is relevant, you can link to that post if necessary.

    If you are wanting to save only the last price you are already doing that if you set that price to a variable. The variable gets updated for every bar and is left with the last set value which is the one you pointed to with the arrow. You print will show all values because each print is individual.

    To make the print only show the last value you would have to add logic to print after that time period has ended on the next bar and then use another bool variable to disable that condition after the first print.

    Comment


      #3
      Thank you for responding, Jesse. I'm now aware old open topic threads require a new topic start in General Development and a back reference to the original. Is there a cutoff date for responding to old posts? (e.g. 1 or 2 calendar years or greater?)


      Also, thanks for the hints on completing a high/low time capture utility the original poster discussed. This is intended to capture the time when morning high occurred and the time the morning low occurred. Price is actually irrelevant.

      As you suggested, to print the high & low time values after the time window has closed I created a bool named PeriodEnd (default = false) and a new Set condition to query if the time window has indeed closed: Current time is greater than the Time_Stop variable mentioned before. Times[Default input][0].TimeOfDay > Time_Stop.TimeOfDay.

      If the time window is closed then bool PeriodEnd = true;

      In the next Set are the two Print Statements. One to print the high time and one to print the low time, however rather than capturing one high time value and one low time value per day the output is far more extensive than in my previous example. There is a data line for every 1 minute bar during the RTH session. The high and low prices remain correct.

      This should not be occurring since I added a final Set which disabled the bool after the first print, as suggested.
      There is no "LastBarOfSession" option so I specified a time value of 11:02am.
      The thinking here is because the time window is open from 08:30AM to 11:00AM (CST) the print will occur at 11:01AM after the following condition: Times[Default input][0].TimeOfDay > Time_Stop.TimeOfDay has been met.

      The next bar is 11:02PM and is the earliest opportunity. The final Set condition is: Times[Default input][0].TimeOfDay = new TimeSpan(11, 2, 0).
      The action is to set PeriodEnd = false;

      The last Price print High and Low are being captured perfectly, but as mentioned, price is unimportant. Time print: Times[Default input][0].TimeOfDay is only accurate on the specific bar where the high or low were made. Not being a coder, it just seems to me the time when the highs and lows occur are not being saved to a variable therefore it simply prints the current date/time.

      Looking forward to your thoughts, Jesse.​

      Comment


        #4
        Hello culpepper,

        I would suggest to post any new questions that you have as a new thread always instead of re opening any old thread. Old threads will be redirected to a specific user which it may take a long time to get a reply. The main reason for moving that is that you had an NT8 question and we were in the NT7 forums, a good way to get an incorrect answer is posting in the incorrect forums because we take note of what forum we in when replying to you. The only reason I knew you were asking about NT8 was that you specifically said that.

        From the details it sounds like you didn't toggle off that condition using your PeriodEnd after the first print. Along with your prints you need to set PeriodEnd = false and make sure your other condition to set it to true does not become true again until the next session.You likely need a more refined condition for after the time window, for example if you use 1 minute bars check if the time is after your time window and not more than 1 minute after that so only 1 bar is valid in that condition. You would just make another time condition greater than the end time and less than the end time plus 1 minute.

        As you mentioned the variables are working to capture the price because you can only store 1 price at a time, prints are a debugging tool so you will see a new print every time you call Print. That has no effect on how your logic works its just not as convenient to see each value printed in your described use case. Your logic is otherwise working correctly to store the last price because the last bar is the value that will be set to the variable. If you needed to also print the time or any other last value you would need to do the same task and set a variable to that time/price/value. The last bar processed within your time window will be what those variables represent because a variable only holds a single value and is replaced each time you set it.






        Comment


          #5

          Thanks Jesse,

          It is working perfectly now with one small exception. The print statement shows one low price and one high price for the session and those prices are indeed the final high and final low. Perfect - except for the most important piece of data - the high/low time stamps I created two variables to store the times named Time_High and Time_Low. When a user tries to assign the Times[0][0] value to the variable, strangely the Time section is missing. It's available everywhere else I believe. Please see the image.

          Click image for larger version

Name:	Available Values.png
Views:	197
Size:	7.3 KB
ID:	1253274

          Here is another look a the Conditions and Actions for printing the time the high was made.

          Conditions
          Times[Default input][0].TimeOfDay >= Time_Start.TimeOfDay
          Times[Default input][0].TimeOfDay <= Time_Stop.TimeOfDay
          High[0] > Price_High


          Actions
          Price_High = High[0]
          Time_High = 0 << The value "Times[0][0];" matching the High[0] bar is not available in the Values window. Setting the date/time value right here is all that is needed.
          PeriodEnd = true;


          Naturally, the Set of conditions for the Low value are the opposite.


          Print Statement
          Print(@Low:"+Convert.ToString(Price_low)+""+Conver t.ToString(Time_Low) + @"High:"+Convert.ToString(Price_High) + "" + Convert.ToString(Time_High));

          Output (The data are from Mon-Fri May 15-19, 2023. Price values are perfect. Zero prints where the data/time value should be.)
          Low:4123:0:High:4149:0
          Low:4127:0:High:4148.75:0
          Low:4125.75:0:High:4166.75:0
          Low:4164.75:0:High:4199.25:0
          Low:4191.5:0:High:4225.75:0

          Just one tiny detail to figure out and I've got it nailed.
          Looking forward.​ Thanks again.

          Comment


            #6
            Hello culpepper,

            It looks like while using the builder that would be a limitation, you would have to manually code it if you wanted to store the time as a variable. Your logic is still working as expected you just won't be able to save the time in the builder.

            Comment


              #7
              That is unfortunate news Jesse, but thank you regardless.

              It explains why I spent the weekend searching for some other way to assign Time to a variable, without success. It's easy for me to lay out what a utility, strategy or any software needs to do, but I was not born with the aptitude to code. However, I'm going to succeed here via sheer determination. Therefore, I duplicated the functioning strategy and added the Time variable syntax. It looks very straightforward but I have been deceived by code many times before. The code is open under alpha name: ALPHAHighLowTimeCapture.

              I found the forum post below from a user who also needed to create a time variable. Is the syntax the same or similar to the one used in this indicator? https://forum.ninjatrader.com/node/1119809. The code example provided by Brandon_H, shows the time as static: 09:30AM.

              protected override void OnBarUpdate()
              {
              startTime = new DateTime(Time[0].Year,Time[0].Month,Time[0].Day,9,30,0,0);
              }

              Time must be fluid for my needs therefore I deleted the static time. When I made several attempts over the weekend to add DateTime and examples provided by Brandon_H, the compile failed each time.

              It looks as though time must be declared "private" just as my other variables are. Could you suggest another place to place the DateTime code? I assume it should be placed with the existing variables and also in the applicable "sets" : One set to establish TimeHigh and one set to establish TimeLow.

              No errors were generated when I placed both variables here:

              public class ALPHAHighLowTimeCapture : Strategy
              {
              private bool PeriodEnd;
              private double PriceHigh;
              private double PriceLow;
              private DateTime TimeHigh;
              private DateTime TimeLow;

              -----------------------------------
              Next, I pasted the variable assignment twice in the section identified by Brandon_H and modified the text to identify the new TimeHigh and TimeLow variables and removed the static time 9:30AM.

              protected override void OnBarUpdate()
              {
              TimeHigh = new DateTime(Time[0].Year,Time[0].Month,Time[0].Day);
              }
              {
              TimeLow = new DateTime(Time[0].Year,Time[0].Month,Time[0].Day);
              }


              ---------------------------
              In the final step I assigned the DateTime value to Set 3 which captures the time the low occurs and Set 4 which captures the time the high occurs.

              // Set 3
              if (
              // LowerLow ConditionGroup
              ((Low[0] < PriceLow)
              || (PriceLow == 0))
              && (Times[0][0].TimeOfDay >= TimeStart.TimeOfDay)
              && (Times[0][0].TimeOfDay <= TimeStop.TimeOfDay))
              {
              PriceLow = Low[0];
              TimeLow = DateTime(Time[0].Year,Time[0].Month,Time[0].Day);
              PeriodEnd = true;
              }

              // Set 4
              if ((High[0] > PriceHigh)
              && (Times[0][0].TimeOfDay >= TimeStart.TimeOfDay)
              && (Times[0][0].TimeOfDay <= TimeStop.TimeOfDay))
              {
              PriceHigh = High[0];
              TimeHigh = DateTime(Time[0].Year,Time[0].Month,Time[0].Day);
              PeriodEnd = true;
              }

              Adding lines to OnBarUpdate per Brandon_H and to the Sets results in the following compile errors:
              • Class member declaration expected. (7 instances)
              • The modifier "new" is not valid for this item. (Error disappears when "new" is deleted)
              • The constructor's name must match the containing type's name.
              • Identifier expected.
              • Type name expected. (15 instances - Many of these errors refer to lines of code that have not been modified at all.)
              • Class member declaration expected.
              • Extern alias declarations, using clauses, or namespace/type declarations expected.

              I could not find a help resource which lists error codes alphabetically and provides solutions. I also went to the links provided by Brandon_H for MSDN and C-Sharp Corner, but the information is far beyond my skill level. The specific error messages above are not explained at either of those resource sites. I don't know what to change or correct in the code. Deleting the word "new" seems to be a starting point.

              Does the internal variable DateTime become invalid when the static time is removed?

              I hope this code only needs a few tweaks.
              Thanks for your input, Jesse.


              p.s. Allow me to suggest adding the Time category and/or DateTime value to the list of available builder options in a future version of Ninjatrader. It would have made this task a breeze for us non-coders.

              Comment


                #8
                Hello culpepper,

                Time simply isn't an option in the Set for a variable in the builder, to store the time directly would require manual coding. Times are only used for defaults or inputs in the builder currently. You can otherwise store times similar to what you are trying in manual coding without any issue.

                I would suggest if you are using the builder to just avoid using the time and print only the variables you used. If you absolutely need to print the time you can unlock the code. Your code is otherwise working as expected.

                Comment


                  #9
                  Hello Jesse,

                  Yes I agree, the existing code is working as expected. It is also incomplete.

                  I do understand Time is not an option in the Set for a variable in the builder and that it would require manual coding.

                  As you saw by the code examples in my prior post, I have unlocked the code and indeed I AM attempting to manually code the final variable of Time to complete the utility.

                  Earlier I mentioned the capture of Time is not only essential, it is the sole purpose of the utility and is the ONLY value required when recording the exact hh:mm:ss when a high or low price was achieved, in any market.

                  I hope you will review my previous post and answer the questions related to where NinjaScript needs new code and if DateTime is the correct internal variable to use. Hopefully you could provide an idea of were to look for solutions to the error messages. I am following the Brandon_H solution provided to another user however it is not compiling.

                  Thank you.​

                  Comment


                    #10
                    Hello culpepper,

                    If you have unlocked the code you can store the time to a variable, times are DateTime objects. You would do the exact same process as storing the prices, the last time the variable is set would be the stored time.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    633 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    364 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    105 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    567 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    568 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X