Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss doesn't change the stop loss

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

    SetStopLoss doesn't change the stop loss

    Hello,

    I enter my positions manually and using an ATM to set the initial target and stop loss. Then as I add to the position I want this strategy script to keep track of my position size and automatically adjust my stop loss so that I never lose more than my pre-specified X$ risk if the stop gets hit. I read somewhere that tracking the position through the Account object is the way to go (is this correct?). I do get updates about position changes, but when I call SetStopLoss nothing really happens to the stop loss and I can't see anything in the logs.

    Here's the code I'm using (I've tried to use different properties of CalculationMode but nothing worked):


    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 AutoSL : Strategy
      {
    
    
        protected override void OnStateChange()
        {
    
            if (State == State.SetDefaults)
            {
                Description = @"Automatically sets the stop loss to risk X$ based on the position size.";
                Name = "AutoSL";
                Calculate = Calculate.OnPriceChange;
                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;
                IsInstantiatedOnEachOptimizationIteration = true;
    
                // Find our Sim account
    
                lock (Account.All)
                    account = Account.All.FirstOrDefault(a => a.Name == "DEMO3188008");
                // Subscribe to position updates
    
                if (account != null) {
                    account.PositionUpdate += OnPositionUpdate;
                 }
             }
        }
    
    
    
        private void OnPositionUpdate(object sender, PositionEventArgs e)
        {
              // The following has no impact on the stop loss that has been set already before the position changed
               SetStopLoss(CalculationMode.Ticks, 5);
        }
    
    
        private Account account;
      }
    }
    ​

    #2
    Hello motradesmoproblems,

    Native strategy methods (like SetStopLoss() or ExitLong()) will only work with positions opened by the strategy.

    As the position was entered from manually submitted orders, you will need to use the addon approach through an account object to modify or send new exit orders.

    The AtmStrategyIdentifier provides helpful code on finding account orders that are part of an Atm.
    This indicator serves to provide labels for Atm strategies that are present on your chart. Each new Atm strategy will rotate through the colors defined in your Brush Collection. Simply add to a chart, select the data series you want to have labels added to, choose your font and add all the brushes/colors you want […]


    The ProfitCaseStopTrailIndicatorExample provides sample code of modifying account orders using the addon approach.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Perfect thanks Chelsea for clearing this up and for pointing me to the right direction.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      110 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      59 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      37 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      41 views
      0 likes
      Last Post TheRealMorford  
      Started by Mindset, 02-28-2026, 06:16 AM
      0 responses
      78 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X