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

Orders getting cancelled and submitted alternate bars

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

    Orders getting cancelled and submitted alternate bars

    I have a strategy that looks something like this.
    for

    onbarupdate below is the code.:

    if(BarsInProgress != 0)
    {
    // order active for 1 minute. cancel it now
    var msg = string.Format("Current Bar is = {0}, Limit bar is = {1}", CurrentBars[1] , limit_bar_number);
    Print(msg);
    if(((CurrentBars[1] - limit_bar_number) >0) && (limit_bar_number != 0))
    {
    if((Limit_order.OrderState != OrderState.Filled) && (Limit_order.OrderState != OrderState.Cancelled) )
    {
    CancelOrder(Limit_order);
    msg = string.Format("Cancelling Limit Order, Position Flat and signal is Long Dumpling with entry of = {0} and current bar at {1}", CurrentBars[1] , limit_bar_number);
    Print(msg);

    }
    }
    trailing_Stop();
    ManagePosition();
    return;
    }

    the secondary dataframe is 1 minute which is added via the code

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    }


    coming back to original code, my orders are managed in ManagePosition described below. Problem is the limit orders and stoploss orders appers on alternate bars. So after end of 1st bar of entry all orders are cancelled. Then on second bar they come back, thrid bar they disappear so on and so forth. It works fine on single frame however when i decided to move the position management to smaller timeframe by adding additional TF(1 minute) they appear and disappear every minute.




    private void ManagePosition()
    {
    if(Position.MarketPosition == MarketPosition.Long)
    {
    if(Stop_Loss_L >= Close[0])
    {
    ExitLong(Position.Quantity);
    }
    else
    {
    if((Stop_Loss_L < (Low[0] - (2 * TickSize))) && (Close[0] > Open[0]) && (BarsInProgress == 0))
    {
    double stoploss_prev = Stop_Loss_L;
    Stop_Loss_L = Low[0] - (2 * TickSize);
    var msg = string.Format("StopLoss Moved from {0} to {1} up {2} ticks", stoploss_prev, Stop_Loss_L, (stoploss_prev - Stop_Loss_L)/TickSize);
    Print(msg);
    }

    ExitLongStopMarket(Position.Quantity, Stop_Loss_L, "StopLoss", "LongDumpling");
    if(Position.Quantity == 3)
    {
    ExitLongLimit(1, First_Target_L , "Emo Target", "LongDumpling");
    ExitLongLimit(1, Second_Target_L , "SPH Target", "LongDumpling");
    ExitLongLimit(1, Final_Target_L , "Final Target", "LongDumpling");
    }
    if(Position.Quantity == 2)
    {
    ExitLongLimit(1, Second_Target_L , "SPH Target", "LongDumpling");
    ExitLongLimit(1, Final_Target_L , "Final Target", "LongDumpling");
    }
    if(Position.Quantity == 1)
    {
    ExitLongLimit(1, Final_Target_L , "Final Target", "LongDumpling");
    }
    }
    }
    if(Position.MarketPosition == MarketPosition.Short)
    {
    Print(Position.Quantity);
    if (position_flag)
    {
    position_flag = false;
    }

    if(Stop_Loss_S <= Close[0])
    {
    ExitShort(Position.Quantity);
    }
    else
    {
    if((Stop_Loss_S > (High[0] - (2 * TickSize))) && (Close[0] < Open[0]) && (BarsInProgress == 0))
    {
    double stoploss_prev = Stop_Loss_S;
    Stop_Loss_S = High[0] + (2 * TickSize);
    var msg = string.Format("StopLoss Moved from {0} to {1} up {2} ticks", stoploss_prev, Stop_Loss_S, (stoploss_prev - Stop_Loss_S)/TickSize);
    Print(msg);
    }

    ExitShortStopMarket(Position.Quantity, Stop_Loss_S, "StopLoss", "ShortDumpling");
    if(Position.Quantity == 3)
    {
    ExitShortLimit(1, First_Target_S , "Emo Target", "ShortDumpling");
    ExitShortLimit(1, Second_Target_S , "SPL Target", "ShortDumpling");
    ExitShortLimit(1, Final_Target_S , "Final Target", "ShortDumpling");
    }
    if(Position.Quantity == 2)
    {
    ExitShortLimit(1, Second_Target_S , "SPL Target", "ShortDumpling");
    ExitShortLimit(1, Final_Target_S , "Final Target", "ShortDumpling");
    }
    if(Position.Quantity == 1)
    {
    ExitShortLimit(1, Final_Target_S , "Final Target", "ShortDumpling");
    }
    }
    }
    }


    debug output looks somehting like this

    Current Bar is = 5846, Limit bar is = 0
    Executing Trailing stop Routine
    2
    2
    Order status = CancelPending, Market Postiion = Short
    Order status = CancelSubmitted, Market Postiion = Short
    Order status = Cancelled, Market Postiion = Short
    Order status = CancelPending, Market Postiion = Short
    Order status = CancelSubmitted, Market Postiion = Short
    Order status = Cancelled, Market Postiion = Short
    Order status = CancelPending, Market Postiion = Short
    Order status = CancelSubmitted, Market Postiion = Short
    Order status = Cancelled, Market Postiion = Short
    Current Bar is = 5847, Limit bar is = 0
    Executing Trailing stop Routine
    2
    Order status = Submitted, Market Postiion = Short
    Order status = Accepted, Market Postiion = Short
    Order status = Submitted, Market Postiion = Short
    Order status = Accepted, Market Postiion = Short
    Order status = Working, Market Postiion = Short
    Order status = Submitted, Market Postiion = Short
    Order status = Accepted, Market Postiion = Short
    Order status = Working, Market Postiion = Short
    2


    So looks like the cancellationa nd submission of orders in happening alternatively.


    #2
    Welcome to the forums sgkcfc!

    There are a couple items we should note. Unless submitted with an IsLiveUntilCancelled overload, non market orders submitted with Enter/Exit methods will cancel on the next bar. We should also note that if we are managing position in OnBarUpdate, we will be waiting until the next bar processes to process our logic and submit protective orders.

    We have an example that demonstrates using Advanced Order Handling methods and IsLiveUntilCancel overloads to submit target/stop orders using Exit methods in OnExecutionUpdate. Modelling after this strategy will allow your strategy to submit target/stop upon the execution of your entry order, and those orders will stay alive until cancelled or filled.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    Advanced Order Handling - https://ninjatrader.com/support/help...r_handling.htm

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,
      Thank you for the response.
      I am aware the orders get cancelled on every bar update and new orders need to be submitted. However i am submitting orders on every bar update. So technically they should be live for entire period and not reappear on every other bar. ManagePosition is called on every barupdate.

      Comment


        #4
        Also i do handle entries in routine OnExecutionUpdate as my order gets filled. However wierd art is the alternate bar. If i am submitting every bar the order should be live always, right?

        Comment


          #5
          Hello sgkcfc,

          Thanks for your replies and your patience.

          If your logic is allowing the order submission method to be reached with each pas of the logic, we can expect it to stay alive. Cancelling orders would either come from automatic cancels if the logic did not allow an order method to be reached or with deliberate cancels with CancelOrder.

          For further debugging steps, you can add a print next to each order method, and comment out other behaviors and deliberate cancels to focus on the single order that is in question. Prints can tell us if the order method is not getting reached when the order is cancelled. If we see this, then we know the order is cancelled because the logic did not allow the order method to be reached.

          You may find it easier to work with IsLiveUntilCancelled overloads and control when the orders should be cancelled, or you could move forward by testing with CancelOrder's commented and debugging prints used to determine if your logic is allowing the order submission methods to be reached.

          The Playback Connection can be used to play over past data and test it as if it were realtime data.

          Playback Connection - https://ninjatrader.com/support/help...connection.htm

          Let us know if there is anything else we can do to help.
          JimNinjaTrader Customer Service

          Comment


            #6
            Thank you Jim. Yes i was testing with playback connection. Let me put statements in the place where i place orders to see as you suggested if the logic is being reached or not.
            Yes i was testing it over the playback connection too.

            Comment


              #7
              Jim,
              Another Question. If i use isliveuntil cancelled method, and say i do not cancel the order ut resubmit the same order with different quantity, however witht he same orderlabel stoploss. Will the previous order implicitly be cancelled and replaced with a new one or will the quantities add up?

              Comment


                #8
                Hi Jim,
                I am pretty convinced this is a bug in Ninja trader. Here is what i did.

                The limit and stop orders are placed in onexecution update. When the bar timer expires and it comes to the manage position routine, placing orders in there , actually cancels them and brings them back om alternate bars. I commented out the code where it places limit orders and sure enough they were not cancelled, it was only the stoploss orders that were cancelled. So this is really weird that my orders should be cancelled because i am placing them? Perhaps Ninja is not updating them. FYI i am dealing with multi frame with P&F bar ending in 1 minute and this minute bar ending in a minute

                Comment


                  #9
                  Hello sgkcfc,


                  Another Question. If i use isliveuntil cancelled method, and say i do not cancel the order ut resubmit the same order with different quantity, however witht he same orderlabel stoploss. Will the previous order implicitly be cancelled and replaced with a new one or will the quantities add up?

                  The order will be changed to the new quantity. You can set up a test on your end to observe the behavior. Below is an example demonstrating with entry orders.

                  Code:
                  private int bar = int.MaxValue;
                  
                  protected override void OnBarUpdate()
                  {
                      if (State == State.Historical)
                          return;
                  
                      if (bar == int.MaxValue)
                      {
                          EnterLongLimit(0, true, 1, Low[0] - 10 * TickSize, "Entry");
                          bar = CurrentBar;
                      }
                  
                      if (CurrentBar > bar + 3)
                      {
                          EnterLongLimit(0, true, 10, Low[0] - 10 * TickSize, "Entry");
                      }
                  }
                  The limit and stop orders are placed in onexecution update. When the bar timer expires and it comes to the manage position routine, placing orders in there , actually cancels them and brings them back om alternate bars. I commented out the code where it places limit orders and sure enough they were not cancelled, it was only the stoploss orders that were cancelled. So this is really weird that my orders should be cancelled because i am placing them? Perhaps Ninja is not updating them. FYI i am dealing with multi frame with P&F bar ending in 1 minute and this minute bar ending in a minute
                  Do you see any useful feedback when testing with TraceOrders?

                  Trace Orders - https://ninjatrader.com/support/help...aceorders2.htm

                  What exactly is the strategy doing when the orders are getting cancelled? Please be specific on what orders are pending and what methods the strategy calls which result in the other orders being cancelled.

                  I look forward to assisting.
                  JimNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by geddyisodin, Yesterday, 05:20 AM
                  7 responses
                  45 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by gbourque, Today, 06:39 AM
                  2 responses
                  5 views
                  0 likes
                  Last Post gbourque  
                  Started by cre8able, Yesterday, 07:24 PM
                  1 response
                  13 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by cocoescala, 10-12-2018, 11:02 PM
                  6 responses
                  939 views
                  0 likes
                  Last Post Jquiroz1975  
                  Started by cmtjoancolmenero, Yesterday, 03:58 PM
                  1 response
                  17 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Working...
                  X