Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stops don't work in backtest

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

    Stops don't work in backtest

    Just a quick question on getting stops to work while back testing. I'm testing an oscillator I wrote and the back test seems to work to all my parameters in the strategy wizard except for the stops.

    Of course I assume stops can be tested in back testing. I have had a quick look around the forum but didn't find what I was after.

    I have had a quick look around the forum but didn't find what I was after. I generated the code using the wizard and I just want a simple 10 point profit target and 10 point stop loss.

    Bet I'm missing something simple lol.

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class TSRSTrategy : Strategy
    {
    #region Variables
    // Wizard generated variables
    private double cheap = -0.5; // Default setting for Cheap
    private double expensive = 0.5; // Default setting for Expensive
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    SetProfitTarget("10", CalculationMode.Ticks, 0);
    SetStopLoss("10", CalculationMode.Ticks, 0, true);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(TSR(21), Cheap, 1))
    {
    EnterLong(1, "");
    }

    // Condition set 2
    if (CrossBelow(TSR(21), Expensive, 1))
    {
    EnterShort(1, "");
    }
    }

    #region Properties
    [Description("")]
    [Category("Parameters")]
    public double Cheap
    {
    get { return cheap; }
    set { cheap = Math.Max(-1.000, value); }
    }

    [Description("")]
    [Category("Parameters")]
    public double Expensive
    {
    get { return expensive; }
    set { expensive = Math.Max(0, value); }
    }
    #endregion
    }
    }

    #2
    Treggs, thanks for the post - of course setting stops and targets can be backtested, however you have set both at 0 -

    Code:
     
    SetProfitTarget("10", CalculationMode.Ticks, [COLOR=red][B]0[/B][/COLOR]);
    SetStopLoss("10", CalculationMode.Ticks, [COLOR=red][B]0[/B][/COLOR], true);
    Also your 'from Entry signal' string will prevent those from getting picked up correctly, either set to an empty string ("") or name your entries the same way ("10")

    Comment


      #3
      Thanks for that. I knew I was missing something simple. Just got the value in the wrong spot.

      Cheers

      Comment


        #4
        Hello Bertrand

        Hope all good. The TSR which appears in the code is the TSR signal line indicator? I found it only for MT. Would you know if there exists an NT version? I looked at some way of using it and its very interesting as well as effective.

        Regards

        Comment


          #5
          Sorry Michael, I would not know, best would be if you asked Treggs who was working with that one.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          61 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          40 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          21 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          23 views
          0 likes
          Last Post TheRealMorford  
          Started by Mindset, 02-28-2026, 06:16 AM
          0 responses
          51 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Working...
          X