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

Cancel order loop logic not cancelling entry orders.

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

    Cancel order loop logic not cancelling entry orders.

    The following simple code does not cancel the working order myEntryOrderTESTTRADELIMIT + a loop created variable integer.
    The code does not cancel TEST TRADE LIMIT0 to TEST TRADE LIMIT9. Does myEntryOrder.Name not pick these order names up? Regards and many thanks.


    if (BarsInProgress == 19
    && Positions[19].MarketPosition == MarketPosition.Flat
    && CurrentBars[0] > barNumberOfOrderTEST + 1)
    {

    for (int gcdlsc = 0, gcdlscx = 10; gcdlsc < gcdlscx; gcdlsc++)
    {
    if (myEntryOrder.Name == "TEST TRADE LIMIT"+ gcdlsc)
    {
    CancelOrder(myEntryOrder);
    }
    }
    }

    #2
    Hello elliot5,

    Thank you for the post.

    From the given information I couldn't really say what may be happening other than it looks like you are only ever trying to cancel a single order.

    Does myEntryOrder.Name not pick these order names up?
    I can't tell how you are managing the myEntryOrder from this small of a sample. The myEntryOrder variable looks to be an order object which would generally be managed from OnOrderUpdate or OnExecutionUpdate. The Name would represent the order which was set to the variable. The variable can contain only 1 order object at a time.

    if you are trying cancel any of the 10 orders you very likely should have saved 10 separate variables for the 10 separate orders instead.

    You very likely would want to avoid a loop here and use individual objects so you can just check each:


    Code:
    if (myFirstEntry!= null)
    {
    CancelOrder(myFirstEntry);
    // myFirstEntry can be set back to null in OnOrderUpdate based on the cancel
    }
    if (mySecondEntry != null)
    {
        CancelOrder(mySecondEntry);
        // mySecondEntry can be set back to null in OnOrderUpdate based on the cancel
    }
    etc


    The above would assume that each order is also set from OnOrderUpdate after its submitted/working.
    ​​​​​​​

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Yes each order is set from OnOrderUpdate via a loop to break up the order size into single lots. You can see that I am trying to loop through the order names to cancel them, and of course the number of lots varies with each trade. Regards

      Comment


        #4
        Hello elliot5,

        From the sample I cannot tell what you are overall doing.

        The CancelOrder in your sample only references a single order object. If you looped in OnOrderUpdate and assigned myEntryOrder in that loop it will be the last order in the loop. This would represent only 1 order.

        The loop will iterate 10 times and 1 of those 10 iterations will equal myEntryOrder's name. The other 9 will never be true because myEntryOrder is a single order with 1 name.

        For a loop to be used like this myEntryOrder would need to represent a collection. You could then check if any of the orders in that collection has the current loops name and pick that order to cancel it. The alternative is like I had shown and not use a loop and use 10 variables. If you have a variable amount of orders you would likely be better off using a collection like a List<Order>.

        I look forward to being of further assistance.






        JesseNinjaTrader Customer Service

        Comment


          #5
          Ok thanks - How can I access the list of current orders?

          Comment


            #6
            Hello elliot5,

            The list of current orders would be something you need to make. Your script by default does not track any list of orders. Similar to how you had tracked the single order myEntryOrder you would instead need to track All of the orders.

            If you have a fixed quantity of orders that is always submitted the logic for this would be much more simple by using 10 individual order variables. If its dynamic you would need to build a list from OnOrderUpdate/OnExecutionUpdate.

            That process would involve
            Making a private variable of List<Order>
            Create a new empty list from OnStateChange.SetDefaults and assign it to the private variable.
            From OnOrderUpdate, check if the order which was passed in is in the list, if not add it.
            From OnOrderUpdate check if orders are being cancelled, if they are check if it was in the list and remove it if so
            From OnExecutionUpdate check if the orders are filled and if so remove them from the list.

            That would set it up for being able to use the list in other locations, for example where you wanted to cancel orders. You could then loop over the list and just cancel each order without using any name checking. Any orders in the list are not filled or cancelled so they could be sent to be cancelled.

            If you are not familiar with lists I would suggest to look at some basic C# examples/materials surrounding using lists to become familiar.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              I have created the list and the following code works in modifying the stop/profit target levels on that list

              {
              foreach(Order order in orderList)
              {
              SetStopLoss("ES SHORT LIMIT MICRO" + esuhsem, CalculationMode.Price, CurrentAskESuhuchSHORT + 0.5, false);
              SetProfitTarget("ES SHORT LIMIT MICRO" + esuhsem, CalculationMode.Price, CurrentBidESuhuchSHORT - 0.5);
              gapEStradetoday = false;
              }
              }


              I am trying to achieve the cancel by looping through the names on the list - in the above example I can access the names via the "SET" managed order route. How do I achieve a cancel? The below code is not cancelling orders on the list. A little help appreciated. Regards

              {
              foreach(Order order in orderList)
              {
              if( order.Name == "ES SHORT LIMIT" + esuhsc)
              CancelOrder(order);
              }
              }


              Comment


                #8
                Hello elliot5,

                Are you trying to check for certain orders? For example ES SHORT LIMIT 1, 3 and 5?

                If you mean to cancel all of the orders you don't need to check the name.

                If you mean to check for certain orders you would need to do it similar to what you have. The esuhsc variable would not be incremented by the foreach loop, is that being incremented by an outer for loop?

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Yes there is an outer loop - see below. I want to check for specific orders as you mention. The if(order.Name on the second to last line is perhaps the problem. in fact with debugging I have found that the second to last line causes "object reference error".



                  if (BarsInProgress == 1
                  && Positions[1].MarketPosition == MarketPosition.Flat
                  && CurrentBars[0] > barNumberOfOrderES + 2
                  && myEntryOrderESSHORT != null)
                  {
                  for (int esuhsc = 0, esuhscx = intSizeESgapES; esuhsc < esuhscx; esuhsc++)
                  {
                  if (BarsInProgress == 1
                  && Positions[1].MarketPosition == MarketPosition.Flat
                  && CurrentBars[0] > barNumberOfOrderES + 2)
                  {
                  foreach(Order order in orderList)
                  {
                  if( order.Name == "ES SHORT LIMIT" + esuhsc)
                  CancelOrder(order);
                  }
                  }
                  }
                  }
                  Last edited by elliot5; 03-16-2021, 03:47 AM.

                  Comment


                    #10
                    Hello elliot5,

                    If you are getting an error you would need to correct that before continuing. The error you mentioned means something being used is null. You could try commenting out your code to find which line is throwing that error.

                    To find what is happening with your loop and cancel you would need to use a print and make sure that is happening as expected

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      I have done this and the error stems from that second to last line in the loop. Is the methodology in that loop correct

                      Comment


                        #12
                        Hello

                        Which line are you referring to specifically? did you mean this line?

                        Code:
                        if( order.Name == "ES SHORT LIMIT" + esuhsc)
                        If so that would indicate the order was null.

                        I look forward to being of further assistance.

                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Yes it is that line. There is already a check on the loop for that -

                          "&& myEntryOrderESSHORT != null*)

                          Comment


                            #14
                            Hello elliot5,

                            myEntryOrderESSHORT is only one of the orders, your loop is covering multiple orders which one of them was null.

                            You would need another null check on order

                            Code:
                            if(order != null)
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Ok so the above check would test for the null status of "ES SHORT LIMIT1" for example?
                              Added the code but get error "is a type but is used as a variable"

                              if (BarsInProgress == 1
                              && Positions[1].MarketPosition == MarketPosition.Flat
                              && CurrentBars[0] > barNumberOfOrderES + 2
                              && myEntryOrderESSHORT != null)
                              {
                              for (int esuhsc = 0, esuhscx = intSizeESgapES; esuhsc < esuhscx; esuhsc++)
                              {

                              if (BarsInProgress == 1
                              && Positions[1].MarketPosition == MarketPosition.Flat
                              && CurrentBars[0] > barNumberOfOrderES + 2
                              && Order!= null)
                              {
                              foreach(Order order in orderList)
                              {
                              if( order.Name == "ES SHORT LIMIT" + esuhsc)
                              CancelOrder(order);
                              gapEStradetoday = false;
                              }
                              }
                              }
                              }
                              Last edited by elliot5; 03-16-2021, 09:46 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by benmarkal, Yesterday, 12:52 PM
                              3 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by helpwanted, Today, 03:06 AM
                              1 response
                              17 views
                              0 likes
                              Last Post sarafuenonly123  
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              244 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Working...
                              X