Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

basic for loop

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

    #16
    Where is your order submission code logic?

    Maybe you should set EntryBar when you submit the order,
    since that event obviously only happens once per order.
    Last edited by bltdavid; 01-12-2022, 04:40 AM.

    Comment


      #17
      Originally posted by bltdavid View Post
      You should have started a new thread for this question.

      CurrentBars[1] is the bar number for the 1-Tick secondary
      data series and, by definition, is changing every tick.

      Is that the number you want to use?
      Hello bltdavid and thanks for the question. Yes I need

      For the EntryBar variable:

      1. the CurrentBars[1] but only to capture the Bar's int value from the instant the Long order it executed.

      Then for the maxBid variable:

      2. then use it to track/store the MAX Bid value for the period comprised between the instant the Long order it executed and when I become flat again.

      Simplified Illustration example:

      my Long order gets executed at 12:00:00 pm, and at that time the Bid bar index= 6.
      I need the EntryBar to capture the 6 only (without capturing the next values).

      Then at 12:15:00 pm I exit the trade and become flat.

      In between 12:00:00 pm and 12:15:00 pm, the bid collection fluctuated (hypothetic) from

      1.01 (at 12:00:00 pm) (new CurentBar[1] = 7)
      to 0.99 (at 12:03:00 pm) (new CurentBar[1] = 8)
      to 1.03 (at 12:07:00 pm) (new CurentBar[1] = 9)
      to 1.05 (at 12:09:00 pm) (new CurentBar[1] = 10)
      to 1.07(at 12:15:00 pm) (new CurentBar[1] = 11)

      I tried to do the 2nd step with
      maxBid = MAX(Closes[1], period)[0];

      (in the illustration example it would give
      1.07 = maxBid = MAX(Closes[1], 11 -6)[0]; )


      Code:
      if ( PositionAccount.MarketPosition == MarketPosition.Long )
      {
           {
                int period = CurrentBars[1] - EntryBar;
                EntryBar = CurrentBars[1];
                maxBid = MAX(Closes[1], period)[0];
                ...
      I would normally have put the Entrybar first as follows
      Code:
      if ( PositionAccount.MarketPosition == MarketPosition.Long )
      {
           {
                EntryBar = CurrentBars[1];
                int period = CurrentBars[1] - EntryBar;
                maxBid = MAX(Closes[1], period)[0];
                ...
      to clearly separate the stocked Entrybar single instance CurrentBars[1] value from the "running" CurrentBars[1] in the perio statement that follows.
      But it's not working in that reversed order. I'm not sure where I should put the int period statement elsewhere.

      Sorry for not starting a new thread when i posted initially, I though I needed to ask from previous related thread from Paul's answer.
      Last edited by PaulMohn; 01-12-2022, 05:55 AM. Reason: added: 1.07 = maxBid = MAX(Closes[1], 11 -6)[0]; (illustration example)

      Comment


        #18
        Hello PaulMohn,

        Thanks for your posts.

        If I understand correctly (looking back at the other thread you started on the same subject) you want to find the Maximum bid that occurred between the entry and exit time.

        Rather than save an entry bar or rather than looping back, why don't you just check for it at the time it is occurring?

        From a psuedo code point of view:

        double mystoredmaximumbid = 0.0;

        if (entry is true)
        {
        get bid value

        if bid value is greater than mystoredmaximumbid then save current bid into mystoredmaximumbid
        }
        if (exit it true)
        {
        print "Maximum bid during trade = "mystoredmaximumbid
        }

        Comment


          #19
          Hello Paul and many thanks for that awesome simplest and straightforward solution. And many thanks for the pseudocode too, that was very helpful!
          It's seems to be working as intended upon preliminary testing now.

          How I transcribed your pseudocode
          Code:
          protected override void OnBarUpdate()
          {
               maxBid = 0.0;
          
               //Print(BarsInProgress);
               //Print("2" + " " + Time[0] + " " + maxBid);
          
               if(CurrentBar<2) return;
          
               if(BarsInProgress == 1 || BarsInProgress == 2)
                    return;
          
          if ( PositionAccount.MarketPosition == MarketPosition.Long )
          {
          
               {
                    GetCurrentBid();
          
                    if(GetCurrentBid() > maxBid)
                    {
                         maxBid = GetCurrentBid();
               }
          I didn't know you could/should initialize the mystoredmaximumbid/maxBid to zero. It didn't occur to me. How a brilliant simplest solution! I'm glad to have learn today! Thanks again. That was truly a great solution.

          Out of curiosity, hope it's not too much asking, also to help me understand DataSeries bar indexes behavior more in depth, what was wrong with my other EntryBar Index? What would you do to make it select first Bar index only? I'd rather avoid this second solution as it's obviously less efficient, but I'm curious to understand why it was not working. Also, it was my first time using secondary series so it would help me cement the concept.


          I'll do extended testing and be back.

          Cheers!

          Comment


            #20
            Hello PaulMohn,

            Thanks for your reply.

            I would suggest reviewing the Multi time frame help guide section here: https://ninjatrader.com/support/help...nstruments.htm

            Comment


              #21
              Ok, I'll do that and be back later. Thanks.

              Comment


                #22
                Originally posted by NinjaTrader_PaulH View Post
                Hello PaulMohn,

                Thanks for your posts.

                If I understand correctly (looking back at the other thread you started on the same subject) you want to find the Maximum bid that occurred between the entry and exit time.

                Rather than save an entry bar or rather than looping back, why don't you just check for it at the time it is occurring?

                From a psuedo code point of view:

                double mystoredmaximumbid = 0.0;

                if (entry is true)
                {
                get bid value

                if bid value is greater than mystoredmaximumbid then save current bid into mystoredmaximumbid
                }
                if (exit it true)
                {
                print "Maximum bid during trade = "mystoredmaximumbid
                }
                Hello Paul. I've done more testing and got the following problem.

                The code with prints for
                3 (maxBid before the code executes)
                4 (GetCurrentBid() before the code executes)
                5 (maxBid after the code executes)
                6 (GetCurrentBid() after the code executes)
                Code:
                if ( PositionAccount.MarketPosition == MarketPosition.Long )
                {
                
                          GetCurrentBid();
                
                          Print("4" + " " + Time[0] + " " + GetCurrentBid());
                
                          if(GetCurrentBid() > maxBid)
                          {
                               maxBid = GetCurrentBid();
                          }
                
                Print("5" + " " + Time[0] + " " + maxBid);
                Print("6" + " " + Time[0] + " " + GetCurrentBid());
                As you can see in the prints below the maxBid still gives a value less than the max intended.
                Please see
                5 13/01/2022 14:50:00 82.25
                6 13/01/2022 14:50:00 82.25
                3 13/01/2022 14:50:00 0
                4 13/01/2022 14:50:00 82.24
                5 13/01/2022 14:50:00 82.24
                Code:
                3 13/01/2022 14:49:00 0
                4 13/01/2022 14:49:00 82.24
                [B]5 13/01/2022 14:49:00 82.24[/B]
                6 13/01/2022 14:49:00 82.24
                3 13/01/2022 14:50:00 0
                4 13/01/2022 14:50:00 82.25
                [B]5 13/01/2022 14:50:00 82.25[/B]
                6 13/01/2022 14:50:00 82.25
                3 13/01/2022 14:50:00 0
                4 13/01/2022 14:50:00 82.25
                [B]5 13/01/2022 14:50:00 82.25[/B]
                6 13/01/2022 14:50:00 82.25
                3 13/01/2022 14:50:00 0
                4 13/01/2022 14:50:00 82.24
                [B]5 13/01/2022 14:50:00 82.24[/B]
                6 13/01/2022 14:50:00 82.24
                3 13/01/2022 14:50:00 0
                4 13/01/2022 14:50:00 82.24
                5 13/01/2022 14:50:00 82.24
                6 13/01/2022 14:50:00 82.24
                3 13/01/2022 14:50:00 0
                4 13/01/2022 14:50:00 82.25
                5 13/01/2022 14:50:00 82.25
                6 13/01/2022 14:50:00 82.25
                Why maxBid changes back to the lesser value (from 84.25 to 84.24)?
                I don't understand because the condition states if(GetCurrentBid() > maxBid). So when GetCurrentBid() (labelled 6) gets back to the lesser value (from 84.25 to 84.24) maxBid should stay at 84.25 because it's greater than GetCurrentBid(). What am I missing?
                Last edited by PaulMohn; 01-13-2022, 08:06 AM. Reason: 84.25 to 84.24

                Comment


                  #23
                  Hello PaulMohn,

                  Thanks for your reply.

                  Can you clarify why you are wanting to get the maximum bid price while you are in a position?

                  What are you intending to do with that information?

                  Comment


                    #24
                    Yes. I need it as condition part in later logic statements.

                    Comment


                      #25
                      Hello PaulMohn,

                      Thanks for your reply.

                      I suspect your repeated calls to GetCurrentBid() are returning the bid values in place at that moment. Also, you would need to ensure that you were calling from the appropriate BarsInProgress in your MTF script.

                      You can look for the maximum bid using the data within OnMarketData() itself: https://ninjatrader.com/support/help...marketdata.htm Note that you would also have to isolate by BarsInProgress

                      Alternately to all that, you could go the easy road here and just check the highest price of the bars between the entry and exit. It would be a 1 tick difference at most.
                      Last edited by NinjaTrader_PaulH; 01-13-2022, 10:19 AM.

                      Comment


                        #26
                        Hello PaulMohn,

                        I neglected to clarify concerning OnMarketData(), which is a real time (or playback with market replay) event only so if looking at historical data you would not get values there.

                        Comment


                          #27
                          Yes that's what it seems, it's returning the lesser Bid values too in place at that time in spite of the if(GetCurrentBid() > maxBid) condition stating to return only the greater Bid values. Why doesn't it do as the condition states? How is it still possible to make it work with your simpler previous solution?

                          I've reread the Multi-time Frame & Instruments documentation (related to my last question from yesterday post #19), to look for a way to use the Series Index instead but I'm still not sure what to look for in it.
                          Do you know the proper syntax to isolate the first index of the EntryBar/CurrentBars[1] Series? I don't find it in the Multi-time Frame & Instruments documentation.
                          That other solution would just require getting the EntryBar/CurrentBars[1] index single value. But there too I don't understand how to isolate the first value only.

                          Code:
                          if ( PositionAccount.MarketPosition == MarketPosition.Long )
                          {
                          
                               Print("3" + " " + Time[0] + " " + maxBid);
                               Print("A" + " " + Time[0] + " " + CurrentBars[1]);
                               Print("B" + " " + Time[0] + " " + EntryBar);
                          
                               int period = CurrentBars[1] - EntryBar;
                               Print("C" + " " + period);
                          
                          [B]    EntryBar = CurrentBars[1];[/B]
                          
                               maxBid = MAX(Closes[1], period)[0];
                          
                               maxbidBool = true;
                          
                               Print("4" + " " + Time[0] + " " + maxBid);
                          }
                          I'm calling from the primary bar ( if(BarsInProgress == 1 || BarsInProgress == 2) return; ) and it works to give the Secondary Series values in the prints.
                          I'm not familiar yet with the OnMarketData(). I'd prefer if possible work within the OnBarUpdate.

                          Just read you new note, ok thanks for that precision on OnMarketData() (I'd rather not use if possible).

                          Last edited by PaulMohn; 01-13-2022, 09:53 AM. Reason: OnMarketData() note

                          Comment


                            #28
                            I've also reviewed the CurrentBars documentation but I don't understand how to isolate the first index in my case (post #19).


                            Syntax
                            CurrentBars[int barSeriesIndex]

                            How is it possible with the above syntax?

                            Comment


                              #29
                              Hello PaulMohn,

                              Thanks for your replies.

                              If you are working with historical data values, please note that the getCurrentBid() will " When accessed during State.Historical, the Close price of the evaluated bar is substituted."
                              Reference: https://ninjatrader.com/support/help...currentbid.htm

                              Can you share what specific data series you are working with, including the chart bars?

                              Can you verify what data series you are placing the orders on?

                              Why are you using PositionAccount instead of Position? https://ninjatrader.com/support/help...l?position.htm


                              Regarding, "I've also reviewed the CurrentBars documentation but I don't understand how to isolate the first index in my case (post #19)."

                              The Chart bars are index [0], the first added data series is [1] and the next [2] and so on.

                              Comment


                                #30
                                If you are working with historical data values, please note that the getCurrentBid() will " When accessed during State.Historical, the Close price of the evaluated bar is substituted."
                                I'd like to get the isolated highest value for the maxBid for the issue of post #22 if we can make your simpler solution ignore the lesser values
                                As you can see in the prints below the maxBid still gives a value less than the max intended.
                                Please see
                                5 13/01/2022 14:50:00 82.25
                                6 13/01/2022 14:50:00 82.25
                                3 13/01/2022 14:50:00 0
                                4 13/01/2022 14:50:00 82.24
                                5 13/01/2022 14:50:00 82.24

                                Can you share what specific data series you are working with, including the chart bars?
                                Primary Series = 1min timeframe
                                Secondary Series =
                                Code:
                                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);
                                }
                                Can you verify what data series you are placing the orders on?
                                I assume the orders are placed on the Primary Series since I have
                                if(BarsInProgress == 1 || BarsInProgress == 2)
                                return;
                                Code:
                                protected override void OnBarUpdate()
                                {
                                     maxBid = 0.0;
                                
                                     if(CurrentBar<2) return;
                                
                                [B]if(BarsInProgress == 1 || BarsInProgress == 2)[/B]
                                [B]return;[/B]
                                
                                     if ( PositionAccount.MarketPosition == MarketPosition.Long )
                                     {
                                
                                
                                          GetCurrentBid();
                                
                                          Print("3" + " " + Time[0] + " " + maxBid);
                                          Print("4" + " " + Time[0] + " " + GetCurrentBid());
                                
                                          if(GetCurrentBid() > maxBid)
                                          {
                                               maxBid = GetCurrentBid();
                                          }
                                Why are you using PositionAccount instead of Position?
                                Because I need it with the Unmanaged approach

                                Code:
                                protected override void OnStateChange()
                                {
                                if (State == State.SetDefaults)
                                {
                                     ...
                                
                                     IsUnmanaged = true;
                                
                                if ( PositionAccount.MarketPosition == MarketPosition.Long )
                                {
                                
                                ...
                                
                                     if (initialExitOrderLong == null)
                                     {
                                               initialExitOrderLong = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, PositionAccount.Quantity, 0, PositionAccount.AveragePrice -25*TickSize, "", @"Exit Long FULL STOP" );
                                     }
                                Regarding, "I've also reviewed the CurrentBars documentation but I don't understand how to isolate the first index in my case (post #19)."

                                The Chart bars are index [0], the first added data series is [1] and the next [2] and so on.
                                Sorry I was referring the issue of post #14 and #19 question. About getting the CurrentBars[1] index/bar number of the first bar of the Secondary Series ( the Bid values) at the instant the Long Order is executed. But in post #14 it gives the Series of Indexes from the moment of entry, not the first index only.

                                post #14 issue:
                                Please see prints labelled B (the EntryBar prints)

                                Code:
                                Enabling NinjaScript strategy 'BidAskIncDecr/238743953' : 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 19:24:00 0
                                A 11/01/2022 19:24:00 6
                                [B]B 11/01/2022 19:24:00 0[/B]
                                C 6
                                4 11/01/2022 19:24:00 81.25
                                5 11/01/2022 19:24:00 81.25
                                3 11/01/2022 19:24:00 81.25
                                A 11/01/2022 19:24:00 10
                                [B]B 11/01/2022 19:24:00 6[/B]
                                C 4
                                4 11/01/2022 19:24:00 81.25
                                5 11/01/2022 19:24:00 81.25
                                3 11/01/2022 19:24:00 81.25
                                A 11/01/2022 19:24:00 12
                                [B]B 11/01/2022 19:24:00 10[/B]
                                C 2
                                4 11/01/2022 19:24:00 81.24
                                5 11/01/2022 19:24:00 81.24
                                3 11/01/2022 19:24:00 81.24
                                A 11/01/2022 19:24:00 14
                                [B]B 11/01/2022 19:24:00 12[/B]
                                C 2
                                4 11/01/2022 19:24:00 81.24
                                5 11/01/2022 19:24:00 81.24
                                3 11/01/2022 19:24:00 81.24
                                A 11/01/2022 19:24:00 15
                                [B]B 11/01/2022 19:24:00 14[/B]
                                C 1
                                4 11/01/2022 19:24:00 81.24
                                5 11/01/2022 19:24:00 81.24
                                3 11/01/2022 19:25:00 81.24
                                A 11/01/2022 19:25:00 17
                                [B]B 11/01/2022 19:25:00 15[/B]
                                C 2
                                4 11/01/2022 19:25:00 81.24
                                5 11/01/2022 19:25:00 81.24
                                3 11/01/2022 19:25:00 81.24
                                A 11/01/2022 19:25:00 18
                                [B]B 11/01/2022 19:25:00 17[/B]
                                C 1
                                4 11/01/2022 19:25:00 81.25
                                5 11/01/2022 19:25:00 81.25
                                3 11/01/2022 19:25:00 81.25
                                A 11/01/2022 19:25:00 19
                                [B]B 11/01/2022 19:25:00 18[/B]
                                C 1
                                4 11/01/2022 19:25:00 81.25
                                5 11/01/2022 19:25:00 81.25
                                3 11/01/2022 19:25:00 81.25
                                A 11/01/2022 19:25:00 22
                                [B]B 11/01/2022 19:25:00 19[/B]
                                C 3
                                4 11/01/2022 19:25:00 81.26
                                5 11/01/2022 19:25:00 81.26
                                3 11/01/2022 19:25:00 81.26
                                A 11/01/2022 19:25:00 22
                                [B]B 11/01/2022 19:25:00 22[/B]
                                C 0
                                Value of property 'Period' of NinjaScript 'MAX' is 0 and not in valid range between 1 and 2147483647.
                                Disabling NinjaScript strategy 'BidAskIncDecr/238743953'
                                In post #8 https://ninjatrader.com/support/foru...333#post813333
                                You recommend saving the Bar number.
                                Hello gordongekko,

                                Thanks for your reply.

                                You could if you are determined to do a loop.

                                A more efficient way is to save either the bar number where the highest value was previously found or save the highest value itself into a variable. Saving the bar number allows you to dynamically return to that bar at any time, for example to get the high of that bar High[CurrentBar - savedBar] so you can always use the barsago.

                                What may be a better solution for you is to plan out what information you need and when it is needed and then decide if it can only be done by looping backwards all the time or if you can save the values as they are determined/discovered by going over the historical data once.
                                I'd like to get that same Bar number but instead of savedBar as your post #8 solution I would do the same but with CurrentBars[1] from my AddDataSeries(null, BarsPeriodType.Tick, 1, MarketDataType.Bid); series. But I don't understand how to get that Bar number from the Series since the CurrentBars series returns an array (the multiple values problem from post #14) and not a single index as the CurrentBar does.
                                https://ninjatrader.com/support/help...nstruments.htm
                                Property Value
                                An array of int values.

                                Versus
                                a single int for your savedBar/CurrentBar
                                https://ninjatrader.com/support/help...nstruments.htm
                                Property Value
                                An int value that represents the current bar.

                                I tried before CurrentBars[1][0] 8adding the bars index reference and setting it to zero) to see if it would give the first bar index/single first Bar Number but it's not working.
                                Last edited by PaulMohn; 01-13-2022, 11:06 AM. Reason: Post #14 code issue

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                647 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                367 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                108 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                571 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                573 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X