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

Track/Store Max Bid Value Since Entry

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

    #31
    I don't understand what you mean by "changing the math you used". What other math than
    Code:
    maxBid = MAX(Closes[1],CurrentBars[1]-EntryBar)[0];
    How would you suggest changing the prints?

    Ah, I think you mean printing CurrentBars[1] and Entrybar separately.
    I'll test that. Thanks.


    Ok, it's printing CurrentBars[1], but not printing EntryBar (prints 0)
    Code:
    Enabling NinjaScript strategy 'BidAskIncDecr/238743945' : 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 18:42:00 0
    A 11/01/2022 18:42:00 49
    B 11/01/2022 18:42:00 0
    Value of property 'Period' of NinjaScript 'MAX' is 0 and not in valid range between 1 and 2147483647.
    Disabling NinjaScript strategy 'BidAskIncDecr/238743945'
    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);
                   Print("A" + " " + Time[0] + " " + CurrentBars[1]);
                   Print("B" + " " + Time[0] + " " + EntryBar);
                   EntryBar = CurrentBars[1];
                   maxBid = MAX(Closes[1],CurrentBars[1]-EntryBar)[0];
                   maxbidBool = true;
                   Print("4" + " " + Time[0] + " " + maxBid);
              }
    
              Print("5" + " " + Time[0] + " " + maxBid);
    Why isn't it returning the EntryBar value?
    Last edited by PaulMohn; 01-11-2022, 11:45 AM.

    Comment


      #32
      Hello PaulMohn,

      Why isn't it returning the EntryBar value?
      I wouldn't be able to tell from this, you would need to review how you are setting the value in your logic.

      I would suggest to try and make a variable for the period and make sure the negative sign has a space between and EntryBar in your code, please try the following:


      Code:
      int period = CurrentBars[1] - EntryBar;
      Print(period);
      JesseNinjaTrader Customer Service

      Comment


        #33
        Ok, thanks for that.

        It's printing some value now:

        Code:
        Enabling NinjaScript strategy 'BidAskIncDecr/238743948' : 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:07:00 0
        A 11/01/2022 19:07:00 20
        B 11/01/2022 19:07:00 0
        C 20
        Value of property 'Period' of NinjaScript 'MAX' is 0 and not in valid range between 1 and 2147483647.
        Disabling NinjaScript strategy 'BidAskIncDecr/238743948'
        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);
                    Print("A" + " " + Time[0] + " " + CurrentBars[1]);
                    Print("B" + " " + Time[0] + " " + EntryBar);
                    int period = CurrentBars[1] - EntryBar;
                    Print("C" + " " + period);
                    EntryBar = CurrentBars[1];
                    maxBid = MAX(Closes[1],CurrentBars[1] - EntryBar)[0];
                    maxbidBool = true;
                    Print("4" + " " + Time[0] + " " + maxBid);
                 }
        
                 Print("5" + " " + Time[0] + " " + maxBid);

        but now I've tested period instead but it's still returning the not valid range error after some successful prints:

        Code:
        Enabling NinjaScript strategy 'BidAskIncDecr/238743952' : 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:16:00 0
        A 11/01/2022 19:16:00 5
        B 11/01/2022 19:16:00 0
        C 5
        4 11/01/2022 19:16:00 81.31
        5 11/01/2022 19:16:00 81.31
        3 11/01/2022 19:16:00 81.31
        A 11/01/2022 19:16:00 6
        B 11/01/2022 19:16:00 5
        C 1
        4 11/01/2022 19:16:00 81.31
        5 11/01/2022 19:16:00 81.31
        3 11/01/2022 19:16:00 81.31
        A 11/01/2022 19:16:00 6
        B 11/01/2022 19:16:00 6
        C 0
        Value of property 'Period' of NinjaScript 'MAX' is 0 and not in valid range between 1 and 2147483647.

        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);
                       Print("A" + " " + Time[0] + " " + CurrentBars[1]);
                       Print("B" + " " + Time[0] + " " + EntryBar);
                       int period = CurrentBars[1] - EntryBar;
                       Print("C" + " " + period);
                       EntryBar = CurrentBars[1];
                       maxBid = MAX(Closes[1],period)[0];
                       maxbidBool = true;
                       Print("4" + " " + Time[0] + " " + maxBid);
                  }
        
                  Print("5" + " " + Time[0] + " " + maxBid);

        I tested again and for some reason the period value becomes 0 after a few prints.
        Why is that?

        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 11/01/2022 19:24:00 0
        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 11/01/2022 19:24:00 6
        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 11/01/2022 19:24:00 10
        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 11/01/2022 19:24:00 12
        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 11/01/2022 19:24:00 14
        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 11/01/2022 19:25:00 15
        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 11/01/2022 19:25:00 17
        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 11/01/2022 19:25:00 18
        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 11/01/2022 19:25:00 19
        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 11/01/2022 19:25:00 22
        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'
        Ok, the CurrentBars[1] value (the A in the prints) seems correct as it keeps increasing.
        but I don't understand why the EntryBar value (the B in the prints) changes? Why doesn't it stay the same? as it's supposed to capture the single int from the Entry/Long trade event/moment only.
        Last edited by PaulMohn; 01-11-2022, 12:58 PM. Reason: CurrentBars seems ok, but the EntryBar changes when it shouldn't

        Comment


          #34
          A 11/01/2022 19:25:00 22
          B 11/01/2022 19:25:00 22
          C 0
          You can use your prints to see the problem, 22 minus 22 is 0. You would need to make sure this math does not equal 0. As I don't know what your overall goal is I can only suggest what the error also suggests, use a value that is greater than 0 to avoid the error.

          If your math comes out to 0 you may need to add a condition to use 1 instead as a minimum or figure out other math that equals a 1 or greater number and works for the goal you had intended.

          Why doesn't it stay the same? as it's supposed to capture the single int from the Entry/Long trade event/moment only.
          You would need to review where you set that value and see why that's happening, that's really outside of the scope of the question. The error is specifically what the error message is saying or that you used a invalid value of 0. The solution to the error is don't use 0 as the value for the period because that wont work. You will need to review the logic you use with EntryBar if that should not be changing.



          JesseNinjaTrader Customer Service

          Comment


            #35
            Thanks for the new answer. According to Paul the EntryBar value should not be changing (the int value/Bar (BarsAgo numbering scheme) of the EntryBar should be unique and immutable). Please see below Paul's answers.

            Why does it work for Paul but not in my case? I can't see the difference. Do you see the difference between my EntryBar and Paul's savedBar?
            Oh I got an idea, is the difference due to the CurrentBars[1] (lacking Bar index)? Is there a way to get the immutable value with the series version of currentBars[1] in

            Code:
            EntryBar = CurrentBars[1];
            Would adding the specific index get the unique and immutable value from the series?
            Code:
            EntryBar = CurrentBars[1][0];
            That's giving this error:
            Cannot apply indexing with [] to an expression of type 'int' CS0021 109 17


            NinjaTrader_PaulH

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

            https://ninjatrader.com/support/foru...317#post813317

            https://ninjatrader.com/support/foru...344#post813344

            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.
            If you want to capture the highest close during a period while S>80 it seems like you have most of what you need with just:
            Code:
            MAX(MACD(12, 26,9), CuurentBar - firstS80bar)[0]; // lookback is the period that S>80
            https://ninjatrader.com/support/foru...325#post813325

            And also:
            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");
            Last edited by PaulMohn; 01-12-2022, 02:10 AM. Reason: Error index to int EntryBar

            Comment


              #36
              Hello PaulMohn,

              I wouldn't be able to comment on the other forum posts You could follow up in those threads if you have a specific question about some information contained in those threads.

              You can otherwise use prints to track down problems in your own code. Debugging is not a service that our support can provide so I would not be able to run/test your script to find out why it is working in a specific way.



              JesseNinjaTrader Customer Service

              Comment


                #37
                Hello Jesse and thank you for the reply and advice and prior help. You've been helpful. Paul suggested a really simple workaround that did solve the issue in a simpler way. Thanks again for your appreciated help. Have a great day!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by tony_28217, Today, 07:04 PM
                0 responses
                3 views
                0 likes
                Last Post tony_28217  
                Started by flybuzz, Today, 10:33 AM
                1 response
                9 views
                0 likes
                Last Post flybuzz
                by flybuzz
                 
                Started by spencerp92, 10-10-2023, 09:56 AM
                4 responses
                304 views
                0 likes
                Last Post flybuzz
                by flybuzz
                 
                Started by samish18, Yesterday, 10:13 AM
                1 response
                25 views
                0 likes
                Last Post NinjaTrader_Eduardo  
                Started by Austiner87, Today, 05:02 PM
                0 responses
                7 views
                0 likes
                Last Post Austiner87  
                Working...
                X