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

Pulling the Bar Count from a Series event satisfying a condition

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

    Pulling the Bar Count from a Series event satisfying a condition

    Let's say I'm running a function on a series such as:

    LowSeries[0] = Low[0];
    MIN1 = MIN(LowSeries,22)[0];

    I'd like to know how many bars back from CurrentBar was the LowSeries event that established MIN1.

    I can write code that goes back sequentially through the last 22 bars and find the answer, but it seems there's gotta be a more simple and elegant way to do this with Ninjascript.

    But I haven't figured it out. Any thoughts?

    #2
    Hello Doctor JR,

    Thanks for your post.

    You could use the LowestBar() method.



    Here is the complementary method: https://ninjatrader.com/support/help...highestbar.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Oh my. So simple. Thanks.

      Comment


        #4
        Paul,

        Another item in search of a simple solution:

        I want to be aggressive in cancelling unfilled orders Instead of waiting for one bar to complete (which works fine when that's what I code) i want to cancel a unfilled long order if price moves 4 ticks above the entry price. Here is the code representing my most recent failure at accomplishing this:

        if (Position.MarketPosition == MarketPosition.Flat && entryOrderLONG != null
        && CurrentBar == LastTradeCount && Close[0] -4*TickSize == Close[1])
        { CancelOrder(entryOrderLONG);

        "LastTradeCount" is the number of the bar from which the unfilled trade was initiated at bar close.

        But this freaks out Market Replay. Seems like there should be a way to cancel orders other than waiting for a full bar to complete. Any thoughts?

        Comment


          #5
          Hello Doctor JR,

          Thanks for your reply.

          For future reference when you have a question that is not related to the thread title we ask that you create a new thread as this helps everyone searching for the same type of questions. Thanks for your understanding.

          "But this freaks out Market Replay. " I do not know what that means, please clarify what you see happening.
          Do you see any errors produced in the "Log" tab of the NinjaTrader control center?

          I am assuming you are running the strategy with Calculate.OnPriceChange or Calculate.OnEachTick, if not you would need to be in order to meet the criteria intrabar.

          It does not look like you are using the actual entry price. You can use Position.AveragePrice (unless you have multiple entries). Also, I would not use == but instead use >= or <= as the case may be, something like Close[0] >= Position.AveragePrice+4 * TickSize, where the assumption is that Close[0] is the current price (based on the calculate setting).
          References:

          ​​​​​​​https://ninjatrader.com/support/help...erageprice.htm

          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the response and your future reference is noted.

            Market Replay is still freaking out over it. To put it more quantitatively, Market Replay still freezes while my log continuously asserts the the order is a working instrument. When I stop Market Repay I get the message "There is no market data available to drive the simulation engine. affected order Buy ......................"

            So maybe it's a Market Replay thing and this would work OK in the live market, but I'd like to be able to run simulations.

            Comment


              #7
              Hello Doctor JR,

              Thanks for your reply.

              Playback is limited to 1 second intervals and if you have a bunch of ticks they will be processed in order which suggests that your cancel condition may be true many many times at once and this could be the freaking out part.

              For test purposes, create a bool variable that is set to true by default and add a check of the bool == true in your conditions and in the cancel order section, set the bool false so that the cancel is sent only once. You will need a reset for the bool which you could do once per bar by resetting the bool back to true when IsFirstTickofBar is true.

              Reference: https://ninjatrader.com/support/help...ttickofbar.htm
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Sounds like a rational plan. I'll get back to you. Thanks.

                Comment


                  #9
                  It certainly was the first tick of the new bar that was causing Market Replay to behave oddly. Inserting the IsFirstTickofBar code seems to have done the trick. Time will tell if the concept was a good idea. Thanks again.

                  Comment


                    #10
                    Hello Doctor JR,

                    Thanks for your reply.

                    Just to be clear the IsFirstickOfBar is used to reset the user-created bool on the next bar. It is the user-created bool being set to false that is protecting your code from resubmitting the cancel order repetitively within the bar.

                    Have a great weekend.
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by love2code2trade, Yesterday, 01:45 PM
                    4 responses
                    27 views
                    0 likes
                    Last Post love2code2trade  
                    Started by funk10101, Today, 09:43 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post funk10101  
                    Started by pkefal, 04-11-2024, 07:39 AM
                    11 responses
                    37 views
                    0 likes
                    Last Post jeronymite  
                    Started by bill2023, Yesterday, 08:51 AM
                    8 responses
                    44 views
                    0 likes
                    Last Post bill2023  
                    Started by yertle, Today, 08:38 AM
                    6 responses
                    26 views
                    0 likes
                    Last Post ryjoga
                    by ryjoga
                     
                    Working...
                    X