public enum TrendFlags
{
Up = 0x01, // Bit is set if trend is up, unset if trend is down
Momentum = 0x02, // Bit is set if there is momentum
Squeeze = 0x04, // Bit is set if there is a squeeze happening
Ma = 0x08 // Bit is set if the MA direction is opposite to trend
};
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Check if there is enough bars for the periods of the indicators below
if (CurrentBar < 25)
return;
//********************************
trend[1] = ATR(Close, 21)[0] * atrFactor; //set series trend value
//********************************
double newValue = value[1]; // Default to previous value
int newTrend = trend[1]; // Default to previous value
// Calc some indicators
double gap =
ATR(Close, 21)[0] * atrFactor;
bool momentum =
ADX(Close, 14)[0] >= 15.0;
KeltnerChannel kc = KeltnerChannel(2, 20);
Bollinger bb = Bollinger(2, 24);
bool squeeze =
bb.Lower[0] > kc.Lower[0] && bb.Upper[0] < kc.Upper[0];
HMA hma = HMA(78);
bool ma = false;
if (squeeze)
newTrend |= (int)TrendFlags.Squeeze;
else
newTrend &= ~(int)TrendFlags.Squeeze;
if (momentum)
newTrend |= (int)TrendFlags.Momentum;
else
newTrend &= ~(int)TrendFlags.Momentum;
//*************************
// question? what is newTrend value at this point?
//*************************
if (Close[0] > value[1])
{
newTrend |= (int)TrendFlags.Up; // Uptrend
ma = (hma[0] < hma[1]); // ma going down in uptrend?
else if (Close[0] < Value[1])
{
newTrend &= ~(int)TrendFlags.Up; // Its downtrend (Not a uptrend)
ma = (hma[0] > hma[1]); // Ma going up in downtrend?
....
if (ma)
newTrend |= (int)TrendFlags.Ma;
else
newTrend &= ~(int)TrendFlags.Ma;
//*************************
// question? what is newTrend value at this point?
//*************************
}
trend[0] = newTrend;
value[0] = newValue;
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Bitwise operation question
Collapse
X
-
Bitwise operation question
I have no idea how bitwise going to increase or decrease a value, please see questions.
Code:Tags: None
-
Hello nkhoi,
Thank you for the post.
Without running and printing the values at the points you are asking about, I would be unsure what they may be. This would be something you would need to test and also reference a general C# bitwise operation guide online to confirm how each of the operators should work in contrast to the bits being used.
If you will be using bitwise operations this would be a good step for you to take to confirm your logic works as you expect. You can locate a lot of information on this subject at MSDN: https://msdn.microsoft.com/en-us/library/17zwb64t.aspx
or other general tutorial websites: https://www.tutorialspoint.com/cshar..._operators.htm
You can search online for "C# bitwise operation examples" for more similar sites.
I would also suggest that you add Prints into your script so you can see how your logic works as you execute it in the platform, we have a general guide to debugging here: https://ninjatrader.com/support/foru...58&postcount=1
The alternative would be to use a more simple type for the use case such as an enum that has not been assigned bits, which would just be integers. Or a simple int value would generally work best for signals.
I look forward to being of further assistance.
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
597 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
343 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
556 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
555 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment