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

SP500 Strategy: Strategy ends early after adding StopLoss

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

    SP500 Strategy: Strategy ends early after adding StopLoss

    Hi!

    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.

    Regards
    Kirk




    #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

    //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)
    return;

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

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

    if (BarsSinceEntryExecution() > 14)
    ExitLong();

    // SetStopLoss(CalculationMode.Currency, 3000);

    }

    #region Properties

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

    #endregion
    }
    }
    Attached Files

    #2
    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?

    Comment


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

      Comment


        #4
        Hi!

        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, 11:09 AM.

        Comment


          #5
          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: https://ninjatrader.com/support/help...d_approach.htm

          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.

          Comment


            #6
            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.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by funk10101, Today, 12:02 AM
            0 responses
            1 view
            0 likes
            Last Post funk10101  
            Started by gravdigaz6, Yesterday, 11:40 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by MarianApalaghiei, Yesterday, 10:49 PM
            3 responses
            10 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by XXtrader, Yesterday, 11:30 PM
            0 responses
            4 views
            0 likes
            Last Post XXtrader  
            Started by love2code2trade, 04-17-2024, 01:45 PM
            4 responses
            28 views
            0 likes
            Last Post love2code2trade  
            Working...
            X