Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with using the CancelOrder()

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

    Help with using the CancelOrder()

    Hello.

    I have created a simple script to get help with. This is just for demonstrative purposes. My script is making use of several lists, as this replicates my real script, where I hold prices for a bunch of different trades.

    The script looks for an outside bar. Once that outside bar is found, I have Lists that store the following:
    • -Bool, if an order has been placed(HasTriggered)
    • -Int, how many bars have passed since that condition occurred(BarsPassed)
    • -Int, what the bar of that condition was(BarLow)

    You will also notice I have another list(MyEntryOrders). I understand this should be used, with the OnOrderUpdate() function to use CancelOrder(). I saw THIS example in the docs, but I can't quit figure out how to get it to work for my script as I am using the lists to store order info instead of just a single variable.

    The script the loops through the Lists. If no order has been placed for a given bar, an order is placed, and flag HasTriggered is set, this way only one order per entry condition. BarsPassed is incremented.

    The next set of code is unfinished, here you see I say if BarsPassed, cancel the order associated with this portion of the loop.


    Code:
        public class nt8ForumCancelOrderHelp : Strategy
        {
            private List<bool> HasTriggered;         //Flag when entry condition occurrs, so only one entry per condition
            private List<Order> MyEntryOrders;
            private List<int> BarsPassed;            //Count of Bars since outside bar
            private List<int> BarLow;                //Bar Where Entry condition was created

    Code:
                else if (State == State.Configure)
                {
                    HasTriggered        = new List<bool>();
                    MyEntryOrders         = new List<Order>();
                    BarsPassed            = new List<int>();
                    BarLow                = new List<int>();
                }

    Code:
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 2)
                    return;
    
    
                if(High[0] > High[1] &&Low[0] < Low[1])
                {
                    Draw.TriangleUp(this,"lowBaseBarTR"+CurrentBar, false, 0, Low[0] - 0*TickSize, Brushes.Yellow);
                    HasTriggered.Add(false);    //Has order been places
                    BarsPassed.Add(0);            //How many bars have passed since condition
                    BarLow.Add(CurrentBar);        //What was the bar number of the condition
                }
    
                for (int i=0; i < HasTriggered.Count; i++) //Loop through the lists to check conditions
                {
                    BarsPassed[i]++;    //increment Bars passed
    
                    if(!HasTriggered[i])    //If no order for that condition has been placed
                    {
                        EnterLongLimit(0, true, Convert.ToInt32(DefaultQuantity), Low[CurrentBar-BarLow[i]], "LongLimit");    
                        HasTriggered[i]=true; //Only one entry per condition
                    }
    
                    //Want to figure out how to cancel here?
                    if( BarsPassed[i] > 10)
                    {
    //                    CancelOrder("LongLimit");    
                    }
    
                }
           }
    Attached Files
    Last edited by forrestang; 02-07-2020, 07:14 AM.

    #2
    Hello forrestang,

    Thank you for the post.

    I reviewed the information here however I am not certain what your specific question is, are you asking how to use the method CancelOrder() specifically?

    We have a working sample of using CancelOrder here: https://ninjatrader.com/support/help...thod_to_ca.htm

    It looks like in your sample you are using a string with CancelOrder however you would instead need to use one of the Order object you saved that represents the order you wanted to cancel. From a list that would likely look like CancelOrder(MyEntryOrders[0]) or the index of the order you wanted to cancel .

    I could also suggest to simplify your test to a single order for the time being to make sure you can cancel that order successfully, once you do that doing other looping or repetition of the code is much more simple.


    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    53 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X