NexusFi: Find Your Edge


Home Menu

 





Indicator to check consolidation


Discussion in NinjaTrader

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




 
Search this Thread
  #1 (permalink)
 danjuma 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, IB TWS
Broker: IB, Kinetic
Trading: Stocks, Forex
Posts: 99 since Nov 2011
Thanks Given: 48
Thanks Received: 16

Hello people.

Would be very grateful for your assistance with this code please. I have already posted it on the NT forum, but not getting much response/assistance at present. Basically its an indicator I am trying to write to use in the Market Analyzer of NT. I am a complete novice to NT scripts and C#, and this is just my crude and primitive attempt using the NT indicator wizard and nicking snippets of code here and there.

What I am after is an indicator to return 1 if a condition is met or 0 otherwise. The condition is basically to look at the last four bars to see if are in a very narrow range (highest range not more than say 0.25c) and also if close to the day's high or low so far.

Not surprisingly, it failed to compile, giving me the following errors:
Class member declaration expected
Invalid token 'namespace' in class, struct, or interface member declaration
Invalid token '{' in class, struct, or interface member declaration
} expected

Many thanks for you assistance.


 
Code
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
    /// <summary>
    /// Checks if last 4 bars have very narrow range and close to day high or low
    /// </summary>
    [Description("Checks if last 4 bars have very narrow range and close to day high or low")]
    public class Consolidating : Indicator
    {
        #region Variables
        // Wizard generated variables
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the indicator and is called once before any bar data is loaded.
        /// </summary>
        protected override void Initialize()
        {
            Overlay                = false;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            bool MaxRng4Bars = false;
            double maxHigh = MAX(High,4)[0];
            double minLow =  MIN(Low,4)[0];
            double maxHlessDayH = CurrentDayOHL().CurrentHigh[0] - maxHigh;
            double minLlessDayL = minLow - CurrentDayOHL().CurrentLow[0];
            bool CloseToHigh = false;
            bool CloseToLow = false;
            
            if (Range()[0]<0.26 && Range()[1]<0.26 && Range()[2]<0.26 && Range()[3]<0.26)
             {
                MaxRng4Bars = true;
             { 
            if (maxHlessDayH >= 0 && maxHlessDayH < 0.5)
             {
                CloseToHigh = true;
             }
            if (minLlessDayL >= 0 && minLlessDayL < 0.5)
             {
                CloseToLow = true;
             }
         
            
             Value.Set(MaxRng4Bars = true && CloseToHigh = true && CloseToLow = true  ? 1 :0);
                
        }


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
CME Lists U.S. Election Event Contracts as 2028 Democrat …
Prediction Markets & Event Contracts
Iran Ceasefire Surges to 19.5% on US 15-Point Plan -- 82 …
Prediction Markets & Event Contracts
The 50/50 Paradox: Peace and Invasion Each at 20% -- Ira …
Prediction Markets & Event Contracts
BofA Projects $1.1 Trillion in Sports Event Contracts -- …
Prediction Markets & Event Contracts
Cboe Files for Near 24x5 Equities Trading -- December 20 …
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
23 thanks
2026 Jlab journal
10 thanks
Trying to learn Volume and price action correlation
7 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
Algo automated / semi-automated trading anyone?
6 thanks
  #2 (permalink)
 danjuma 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, IB TWS
Broker: IB, Kinetic
Trading: Stocks, Forex
Posts: 99 since Nov 2011
Thanks Given: 48
Thanks Received: 16

Ok, been working on the code (basically through trial and error, not too sure exactly what I am doing ). However, the amended code below so far has compiled successfully. I have not tested it yet as the markets (US stocks) are currently closed. Logically though, does it look correct, will it do what I want it to do? Many thanks.


 
Code
protected override void OnBarUpdate()
        {
            
            
            double maxHlessDayH = CurrentDayOHL().CurrentHigh[0] - MAX(High,4)[0];
            double minLlessDayL = MIN(Low,4)[0] - CurrentDayOHL().CurrentLow[0];
            int MaxRng4Bars, CloseToHigh, CloseToLow;
            
            if (Range()[0]<0.26 && Range()[1]<0.26 && Range()[2]<0.26 && Range()[3]<0.26)
                          
                 MaxRng4Bars = 1;
            else
            {
                 MaxRng4Bars = 0;
            }
              
            if (maxHlessDayH >= 0 && maxHlessDayH < 0.5)
             
                 CloseToHigh = 1;
            else
            {
                 CloseToHigh = 0;
            }
             
            if (minLlessDayL >= 0 && minLlessDayL < 0.5)
            
                 CloseToLow = 1;
            else
             {
                  CloseToLow = 0;
             }
           
            if (MaxRng4Bars == 1 && CloseToHigh == 1 && CloseToLow == 1)
                
             Value.Set(1);
            else
            {
             Value.Set(0);
            
            }


Started this thread Reply With Quote
  #3 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623


As a novice, make sure you use curly brackets with every if statement, ie:

 
Code
                            
if (X)
{
     
//statement 
}
else
{
    
//statement

C# will process only first line after if when curly bracket is missing so it is a good practice to use them, especially when you don't know what your are doing


Reply With Quote
Thanked by:
  #4 (permalink)
 danjuma 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, IB TWS
Broker: IB, Kinetic
Trading: Stocks, Forex
Posts: 99 since Nov 2011
Thanks Given: 48
Thanks Received: 16


gregid View Post
As a novice, make sure you use curly brackets with every if statement, ie:

 
Code
                            
if (X)
{
     
//statement 
}
else
{
    
//statement

C# will process only first line after if when curly bracket is missing so it is a good practice to use them, especially when you don't know what your are doing

Thanks for the tip gregid. Logically though, apart from the missing curly brackets, does the code look ok? Thanks


Started this thread Reply With Quote
  #5 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623

your conditions are quite simple so I don't see why it wouldn't work. Just load it, test and see for yourself =)


Reply With Quote
Thanked by:




Last Updated on February 26, 2012


© 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