Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Not exiting properly

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

    Not exiting properly

    I have a simple strategy that has clearly defined entries and exits. Unfortunately, the strategy only shows one trade (it should show many) It apparently won't exit even though I have code to determine the exit. What am i doing wrong that would cause the entry to sit there forever and never exit?

    Code:
            protected override void Initialize()
            {
    			
                CalculateOnBarClose = false; //calculate intrabar
    			
    			Add(PeriodType.Minute, 60); //add hourly data
    			
    			TenMinCloses = new DataSeries(this); //sync primary ten minute series
    			
            }
    
    
            protected override void OnBarUpdate()
            {
    			string Direction;		//holds trade direction
    			
    			bool SmaRising = false;	//holds SMA Rising condition
    			bool MaUp = false;		//holds 8EMA above 25SMA condition
    			
    			//LeConditions and SeConditions not in use - for later development
    			bool LeConditions = Low[0] < Low[1] && High[0] < High[1] && Close[0] < Open[0]; //not in use
    			bool SeConditions = Low[0]> Low[1] && High[0] > High[1] && Close[0] > Open[0];	//not in use
    			
    			if(CurrentBars[0] <= BarsRequired ||
    				CurrentBars[1] <= BarsRequired)
    				return;
    			
    			//sync hourly data series
    			HourlyCloses = new DataSeries(SMA(BarsArray[1], 3));
    
    			
    			//process orders in the primary series (Ten Minute)
    			if(BarsInProgress ==0)
    			{
    				//check if SMA is rising on the Hourly chart------------------------
    				if(Rising(SMA(HourlyCloses,25)))
    				{
    					SmaRising = true;
    				} 
    				else
    					if(Falling(SMA(HourlyCloses,25)))
    					{
    						SmaRising = false;
    					}
    				
    				//check is 8EMA is above 25SMA on the Hourly chart------------------
    				if(EMA(HourlyCloses, 8)[0] > SMA(HourlyCloses, 25)[0])
    				{
    					MaUp = true;
    				}
    				else
    					if(EMA(HourlyCloses, 8)[0] < SMA(HourlyCloses, 25)[0])
    					{
    						MaUp = false;
    					}
    
    					
    				//establish directional bias based on hourly conditions ------------	
    				if(SmaRising && MaUp)
    				{
    					Direction = "LONG";
    				}
    				else
    					if(SmaRising == false && MaUp == false)
    					{
    						Direction = "SHORT";
    					}
    					else
    						Direction = "FLAT";
    				
    				
    				//ENTRIES-----------------------------------------------------------
    				if(Direction == "LONG" && //enter long if flat and conditions met
    					Close[0] < RegressionChannel(150, RcDeviation).Lower[0] &&
    					Position.MarketPosition == MarketPosition.Flat)
    				{
    					EnterLongStop(High[0], "LE");
    
    				}
    				else
    					if(Direction == "SHORT" && //enter short if flat and conditions met
    						Close[0] > RegressionChannel(150, RcDeviation).Upper[0] &&
    						Position.MarketPosition == MarketPosition.Flat)
    					{
    						EnterShortStop(Low[0], "SE");
    
    					}							
    
    				
    				//EXITS-------------------------------------------------------------
    				if(Position.MarketPosition == MarketPosition.Long &&
    					Direction == "SHORT") //exit long if long term conditions reverse
    				{
    					ExitLong();
    				}
    				else
    					if(Position.MarketPosition == MarketPosition.Short &&
    						Direction == "LONG") //exit short if long term conditions reverse
    					{
    						ExitShort();
    					}
    				
    
    							
    				//PRINT CONDITIONS TO OUTPUT WINDOW---------------------------------
    					Print("EMA above SMA is " + MaUp);
    					Print("SMA rising is " + SmaRising);
    				
    						
    			} //ENDS PRIMARY BARSINPROGRESS
    			
    			
            }

    #2
    Hi ShruggedAtlas,

    It will exit when the conditions are true for it to do so. You have a lot going on with your bool flags and if/ else if checks. Best is to simplify this until you are able to properly follow the code flow.

    One thing I would check first is if you need to assign your bool flags during OnBarUpdate() like you're doing here:
    bool SmaRising = false; //holds SMA Rising condition
    bool MaUp = false;

    To provide an initial value that doesn't get updated with every bar, declare and assign in variables region, like:
    private bool SmaRising = false;

    Then access in your code with SmaRising.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      Hi ShruggedAtlas,

      It will exit when the conditions are true for it to do so. You have a lot going on with your bool flags and if/ else if checks. Best is to simplify this until you are able to properly follow the code flow.

      One thing I would check first is if you need to assign your bool flags during OnBarUpdate() like you're doing here:
      bool SmaRising = false; //holds SMA Rising condition
      bool MaUp = false;

      To provide an initial value that doesn't get updated with every bar, declare and assign in variables region, like:
      private bool SmaRising = false;

      Then access in your code with SmaRising.
      I've cleaned up the code by putting those initial variables in the variables section but i'm not sure how else to simplify the code. Am I not using the if else format correctly? I looked at my output and noticed that even though conditions change to true (SMA Rising and 8EMA above 25SMA, the output never shows true conditions. Am I misusing the if else format in some way?

      Thinking out loud here:

      In order to get that first short entry, the following else statement MUST have fired:
      Code:
      				else
      					if(SmaRising == false && MaUp == false)
      					{
      						Direction = "SHORT";
      					}
      and of course:
      Code:
      				else
      					if(Direction == "SHORT" && //enter short if flat and conditions met
      						Close[0] > RegressionChannel(150, RcDeviation).Upper[0] &&
      						Position.MarketPosition == MarketPosition.Flat)
      					{
      						EnterShortStop(Low[0], "SE");
      					}
      but from that point on the position stayed short even though conditions did in fact change to long. Therefore the following code snippets should have fired but didn't :

      Code:
      				if(SmaRising == true & MaUp)
      				{
      					Direction = "LONG";
      				}
      and of course:
      Code:
      					if(Position.MarketPosition == MarketPosition.Short &&
      						Direction == "LONG") //exit short if long term conditions reverse
      					{
      						ExitShort();
      					}
      I just need to figure out why that code was ignored. Any ideas on how to investigate this? Any standard methods for debugging this?

      Comment


        #4
        The main approaches to debugging this is simplify, and using print statements to confirm values and follow code flow. The thing I would check is if your bool flags are serving some useful purpose, or just add to the complexity unnecessarily. They're useful in sequences, but I don't see that's what your using them for here. Your code will be much easier to follow and debug if you would just move all conditions for entry within one check.

        Take this example from your EnterLongStop condition. Why stretch it out over all these bool flags and string assignments?:

        Code:
        if(Rising(SMA(HourlyCloses,25)))
        {
        	SmaRising = true;
        } 
        				
        if(EMA(HourlyCloses, 8)[0] > SMA(HourlyCloses, 25)[0])
        {
        	MaUp = true;
        }
        				
        if(SmaRising && MaUp)
        {
        	Direction = "LONG";
        }
        				
        				
        if(Direction == "LONG" && //enter long if flat and conditions met
        Close[0] < RegressionChannel(150, RcDeviation).Lower[0] &&
        Position.MarketPosition == MarketPosition.Flat)
        {
        EnterLongStop(High[0], "LE");
        }
        Instead, just a single if statement with all conditions joined by && does the same thing as is much easier to read and debug.

        Code:
        if(Close[0] < RegressionChannel(150, RcDeviation).Lower[0] &&
        		Position.MarketPosition == MarketPosition.Flat &&
        		Rising(SMA(HourlyCloses,25)) &&
        		EMA(HourlyCloses, 8)[0] > SMA(HourlyCloses, 25)[0])
        {
        EnterLongStop(High[0], "LE");
        }
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Ok that gives me something to work with. It will definitely shorten my code. The reason I drew it out like that was so I could break each condition out so I could have more room for comments and organize it incrementally mostly for my own understanding but your example shows that that really isn't necessary and makes the code much more concise. Also it removes the need for the extra variables which means less to track and probably better compile and execution performance in principle.

          I'll add print statements and see what the output file shows. Thx!!

          Comment


            #6
            Great - best of luck tracking it down and please let us know if we can be of further assistance.
            Ryan M.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            656 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            371 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            109 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            574 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            579 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X