NexusFi: Find Your Edge


Home Menu

 





Splitting one big IF in different sinlge "OR"-parts


Discussion in NinjaTrader

Updated
    1. trending_up 1,283 views
    2. thumb_up 4 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 2 attachments




 
Search this Thread
  #1 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 541 since May 2012
Thanks Given: 1,422
Thanks Received: 227

Hello programmers,

I am having a logical problem with the "if-solution", that I got here in this forum from a very helpful person

 
Code
List<bool> conditionEnabledEntryShort = new List<bool>();
conditionEnabledEntryShort.Add(activShort);
    if (activEntry1) conditionEnabledExitShort.Add(shortEntry1());
    if (activEntryFilter1) conditionEnabledExitShort.Add(shortEntryFilter1());
    if (activEntryFilter2) conditionEnabledExitShort.Add(shortEntryFilter2());
    if (activEntryFilter3) conditionEnabledExitShort.Add(shortEntryFilter3());
					
bool shouldEnterShort = conditionEnabledEntryShort.All(condition => condition);

if (shouldEnterShort)
    {
        EnterShort(Convert.ToInt32(contractsShort), @"EntryShort");
    }
This code works perfect in the case, that different conditions are working at one time, if activated with "true" in the parameter list. With deactivating als false I can reduce the conditions. So the "if" are connected with an imaginary "and".

If I want to use this structure in case of imaginary "or" for different stopp conditions, I cant use this with "if". I need an "or"-connector - but with "else if", "else" or reverse order or the negation with "!" like (!activEntry1) I cant build the scenario. I tried a while and with debugging - but I did not find the way of solution, that is working.

Have you got an idee or can telling me, what I am doing wrong?

This is, what I am searching (but not in one statement - I need 4 single statements:
 
Code
    IF (activExit1) conditionEnabledExitShort.Add(shortExit1());
    OR (activExit2) conditionEnabledExitShort.Add(shortExit2());
    OR (activExit3) conditionEnabledExitShort.Add(shortExit3());
    OR (activExit4) conditionEnabledExitShort.Add(shortExit4());
Thank you for open my eyes.

Best regards,
Renkotrader


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Weekend Update: First Qatari LNG Transit Attempted -- IR …
Traders Hideout
March Jobs Report Update: 178K Beat vs 59K Expected, Wag …
Traders Hideout
Iran Ceasefire Surges to 19.5% on US 15-Point Plan -- 82 …
Prediction Markets & Event Contracts
Iran Airspace Collapses 18 Points to 15.5% While Hormuz …
Prediction Markets & Event Contracts
Eurex Eyes Prediction Markets -- Europes Biggest Derivat …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
21 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
2026 Fire Horse
5 thanks
  #2 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Legendary Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,580 since Oct 2009
Thanks Given: 4,266
Thanks Received: 6,199

@Renkotrader

What about this:

 
Code
List<bool> conditionEnabledExitShort = new List<bool>();

if (activExit1)
{
    conditionEnabledExitShort.Add(shortExit1());
}
if (activExit2)
{
    conditionEnabledExitShort.Add(shortExit2());
}
if (activExit3)
{
    conditionEnabledExitShort.Add(shortExit3());
}
if (activExit4)
{
    conditionEnabledExitShort.Add(shortExit4());
}

bool shouldExitShort = conditionEnabledExitShort.Contains(true);

if (shouldExitShort)
{
    // Execute your code when at least one of the conditions is true
}


Reply With Quote
Thanked by:
  #3 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 541 since May 2012
Thanks Given: 1,422
Thanks Received: 227


Hello trendisyourfriend,

thank you for you help

I reduced my project for this example. It is compiling without errors, but its not works. That means: no results. I hope, that I did not generated a new bug.

In this example are only four time exits - no other kind of exit strategy:
a) open COMEX/NYMEX
b) open NYSE
c) close XETRA
d) End of trading day

 
Code
		// Variables Fix Exit Times (realized as period: Start & End)	
		private DateTime nymexOpenStart;
		private DateTime nymexOpenEnd;		
		private DateTime nyseOpenStart;
		private DateTime nyseOpenEnd;		
		private DateTime xetraOpenStart;
		private DateTime xetraOpenEnd;	
		private DateTime endTradingDayStart;
		private DateTime endTradingDayEnd;		

		// Entry Long
		private bool longEntry1() { return (Close[0] > High[1]); }																									
		private bool longEntry2() { return (Close[0] > SMA1[0]); }											
		
		// Conditions Fix Exit Times (realized as period: Start & End)
		private bool fixExit1() { return ((Times[0][0].TimeOfDay >= nymexOpenStart.TimeOfDay) 		&& (Times[0][0].TimeOfDay <= nymexOpenEnd.TimeOfDay)); }
		private bool fixExit2() { return ((Times[0][0].TimeOfDay >= nyseOpenStart.TimeOfDay) 		&& (Times[0][0].TimeOfDay <= nyseOpenEnd.TimeOfDay)); }		
		private bool fixExit3() { return ((Times[0][0].TimeOfDay >= xetraOpenStart.TimeOfDay) 		&& (Times[0][0].TimeOfDay <= xetraOpenEnd.TimeOfDay)); }				
		private bool fixExit4() { return ((Times[0][0].TimeOfDay >= endTradingDayStart.TimeOfDay) 	&& (Times[0][0].TimeOfDay <= endTradingDayEnd.TimeOfDay)); }		
	

		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				BarsRequiredToTrade							= 20;				
				Calculate									= Calculate.OnBarClose;
				Description									= @"";
				EntriesPerDirection							= 1;
				EntryHandling								= EntryHandling.UniqueEntries;
				ExitOnSessionCloseSeconds					= 300;
				IsExitOnSessionCloseStrategy				= true;				
				IsFillLimitOnTouch							= false;
				IsInstantiatedOnEachOptimizationIteration	= true;
				MaximumBarsLookBack							= MaximumBarsLookBack.TwoHundredFiftySix;
				Name										= "ExampleActiveFilterWithOR";
				OrderFillResolution							= OrderFillResolution.Standard;
				RealtimeErrorHandling						= RealtimeErrorHandling.StopCancelClose;
				Slippage									= 0;
				StartBehavior								= StartBehavior.WaitUntilFlat;
				StopTargetHandling							= StopTargetHandling.ByStrategyPosition;
				TimeInForce									= TimeInForce.Day;
				TraceOrders									= false;
						
				activeLong 									= true;
				activeShort 								= true;
				activeEntry1 								= true;
				activeEntry2 								= true;		
				activeFixExit1								= false;
				activeFixExit2								= false;
				activeFixExit3								= false;
				activeFixExit4								= false;									

				contractsLong								= 1;
				contractsShort								= 1;	
				smaPeriods 									= 40;
				
				// Fix Exit Times (realized as period: Start & End)		
				nymexOpenStart								= DateTime.Parse("14:15", System.Globalization.CultureInfo.InvariantCulture);
				nymexOpenEnd								= DateTime.Parse("14:25", System.Globalization.CultureInfo.InvariantCulture);							
				nyseOpenStart								= DateTime.Parse("15:15", System.Globalization.CultureInfo.InvariantCulture);
				nyseOpenEnd									= DateTime.Parse("15:25", System.Globalization.CultureInfo.InvariantCulture);				
				xetraOpenStart								= DateTime.Parse("17:20", System.Globalization.CultureInfo.InvariantCulture);
				xetraOpenEnd								= DateTime.Parse("17:30", System.Globalization.CultureInfo.InvariantCulture);							
				endTradingDayStart							= DateTime.Parse("21:40", System.Globalization.CultureInfo.InvariantCulture);
				endTradingDayEnd							= DateTime.Parse("21:55", System.Globalization.CultureInfo.InvariantCulture);
			}
			
			else if (State == State.Configure)
			{
			}
			
			else if (State == State.DataLoaded)
			{											
			}
		}

		protected override void OnBarUpdate()
		{								
			if (CurrentBar < BarsRequiredToTrade)
		        return;
			
			if (BarsInProgress != 0) 
					return;
			
			{		
				List<bool> conditionEnabledExitLong = new List<bool>();
				if (activeFixExit1)
				{
				    conditionEnabledExitLong.Add(fixExit1());
				}
				if (activeFixExit2)
				{
				    conditionEnabledExitLong.Add(fixExit2());
				}
				if (activeFixExit3)
				{
				    conditionEnabledExitLong.Add(fixExit3());
				}
				if (activeFixExit4)
				{
				    conditionEnabledExitLong.Add(fixExit4());
				}
				bool shouldExitLong = conditionEnabledExitLong.Contains(true);
				if (shouldExitLong)
				{
				    Print("ExitLong " + Time [0]);
					ExitLong(Convert.ToInt32(contractsLong), @"ExitLong", @"EntryLong");
				}						
			}				
		}
Best regards,
Renkotrader


Attached Files
Elite Membership required to download: ExampleActiveFilterWithOR.cs
Started this thread Reply With Quote
  #4 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Legendary Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,580 since Oct 2009
Thanks Given: 4,266
Thanks Received: 6,199

You did not say what went wrong. I've observed something in your script: the way you've defined your exit time may not be effective because it doesn't account for the possibility of a trading day spanning two days. For instance, the ES market opens at 18:00 ET but closes at 17:00 ET on the following day. To address this, consider using the SessionIterator to accurately assign time values to your variables to assign the correct time to your variables:

Look at my last indicator and study how i use it.



Renkotrader View Post
Hello trendisyourfriend,

thank you for you help

I reduced my project for this example. It is compiling without errors, but its not works. That means: no results. I hope, that I did not generated a new bug.

In this example are only four time exits - no other kind of exit strategy:
a) open COMEX/NYMEX
b) open NYSE
c) close XETRA
d) End of trading day

 
Code
		// Variables Fix Exit Times (realized as period: Start & End)	
		private DateTime nymexOpenStart;
		private DateTime nymexOpenEnd;		
		private DateTime nyseOpenStart;
		private DateTime nyseOpenEnd;		
		private DateTime xetraOpenStart;
		private DateTime xetraOpenEnd;	
		private DateTime endTradingDayStart;
		private DateTime endTradingDayEnd;		

		// Entry Long
		private bool longEntry1() { return (Close[0] > High[1]); }																									
		private bool longEntry2() { return (Close[0] > SMA1[0]); }											
		
		// Conditions Fix Exit Times (realized as period: Start & End)
		private bool fixExit1() { return ((Times[0][0].TimeOfDay >= nymexOpenStart.TimeOfDay) 		&& (Times[0][0].TimeOfDay <= nymexOpenEnd.TimeOfDay)); }
		private bool fixExit2() { return ((Times[0][0].TimeOfDay >= nyseOpenStart.TimeOfDay) 		&& (Times[0][0].TimeOfDay <= nyseOpenEnd.TimeOfDay)); }		
		private bool fixExit3() { return ((Times[0][0].TimeOfDay >= xetraOpenStart.TimeOfDay) 		&& (Times[0][0].TimeOfDay <= xetraOpenEnd.TimeOfDay)); }				
		private bool fixExit4() { return ((Times[0][0].TimeOfDay >= endTradingDayStart.TimeOfDay) 	&& (Times[0][0].TimeOfDay <= endTradingDayEnd.TimeOfDay)); }		
	

		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				BarsRequiredToTrade							= 20;				
				Calculate									= Calculate.OnBarClose;
				Description									= @"";
				EntriesPerDirection							= 1;
				EntryHandling								= EntryHandling.UniqueEntries;
				ExitOnSessionCloseSeconds					= 300;
				IsExitOnSessionCloseStrategy				= true;				
				IsFillLimitOnTouch							= false;
				IsInstantiatedOnEachOptimizationIteration	= true;
				MaximumBarsLookBack							= MaximumBarsLookBack.TwoHundredFiftySix;
				Name										= "ExampleActiveFilterWithOR";
				OrderFillResolution							= OrderFillResolution.Standard;
				RealtimeErrorHandling						= RealtimeErrorHandling.StopCancelClose;
				Slippage									= 0;
				StartBehavior								= StartBehavior.WaitUntilFlat;
				StopTargetHandling							= StopTargetHandling.ByStrategyPosition;
				TimeInForce									= TimeInForce.Day;
				TraceOrders									= false;
						
				activeLong 									= true;
				activeShort 								= true;
				activeEntry1 								= true;
				activeEntry2 								= true;		
				activeFixExit1								= false;
				activeFixExit2								= false;
				activeFixExit3								= false;
				activeFixExit4								= false;									

				contractsLong								= 1;
				contractsShort								= 1;	
				smaPeriods 									= 40;
				
				// Fix Exit Times (realized as period: Start & End)		
				nymexOpenStart								= DateTime.Parse("14:15", System.Globalization.CultureInfo.InvariantCulture);
				nymexOpenEnd								= DateTime.Parse("14:25", System.Globalization.CultureInfo.InvariantCulture);							
				nyseOpenStart								= DateTime.Parse("15:15", System.Globalization.CultureInfo.InvariantCulture);
				nyseOpenEnd									= DateTime.Parse("15:25", System.Globalization.CultureInfo.InvariantCulture);				
				xetraOpenStart								= DateTime.Parse("17:20", System.Globalization.CultureInfo.InvariantCulture);
				xetraOpenEnd								= DateTime.Parse("17:30", System.Globalization.CultureInfo.InvariantCulture);							
				endTradingDayStart							= DateTime.Parse("21:40", System.Globalization.CultureInfo.InvariantCulture);
				endTradingDayEnd							= DateTime.Parse("21:55", System.Globalization.CultureInfo.InvariantCulture);
			}
			
			else if (State == State.Configure)
			{
			}
			
			else if (State == State.DataLoaded)
			{											
			}
		}

		protected override void OnBarUpdate()
		{								
			if (CurrentBar < BarsRequiredToTrade)
		        return;
			
			if (BarsInProgress != 0) 
					return;
			
			{		
				List<bool> conditionEnabledExitLong = new List<bool>();
				if (activeFixExit1)
				{
				    conditionEnabledExitLong.Add(fixExit1());
				}
				if (activeFixExit2)
				{
				    conditionEnabledExitLong.Add(fixExit2());
				}
				if (activeFixExit3)
				{
				    conditionEnabledExitLong.Add(fixExit3());
				}
				if (activeFixExit4)
				{
				    conditionEnabledExitLong.Add(fixExit4());
				}
				bool shouldExitLong = conditionEnabledExitLong.Contains(true);
				if (shouldExitLong)
				{
				    Print("ExitLong " + Time [0]);
					ExitLong(Convert.ToInt32(contractsLong), @"ExitLong", @"EntryLong");
				}						
			}				
		}
Best regards,
Renkotrader


Reply With Quote
Thanked by:
  #5 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 541 since May 2012
Thanks Given: 1,422
Thanks Received: 227

Hi trendisyourfriend

1. The time zone, the nightly break, is not a problem in my case. The reason is: I am German and I am working inside the Central European Time zome. That means: our trading days in Futures are starting at 00:00 o'clock and they ends at 22:00 at EUREX or 23:00 at CME. The only problem could be there, if Germany or the USA are switching to summer or winter time, because there is a time period from three weeks difference, for that the periods are not synchronized. But I will take a look in your Indicator - its looks very professional.

2. I had forgotten to put an entry condition in my example script. Here is the complete example script, that is working. So another very good job from you and I am saying "thank you!".

3. Another problem with the script is, that I am using its with a renko bar type. I added "AddDataSeries(BarsPeriodType.Minute, 1);" and this works well in combination with "if (BarsInProgress == 0)" and "if (BarsInProgress == 1)", if I am using time candles. So in using the close for a candle in my strategy, the fix exit for example 14:20 is working at 14:21 instead 14:25 at 5-minutes-chart. Same to other higher time bars. But in using renko bars it is not working. Have you got a solution for me in this?

4. I tried to move the time conditions for "Exit Long" and "Exit Short" to "OnExecutionUpdate", because this is the right place, if I am understanding the things. I dont know, why the startegy builder is not doing its. So I made each thing in "OnBarUpdate". If I am moving the code in this section, then the 4 activating/deactivating buttons in the parameter list are having no effect. That means: ignoring the decission for "on". For that an idea or a solution would be great, too.

Have a great day,
Renkotrader


Attached Files
Elite Membership required to download: ExampleActiveFilterWithOR2.cs
Started this thread Reply With Quote




Last Updated on September 16, 2023


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts