• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 Dan_D_Trader, Today, 03:49 PM
    0 responses
    4 views
    0 likes
    Last Post Dan_D_Trader  
    Started by JohnAymin, Today, 02:38 PM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Tyler  
    Started by Rmos22, Today, 02:20 PM
    2 responses
    23 views
    0 likes
    Last Post Rmos22
    by Rmos22
     
    Started by sawtooth500, Today, 12:59 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Spencer  
    Started by Steve L, Yesterday, 09:36 PM
    1 response
    24 views
    0 likes
    Last Post NinjaTrader_Kate  
    Working...
    X