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

OptimizationFitness to access backtest date range

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

    OptimizationFitness to access backtest date range

    Hi All,
    I'd like my OptimizationFitness to be able to take into account the date range of the backtest, specifically the number of days the backtest was run on. Is this possible? If so, could you point me in the direction of a code snippet?

    Thanks,
    ghart

    #2
    Hello ghart19,

    Thanks for opening the thread.

    Currently it is not possible to get the start date and end date of a strategy through an Optimization Metric. However it is possible to devise a work around by assigning some values in a static class from the running strategy to place logic in the Optimization Fitness Metric based off that static value.

    I have attached some small scripts that demonstrate how you can share some data in a static class within an AddOn and how you can have a running strategy set a value there and then have the Optimization Metric take a certain action.

    You can observe that changing the code in the Optimization Metric will affect the Performance column in the Strategy Analyzer.

    For getting the From and To dates in a strategy, we do not support using these properties directly as they are not documented. You may wish to use unsupported code, but we would not be able to provide direction for exact use as the properties might be subject to change.

    You could create a condition that checks the DateTime for the first bar that gets iterated in OnBarUpdate() to do reference the starting bar in a supported way, however. As a rough example:
    Code:
    private bool doOnce = true;
    protected override void OnBarUpdate()
    {
    	if(doOnce)
    	{
    		//get Time[0] and assign to static variable in AddOn.
    	}
    	
    }
    Please let me know if I may be of further assistance.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hey Jim,
      Thank you very much, this is great!

      It seems like I could have the strategy count the number of days between the 1st bar and last bar by storing the bar's date on each execution on OnBarUpdate. Then, using the ShareData add-on you provided, store the number of days for use by the OpFit. Is that the way you see it as well?

      Thanks again,
      Gabriel

      Comment


        #4
        Hi Gabriel,

        Yep, you follow my proposed work around correctly.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by f.saeidi, Today, 12:14 PM
        8 responses
        18 views
        0 likes
        Last Post f.saeidi  
        Started by Mikey_, 03-23-2024, 05:59 PM
        3 responses
        49 views
        0 likes
        Last Post Sam2515
        by Sam2515
         
        Started by Russ Moreland, Today, 12:54 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Erick  
        Started by philmg, Today, 12:55 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by TradeForge, 04-19-2024, 02:09 AM
        2 responses
        32 views
        0 likes
        Last Post TradeForge  
        Working...
        X