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

Stoploss Percent Optimization

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

    Stoploss Percent Optimization

    Hi there,

    How do I setup a percent stop loss in Editor which can be optimized (using Backtest type: Optimization)? I tried so many ways, but can't get it to work. I saw an old post from a NT staff member saying to use double instead of int, and he claimed it can be optimized that way. But that's not true, is it? It's my understanding is that Optimization takes whole numbers only.

    To me, the most obvious way would be to use the following code.

    Stop = 100;

    SetStopLoss("", CalculationMode.Percent, Stop / 10000, false);

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


    Thanks.

    #2
    Hello kabes223, and thank you for your question.

    To the first part of your question, the following video demonstrates setting up a strategy with a double parameter, and optimizing this through the strategy analyzer.



    To your second question, whenever you divide one integer by another integer in C#, the result is an integer. For example, while you might expect the result of

    1 / 10

    to be 0.1, the actual result of this calculation is 0. Similarly, 9 / 10 is also 0, and 15 / 10 is 1. The fractional part, in other words, is simply cut off. The only way to avoid this is to force C# to do math using doubles, and the best way in turn to guarantee this is to make your parameter a double. Even if you would like to set percentages like e.g.

    Code:
    [FONT=Courier New]
    SetStopLoss("", CalculationMode.Percent, Stop / 100[B].0[/B], false);[/FONT]
    and then make Stop a number between 0 and 100, it's best to make Stop a double. However, if you would like to use integers, please see the bolded .0 in the above code. Since at least one of the two numbers we are doing math is a double - 100 is an integer, 100.0 is a double - then the result will also be a double, and you can if you choose make Stop an integer.

    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Jessica, that really helped. Problem solved!

      Comment


        #4
        Hello JessicaP.

        I used the same approach you proposed above but the issue arised when I tried to optimize double parameter as Optimizer does not support it. Would you know of any workaround for that please?
        I found one proposed but ChelseaB but it is not clear enough for me..



        Thank you
        David

        Comment


          #5
          Hi Paco0, thanks for your note.

          I made a test script that is able to increment double data types by another double. Please see attached. Optimize this in the strategy analyzer from 0 to 1 and increment by 0.01.

          Kind regards.
          Attached Files
          Chris L.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Radano, 06-10-2021, 01:40 AM
          20 responses
          615 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by Mizzouman1, Today, 07:35 AM
          0 responses
          4 views
          0 likes
          Last Post Mizzouman1  
          Started by i019945nj, 12-14-2023, 06:41 AM
          6 responses
          66 views
          0 likes
          Last Post i019945nj  
          Started by aa731, Today, 02:54 AM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by BarzTrading, Today, 07:25 AM
          0 responses
          3 views
          0 likes
          Last Post BarzTrading  
          Working...
          X