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

OCO ID cannot bre reused. Please use a new OCO ID.

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

    #16
    Rydatallen,

    How are you setting the OCO ID in your code?
    Josh G.NinjaTrader Customer Service

    Comment


      #17
      OnState.Change()
      {
      if (State == State.SetDefaults)
      {
      BuyStopSignal = @"Buy stop order placed";
      }
      }

      OnBarUpdate()
      {
      myOrder = EnterLongStopMarket(0, true, Convert.ToInt32(DefaultQuantity), buyStopLevel, BuyStopSignal.ToString());
      }

      OnExecutionUpdate(..................)
      {
      SetProfitTarget(BuyStopSignal.ToString(), CalculationMode.Price, buyProfitTarget);
      SetStopLoss(BuyStopSignal.ToString(), CalculationMode.Price, buyStopLoss, false);
      }

      Works for about 4 trades and then I get the errors.

      Comment


        #18
        I think I managed to work around it by creating another string that gets assigned the BuyStopSignal.ToString + CurrentBar.ToSTring() on each and every order. It has made more trades now than before without any errors.

        Comment


          #19
          Rydatallen,

          Thats a great way to set your OCO ID.

          Let me know if there is anything else I can do to assist.
          Josh G.NinjaTrader Customer Service

          Comment


            #20
            You can also find another solution here: https://ninjatrader.com/support/foru...-oco-id-reused

            Comment


              #21
              Chelsea's got a sample with ocostring


              namespace NinjaTrader.NinjaScript.Strategies
              {
              public class UnmanagedOCOBracketExample : Strategy
              {

              private string ocoString;



              protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
              Cbi.MarketPosition marketPosition, string orderId, DateTime time)
              {
              // if the long entry filled, place a profit target and stop loss to protect the order
              if (longStopEntry != null && execution.Order == longStopEntry)
              {
              // generate a new oco string for the protective stop and target
              ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
              // submit a protective profit target order
              SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, 1, (High[0] + 20 * TickSize), 0, ocoString, "longProfitTarget");
              // submit a protective stop loss order
              SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, (Low[0] - 10 * TickSize), ocoString, "longStopLoss");
              }

              // reverse the order types and prices for a short
              else if (shortStopEntry != null && execution.Order == shortStopEntry)
              {
              ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
              SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.Limit, 1, (Low[0] - 20 * TickSize), 0, ocoString, "shortProfitTarget");
              SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.StopMarket, 1, 0, (High[0] + 10 * TickSize), ocoString, "shortStopLoss");
              }

              // I didn't use Order variables to track the stop loss and profit target, but I could have
              // Instead, I detect the orders when the fill by their signalName
              // (the execution.Name is the signalName provided with the order)

              // when the long profit or stop fills, set the long entry to null to allow a new entry
              else if (execution.Name == "longProfitTarget" || execution.Name == "longStopLoss" || execution.Name == "shortProfitTarget" || execution.Name == "shortStopLoss")
              {
              longStopEntry = null;
              shortStopEntry = null;
              }
              }

              protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
              {
              // only places orders in real time
              if (State != State.Realtime || ExitOnCloseWait(marketDataUpdate.Time))
              return;

              // require both entry orders to be null to begin the entry bracket
              // entry orders are set to null if the entry is cancelled due to oco or when the exit order exits the trade
              // if the Order variables for the entries are null, no trade is in progress, place a new order in real time
              if (longStopEntry == null && shortStopEntry == null)
              {
              // generate a unique oco string based on the time
              // oco means that when one entry fills, the other entry is automatically cancelled
              // in OnExecution we will protect these orders with our version of a stop loss and profit target when one of the entry orders fills
              ocoString = string.Format("unmanagedentryoco{0}", DateTime.Now.ToString("hhmmssffff"));
              longStopEntry = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.StopMarket, 1, 0, (High[0] + 15 * TickSize), ocoString, "longStopEntry");
              shortStopEntry = SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.StopMarket, 1, 0, (Low[0] - 15 * TickSize), ocoString, "shortStopEntry");
              }
              }

              protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
              int quantity, int filled, double averageFillPrice,
              Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
              {
              AssignOrderToVariable(ref order);
              // when both orders are cancelled set to null for a new entry
              // if the exit on close fills, also reset for a new entry
              if ((longStopEntry != null && longStopEntry.OrderState == OrderState.Cancelled && shortStopEntry != null && shortStopEntry.OrderState == OrderState.Cancelled) || (order.Name == "Exit on session close" && order.OrderState == OrderState.Filled))
              {
              longStopEntry = null;
              shortStopEntry = null;
              }

              }
              Last edited by PaulMohn; 05-10-2022, 09:47 AM.

              Comment


                #22
                I know this is an old thread but is there a way to add this to the builder. I am not exactly adept at programming and have no clue where to add the above code in my strategy after I unlock it. Any help would be appreciated. Thanks

                Comment


                  #23
                  Originally posted by sprks7979 View Post
                  I know this is an old thread but is there a way to add this to the builder. I am not exactly adept at programming and have no clue where to add the above code in my strategy after I unlock it. Any help would be appreciated. Thanks
                  Hello sprks7979,

                  Thank you for your note.

                  Coding with OCO requires an unmanaged strategy, although the Strategy Builder only uses the managed approach for order methods. With the managed approach, Profit Target, Stop Loss, and Trailing Stop orders are tied together via OCO but for other order types, OCO is not an option. Due to this limitation, you would not be able to add the above code in a strategy unlocked from the Strategy Builder.

                  Please let us know if we may be of further assistance.
                  Emily C.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by gbourque, Today, 06:39 AM
                  2 responses
                  14 views
                  0 likes
                  Last Post gbourque  
                  Started by rexsole, Today, 08:39 AM
                  0 responses
                  4 views
                  0 likes
                  Last Post rexsole
                  by rexsole
                   
                  Started by trilliantrader, Yesterday, 03:01 PM
                  3 responses
                  30 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Started by cmtjoancolmenero, Yesterday, 03:58 PM
                  4 responses
                  26 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by Brevo, Today, 01:45 AM
                  1 response
                  14 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X