• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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:	1
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 swcooke, Today, 12:33 PM
      0 responses
      1 view
      0 likes
      Last Post swcooke
      by swcooke
       
      Started by NinjaCustomer, Today, 12:10 PM
      0 responses
      5 views
      0 likes
      Last Post NinjaCustomer  
      Started by KINGKODA, Today, 11:48 AM
      0 responses
      3 views
      0 likes
      Last Post KINGKODA  
      Started by mlarocco, Today, 10:52 AM
      5 responses
      16 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by Eros Lima, Today, 09:45 AM
      2 responses
      5 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Working...
      X