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

Finding nr of bars indicator rising/falling

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

    Finding nr of bars indicator rising/falling

    Hi guys

    I would like to find some simple code for determining whether a study, such as MACD, has been rising (or falling) for n bars.

    Let’s say (as a pure example – I’m picking these indicators out of a hat!) that I wish to enter long if:

    a) EMA is rising, and

    b) MACD has been rising for n <= 3 bars.

    So, to determine b), is there a simple way of effectively counting the number of bars an indicator has been rising?

    I think this might be achievable using the slope function and a loop of some sort, but can this be avoided?

    Any help with this much appreciated.

    #2
    Hello,

    For EMA rising, you can use the Rising() method. This will only check if it is greater than the last bar.

    If you want to check the last 3 bars, you can create your own rising condition by checking the last 3 bars

    Code:
    			if(MACD(12, 26, 9)[0] > MACD(12, 26, 9)[1] && MACD(12, 26, 9)[1] > MACD(12, 26, 9)[2])
    			{
    				//do something
    			}

    Please let me know if you have additional questions.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Thanks very much, Matthew, for your quick reply.

      Of course, the code you very kindly gave me would be completely effective to check the last three bars for MACD rising.

      Forgive me, but I wasn’t totally precise in what I was searching for, which was something more algebraic in character.

      What I would like to do is to be able to declare a variable n, such as:

      Code:
      private int n = 5
      so that by changing n in the ‘Strategies’ box or in Strategy Analyzer, I can vary the number of bars to be checked for ‘rising’.

      I was wondering whether they might exist a way of doing this that avoided looping routines. If you don’t think there is a simple way, maybe you could kindly let me know and I’ll try to piece together some code and run it past you, if that’s OK?

      I look forward to hearing from you again.

      Comment


        #4
        ...
        private int n = 5; \\ This is user input
        private int RisingBarsCount = 0, FallingBarsCount = 0;
        ....

        protected override void OnBarUpdate()
        {
        ....
        if( MACD(12, 26, 9)[0] > MACD(12, 26, 9)[1] ) { RisingBarsCount++; FallingBarsCount = 0; }
        if( MACD(12, 26, 9)[0] < MACD(12, 26, 9)[1] ) { FallingBarsCount++; RisingBarsCount = 0; }
        if( RisingBarsCount == N ) { /* do something */ }
        if( FallingBarsCount == N ) { /* do something */ }
        .....
        }
        fx.practic
        NinjaTrader Ecosystem Vendor - fx.practic

        Comment


          #5
          Hi fx.practic

          Thanks so much for your help this. I haven't been able to spend a moment on trying to code this since my last post. It would have have required a great deal of work on my part to get anything that worked and I would never have come up with anything as compact.

          I'll try to 'put it to work' as soon as I can. I'm very much obliged to you.

          This is what the Forum is for!

          Best of luck to your trading.

          Comment


            #6
            This is what the Forum is for!
            Get Your profit!
            Last edited by fx.practic; 09-18-2012, 06:17 AM.
            fx.practic
            NinjaTrader Ecosystem Vendor - fx.practic

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by ursavent, Today, 12:54 PM
            0 responses
            2 views
            0 likes
            Last Post ursavent  
            Started by Mizzouman1, Today, 07:35 AM
            3 responses
            17 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by RubenCazorla, Today, 09:07 AM
            2 responses
            13 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by i019945nj, 12-14-2023, 06:41 AM
            7 responses
            82 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by timmbbo, 07-05-2023, 10:21 PM
            4 responses
            158 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Working...
            X