Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to Optimise Percent Profit Target and Stop Loss

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

    How to Optimise Percent Profit Target and Stop Loss

    Hey,

    I'm new to coding and NinjaTrader.

    I'm trying out a Simple Moving Average Crossover Strategy.

    I've been able to set it up so that on the Strategy Analyzer I can find the best Fast and Slow SMA's (when using the Optimization Backtest Type).

    I'm now trying to do the same thing but for the Profit Target and Stop Loss Variables, when the Calculation Mode is set for Percentage.

    My problem is that when I try and optimise the strategy in the Strategy Analyser; with a range of 0 to 1 and an increment of 0.01 (so I would want to test, e.g. Profit Target 0.02 (i.e. 2%) and Stop Loss of 0.01 (i.e. 1%) and then all other possible percentages), it only runs when the Profit Target and Stop loss are set to 1 (i.e. 100%) and does not run any decimals (referring to them as 0 instead).

    I've attached a screenshot of what I mean (https://ibb.co/30szL1J) as well as images of my code (Code 1: https://ibb.co/vq9G9jZ , Code 2: https://ibb.co/BCY4zfq , Code 3: https://ibb.co/W5Pqp4p)

    How can I fix this, so that I can find the optimal Profit Target and Stop Loss percentage?

    Kind regards,
    Stephen

    #2
    Hi,

    try changin the parameter type from int to double.

    Something like this.
    PHP Code:
    [NinjaScriptProperty]
    [Range(0, double.MaxValue)]
    [Display(Name = "Profit Target", Order = 2]
    public double ProfitTarget { get; set; } 
    

    Comment


      #3
      Hey,

      Thank you so much for your reply!

      Your idea partially worked, I am now able to input decimal values into the State.SetDefaults (as shown here https://ibb.co/SJPCQ9V) but the Strategy Analyser is still not testing all possible options and also not providing the values of what Profit Target or Stop Loss Percentage was tested (as shown here https://ibb.co/Cwv5cNw)

      Comment


        #4
        Hi,
        unfortunately that approach works for me, but there is an alternative.
        The parameter will be entered as int but converted to a double and divided as needed.

        PHP Code:
        [NinjaScriptProperty]
        [Range(0, int.MaxValue)]
        [Display(Name = "Profit Target", Order = 2]
        public int ProfitTarget { get; set; } 
        
        PHP Code:
        SetProfitTarget(CalculationMode.Percent, Convert.ToDouble(ProfitTarget) / 100d); 
        
        Probably it's better to divide by 1000d because a 1% target might be too large, depending on your style of trading.
        Sadly this way is less flexible but in many cases its sufficient and more convenient to configure.

        Comment


          #5
          Hey,

          I messed around with it a little more and realised that if I changed the max value to a decimal, like 0.1 then it works using your first method.

          Thank you so much for you help, I really appreciate it!

          I hope you have an amazing rest of your day!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          50 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          126 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          69 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X