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 NullPointStrategies, 03-13-2026, 05:17 AM
    0 responses
    86 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    151 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    79 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    53 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    61 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X