Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancels In Umanaged Orders

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

    Cancels In Umanaged Orders

    Enclosed is my code sample which is under OnBarUpdate():

    if(ToTime(Time[0]) >= timeOpen && ToTime(Time[0])< timeClose &&
    Position.MarketPosition == MarketPosition.Long /*&& Close[0]<=s1*/)
    {

    if(stopLossLong==null )
    {
    Print("Stop entered in onbarupdate");
    stopLossLong = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, Position.AvgPrice - 6, "protectLong", "stop loss long");
    }
    if(stopLossLong.StopPrice==0)
    {
    //ChangeOrder(stopLossLong, 1, 0, Position.AvgPrice-6);
    CancelOrder(stopLossLong);
    stopLossLong = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, Position.AvgPrice - 6, "protectLong", "stop loss long");

    }

    if (longEntry != null)
    {
    CancelOrder(longEntry);
    Print ("Long entry cancelled");
    longEntry=null;
    }
    if (shortEntry != null)
    {
    //ChangeOrder(shortEntry, 1, r2, 0);
    CancelOrder(shortEntry);
    Print ("Short entry cancelled");
    //shortEntry=null;
    shortEntry = SubmitOrder(0, OrderAction.SellShort, OrderType.Limit , 1, ShortLimitPrice, 0, "s", "Short");

    }
    else
    shortEntry = SubmitOrder(0, OrderAction.SellShort, OrderType.Limit , 1, ShortLimitPrice, 0, "s", "Short");

    if(targetLong!=null)
    {
    //ChangeOrder(targetLong, 1, r2, 0);
    CancelOrder(targetLong);
    //targetLong=null;
    targetLong = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, r2, 0, "TgtLong", "profit target long");

    }
    else
    targetLong = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, r2, 0, "TgtLong", "profit target long");

    I am running this on TopStepTrader using the Sim101 account as I want to see that the program acts right in real time before running it on Top StepTrader's Combine. The issue I am having is that when the bar changes, the targetLong and shortEntry get cancelled but does not get submitted until the next bar. I want the old order cancelled and the new order submitted on same bar in the OnBarUpdate(). Instead what I am seeing is the orders cancelled on one bar and then submitted on the nextbar. Should I be resubmitting the orders on the OnOrderUpdate event or another event and only cancel for OnBarUpdate?


    In the backtest, everything works fine.

    Thanks,

    Cowpux
    Last edited by Cowpux; 01-22-2015, 04:05 PM.

    #2
    Cowpux,

    Thank you for your post.

    You would want to modify the order instead of cancelling and resubmitting with each bar. Use ChangeOrder() for this -

    http://www.ninjatrader.com/support/h...hangeorder.htm
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Thank you Cal,

      That worked. Now I have noticed another problem (because I didn't realize this about unmanaged orders!) is that no matter what order you submit orders, the fill order can be in any order. Let me explain better.

      The system I am programming is a stop and reverse system. Initially there is an order to sell above the market on a limit and an order to buy below the market on a limit. For example when a buy order is filled, there is an order to take profits at a limit above the market and an order to go short at the same price. The problem I am running into is that all open positions must have a stop loss. Most of the time whenever my orders labeled as entry get filled, the stop loss is entered as it should be. However, occasionally, the orders labeled as profit targets get filled last and hence are really an entry order and no stop loss gets entered.



      I want to code to handle this situation. Whenever a profit target order gets filled that results in a new position, I want to enter a stop. If a profit target order gets filled and the position is flat, I want nothing to happen. My question is where do I code for this situation? Should it be done in the OnExecution, OnOrderUpdate, or the OnPositionUpdate event handler? I am guessing that any change in position information comes at a different time than a change in order status or execution.


      Again thank you for your help,

      Cowpux

      Comment


        #4
        Originally posted by Cowpux View Post
        Thank you Cal,

        That worked. Now I have noticed another problem (because I didn't realize this about unmanaged orders!) is that no matter what order you submit orders, the fill order can be in any order. Let me explain better.

        The system I am programming is a stop and reverse system. Initially there is an order to sell above the market on a limit and an order to buy below the market on a limit. For example when a buy order is filled, there is an order to take profits at a limit above the market and an order to go short at the same price. The problem I am running into is that all open positions must have a stop loss. Most of the time whenever my orders labeled as entry get filled, the stop loss is entered as it should be. However, occasionally, the orders labeled as profit targets get filled last and hence are really an entry order and no stop loss gets entered.



        I want to code to handle this situation. Whenever a profit target order gets filled that results in a new position, I want to enter a stop. If a profit target order gets filled and the position is flat, I want nothing to happen. My question is where do I code for this situation? Should it be done in the OnExecution, OnOrderUpdate, or the OnPositionUpdate event handler? I am guessing that any change in position information comes at a different time than a change in order status or execution.


        Again thank you for your help,

        Cowpux
        That means that your Profit Targets and Stops are not OCO?

        Comment


          #5
          Hi Koganam,

          I am handling the OCO part of it in the OnExecution event handler instead of giving the 2 orders the same OCO id. The reason I am not entering as an OCO is if the profit target ends up being an entry order instead and then my stop is cancelled. That is why I am handling it this way. I just need to know which even has the most current position and order fill information

          Cowpux
          Last edited by Cowpux; 01-23-2015, 01:43 PM.

          Comment


            #6
            Originally posted by Cowpux View Post
            Hi Koganam,

            I am handling the OCO part of it in the OnExecution event handler instead of giving the 2 orders the same OCO id. The reason I am not entering as an OCO is if the profit target ends up being an entry order instead and then my stop is cancelled. That is why I am handling it this way. I just need to know which even has the most current position and order fill information

            Cowpux
            The event handlers, as do almost any event handlers, run asynchronously, so you use each to determine and track what you want.

            Fills, and partial fills: Use OnExecution().
            Order status and updates: use OnOrderUpdate();
            Position and changes: Use OnPositionUpdate();

            There is of course a natural order to things. There cannot be a Position change that is not preceded by an Execution of some sort, and an Execution always must have been preceded by an OrderUpdate, but there can be many OrderUpdates that affect nothing else.

            So no matter what order gets filled, you know that there is an OnExecution() event. You can query the IOrder object of that event to see what got executed. You can choose to confirm the Execution by querying OnPositionUpdate(). With the information in hand, you can issue any orders that you want to issue, then confirm their state with OnOrderUpdate().
            Last edited by koganam; 01-26-2015, 04:06 PM. Reason: Corrected grammar and spelling.

            Comment


              #7
              Thanks Osikani! This clears things up for me!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              596 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              343 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              556 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              554 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X