Announcement

Collapse
No announcement yet.

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

    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.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        44 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 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