#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 } }
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
not valid error for optimization parameter
Collapse
X
-
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:
Tags: None
-
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.
Kate W.NinjaTrader Customer Service
-
Originally posted by NinjaTrader_Kate View PostHello 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.
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 chbruno, Today, 04:10 PM
|
0 responses
3 views
0 likes
|
Last Post
by chbruno
Today, 04:10 PM
|
||
Started by josh18955, 03-25-2023, 11:16 AM
|
6 responses
436 views
0 likes
|
Last Post
by Delerium
Today, 03:42 PM
|
||
Started by FAQtrader, Today, 03:35 PM
|
0 responses
6 views
0 likes
|
Last Post
by FAQtrader
Today, 03:35 PM
|
||
Started by rocketman7, Today, 09:41 AM
|
5 responses
19 views
0 likes
|
Last Post Today, 03:33 PM | ||
Started by frslvr, 04-11-2024, 07:26 AM
|
9 responses
127 views
1 like
|
Last Post
by caryc123
Today, 03:12 PM
|
Comment