• 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.


No announcement yet.

Partner 728x90


setting a crossover within n bars form another condition

  • 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?

    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.

    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


      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])"
      " >=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,



        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.

        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.

        Click image for larger version

Name:	guidoisot-2.PNG
Views:	166
Size:	47.1 KB
ID:	1078539


        Paul H.NinjaTrader Customer Service


          Thank you Paul.
          very interesting!
          Best regards.


          Latest Posts


          Topics Statistics Last Post
          Started by mr_trade, Today, 01:32 PM
          0 responses
          Last Post mr_trade  
          Started by sgordet, Today, 10:44 AM
          1 response
          Last Post bltdavid  
          Started by Parmenides48, Today, 08:37 AM
          2 responses
          Last Post Parmenides48  
          Started by EnveousColnago, 08-06-2020, 08:31 AM
          8 responses
          Last Post Emma1
          by Emma1
          Started by dieci, Today, 08:50 AM
          0 responses
          Last Post dieci
          by dieci