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

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:	349
Size:	80.3 KB
ID:	1046582[ATTACH]n1046583[/ATTACH]
          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, 04:49 PM.

            Comment


              #7
              Click image for larger version

Name:	ES 03-19 (1 Min LineBreak) 2019_01_30 (23_34_50).png
Views:	366
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 DJ888, 04-16-2024, 06:09 PM
                4 responses
                12 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by terofs, Today, 04:18 PM
                0 responses
                11 views
                0 likes
                Last Post terofs
                by terofs
                 
                Started by nandhumca, Today, 03:41 PM
                0 responses
                7 views
                0 likes
                Last Post nandhumca  
                Started by The_Sec, Today, 03:37 PM
                0 responses
                3 views
                0 likes
                Last Post The_Sec
                by The_Sec
                 
                Started by GwFutures1988, Today, 02:48 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Working...
                X