Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

oco orders when the internet falls

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

    oco orders when the internet falls

    Hi,
    I can't solve a problem. I have an automatic unmanaged strategy, which, under certain conditions, automatically places a buy/sell order. When a buy/sell order is entered, the, "stop-loss" and "target" oco orders are automatically entered as well.
    When the internet falls the "stop-loss and "taget" orders are canceled. I don't know how to resolve this situation, I tried to change "StartBehavior" with all the possible options, but I can't get the account position to be able to enter the orders.
    Same solutions? How can I know the status (LONG, SHORT) of the executed order (buy/sell) on that market? How do I place "target" and "stop-loss" orders again?


    Thanks Alberto​​

    #2
    Post the simplified example of the NY code

    if ( ... )
    {
    SendOrder( OrderAction.Buy, OrderType.Market, 1, 0, 0, strLong );
    {
    ...
    protected override void OnExecutionUpdate(Execution e, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {​
    ...
    if ( o.Name==strLong && Position.MarketPosition==MarketPosition.Long )
    {
    double stopLoss = ...;
    double target = ...;
    string oco = System.IO.Path.GetRandomFileName().Replace(".","") ;
    SendOrder(OrderAction.Sell, OrderType.StopMarket, o.Filled, 0, stopLoss, strLongStop, oco);
    SendOrder(OrderAction.Sell, OrderType.Limit, o.Filled, target, 0, strLongTarget, oco);
    }
    else if ( o.Name==strShort && Position.MarketPosition==MarketPosition.Short )
    {
    double stopLoss = ...;
    double target = ...;
    string oco = System.IO.Path.GetRandomFileName().Replace(".","") ;
    SendOrder(OrderAction.BuyToCover, OrderType.StopMarket, o.Filled, 0, stopLoss, strShortStop, oco);
    SendOrder(OrderAction.BuyToCover, OrderType.Limit, o.Filled, target, 0, strShortTarget, oco);
    }
    ...
    ​}

    private void SendOrder(OrderAction action, OrderType type, int quantity, double limitPrice, double stopPrice, string name, string oco = "")
    {
    double limit = Instrument.MasterInstrument.RoundToTickSize(limitP rice);
    double stop = Instrument.MasterInstrument.RoundToTickSize(stopPr ice);
    SubmitOrderUnmanaged(0,action,type,quantity,limit, stop,oco,name);
    }​

    Comment


      #3
      Hello Alberto,

      "When the internet falls the "stop-loss and "taget" orders are canceled"

      The orders are cancelled immediately on the brokerage side?

      Or are the orders cancelled at the moment the strategy is re-enabled? (This would be due to either not using Immediately Submit or not having intra-bar granularity for accurate historical order fills)

      See the forum post below about resuming a strategy position and orders that has been disabled.



      "How can I know the status (LONG, SHORT) of the executed order (buy/sell) on that market?"

      "How do I place "target" and "stop-loss" orders again?"

      Below is a link to an example of how to place a stop market and limit order from an unmanaged strategy using SubmitOrderUnmanaged().
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thanks for the reply. I'll read it all over the weekend. Alberto.

        Comment


          #5
          I tried modifying the code in playback.

          everything is fine until the error comes:

          Strategy 'XXXX' has been disabled because it attempted to modify a historical order that has transitioned to a live order. Please see the help guide for more information on transitioning order references from historical to live.

          I tried reading the guide, I don't understand how to use "GetRealtimeOrder" with "unmanaged approach". Some solutions?
          Thanks Alberto​

          Comment


            #6
            I understand that when you reactivate the strategy the entry time (Consequently entry price) of the order changes, I think this is the problem, how can I solve it?

            08:30:00.0080000 ------------------------------------------------------------> original enter time

            08:30:59.6640000 ------------------------------------------------------------> restart strategy enter time​
            Last edited by Apo84; 10-15-2023, 06:54 AM.

            Comment


              #7
              Hello Alberto,

              If an order is submitted in State.Historical, the real-time order needs to be fetched when the State becomes State.Realtime to make any modifications to it or cancel it.

              The SampleOnOrderUpdate reference sample provides sample code that demonstrates.



              "I understand that when you reactivate the strategy the entry time (Consequently entry price) of the order changes"

              This only occurs if the order is submitted at a different time or price in historical when the strategy is re-enabled.

              1-tick intra-bar granularity and TickReplay (if Calculate is OnPriceChange or OnEachTick) can help the strategy calculate the same in historical as it did in real-time.

              See the forum post below about resuming strategy orders and positions.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                I modified the code, I put:
                - 1-tick intra-bar granularity
                - TickReplay (OnEachTick)
                now the time of the entry order coincides, but the price does not yet.
                The entry order is not canceled (so the price does not change) when the internet is reactivated, but the price on "ninjascript output" is different. Real-time and historical data do not match. I tried using GetRealtimeOrder, but it doesn't seem to change anything.

                Thanks Alberto​

                Comment


                  #9
                  Hello Alberto,

                  With the OCO, instead of generating a random string, try using the bar time or bar number to generate the OCO so this is not random and will be the same in historical as real-time.

                  To confirm, you are placing the orders to the BarsInProgressIndex of the added 1 tick series?

                  It may be necessary to write the information to a text file so that we can find where the differences are coming from.

                  You will want to write the Open, High, Low, Close, and all values used in the condition, and print the order object in OnOrderUpdate().

                  Below is a link to an example with sample code.
                  Citizens of the NinjaTrader Community, A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'. Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics.

                  Chelsea B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  62 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  134 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  75 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  50 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X