No announcement yet.

Partner 728x90


Keeping Orders "alive"

  • Filter
  • Time
  • Show
Clear All
new posts

    Keeping Orders "alive"


    I understand (some kinds of ?) orders cancel on the next bar unless renewed. I've read many posts and good answers - mostly referring to Samples - CancelOrders etc. which I've also studied.

    My strategy - (all working)
    All Orders filled, StopLosses working, trailStops working
    Unique labelled entries, 3 in each direction with different trailstop levels
    All FINE! BUT only when I use: EnterLong() or Short()

    Strategy Consists of:
    Initialise and OnBarUpdate() method leading into
    Condition 1 - EnterLong OR
    Condition 2 - EnterShort

    Change to: EnterLongStop or EnterShortStop
    and only Orders where the next bar hits the stop - are filled.
    Log shows all other orders CANCELLED.

    I dont understand why standard EnterLong and Short Orders stay LIVE - and EnterStop orders do not - by default. Bit odd to me.

    I see that I can use long code to keep EnterLongStop live - but it seems to make no difference.

    After reviewing your Sample CloseOrders and IORders etc I see your samples have simple conditions contained in one set of brackets. I have some 50 lines of code for each of my two Conditions.

    If I am on the right track, I think my dilemma is:
    WHERE do I enter all this code for my Conditions 1 & 2
    in amongst the CancelOrder coding?

    AFTER the OnBarUpdate part? and Before OnOrderUpdate IORder?
    After ALL of the IOrder part?
    Or somewhere else?
    Or am I on the wrong track?

    Your assistance much appreciated.


    Hello CaptChris,

    Welcome to our forums.

    I believe you're after the liveUntilCanceled property and this can be set by using that overload for the EnterLongStop() and EnterShortStop() methods.
    EnterLongStop(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double stopPrice, string signalName)
    EnterShortStop(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double stopPrice, string signalName)
    By default orders will cancel if not filled on the bar they're submitted on. The difference is that when you specify the stop price there's an extra condition that must be met before you'll get a fill. If you have liveUntilCancelled set to false and the stop price isn't reached, then your order will be canceled.
    Ryan M.NinjaTrader Customer Service



      Thanks so much for the prompt reply.

      I tried the "long" code earlier for EnterLongStop() - with bool liveUntilCancelled = true, without success.

      I think it is the "int barsInProgressIndex" causing mischief.
      Cant trace any definition for this guy.
      Tried 0, 1,10,100, etc

      Can you tell me, please:
      What is meant by barsinProgressIndex,
      is this a "protected int",
      what does it do and w
      here do I get the integer info from?

      (I thought it might count the number of bars forward you intend the order to prevail?) A wee guidance would help me understand its purpose, thanks.

      And should this line of code be placed at the bottom of each conditional loop?
      (Like my EnterLong() and EnterShort() are, where these DO remain ALIVE?)



        Hello CaptChris,

        Please see the help guide article below on BarsInProgress.

        This is used to define the context you're working in when using multieries strategies. If you only have one series, then the only valid BarsInProgress value will be 0.

        Your orders belong within brackets after your conditional statements.
        if (yourCondition)
        Ryan M.NinjaTrader Customer Service


          Separating segments within Code

          Thanks Ryan!

          The only integer I didnt try = 0 !!!!
          Now I have electric results!!

          Just one more point, please.
          From you answer:

          if Condition

          orders etc
          Using a goto instruction ()cant find definitions on that)
          I would like to come into this ORDER segment from another point in the code.

          Can you point me to HOW do I name and separate THIS and the OTHER, to enable goto SOMWHERE ?
          Line number ?
          Area separate by - what - for example?

          Much appreciated!


            Hi CaptChris,

            C# does not have GoTo instructions. It "listens" for events to happen and then processes the commands within the event.

            You can access order states through IOrder objects.

            Let us know what you're trying to do and we'll do our best to indicate the appropriate C# or NinjaScript equivalent.
            Ryan M.NinjaTrader Customer Service


              Understoo Ryan,

              Here's the rub....

              if (condition 1)
              // do loads of stuff and finally place an order
              60 lines of code here giving numerous variables different values - valuable info I need later for displaying Text on screen
              EnterLongStop - for example.

              then if (Condition 2)
              // loads more stuff = all working electrically I should add :-)
              EnterShortStop etc

              Now I want to introduce a further Condition - on the NEXT BAR UPDATE.
              All my variables in the last bar are efectively lost/zeroed.
              I was hoping to be able to use those same variables' values - from the last loop/last bar, in this next bar.

              if (condition 1 ) results in Place Order after CrossAbove: Hi = 4.35

              if (NEW condition 3) short term trend line implies: GTFO
              if Hi > 435 ClosePostions for example.

              returns 'x' unknown in this context.

              Not making sense? You wonna try being this end!! :-)

              There must be a simpler way?



                Hi not known in this context!


                  Hi CaptChris,

                  I believe it's a matter of tracking the states of variables. First consideration here is to define them in the variables region. If they're defined there, then you should be able to access them anywhere in the OnBarUpdate() method.

                  Next consideration is the actual values of these variables. To evaluate this, add Print(variable); statements at various points in the code to make sure the values are what you expect.
                  Ryan M.NinjaTrader Customer Service


                    Hi Ryan
                    I have print statements at ALL outcomes and all are amazingly correct.
                    I tried defining in the Variables Region but that re-sets their values to the input or default - which is of no use.

                    Is there a way I can call up the THIS BAR value of a dynamicTrailStop executed and re-executed (I now see on every bar update) since its origin a few bars before?

                    Those are the kinda values I'm trying to retrieve.



                      Hi Ryan,

                      Or is it just my code, resetting those variable values on each loop, I'm wondering?

                      Can I use a next instance of a defined variable in the OnBarUpdate() segment - and it would have retained a value after passing thru conditions in an earlier loop? I didnt think so.


                        Hi CaptChris,

                        It sounds like you want to use Arrays, which is possible in C# but outside of NinjaScript support.

                        You can also create DataSeries, which allow you to access values indexed to every bar in the series.

                        The tutorial below helps with creating a DataSeries based off the Range (high - low)

                        Ryan M.NinjaTrader Customer Service


                        Latest Posts


                        Topics Statistics Last Post
                        Started by JTizz, Today, 01:20 PM
                        1 response
                        Last Post NinjaTrader_Jesse  
                        Started by omermirza, Today, 12:39 PM
                        1 response
                        Last Post NinjaTrader_Jim  
                        Started by mballagan, Today, 01:02 PM
                        0 responses
                        Last Post mballagan  
                        Started by larkmail, Today, 12:30 PM
                        1 response
                        Last Post NinjaTrader_Kate  
                        Started by Danila, Today, 11:08 AM
                        1 response
                        Last Post NinjaTrader_Jesse