Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Closing stop orders when the strategy is disconnected

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

    Closing stop orders when the strategy is disconnected

    Hello everybody!

    In my strategy, I use SetTrailStop. When you enter the market, the corresponding stop order is automatically opened. When in this situation I turn off the strategy, the stopper disappears, and the open position remains in the market. Thus, the account is confirmed at great risk.
    Question: how to set up a strategy so that if it is disabled (manually or when an error occurs) does the stop order protect the position not disappear? Below is the template for my strategy (without real logic):

    Also I ask to check up working capacity of a template of strategy as a whole. Especially OnStateChange ().


    PHP 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
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class ksMaket : Strategy
        {
            #region OnStateChange
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Strat  ";
                    Name                                        = "ksMaket";
                    
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 1800;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    IsAdoptAccountPositionAware                 = true;
                    StartBehavior                                = StartBehavior.AdoptAccountPosition;
                    TimeInForce                                    = TimeInForce.Day;
                    TraceOrders                                    = true;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 5;
                    ConnectionLossHandling                         = ConnectionLossHandling.KeepRunning;
                    IsInstantiatedOnEachOptimizationIteration    = true;
                    
                    QuantityContracts                            = 21;
                    
                }
                else if (State == State.Configure)
                {
                                    
                    AddDataSeries(Data.BarsPeriodType.Day, 1);
                    SetTrailStop(@"MyEntryLong", CalculationMode.Ticks, 5, false);
                    SetTrailStop(@"MyEntryShort", CalculationMode.Ticks, 5, false);
                }
            }
            #endregion
            
            #region OnBarUpdate
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0) return;
                if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade) return;
                Print("BarsInProgress & CurrentBars - yes");
                
                if(        
                    //Any logic 
                    Close[0] > CurrentDayOHL().CurrentOpen[0]
                )
                    
                {    
                    EnterLong(Convert.ToInt32(QuantityContracts), @"MyEntryLong");
                }
                
                else if( 
                    Close[0] < Opens[1][0]
                )
                    
                {
                    EnterShort(Convert.ToInt32(QuantityContracts), @"MyEntryShort");
                }
                
            }
            #endregion
            
            #region Properties
            
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="QuantityContracts", Description="Количество контрактов", Order=1, GroupName="Parameters")]
            public int QuantityContracts
            { get; set; }
            
            #endregion
    
        }
    } 
    
    Last edited by Kostiantyn; 01-28-2018, 11:38 AM.

    #2
    Hello Kostiantyn,

    Thank you for your note.

    Under Control Center>Tools>Options>Strategies Category, Properties>NinjaScript, uncheck Cancel Exit Orders when a strategy is disabled.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    81 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    66 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X