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(0double.MaxValue)]
    [
    Display(Name "Profit Target"Order 2]
    public 
    double ProfitTarget getset; } 

    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(0int.MaxValue)]
        [
        Display(Name "Profit Target"Order 2]
        public 
        int ProfitTarget getset; } 
        PHP Code:
        SetProfitTarget(CalculationMode.PercentConvert.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 usglucofreeze, Today, 01:19 AM
          0 responses
          7 views
          0 likes
          Last Post usglucofreeze  
          Started by f.saeidi, Today, 01:12 AM
          0 responses
          7 views
          0 likes
          Last Post f.saeidi  
          Started by NinjaTrader_ChelseaB, 03-14-2017, 10:17 AM
          227 responses
          34,318 views
          7 likes
          Last Post rare312
          by rare312
           
          Started by f.saeidi, Today, 12:11 AM
          0 responses
          6 views
          0 likes
          Last Post f.saeidi  
          Started by Graci117, Yesterday, 11:40 PM
          0 responses
          7 views
          0 likes
          Last Post Graci117  
          Working...
          X