• 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

How to check whether a condition is true within several bars back ?

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

    How to check whether a condition is true within several bars back ?

    Hi all,

    I am new to NT 8, and very experienced with programming with Ninjascript. So, my question is, How to check whether a condition is true within several bars back ?

    For example, how to check whether the following condition is true in the last 6 bars ?

    Code:
    CrossAbove(smaFast, smaSlow, 1)

    Thank you !!

    #2
    Hello wolfcuring,

    Thanks for your post.

    You can use an int variable and store the bar number when the cross condition is true. Then you can test to see if the current bar is greater then 6 from the saved bar number. For example, using the CurrentBar of the system and an int variable you create called savedBar:

    if (CrossAbove(smaFast, smaSlow, 1))
    {
    savedBar = CurrentBar; // assign the current bar number to savedBar
    }

    if (your other entry conditions && CurrentBar - savedBar <= 6)
    {
    // do something
    }

    For clarity, CurrentBar: A number representing the current bar in a Bars object that the OnBarUpdate() method in an indicator or strategy is currently processing. For example, if a chart has 100 bars of data, the very first bar of the chart (left most bar) will be number 0 (zero) and each subsequent bar from left to right is incremented by 1. Reference: https://ninjatrader.com/support/help...currentbar.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you, Paul, will give a try.

      Comment


        #4
        Originally posted by NinjaTrader_PaulH View Post
        ...

        if (your other entry conditions && CurrentBar - savedBar <= 6)

        Hello
        Is there a way you would use in order to achieve the same result using the strategy builder?
        I understand that with the strategy builder one cannot set the underscore part of this condition:

        if (your other entry conditions && CurrentBar - savedBar <= 6)

        Thank you.

        Comment


          #5
          Hello guidoisot,

          Thanks for your post.

          I've created and attached a strategy builder example that will, in set 1, determine a cross above condition, save the current bar number into a custom in series and set a bool from false to true and draw a magenta dot on the chart indicating the crossover bar. In another set if the bool is true then the current bar is subtracted from the custom series 6 bars ago and when the custom series - the current bar matches -6 it will draw a blue dot on the chart and then reset the bool back to false. The blue dot should be 6 bars after the crossover dot.


          Click image for larger version

Name:	BarsAgoexample.PNG
Views:	3
Size:	80.3 KB
ID:	1046582BarsAgoExample for strategy builder.zip
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Thank you Paul.
            I have tried to replace in set 2 the condition =-6 with >=-9, and the blu dots are still plotted, so can this be interpreted as: "the crossover has happened within 9 bars" (but I do not know that it happened right at bar -6) ?
            if I replace =-6 with <=-9 the fucsia dots disappear and they are replaced by the blu dots. I do not understand why this happens.



            Last edited by guidoisot; 01-30-2019, 03:49 PM.

            Comment


              #7
              Click image for larger version

Name:	ES 03-19 (1 Min LineBreak) 2019_01_30 (23_34_50).png
Views:	3
Size:	67.7 KB
ID:	1046627

              Comment


                #8
                Hello guidoisot,

                Thanks for your reply.

                You would need to change the SavedBar[] bars ago index from 6 to 9 in addition to changing the numeric value of -6 to -9.

                If you find that you do not understand how/why a particular script works, the best advice we can provide is to use print statements to print out the values of the logic, you can do this in the strategy builder as well as any Ninjascript strategy.

                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Simon Investimens, Today, 03:26 AM
                0 responses
                3 views
                0 likes
                Last Post Simon Investimens  
                Started by EnveousColnago, Yesterday, 06:58 AM
                1 response
                13 views
                0 likes
                Last Post NinjaTrader_ChrisSch  
                Started by owreivmatot, Today, 01:38 AM
                0 responses
                1 view
                0 likes
                Last Post owreivmatot  
                Started by lindsayf, Today, 01:13 AM
                0 responses
                6 views
                0 likes
                Last Post lindsayf  
                Started by thgamey, Yesterday, 06:46 PM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_ChrisSch  
                Working...
                X