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

setting a crossover within n bars form another condition

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

    setting a crossover within n bars form another condition

    with regard to the attached image:
    Line A is an indicator
    Line B is the m period MAX of line A
    Line C is an average of this indicator A
    Line D is another average of this indicator A
    is it possible, possibly using the strategy builder, but without adding a temporary variable, to set on the condition:
    line C crosses below line D within a variable n number of bars since when line B has been detaching from line A (ie: its last highest value)
    I understand this last part would be a condition like:
    if ((LineB[1] > LineB[0]) AND lineB[1] == MAX (lineB, period m)[0]),
    but I do not see how to combine this “if” with the first part of the condition.
    Any suggestion on how to get this? with or without the strategy builder?
    Thanks.


    Click image for larger version  Name:	setting a crossover withiin n bars form another condition.jpg Views:	0 Size:	111.0 KB ID:	1078476
    Last edited by guidoisot; 11-21-2019, 05:49 AM.

    #2
    Hello guidoisot,

    Thanks for your post.

    We can certainly leave this open for any community members who may want to assist with your strategy. An alternative would be to use a 3rd party programmer and if this is of interest we can provide a link to such.

    I would suggest that lineB[1] == MAX (lineB, period m)[0]) would be problematic because you are dealing with double values that rarely line up exactly. You may want to print these values out to see if that is or is not the case. If they do not line up then you may want to consider using a filter of 1 tick to see if they are aligned within 1 tick (or some other close approximation that is appropriate for the values you are working with).

    When you have a cross condition, you can save the current bar number (of when the cross occurs) into an int variable and then compare the int variable to CurrentBar to see how many bars have occurred since the cross event. This would be very difficult and may not be possible in the strategy builder but can be easily done in Ninjascript.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello Paul,
      thank you for your reply.
      since lineA is an indicator I will try to apply a proximity filter by replacing:

      "== MAX (lineB, period m)[0])"
      __with___
      " >=0.99* MAX (lineB, period m)[0])
      "
      because I would not know how to make such filter by using the price tick value of the underlying instrument.

      Regarding how to store the bar number at the crossover, in order to then compare it with the current bar, do you know whether there is an example of this on another post here of the forum, maybe a similar type of question has already been asked here by someone else, or whether a similar example is considered by the ninjascript help documentation ?

      One last question, do you think this storing and comparing bar numbers, would be a possible and worthwhile new feature to add to the Strategy Builder?
      If it is could I ask to submit this new feature request?

      Thank you,

      gt

      Comment


        #4
        Hello guidoisot,

        Thanks for your reply.

        I have attached an example of a strategy builder script that will count bars after an event. As you know math in the strategy builder is done by using "Offset". In the strategy builder offset cannot be used on user defined variables and on certain other variables such as current bar. What can be done is to create custom series which will allow you to perform math operations.

        BarCounterBuilderExample_NT8.zip

        in the attached example there are two custom int series, one to hold the bar number where the event occurs and the other is to hold the current bar number.

        Sets 1 & 2 must be in the order provided.

        Set1 initializes the custom series.
        Set 2 updates the custom series, the counter gets updated with the previous bar value, the current bar series gets updated with the current bar.
        There are no conditions in Set2, there is only 1 condition in set 1.

        Set 3 demonstrates a crossover condition of two moving averages (and adds them to a chart), saves the current bar number into the bar counter (myCounter) and draws a color on the pane;l to indicate the bar that the cross is determined.

        Set 4 Demonstrates the bar counting, the condition checks to see if the current bar counter (CBcounter) minus the event counter (myCounter) is equal 10 10 meaning 10 bars have passed, this is proven on the chart by drawing a dot 10 bars after the cross.

        Here is what you would expect to see on a 1 minute chart of ES 12-19 when the strategy is applied.
        Attached Files
        Last edited by NinjaTrader_ChelseaB; 10-14-2022, 01:00 PM.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thank you Paul.
          very interesting!
          Best regards.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by selu72, Today, 02:01 PM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Started by WHICKED, Today, 02:02 PM
          2 responses
          8 views
          0 likes
          Last Post WHICKED
          by WHICKED
           
          Started by f.saeidi, Today, 12:14 PM
          8 responses
          21 views
          0 likes
          Last Post f.saeidi  
          Started by Mikey_, 03-23-2024, 05:59 PM
          3 responses
          50 views
          0 likes
          Last Post Sam2515
          by Sam2515
           
          Started by Russ Moreland, Today, 12:54 PM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Erick  
          Working...
          X