Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Storing a Bool Flag
Collapse
X
-
Yes, your code makes perfect sense.Originally posted by NinjaTrader_ChelseaB View PostLast edited by Hammerhorn; 02-18-2014, 02:06 PM.
-
-
No, your test strategy works exactly as mine does. It uses the simple on off switch, but what I want to do is to store the bool = false (your does not in my modified tests). I attempted this in OnExecution, but must have failed, if I was even on the right track. I am confused on why it works now using the bool = false when the strategy exits, verses using the the bool = false on entry. I really need to have it on entry.Originally posted by NinjaTrader_ChelseaB View PostHammerhorn,
Please let me rephrase.
This demonstrates how to flip a bool and prevent orders.
Are you able to finish your strategy with this information?
Comment
-
Hello Hammerhorn,
To clarify, the example script is not working as expected.
Can you clarify how you want the false to be stored?
Currently, it is stored until the next bar at which time the bool is flipped back to true.
Was this not what you were asking for?
From post #15:
"On the next bar the longTrades bool can be set back to true if the SMA(14) is still above the close even if the strategy is in a position."Chelsea B.NinjaTrader Customer Service
Comment
-
I need to have the bool stay false after a trade until Close[0] is less than the indicator.Originally posted by NinjaTrader_ChelseaB View PostHello Hammerhorn,
To clarify, the example script is not working as expected.
Can you clarify how you want the false to be stored?
Currently, it is stored until the next bar at which time the bool is flipped back to true.
Was this not what you were asking for?
From post #15:
"On the next bar the longTrades bool can be set back to true if the SMA(14) is still above the close even if the strategy is in a position."
So, for example, once Close[0] is greater than the SMA enter a long position, exit on a profit target, and do not enter again until it crosses below and then back above the SMA. I know I can use the crossabove or other techniques to get one trade, but this will not work as my original script is much more complex and requires other conditions before an entry.
Comment
-
You cannot have it both ways. You say what I have emphasized, then in the next breath say that you do not want to use the cross. If you want to use a crossing condition, then you either use the supplied method or roll your own.Originally posted by Hammerhorn View PostI need to have the bool stay false after a trade until Close[0] is less than the indicator.
So, for example, once Close[0] is greater than the SMA enter a long position, exit on a profit target, and do not enter again until it crosses below and then back above the SMA. I know I can use the crossabove or other techniques to get one trade, but this will not work as my original script is much more complex and requires other conditions before an entry.
Comment
-
Are you referring to this,Originally posted by koganam View PostYou cannot have it both ways. You say what I have emphasized, then in the next breath say that you do not want to use the cross. If you want to use a crossing condition, then you either use the supplied method or roll your own.
"I know I can use the crossabove or other techniques to get one trade, but this will not work as my original script is much more complex and requires other conditions before an entry. "
If so I apologize as I was talking about CrossAbove(), which is designed to have just one trade within so many bars of the cross above. Unfortunately, I can't use this so I need to use the bool flag due to not knowing how many bars. Am I wrong??
A quick recap to what I am attempting,
1. pirvate int longTrades == true
2. Close[0] > SMA(14)
3. Long position entered, then longTrades == false
4. Profit target or whatever reached, no change, longTrades == false
4. Close[0] < SMA(14), then longTrades == true
5. Close[0] > SMA(14), no change, longTrades == true
6. Long Position entered, then longTrades == false
A quick recap on what is actually working
1. pirvate int longTrades == true
2. Close[0] > SMA(14)
3. Long position entered, then longTrades == false (not working)
4. Exit conditions met, exit long, longTrades == false
4. Close[0] < SMA(14), then longTrades == true
5. Close[0] > SMA(14), no change, longTrades == true
6. Long Position entered, then longTrades == falseLast edited by Hammerhorn; 02-18-2014, 07:34 PM.
Comment
-
You are using Market Orders, so the OnExecution() event handler is not needed in this particular instance.Originally posted by Hammerhorn View PostSee below.
Code:#region Variables // Wizard generated variables private bool longTrades = true; private bool shortTrades = true; #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; SetStopLoss(CalculationMode.Ticks, stopLossTicks); //Debugging TraceOrders = true; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { #region EXIT // Long: EXIT - a fail safe in case a stop does not get submitted properly. if(Position.MarketPosition == MarketPosition.Long && Close[0] < SMA(100)) { ExitLong(DefaultQuantity, "", ""); longTrades = false; } // Short: EXIT - a fail safe in case a stop does not get submitted properly. if(Position.MarketPosition == MarketPosition.Short && Close[0] > SMA(100)) { ExitShort(DefaultQuantity,"", ""); shortTrades = false; } #endregion #region ENTRY // if ((ToTime(Time[0]) >= 00000 && ToTime(Time[0]) < 103000)|| (ToTime(Time[0]) >= 164500 && ToTime(Time[0]) <= 235959)) { // Long: Enter if(Position.MarketPosition == MarketPosition.Flat && longTrades == true && Close[0] > SMA(100) && BarsSinceExit() > 1 || BarsSinceExit() == -1) { EnterLong(DefaultQuantity,""); longTrades = false; } // Short: Enter if(Position.MarketPosition == MarketPosition.Flat && shortTrades == true && Close[0] < SMA(100) && BarsSinceExit() > 1 || BarsSinceExit() == -1) { EnterShort(DefaultQuantity,""); shortTrades = false; } } //Bool Flag Reset if (Close[0] < SMA(100)) { longTrades = true; PrintWithTimeStamp(longTrades.ToString()); } if (Close[0] > SMA(100)) { shortTrades = true; } #endregion } #region OnExecution protected override void OnExecution(IExecution execution) { // Long Bool Flag if (execution.Order != null && Position.MarketPosition == MarketPosition.Long) { longTrades = false; PrintWithTimeStamp(longTrades.ToString()); } // Long Bool Flag if (execution.Order != null && Position.MarketPosition == MarketPosition.Short) { shortTrades = false; } } #endregion
Change the flag reset to use CrossBelow() and CrossAbove() with a 1 bar lookback period.
Comment
-
Sorry, to push, but why? Did not think of using the CrossBelow and CrossAbove as the flag reset, I was straight thinking entry. Thank you for your patience.Originally posted by koganam View PostYou are using Market Orders, so the OnExecution() event handler is not needed in this particular instance.
Change the flag reset to use CrossBelow() and CrossAbove() with a 1 bar lookback period.
Comment
-
Hi Hammerhorn,
To clarify, when an order is entered the longTrades bool is changed to false. At that point the you would not like the bool reset to true until the indicator value drops below Close[0] price and the rises above it once again. Is this correct?
For this you will need two variables. I have modified the sample I have created to show this.
Let me know if this is still not what you are looking for.Attached FilesChelsea B.NinjaTrader Customer Service
Comment
-
I've done following and it works
I want to execute custom stuff whenever strategy closes out of a position and I've done following to make it work reliably. Most crucial word here is volatile.
By declaring a variable as volatile, I effectively tell C# to never cache or
skip execution step that involves that variable.
Code:public volatile bool myBooleanFlag = false; protected override void OnExecution(IExecution execution) { if (execution.MarketPosition == MarketPosition.Flat) { if (myBooleanFlag == true) DoCustomStuff(); myBooleanFlag = false; } else { myBooleanFlag = true; } }
Comment
-
Because that is the meaning of the term, "CrossBelow": As long as we are above, be this; change only when we go below, means "do something when we cross below."Originally posted by Hammerhorn View PostSorry, to push, but why? Did not think of using the CrossBelow and CrossAbove as the flag reset, I was straight thinking entry. Thank you for your patience.
Comment
-
Sorry for my ignorance, but how is that different that Close[0] greater or less than an indicator in this instance?Originally posted by koganam View PostBecause that is the meaning of the term, "CrossBelow": As long as we are above, be this; change only when we go below, means "do something when we cross below."
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
599 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
345 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
558 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
558 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment