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

Trying to Cancel order after X# of bars order not filled

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

    Trying to Cancel order after X# of bars order not filled

    Greetings!

    I finally had a day where I could see this happening more than once, and in one instance it was very obvious (ie many bars order not filled), so I thought it would be good to review/fix. MBTrading, SIM with automated strategy, NT7R1, Wint7 32bit. No other data feed. This happened about 6 hours into todays session. I do not use OnOrderUpdate

    The following code is in the OnBarUpdate to place an order (cut down as much as possible):
    //
    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 100 || CurrentBars[1] < 100)
    return;
    {
    if (BarsInProgress == 0)

    { if ( (Position.MarketPosition == MarketPosition.Flat) && (timeSeries[0] >= 1) )
    // Try to go LONG
    { if ( // all conditions code is here )
    //
    { // if there is open order then cancel it and try again
    if(iOrderL1 != null)
    CancelOrder(iOrderL1);
    iOrderL1 = null;
    //
    SetStopLoss(CalculationMode.Ticks, tVars.cStopLossTicks * vTickMult);
    // Enter Long
    if ((iOrderL1 == null || Historical) && iQuantity1 != 0)iOrderL1 = EnterLongLimit(0, true, iQuantity1, Closes[0][0], sENTRY1L);
    //
    MyEntryBar = CurrentBar;
    return;
    }
    }
    }
    The 'MyEntryBar' is used to track #of bars since order was placed. The following code is in the OnExecution part of the strategy:

    protected override void OnExecution(IExecution execution)

    { if (iOrderL1 != null)
    {
    if (iOrderL1.Token == execution.Order.Token)
    {// PENDING ORDERS (PendingSubmit / Working / Accepted)
    if (
    execution.Order.OrderState == OrderState.PendingSubmit ||
    execution.Order.OrderState == OrderState.Working ||
    execution.Order.OrderState == OrderState.Accepted
    )
    // ------------ other order code is installed here ---------- //
    // Not Filled Order Response on 1st bar after orderbar ?
    if (execution.Order.OrderState != OrderState.Filled && CurrentBar == MyEntryBar+1 )
    {

    if(iOrderL1 != null)
    Print("Failed to fill L1 order. Time of bar="+Time[0]+", CurrentBar#="+CurrentBar);
    }
    // Not Filled Order Response after 1 bars ?
    //----------------------------
    if (execution.Order.OrderState != OrderState.Filled && CurrentBar > MyEntryBar+1 )
    {

    if(iOrderL1 != null)
    CancelOrder(iOrderL1);
    Print("Failed to fill L1 CANCEL #1 Time of bar="+Time[0]");
    //clear the order variable
    iOrderL1 = null;
    }
    // ------------ other order code is installed here ---------- //

    The following code is in the structures:
    //_______________________
    public struct TradeStruct_EnterLongLimitV07F
    { public int enterLong;
    public double cStopLossTicks;
    }
    //
    private const string sENTRY1L = "e1L";
    private const string sTARGET1 = "eT1";
    //
    private const string sSTOPLOSS = "eSL";
    // ** STRATEGY CALC VARIABLES
    private TradeStruct_EnterLongV07F tVars;
    double vLosses, vWins;
    double cAsk, cBid, cSpread;
    private bool strategyInitialized = false;
    // ** INDIE CALC VARIABLES
    private Font vFont;
    private bool init = false;
    //
    private int MyEntryBar = 0;
    private DataSeries timeSeries;
    // -----and so on ------------- //

    There is no print output in the output box to show that the code is in use for the monitoring of the #of bars since unfilled order was placed, and the order stays outstanding until filled. Not exactly what I enjoy seeing!!!

    Do I have code in the wrong section? Should ' MyEntryBar ' be a double ? (if so, how to increment a double each bar?) Should there be another entry of some kind in the OnInitialize ??

    Other than this the code runs fine: no compile issues.

    Help is appreciated!
    Jon

    ps attach snippets from the 830am time period showing that the order print statements are there but not the prints from the OnExecution
    Attached Files
    Last edited by Trader.Jon; 02-17-2011, 05:41 PM.

    #2
    Jon,

    OnExecution() is not going to evaluate your code on every single bar update. OnExecution() only triggers when an execution event is received. Just because a new bar is being built does not mean you will get an OnExecution(). Your logic looks to be bar driven and as such you should move it back to OnBarUpdate() where you are guaranteed events based on bars.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      OOpps

      Originally posted by NinjaTrader_Josh View Post
      Jon,

      OnExecution() is not going to evaluate your code on every single bar update. OnExecution() only triggers when an execution event is received. Just because a new bar is being built does not mean you will get an OnExecution(). Your logic looks to be bar driven and as such you should move it back to OnBarUpdate() where you are guaranteed events based on bars.
      Thanks, Ill try that [slap side of my head]
      Last edited by Trader.Jon; 02-17-2011, 05:45 PM.

      Comment


        #4
        You're welcome. Let us know should you need further assistance.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh,

          Is this valid in OnBarUpdate as it would be in OnExecution (asking due to the 'execution' in format)

          if (execution.Order.OrderState != OrderState.Filled && CurrentBar == MyEntryBar+1 )

          Thanks

          ps will give this a try
          (Position.MarketPosition == MarketPosition.Flat) && (timeSeries[0] >= 1) && CurrentBar == MyEntryBar+1
          Last edited by Trader.Jon; 02-17-2011, 06:35 PM.

          Comment


            #6
            Jon,

            That would not work because "execution" is not an object that exists in OnBarUpdate() by default. Instead though, you can check the IOrder object of the order you are looking for instead of the execution.Order.

            Something like this:
            Code:
            if (myOrder != null && myOrder.OrderState != OrderState.Filled && CurrentBar > myEntryBar + 1)
                 CancelOrder(myOrder);
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Josh,

              I tried, and only partly successful ... can you please have a look at the follow-on thread please?


              Thanks,
              Jon

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by futtrader, Yesterday, 01:16 PM
              1 response
              19 views
              0 likes
              Last Post NinjaTrader_Eduardo  
              Started by lorem, 04-25-2024, 09:18 AM
              8 responses
              27 views
              0 likes
              Last Post lorem
              by lorem
               
              Started by junkone, Today, 02:19 PM
              3 responses
              33 views
              1 like
              Last Post junkone
              by junkone
               
              Started by Karado58, 11-26-2012, 02:57 PM
              9 responses
              14,846 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by Haiasi, 04-25-2024, 06:53 PM
              3 responses
              28 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Working...
              X