Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calmar Ratio Optimizer

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

    Calmar Ratio Optimizer

    Hey guys - I need a bit of help with implementing an OptimizationFitness for the Calmar ratio. The basic methods have been written:

    Code:
        public static double AnnualReturns(double[] returns) {
            double num_years = returns.Length / 252.0;
            double cum_ret_final = (returns.Select(r => r + 1).Aggregate((a, b) => a * b));
            return Math.Pow(cum_ret_final, 1.0 / num_years) - 1.0;
        }
    
        public static double Calmar(double[] returns) {
            double max_dd = MaxDrawdown(CumulativeReturns(returns));
            if (max_dd < 0) {
                return AnnualReturns(returns) / Math.Abs(max_dd);
            }
            return double.NaN;
        }
    I omitted CumulativeReturns and MaxDrawdowns as I assume that the platform already has those values.

    How do I fold the code above into OnCalculatePerformanceValue()? I understand that I have to set the return Value but need a bit of help putting it together.

    Thanks in advance.

    #2
    Hello molecool,

    Thanks for your post.

    I have moved your forum thread over to the General Development section of the NinjaTrader Forums since it is not specifically related to making a NinjaScript strategy.

    That said, an OptimizationFitness allows one to create a data point (Value) to optimize a strategy on (min or max). So if there is some formula that a trader wants to optimize on they can calculate it in an OptimizationFitness.

    You would need to iterate through the SystemPerformance.AllTrades TradeCollection to calculate your metric within the OptimizationFitness script. Currently, a custom PerformanceMetric will not be added to the TradesPerformance class because custom performance metrics do not tie into this class.

    We are tracking interest for this in a feature request and I have added your vote. This request is being tracked under the number SFT-4723.

    As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted in the Release Notes page of the Help Guide.

    Release Notes — https://ninjatrader.com/support/help...ease_notes.htm

    The OptimizationFitness scripts that come default with NinjaTrader could be used to see how this is done. To view an OptimizationFitness NInjaScript, open a New > NinjaScript Editor window, open the OptimizationFitness folder, and double-click on the file that you want to view.

    See the help guide documentation below for more information.

    Value: https://ninjatrader.com/support/help...ness_value.htm
    OnCalculatePerformanceValue(): https://ninjatrader.com/support/help...lue.htm​
    SystemPerformance.AllTrades: https://ninjatrader.com/support/help.../alltrades.htm

    See this forum thread as well for more information on this topic and sample code: https://forum.ninjatrader.com/forum/...ess-metric-nt8
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    546 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X