Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Bitwise operation question

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

    Bitwise operation question

    I have no idea how bitwise going to increase or decrease a value, please see questions.
    Code:
    		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;
            }

    #2
    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.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by gentlebenthebear, Today, 01:30 AM
    0 responses
    1 view
    0 likes
    Last Post gentlebenthebear  
    Started by samish18, Yesterday, 08:31 AM
    2 responses
    8 views
    0 likes
    Last Post elirion
    by elirion
     
    Started by Mestor, 03-10-2023, 01:50 AM
    16 responses
    389 views
    0 likes
    Last Post z.franck  
    Started by rtwave, 04-12-2024, 09:30 AM
    4 responses
    31 views
    0 likes
    Last Post rtwave
    by rtwave
     
    Started by yertle, Yesterday, 08:38 AM
    7 responses
    29 views
    0 likes
    Last Post yertle
    by yertle
     
    Working...
    X