Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

not valid error for optimization parameter

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

    not valid error for optimization parameter

    I have been running my back testing in standard mode just fine. I switched to optimization mode and now one of my optimization parameters is throwing 'not valid' error even though the optimization range I have set is within the range in the code. I have crawled through the code to see what I am missing here and nothing is standing out. Below is source code and a screenshot of the error.


    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Strategies in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class AfterHoursStudy : Strategy
        {
            private DateTime openTradeTime;
            private DateTime closeTradeTime;
            private bool buyAndHold;
            private int sharesToTrade;
            private double previousDayBuyThresholdGreaterThan;
            private double previousDayBuyThresholdLessThan;
            private int thresholdPeriodInDays;
            private bool doReinvestGains;
            private bool doTradeMondayTuesdayOnly;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"To backtest the performance of trading market hours and after hours seperately.";
                    Name                                        = "AfterHoursStudy";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = false;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 1;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
    
                    OpenTradeTime = default(DateTime);
                    CloseTradeTime = default(DateTime);
                    BuyAndHold = false;
                    SharesToTrade = 500;
                    PreviousDayBuyThresholdGreaterThan = 5;
                    PreviousDayBuyThresholdLessThan = -5;
                    ThresholdPeriodInDays = 5;
                    DoReinvestGains = false;
                    DoTradeMondayTuesdayOnly = false;
                }
                else if (State == State.Configure)
                {
                    openTradeTime = OpenTradeTime;
                    closeTradeTime = CloseTradeTime;
                    buyAndHold = BuyAndHold;
                    sharesToTrade = SharesToTrade;
                    previousDayBuyThresholdGreaterThan = PreviousDayBuyThresholdGreaterThan;
                    previousDayBuyThresholdLessThan = PreviousDayBuyThresholdLessThan;
                    thresholdPeriodInDays = ThresholdPeriodInDays;
                    doReinvestGains = DoReinvestGains;
                    doTradeMondayTuesdayOnly = DoTradeMondayTuesdayOnly;
                }
            }
    
            protected override void OnBarUpdate()
            {
    
                //Print(Time[0].ToString("MM/dd/yyyy HH:mm") + "-" + Close[0]);
    
                if (CurrentBar < BarsRequiredToTrade)
                    return;
    
                DateTime currentTimeInLocalTimezone = DateTime.SpecifyKind(Time[0], DateTimeKind.Local);
                DateTime _openTradeTime = openTradeTime;
                DateTime _closeTradeTime = closeTradeTime;
    
                if(currentTimeInLocalTimezone.IsDaylightSavingTime())
                {
                    _openTradeTime = openTradeTime.AddHours(1);
                    _closeTradeTime = closeTradeTime.AddHours(1);
                }            
    
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    if(buyAndHold)
                        EnterLong();
                    else if (currentTimeInLocalTimezone.Hour == _openTradeTime.Hour && currentTimeInLocalTimezone.Minute == _openTradeTime.Minute)
                    {
                        double previousSessionPerformance = (Close[0] - Close[thresholdPeriodInDays * 2]) / Close[thresholdPeriodInDays * 2] * 100;
    
    //                    Print("session start: " + Time[2].ToString("MM/dd/yyyy HH:mm") + "-" + Close[2]);
    //                    Print("session end (current): " + Time[0].ToString("MM/dd/yyyy HH:mm") + "-" + Close[0]);
    //                    Print("Previous Session performance: " + previousSessionPerformance);
    
                        if(previousSessionPerformance > PreviousDayBuyThresholdGreaterThan || previousSessionPerformance <= PreviousDayBuyThresholdLessThan)                        
                        {
                            int additionalShares = 0;
    
                            if(doReinvestGains)
                                additionalShares = (int)(SystemPerformance.AllTrades.TradesPerformance.NetProfit / Close[0]);
    
                            if(doTradeMondayTuesdayOnly)
                            {
                                if(currentTimeInLocalTimezone.DayOfWeek == DayOfWeek.Friday ||
                                    currentTimeInLocalTimezone.DayOfWeek == DayOfWeek.Monday) // Friday to Monday and Monday to Tuesday
                                    EnterLong(sharesToTrade + additionalShares);                            
                            }
                            else
                                EnterLong(sharesToTrade + additionalShares);
                        }
    
                        //Print("Opened: " + Time[0].ToString("MM/dd/yyyy HH:mm") + "-" + Close[0]);
                    }
                }
                else
                {
                    if(!buyAndHold)
                    {
                        //Print("Closed: " + Time[0].ToString("MM/dd/yyyy HH:mm") + "-" + Close[0]);
                        if (currentTimeInLocalTimezone.Hour == _closeTradeTime.Hour && currentTimeInLocalTimezone.Minute == _closeTradeTime.Minute)
                            ExitLong();
                    }
                }
            }
    
            #region Properties
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Shares to Trade", GroupName = "Order Settings", Order = 1)]
            [Range(1, int.MaxValue), NinjaScriptProperty]
            public int SharesToTrade {get; set;}
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Open Trade Time (local DST)", GroupName = "Order Settings", Order = 2)]
            [Gui.PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
            public DateTime OpenTradeTime {get; set;}
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Close Trade Time (local DST)", GroupName = "Order Settings", Order = 3)]
            [Gui.PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
            public DateTime CloseTradeTime {get; set;}
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Buy and Hold (override)", GroupName = "Trade Settings", Order = 1)]
            public bool BuyAndHold {get; set;}                
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Reinvest Gains", GroupName = "Trade Settings", Order = 2)]
            public bool DoReinvestGains {get; set;}
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Previous Day Gains Buy Threshhold (greater than)", GroupName = "Trade Filters", Order = 1)]
            [Range(0, 100), NinjaScriptProperty]
            public double PreviousDayBuyThresholdGreaterThan {get; set;}
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Previous Day Gains Buy Threshhold (less than)", GroupName = "Trade Filters", Order = 2)]
            [Range(-100, 0), NinjaScriptProperty]
            public double PreviousDayBuyThresholdLessThan {get; set;}
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Threshold Period in Days", GroupName = "Trade Filters", Order = 3)]
            [Range(1, 60), NinjaScriptProperty]
            public int ThresholdPeriodInDays {get; set;}
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "Only Trade Monday and Tuesday", GroupName = "Trade Filters", Order = 4)]
            public bool DoTradeMondayTuesdayOnly {get; set;}
    
            #endregion
        }
    }


    Click image for larger version

Name:	ErrorMessage.png
Views:	157
Size:	1.07 MB
ID:	1054807

    #2
    Hello krugman25,

    Thank you for your post.

    I was able to copy and import your script and run the optimization without hitting this error. Do you have data available for SPY all the way back to 1992?

    I'll definitely need to take a look at your log and trace files to further troubleshoot. Please write into to PlatformSupport[AT]NinjaTrader[DOT]Com along with your log and trace files and a link to this forum post.

    Include ticket number 2133663 in the body of the email.

    Please follow the steps below to manually attach your log and trace files to your response so I may investigate this matter further.
    • Open your NinjaTrader folder under Documents.
    • Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
    • Send the 2 compressed folders as attachments to this email.
    • Once complete, you can delete these compressed folders.
    We look forward to assisting further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Kate View Post
      Hello krugman25,

      Thank you for your post.

      I was able to copy and import your script and run the optimization without hitting this error. Do you have data available for SPY all the way back to 1992?

      I'll definitely need to take a look at your log and trace files to further troubleshoot. Please write into to PlatformSupport[AT]NinjaTrader[DOT]Com along with your log and trace files and a link to this forum post.

      Include ticket number 2133663 in the body of the email.

      Please follow the steps below to manually attach your log and trace files to your response so I may investigate this matter further.
      • Open your NinjaTrader folder under Documents.
      • Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
      • Send the 2 compressed folders as attachments to this email.
      • Once complete, you can delete these compressed folders.
      We look forward to assisting further.
      My SPY data starts in 1993.

      I sent the email to support as you instructed. I look forward to seeing what you find. Thank you.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by sightcareclickhere, Today, 01:55 PM
      0 responses
      1 view
      0 likes
      Last Post sightcareclickhere  
      Started by Mindset, 05-06-2023, 09:03 PM
      9 responses
      258 views
      0 likes
      Last Post ender_wiggum  
      Started by Mizzouman1, Today, 07:35 AM
      4 responses
      18 views
      0 likes
      Last Post Mizzouman1  
      Started by philmg, Today, 01:17 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Started by cre8able, Today, 01:01 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X