No announcement yet.

Partner 728x90


SP500 Strategy: Strategy ends early after adding StopLoss

  • Filter
  • Time
  • Show
Clear All
new posts

    SP500 Strategy: Strategy ends early after adding StopLoss


    I couldn't find any threads on my following Problem in NT8:

    I am back testing a strategy on the S&P500 with daily bars. The timeframe is 1995-2016.

    If I don't add a stop loss in the code everything is fine (see screenshot 1). However, as soon as I add a stop loss, the strategy ends during 1999, so it doesn't execute any further trades.

    The code is set out below. To add the stop loss just uncomment (delete "//") the stop loss code right before the "Region Properties" pretty much in the end of the code.

    Would very much appreciate your help.


    #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;

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    public class Test : Strategy

    protected override void OnStateChange()
    if (State == State.SetDefaults)
    Description = @"Test";
    Name = "Test";
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;

    Capital = 100000;

    else if (State == State.Configure)


    protected override void OnBarUpdate()

    if (this.CurrentBar < this.BarsRequiredToTrade)

    double entryPrice = Close[0];
    int size = (int) (this.Capital / entryPrice);

    if (Close[0] < WMA(14)[0])
    EnterLong(size, "Buy");

    if (BarsSinceEntryExecution() > 14)

    // SetStopLoss(CalculationMode.Currency, 3000);


    #region Properties

    [Range(0, int.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="Capital", Order=1, GroupName="NinjaScriptStrategyParameters")]
    public int Capital
    { get; set; }

    Attached Files

    Typically, when a strategy stops running prematurely it is because it gets a run-time error. Have you checked the Log tab in the Control Center to see if adding the StopLoss method call somehow creates a run-time error?


      Hello KirkHammett,

      You are trying to set a stop loss at a price the SP has never traded, 3000. If you enter long and try to place a buy stop 400% above the market it will be rejected.

      Your code,

      // SetStopLoss(CalculationMode.Currency, 3000);

      Consider using,

      SetStopLoss(CalculationMode.Ticks, 300);

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



        Sorry for the late Reply, was quite busy. First of all, thanks a lot for your responses!

        @ Conan
        There is no error or anything in the Log.

        @ Alan P.
        It is a Currency stop, not a Price stop. So, the calculationmode.currency just says: Sell if my loss is 3000$. The price of the S&P is not relevant. With a tick stop the problem still remains.

        I checked the Code again. There is a problem between the StopLoss and the the "BarsSinceEntryExecution()". As soon as I delete the BarsSinceEntryExecution code, everything works fine again.

        Any other ideas? To me, it looks like a bug.
        Last edited by KirkHammett; 10-11-2017, 10:09 AM.


          Hello KirkHammett,

          Thank you for your patience.

          This is occurring due to two exits filling on the same bar in historical processing. Essentially, the historical fill processing is seeing that the exits can fill on the same bar thus an Overfill occurs and the entry gets "stuck" in state that it cannot be cancelled.

          To avoid this behavior we must use just the SetStopLoss, SetTrailStop, and SetProfitTarget methods without any Exit methods in our Managed code.
          Alternatively, you can use the Unmanaged Order Methods in order to work around the Manage Order Methods resulting in such behavior.

          Please refer to the Unmanaged Order Methods at the following link:

          We have however submitted this report as a feature request for Historical Fill Processing to avoid such behavior.

          Please let me know if you have any questions.


            Originally posted by NinjaTrader_PatrickH View Post
            We have however submitted this report as a feature request for Historical Fill Processing to avoid such behavior.
            The internal tracking number for your feature request is SFT-2278. Please reference this internal tracking number if you ever have questions regarding this feature request.


            Latest Posts


            Topics Statistics Last Post
            Started by Revazi123, Today, 01:01 PM
            0 responses
            1 view
            Last Post Revazi123  
            Started by sushilake, Today, 12:21 PM
            0 responses
            Last Post sushilake  
            Started by AaronKoRn, Today, 10:17 AM
            0 responses
            Last Post AaronKoRn  
            Started by PWillard, Today, 10:02 AM
            0 responses
            Last Post PWillard  
            Started by jmezz, 09-11-2020, 09:12 AM
            2 responses
            Last Post stafe
            by stafe