• 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

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, 05:17 AM.
            fx.practic
            NinjaTrader Ecosystem Vendor - fx.practic

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by MARTIN MARTIN, Today, 01:01 PM
            0 responses
            4 views
            0 likes
            Last Post MARTIN MARTIN  
            Started by bouncetherubble, Today, 10:21 AM
            2 responses
            12 views
            1 like
            Last Post bouncetherubble  
            Started by tcbarrow, Today, 10:53 AM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by dmking, Today, 11:31 AM
            2 responses
            17 views
            0 likes
            Last Post dmking
            by dmking
             
            Started by TazoTodua, Yesterday, 03:01 PM
            4 responses
            27 views
            0 likes
            Last Post koganam
            by koganam
             
            Working...
            X