Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OCO order does not close operations in automated strategy

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

    OCO order does not close operations in automated strategy

    Hello everybody!

    When I open a trade manually with an OCO created by the ATM strategy manager, the target order is green and the stop order is red.
    However, when the operation is opened by my automated strategy, the OCO orders are in gold and pink, and do not close the operation when the price reaches them.

    A few months ago I used automated strategies and everything worked perfectly.
    However, now this problem has started to happen, with the same strategies that worked before.

    In the image you can see a sales operation carried out by the strategy. An OCO with a target and stop of 100 ticks was generated in the strategy, but the price fell to the target and did not close the operation.

    Does anyone know what it could be?
    Thanks.

    #2
    Hello Tecnostudio,

    What code are you using in the strategy for the targets?

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello Tecnostudio,

      What code are you using in the strategy for the targets?
      SetProfitTarget("", CalculationMode.Ticks, Alvo);
      SetStopLoss("", CalculationMode.Ticks, Stop, false);​

      Comment


        #4
        Hello Tecnostudio,

        Are you using a signal name with the entry order?

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello Tecnostudio,

          Are you using a signal name with the entry order?
          we don't use name

          Comment


            #6
            Hello Tecnostudio,

            Please try specifying a signal name and then see if that still has the same issue. If sso can you share a sample of your code showing the entry order and also where you are calling the targets from?

            Comment


              #7
              Originally posted by NinjaTrader_Jesse View Post
              Hello Tecnostudio,

              Please try specifying a signal name and then see if that still has the same issue. If sso can you share a sample of your code showing the entry order and also where you are calling the targets from?
              Now I entered a name in the OCO positions and order and still, OCO did not close the transaction.

              I used this example strategy below. But all the ones I use are giving the same error.

              Code:
              #region Using declarations
              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.ComponentModel.DataAnnotations;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
              using System.Windows;
              using System.Windows.Input;
              using System.Windows.Media;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Gui;
              using NinjaTrader.Gui.Chart;
              using NinjaTrader.Gui.SuperDom;
              using NinjaTrader.Gui.Tools;
              using NinjaTrader.Data;
              using NinjaTrader.NinjaScript;
              using NinjaTrader.Core.FloatingPoint;
              using NinjaTrader.NinjaScript.Indicators;
              using NinjaTrader.NinjaScript.DrawingTools;
              #endregion
              
              //This namespace holds Strategies in this folder and is required. Do not change it.
              namespace NinjaTrader.NinjaScript.Strategies
              {
                  public class test : Strategy
                  {
                      private bool Autorizado;
              
              
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Digite a descriçăo para seu novo Estratégia personalizado aqui.";
                              Name                                        = "test";
                              Calculate                                    = Calculate.OnBarClose;
                              EntriesPerDirection                            = 1;
                              EntryHandling                                = EntryHandling.AllEntries;
                              IsExitOnSessionCloseStrategy                = true;
                              ExitOnSessionCloseSeconds                    = 30;
                              IsFillLimitOnTouch                            = false;
                              MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                              OrderFillResolution                            = OrderFillResolution.Standard;
                              Slippage                                    = 0;
                              StartBehavior                                = StartBehavior.WaitUntilFlat;
                              TimeInForce                                    = TimeInForce.Gtc;
                              TraceOrders                                    = false;
                              RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                              StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                              BarsRequiredToTrade                            = 20;
                              // Disable this property for performance gains in Strategy Analyzer optimizations
                              // See the Help Guide for additional information
                              IsInstantiatedOnEachOptimizationIteration    = true;
                              Hora                        = DateTime.Parse("14:00", System.Globalization.CultureInfo.InvariantCulture);
                              Contratos                = 1;
                              Alvo                        = 100;
                              Stop                        = 100;
                              Autorizado              = true;
                          }
                          else if (State == State.Configure)
                          {
                              SetProfitTarget(@"teste", CalculationMode.Ticks, Alvo);
                              SetStopLoss(@"teste", CalculationMode.Ticks, Stop, false);
                          }
                      }
              
                      protected override void OnBarUpdate()
                      {
                          if (BarsInProgress != 0)
                              return;
              
                          if (CurrentBars[0] < 1)
                              return;
              
                           // Definir 1
                          if ((Position.MarketPosition == MarketPosition.Flat)
                               && (Autorizado == true)
                               && (Times[0][0].TimeOfDay == Hora.TimeOfDay))
                          {
                              EnterLong(Convert.ToInt32(Contratos), @"teste");
                              Autorizado = false;
                          }
                          
                      }
              
                      #region Properties
                      [NinjaScriptProperty]
                      [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
                      [Display(Name="Hora", Order=1, GroupName="Parameters")]
                      public DateTime Hora
                      { get; set; }
              
                      [NinjaScriptProperty]
                      [Range(1, int.MaxValue)]
                      [Display(Name="Contratos", Order=2, GroupName="Parameters")]
                      public int Contratos
                      { get; set; }
              
                      [NinjaScriptProperty]
                      [Range(100, int.MaxValue)]
                      [Display(Name="Alvo", Order=3, GroupName="Parameters")]
                      public int Alvo
                      { get; set; }
              
                      [NinjaScriptProperty]
                      [Range(100, int.MaxValue)]
                      [Display(Name="Stop", Order=4, GroupName="Parameters")]
                      public int Stop
                      { get; set; }
                      #endregion
              
                  }
              }
              ​

              Comment


                #8
                Hello Tecnostudio,

                When you are testing this what connection and account are you using?

                You are saying that once the price fills one of the targets, the other target is left open and the strategy is flat at that point?

                Comment


                  #9
                  Hello Jesse.

                  In the image I posted previously, you can see that the price reached the profit order three times and the operation was not closed.

                  That's what's happening. The strategy opens the operation according to the entry trigger, opens the OCO orders, but does not close the operation when the price reaches one of the OCO orders. The operation remains open and needs to be closed manually.

                  I've already tested it with a Rithmic account from Bulenox and a Tradovate account from Apex. The same error occurred in both.​

                  Comment


                    #10
                    Hello Tecnostudio,

                    In the imagee the targets are not filled so no OCO action would happen, OCO is one cancels other and that only happens when one of the order fills. To confirm, you are seeing that 1 order stays open when one fills? We need to focus on the fill if you are saying there is a problem with OCO happening. The price reaching that point does not necessarily guarantee a fill, the market needs to trade at that price with volume and reach your place in line to fill the order for it to fill.

                    Comment


                      #11
                      Originally posted by NinjaTrader_Jesse View Post

                      Hello Tecnostudio,

                      In the imagee the targets are not filled so no OCO action would happen, OCO is one cancels other and that only happens when one of the order fills. To confirm, you are seeing that 1 order stays open when one fills? We need to focus on the fill if you are saying there is a problem with OCO happening. The price reaching that point does not necessarily guarantee a fill, the market needs to trade at that price with volume and reach your place in line to fill the order for it to fill.

                      Hello Jesse, in the image the price touched 3 times and came back, but on a daily basis the candles close several times and do not close the operation either.

                      I carried out another test now (image) and you can see that today it was different. He filled the stop exit order, but did not close the trade and also did not close the gain limit exit order.

                      And as I said in the first post, I saw that these colors that the orders are showing are not the normal colors.

                      When I open a trade manually, OCO exit orders are green and red.
                      And when they don't work, they are orange and/or pink.

                      Should I try to reinstall Ninja?​

                      Comment


                        #12
                        I reinstalled NinjaTrader8.
                        Last edited by Tecnostudio; 10-21-2024, 02:21 PM.

                        Comment


                          #13
                          I'll do some more tests to make sure it's working correctly.

                          Comment


                            #14
                            Unfortunately, even after all the work I did to reinstall NinjaTrader8, I continue to have the same problems with OCO orders in operations opened by automated strategies (I will attach the video link that says it in the next message).

                            Sometimes order works.

                            Other times neither of the two OCO exit orders work.

                            And sometimes one of the orders leaves the screen when it is reached, but does not close the operation and also does not close the other OCO order.​

                            Comment


                              #15
                               

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              41 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              124 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              64 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              41 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X