Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Not Enabling

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

    Strategy Not Enabling

    Hello

    I have written a basic strategy and am now starting to test it out. As I put it on a chart this morning, it would not Enable. I changed it to Enable, but there continued to be a (D) in front of the Strategy name on the chart and, as I would reopen the settings, the Enable section was automatically reset to False.

    I do not even know where to begin troubleshooting this. Could you please give me some starting points?

    Thank you for your help

    #2
    Here is the code. All of this compiles

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Basic London Breakout - Manually Defined Inputs
        /// </summary>
        [Description("Basic London Breakout - Manually Defined Inputs")]
        public class LondonBreakout : Strategy
        {
            #region Variables
    		// Range Variables
            private DateTime startRange = DateTime.Today.AddHours(2); // Hour that the range will start
           	private DateTime endRange = DateTime.Today.AddHours(9); // Hour that the range will end
    		private DateTime removeOrderTime = DateTime.Today.AddHours(18); // Hour that unfilled orders will be removed
    		private double rangeHigh; // High of range
    		private double rangeLow; // Low of range
    		
    		// Trend Variables
    		private TrendDirection direction = TrendDirection.NoTrend; // holds direction of trend to determine direction of trade
    		private string noTrend; // user defined stating no trend
    		private string up; // user defined stating Up Trend 
    		private string down; // user defied stating Down Trend
    
    		// Misc. Variables
    		private int positionSize = 1; // Default setting for PositionSize
    		private double pipBuffer = 0.0000; // distance from range that trade will be placed
    		private bool restrictTrade = false;
    		
    		// Order Management Variables
    		private IOrder LongEntry = null;
    		private IOrder ShortEntry = null;
    		private IOrder LongTarget= null;
    		private IOrder LongStop = null;
    		private IOrder ShortTarget = null;
    		private IOrder ShortStop = null;
    		private IOrder LongEntryOCO = null;
    		private IOrder ShortEntryOCO = null;
    		private IOrder LongTargetOCO = null;
    		private IOrder LongStopOCO = null;
    		private IOrder ShortTargetOCO = null;
    		private IOrder ShortStopOCO = null;
    
    		
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = false;
    			
    			// Setting overnight range
    			rangeHigh = HighestBar(High, CurrentBar-Bars.GetBar(startRange));
    			rangeLow = LowestBar(Low, CurrentBar-Bars.GetBar(startRange));
    			
    			if (Time[0] > startRange
    				&& Time[0] < endRange)
    			{
    				restrictTrade = false;
    			}
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			if (CurrentBar != Bars.GetBar(endRange))
    				return;
    			
    				// Submitting Long Only, Short Only, & OCO Entry Orders
    				if (direction == TrendDirection.Up)
    				{
    					if (LongEntry == null
    						&& Time[0] > endRange
    						&& Time[0] < removeOrderTime
    						&& restrictTrade == false)
    					{
    						LongEntry = EnterLongStop(0, true, positionSize, rangeHigh + pipBuffer, "BreakoutLong");
    					}
    				}
    				
    				else if (direction == TrendDirection.Down)
    				{
    					if (ShortEntry == null
    						&& Time[0] > endRange
    						&& Time[0] < removeOrderTime
    						&& restrictTrade == false)
    					{
    						ShortEntry = EnterShortStop(0, true, positionSize, rangeLow - pipBuffer, "BreakoutShort");
    					}
    				}
    				
    				if (direction == TrendDirection.NoTrend)
    				{
    					if (LongEntryOCO == null
    						&& ShortEntryOCO == null
    						&& Time[0] > endRange
    						&& Time[0] < removeOrderTime
    						&& restrictTrade == false)
    					{
    						LongEntryOCO = EnterLongStop(0, true, positionSize, rangeHigh + pipBuffer, "BreakoutLongOCO");
    						ShortEntryOCO = EnterShortStop(0, true, positionSize, rangeLow - pipBuffer, "BreakoutShortOCO");
    					}
    					
    				}
    				// Cancel Entry Orders if not filled by certain time
    				if (Time[0] >= removeOrderTime
    					&& LongEntry == null)
    				{
    					CancelOrder(LongEntry);
    				}
    				if (Time[0] >= removeOrderTime
    					&& ShortEntry == null)
    				{
    					CancelOrder(ShortEntry);
    				}
    				if (Time[0] >= removeOrderTime
    					&& LongEntryOCO == null)
    				{
    					CancelOrder(LongEntryOCO);
    				}
    				if (Time[0] >= removeOrderTime
    					&& ShortEntryOCO == null)
    				{
    					CancelOrder(ShortEntryOCO);
    				}
    				
    
    		}
    		protected override void OnExecution(IExecution execution)
    		{
    			// Setting Stop Loss & Profit Target (Limit) Orders -- Long Only & Short Only
    			if (execution.Order.OrderState != OrderState.Filled)
    				return;
    			
    			if (LongEntry != null
    				&& LongEntry == execution.Order)
    			{
    				LongStop = ExitLongStop((rangeLow - pipBuffer),"LongStop", "BreakoutLong");
    				LongTarget = ExitLongLimit(LongEntry.AvgFillPrice + (LongEntry.AvgFillPrice - (rangeLow - pipBuffer)), "LongTarget", "BreakoutLong");
    				restrictTrade = true;
    			}
    			
    			if (ShortEntry != null
    				&& ShortEntry == execution.Order)
    			{
    				ShortStop = ExitShortStop((rangeHigh + pipBuffer),"ShortStop", "BreakoutShort");
    				ShortTarget = ExitShortLimit(ShortEntry.AvgFillPrice - ((rangeHigh + pipBuffer) - ShortEntry.AvgFillPrice), "ShortTarget", "BreakoutShort");
    				restrictTrade = true;
    			}
    			
    			// Setting Stop Loss & Profit Target for OCO orders &&
    			// Cancel Short Entry Order if Long Entry Order is filled & vice versa
    			if (LongEntryOCO != null
    				&& LongEntryOCO == execution.Order)
    			{
    				LongStopOCO = ExitLongStop((rangeLow - pipBuffer),"LongStopOCO","BreakoutLongOCO");
    				LongTargetOCO = ExitLongLimit(LongEntryOCO.AvgFillPrice + (LongEntryOCO.AvgFillPrice - (rangeLow - pipBuffer)), "LongTargetOCO", "BreakoutLongOCO");
    				CancelOrder(ShortEntryOCO);
    				restrictTrade = true;
    			}
    			
    			if (ShortEntryOCO != null
    				&& ShortEntryOCO == execution.Order)
    			{
    				ShortStopOCO = ExitShortStop((rangeHigh + pipBuffer), "ShortStopOCO", "BreakoutShortOCO");
    				ShortTargetOCO = ExitShortLimit(ShortEntryOCO.AvgFillPrice - ((rangeHigh + pipBuffer) - ShortEntry.AvgFillPrice), "ShortTargetOCO", "BreakoutShortOCO");
    				CancelOrder(LongEntryOCO);
    				restrictTrade = true;
    			}
    			
    
    		}
    				
    				
    
    		
    
            #region Properties
    		[Description("Direction")]
    		[GridCategory("Parameters")]
    		[Gui.Design.DisplayName("4 Direction")]
    		public TrendDirection Direction
    		{
    			get { return direction; }
    			set { direction = value; }
    		}
    		
            [Description("Position Size of Trade")]
            [GridCategory("Parameters")]
    		[Gui.Design.DisplayName("5 Position Size")]
            public int PositionSize
            {
                get { return positionSize; }
                set { positionSize = Math.Max(1, value); }
            }
    
            [GridCategory("Parameters")]
    		[Gui.Design.DisplayName("1 Start Range")]
            public DateTime StartRange
            {
                get { return startRange; }
                set { startRange = value; }
            }
    		
            [Description("Range End Hour")]
            [GridCategory("Parameters")]
    		[Gui.Design.DisplayName("2 End Range")]
            public DateTime EndRange
            {
                get { return endRange; }
                set { endRange = value; }
            }
    
            [Description("Hour to Remove Orders")]
            [GridCategory("Parameters")]
    		[Gui.Design.DisplayName("3 Remove Order")]
            public DateTime RemoveOrderHour
            {
                get { return removeOrderTime; }
                set { removeOrderTime = value; }
            }
    		
    		[Description("Pip Buffer")]
    		[GridCategory("Parameters")]
    		[Gui.Design.DisplayName("6 Pips")]
    		public double PipBuffer
    		{
    			get { return pipBuffer; }
    			set { pipBuffer = Math.Max(0.0001, value); }
    		}
            #endregion
        }
    }
    
    public enum TrendDirection
    {
    	NoTrend,
    	Up,
    	Down
    }

    Comment


      #3
      jg123, would you see any log errors? I think you should see an issue stating CurrentBar property could not be accessing from your Initialize() method here.

      Comment


        #4
        Hi Bertrand

        You are definitely correct. It was as simple as looking at the Log in order to find out what may be going on here (need to get better at debugging!)

        Which leads to me to my next issue. I don't know where to put that bit of code that is causing the problem

        Code:
        					// Setting overnight range
        			rangeHigh = HighestBar(High, CurrentBar-Bars.GetBar(startRange));
        			rangeLow = LowestBar(Low, CurrentBar-Bars.GetBar(startRange));
        If I put it in OnBarUpdate() then it will not be recognized in OnExecution() and I can't put it in the Variables section because I get other errors with that.

        Could you help me with where to properly put that code?

        Comment


          #5
          You definitely want to process those bar related calculations in your OnBarUpdate(). If the variables have global scope then other methods could for sure access them as well.

          Comment


            #6
            Oh okay. I put it there now and it is working. This confuses me a bit because I thought that if a variable was defined within a method (are they called OnBarUpdate(), etc called methods?) that they would not be readable in others. Clearly I misunderstood something along the way.

            Expected something to happen that hasn't happened yet, so attempting to debug it on my own and will be back if I have any questions. Thanks!

            Comment


              #7
              That's true, however in your script you define them with class scope (in your variables region), so outside any of the methods themselves.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              669 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              378 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              111 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              575 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              580 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X