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

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.Day1);
                    
    SetTrailStop(@"MyEntryLong"CalculationMode.Ticks5false);
                    
    SetTrailStop(@"MyEntryShort"CalculationMode.Ticks5false);
                }
            }
            
    #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(1int.MaxValue)]
            [
    Display(Name="QuantityContracts"Description="Количество контрактов"Order=1GroupName="Parameters")]
            public 
    int QuantityContracts
            
    getset; }
            
            
    #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 RookieTrader, Today, 09:37 AM
    2 responses
    10 views
    0 likes
    Last Post RookieTrader  
    Started by alifarahani, Today, 09:40 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by Gerik, Today, 09:40 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by KennyK, 05-29-2017, 02:02 AM
    3 responses
    1,285 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by AttiM, 02-14-2024, 05:20 PM
    11 responses
    186 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X