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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    90 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    137 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    120 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    72 views
    0 likes
    Last Post PaulMohn  
    Working...
    X