Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Track/Store Max Bid Value Since Entry

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

    #16
    Hello PaulMohn,

    While I wouldn't be able to debug your conditions I can make a recommendation of starting with a more simple test so that you are certain to understand how BarsInProgress works.

    A simple way to observe if the series is calling OnBarUpdate would be to use a print:

    Code:
    protected override void OnBarUpdate()
    {
        Print(BarsInProgress);
    That should output 0's and 1's to the NinjaScript output window when using a script that has 1 extra series added using AddDataSeries.

    You can also use prints for the values you used in the conditions.

    To execute logic for the secondary series you would surround that with a condition like you are using.

    Code:
    if (BarsInProgress ==1)
    {
    
    }

    Comment


      #17
      Thanks for that nice print solution. Hmm. I've just tested it and it's returning 0s only.

      So that confirms the series's not calling OnBarUpdate.
      There's something I miss about series and BarsInProgress.

      Would you perhaps have a working demo strategy sample I could test to see further into it? I previously used the SampleCustomSeries_NT8.zip in post #4 but that is an indicator and I would like to test a strategy to look for differences I could apply.


      Comment


        #18
        I've made progress reading the whole Multi-Time Frame doc


        It seems for now I can use
        if(BarsInProgress == 1 || BarsInProgress == 2)
        return;

        and call Closes[1][0] to get the Bid from the secondary series. is that correct for my case?
        The Print(BarsInProgress); do show 0 and 1 now thanks.

        Going back to what Chris suggested in post #2
        When the Position changes to long/short, set the boolean to true and while it is true in OnBarUpdate, assign the bid/ask values to a custom Series<T> object that can hold the values (a Series object is an array with a size always equal to the number of bars on the chart).
        I'm not sure if that's what he meant, but here's the code I've come up with from it.

        Code:
        protected override void OnBarUpdate()
        {
             Print(BarsInProgress);
        
             if(CurrentBar<2) return;
        
             if(BarsInProgress == 1 || BarsInProgress == 2)
                  return;
        
             if ( PositionAccount.MarketPosition == MarketPosition.Long )
             {
                  {
                       maxbidBool = true;
                  }
        
                  Print("4" + " " + Time[0] + " " + maxbidBool);
                  Print("5" + " " + Time[0] + " " + Closes[1][0]);
        
        
                  while(maxbidBool == true)
                  {
                       bidValsfrmEntry[0] = Closes[1][0];
                  }
        
                  Print("4" + " " + Time[0] + " " + bidValsfrmEntry);
        
                  if (initialExitOrderLong == null)
                  {
             ​​​​​​​     ​​​​​​​     initialExitOrderLong = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, PositionAccount.Quantity, 0, PositionAccount.AveragePrice -25*TickSize, "", @"Exit Long FULL STOP" );
        ​​​​​​​     ​​​​​​​     }
        Is the While loop the use Chris meant?
        Currently it's not working with the while loop, but it's working without it (the stop loss executes only without the while loop snippet).

        If that's not what Chris meant, what's the correct way to formalize the concept?

        Also, the
        bidValsfrmEntry[0] = Closes[1][0];
        would track/store the bids values, but I'm not sure then how to get the max value form it.
        I've looked at the MAX() doc
        https://ninjatrader.com/support/help...aximum_max.htm​​​​​​​
        Description
        Returns the highest value over the specified period.
        Syntax
        MAX(int period)
        MAX(ISeries<double> input, int period)

        // Prints the highest high value over the last 20 periods
        double value = MAX(High, 20)[0];
        Print("The current MAX value is " + value.ToString());


        But the MAX() needs an Int as period. How can I get it to calculate/retrieve the max bid value from the long entry event/moment?
        I'm stuck with that so far: bidValsfrmEntry[0] = MAX(Closes[1][0],???)[0];
        If the MAX() is not the appropriate method to do it, what other method to use?

        I've also found an older answer from Chelsea about getting the largest value from an array
        https://ninjatrader.com/support/forum/forum/ninjatrader-7/general-development/73351-get-largest-val-from-array-of-plots?p=659531#post659531
        Code:
        double largest = 0;
        for (int i = 0; i<BarsArray.Count-1; i++)
        {
             if (Closes[i][0] > largest)
             largest = Closes[i][0];
        }
        Would that be a better and simpler way to the same result? The problem with it is I don't see how to set the array from the Long entry event/moment either with this solution. What would you suggest?
        ​​​​​​​

        Comment


          #19
          Hello PaulMohn,

          Jesse has been out of the office today.

          Our Scripting Support team has been a bit busy to today to reach this message, but this ticket is still open and we will be able to have a look next week.

          Thanks for your patience.

          Comment


            #20
            Hello Jim, many thanks for the great support. I'll be back next week too.

            I've had time to look some more amazing posts and found new insights:

            array indexing and current bar indexing confusion.
            Best Explanation by bltdavid
            https://ninjatrader.com/support/foru...777#post829777
            Code:
            // save the event 'location', perhaps it was a crossover
            EventBar = CurrentBar;
            
            // what was the high price of the event?
            int BarsAgo = CurrentBar - EventBar;
            Print("High of event was "+High[BarsAgo]+" from "+BarsAgo+" bars ago");
            array indexing and current bar indexing confusion.
            NinjaTrader_ChelseaB
            https://ninjatrader.com/support/foru...751#post829751


            basic for loop
            NinjaTrader_PaulH

            https://ninjatrader.com/support/foru...333#post813333




            Code:
            MAX(MACD(12, 26,9), CuurentBar - firstS80bar)[0]; // lookback is the period that S>80

            For and while loops
            https://ninjatrader.com/support/foru...nd-while-loops

            while loop problem
            https://ninjatrader.com/support/foru...e-loop-problem



            accessing "current value" of bars
            https://ninjatrader.com/support/foru...759#post819759

            BarsRequest
            https://ninjatrader.com/support/help...arsrequest.htm

            Time stamp properties of a bar?
            NinjaTrader_ChelseaB
            https://ninjatrader.com/support/foru...162#post720162


            Have a nice weekend!

            Comment


              #21
              Hello ,PaulMohn

              If you are still having difficulty with the BarsInProgress question in some way we would need to simplify your questions around that and isolate that question in this thread. It looks like a lot more information has been provided since I was last able to answer so I am not certain if you are still stuck on the initial concern. If you are now working on other parts of the logic and have passed the BarsInProgress question please open a new threw for any new questions that you have. We ask that so we can answer your questions directly and then close the issue to avoid any confusion.


              Please let me know if I may be of additional assistance.

              Comment


                #22
                Hello Jesse and thank you for the new reply.
                The BarsInProgress issue seems resolved (post #18 above) but I'm not sure because I've not tested it yet for the end use (I'll test it without multiple BarsInProgress conditions first, then with it) because of the following errors:

                The best overloaded method match for 'NinjaTrader.NinjaScript.Strategies.Strategy.MAX(N injaTrader.NinjaScript.ISeries<double>, int)' has some invalid arguments CS1502 102 24
                NinjaScript File Error Code Line Column
                Argument 1: cannot convert from 'double' to 'NinjaTrader.NinjaScript.ISeries<double>' CS1503 102 28
                The error line is:
                Code:
                bidValsfrmEntry = MAX(Closes[1][0],CurrentBars[1]-EntryBar);
                I don't understand the double to ISeries<double> issue in the context of my code below:

                Code:
                namespace NinjaTrader.NinjaScript.Strategies
                {
                public class BidAskIncDecr : Strategy
                {
                     private int EntryBar;
                
                     private bool maxbidBool;
                
                     private Series<double> bidValsfrmEntry;
                
                     private Order initialExitOrderLong = null;
                
                     private double chase1ExitOrderLong0;
                
                     protected override void OnStateChange()
                     {
                          if (State == State.SetDefaults)
                          {
                               ...
                               IsUnmanaged = true;
                               maxbidBool = false;
                          }
                          else if (State == State.Configure)
                          {
                               // Add a secondary data series to sync our Secondary Series<double>
                               AddDataSeries(null, BarsPeriodType.Tick, 1, MarketDataType.Bid);
                          }
                          else if (State == State.DataLoaded)
                          {
                               bidValsfrmEntry = new Series<double>(this, MaximumBarsLookBack.Infinite);
                          }
                          else if (State == State.Realtime)
                          {
                               if (initialExitOrderLong != null)
                               initialExitOrderLong = GetRealtimeOrder(initialExitOrderLong);
                          }
                     }
                
                     protected override void OnBarUpdate()
                     {
                          Print(BarsInProgress);
                
                          if(CurrentBar<2) return;
                
                          if(BarsInProgress == 1 || BarsInProgress == 2)
                               return;
                
                          if ( PositionAccount.MarketPosition == MarketPosition.Long )
                          {
                               {
                                    EntryBar = CurrentBars[1];
                                    bidValsfrmEntry = MAX(Closes[1][0],CurrentBars[1]-EntryBar);
                                    maxbidBool = true;
                               }
                I've looked at those related answers but without success:

                https://ninjatrader.com/support/forum/forum/ninjatrader-8/indicator-development/108339-please-help-cannot-convert-from-double-to-ninjatrader-ninjascript-series-double?p=835603#post835603

                https://ninjatrader.com/support/foru...68#post1103068

                https://ninjatrader.com/support/foru...404#post684404


                Please can you explain what's wrong and what I should be doing instead with the error line?
                Code:
                bidValsfrmEntry = MAX(Closes[1][0],CurrentBars[1]-EntryBar);

                Comment


                  #23
                  Hello PaulMohn,

                  Thank you for letting me know that the initial concern was resolved. If you find that it was not please reopen this thread. I would suggest to break the other question into a new threads if you need further assistance as that is not related to your BarsInProgress question. We suggest to avoid grouping many questions into a single thread as that makes the thread less useful for others who are researching similar topics.

                  The error is stating that MAX is being used incorrectly, as I don't know the goal you had in mind I can only suggest to research the MAX documentation/sample in the help guide first and if that is not useful please open a new thread for those questions.

                  The Code Closes[1][0] is getting a double value from the series which MAX does not want, if you meant a specific series it would be Closes[1].




                  Comment


                    #24
                    Hello Jesse. The current question is on topic with the current thread. The BarInProgress part was part of a previous question I needed to solve (2 questions back in my recent posts). I mixed it with this thread because I needed to restart from scratch. But I'll keep the current issue on this thread and if a new issue arises with the BarInProgress (if/when I need to test a multiple BarsInProgress structure) later I'll move it to the previous related thread.

                    Thank you for the MAX recommendation, however I tested your suggestion (Closes[1]) and it's not working (it still returns the same 2 errors).
                    I did study carefully the MAX documentation (post #18 above), but I don't understand why it's not working as is.

                    The goal I have in mind: it's the one from post #1.
                    1. Storing/getting the max value of the bids from the long entry event/moment into a variable.


                    Comment


                      #25
                      Oh, I just did as you suggested but also creating another variable (separate from the bidValsfrmEntry) as follows
                      Code:
                      maxBid = MAX(Closes[1],CurrentBars[1]-EntryBar)[0];
                      and now it's not returning the errors.

                      I'll do more tests and be back thanks.

                      Now I'm getting this new error:
                      11/01/2022 17:07:09 Default Value of property 'Period' of NinjaScript 'MAX' is 0 and not in valid range between 1 and 2147483647.
                      Please help. Thanks.
                      Last edited by PaulMohn; 01-11-2022, 10:10 AM.

                      Comment


                        #26
                        Hello PaulMohn,

                        The error is stating the problem, you would need to use a print to find out what value you used for the period. You need a value that is between the values 1 and the int max.

                        Comment


                          #27
                          Originally posted by NinjaTrader_Jesse View Post
                          Hello PaulMohn,

                          The error is stating the problem, you would need to use a print to find out what value you used for the period. You need a value that is between the values 1 and the int max.
                          It's not printing the maxBid value.

                          Enabling NinjaScript strategy 'BidAskIncDecr/238743939' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=True MaxRestarts=4 in 5 minutes

                          Value of property 'Period' of NinjaScript 'MAX' is 0 and not in valid range between 1 and 2147483647.

                          Disabling NinjaScript strategy 'BidAskIncDecr/238743939'
                          Here's my prints statements:
                          Code:
                          if ( PositionAccount.MarketPosition == MarketPosition.Long )
                          {
                               {
                                    EntryBar = CurrentBars[1];
                                    maxBid = MAX(Closes[1],CurrentBars[1]-EntryBar)[0];
                                    maxbidBool = true;
                                    Print("4" + " " + Time[0] + " " + maxBid);
                               }
                          
                                    Print("5" + " " + Time[0] + " " + maxBid);
                          What's wrong?

                          Comment


                            #28
                            Hello PaulMohn,

                            The script should not be doing anything after it had an error. The print would need to go before the MAX use because that is what caused the error. You are trying some math but that is not resulting in a valid period: CurrentBars[1]-EntryBar

                            Comment


                              #29
                              Originally posted by NinjaTrader_Jesse View Post
                              Hello PaulMohn,

                              The script should not be doing anything after it had an error. The print would need to go before the MAX use because that is what caused the error. You are trying some math but that is not resulting in a valid period: CurrentBars[1]-EntryBar
                              Ok.

                              I just tested that:

                              Code:
                              if ( PositionAccount.MarketPosition == MarketPosition.Long )
                              {
                                   {
                                        Print("3" + " " + Time[0] + " " + maxBid);
                                        EntryBar = CurrentBars[1];
                                        maxBid = MAX(Closes[1],CurrentBars[1]-EntryBar)[0];
                                        maxbidBool = true;
                                        Print("4" + " " + Time[0] + " " + maxBid);
                                   }
                              
                                        Print("5" + " " + Time[0] + " " + maxBid);
                              But still no prints:

                              Enabling NinjaScript strategy 'BidAskIncDecr/238743940' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=True MaxRestarts=4 in 5 minutes
                              3 11/01/2022 17:29:00 0

                              Value of property 'Period' of NinjaScript 'MAX' is 0 and not in valid range between 1 and 2147483647.

                              Disabling NinjaScript strategy 'BidAskIncDecr/238743940'
                              But I do get prints of 0 with the prints statement as follow, at the top of the OnbarUpdate scope:

                              Code:
                              2 11/01/2022 17:35:49 0
                              2 11/01/2022 17:36:00 0
                              2 11/01/2022 17:35:49 0
                              2 11/01/2022 17:36:00 0
                              2 11/01/2022 17:36:00 0
                              2 11/01/2022 17:36:00 0
                              2 11/01/2022 17:35:50 0
                              2 11/01/2022 17:36:00 0
                              2 11/01/2022 17:36:00 0
                              2 11/01/2022 17:35:50 0
                              Code:
                              protected override void OnBarUpdate()
                              {
                                   //Print(BarsInProgress);
                                   Print("2" + " " + Time[0] + " " + maxBid);
                              
                                   if(CurrentBar<2) return;
                              
                                   if(BarsInProgress == 1 || BarsInProgress == 2)
                                        return;
                              
                                   if ( PositionAccount.MarketPosition == MarketPosition.Long )
                                   {
                                        {
                                             Print("3" + " " + Time[0] + " " + maxBid);
                                             EntryBar = CurrentBars[1];
                                             maxBid = MAX(Closes[1],CurrentBars[1]-EntryBar)[0];
                                             maxbidBool = true;
                                             Print("4" + " " + Time[0] + " " + maxBid);
                                        }
                              
                                        Print("5" + " " + Time[0] + " " + maxBid);
                              What's wrong?

                              my MAX calculation was inspired by Paul's answer

                              (also post #20 above)
                              Last edited by PaulMohn; 01-11-2022, 10:42 AM.

                              Comment


                                #30
                                Hello PaulMohn,

                                From the prints provided its reaching 3 but your print is not helpful, you need to print the math you used for the MAX period. Here is your print:

                                Code:
                                3 11/01/2022 17:29:00[B] 0[/B]
                                But that doesn't tell you what period you used for the max, you would need to change the print and then try the test again. The error does already tell you that the value was 0 so you need to use other math to make a valid value 1 or greater.


                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by NullPointStrategies, Today, 05:17 AM
                                0 responses
                                52 views
                                0 likes
                                Last Post NullPointStrategies  
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                130 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                70 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                44 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                49 views
                                0 likes
                                Last Post TheRealMorford  
                                Working...
                                X