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

use of look back

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

    use of look back

    hi,

    i have buildt an indicator with the conditions:
    if event #1 happens and also event #2 -> draw an arrow.
    this is working so far, as it's based at the same point of time (position [0]).

    now it may happen that the events #1 and #2 don't happen at the same point of time.
    for example #1 comes first and some bars later #2 appears. as the condition is true, i'd like to get an arrow, if #2 is apearing.

    i guess i have to use "look back" therefore?
    - how and where do i have to put "look back" into my indicator?
    - the look back period shall be 10 bars
    - do i have to "clean up" the indicator than for possible events in the future (as i'd like to start than from the beginning)
    - is there any other way to realise this?

    thx

    #2
    Hello Tradexxx,

    Thanks for your post and great questions.

    If you are using a crossover you can certainly use the lookback period however I think a better option is to instead set a logical variable true indicating that the condition has occurred and then use that variable as a condition for the second event.

    For example:

    Code:
    if (CrossAbove(EMA(14), SMA(20), 1))  // One bar look back
    	{
    		emaXaboveSma = true;        // set bool variable to true 
    		emaXaboveSmaBar = CurrentBar;   // Save the current bar number
    	}
    			
    if (Event2 && emaXaboveSma && (CurrentBar - emaXaboveSmaBar ) < 10)  // here is your second event where your conditions plus the bool plus the bar count since the crossabove
    	{
    		// do whatever is needed here
    	}
    			
    if (emaXaboveSma && (CurrentBar - emaXaboveSmaBar ) >= 10)  // test to see if too many bars and then reset the bool.
    	{
    				emaXaboveSma = false;
    	}
    So in the example above, we are using the crossAbove to detect a cross of the two moving averages. Once that is found then we set a bool variable (emaXaboveSma) to true and we also store the current bar number into an integer variable called emaXaboveSmaBar.

    Next in whatever your conditions are for event 2 we add the test for a true emaXaboveSma and we test that the currentbar - the saved bar is less than 10 bars ago (or whatever number works for you).

    Finally we reset the bool after 10 bars.

    The only thing else is to make sure to declare the variables in the region Variables:
    private bool emaXaboveSma = false; // initialize to false
    private int emaXaboveSmaBar = 0; // initialize to zero

    I hope this helps to give you a way to meet your goals.

    Please let me know if I can be of further assistance.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      hi Paul,
      thanks a lot!
      i'll try and come back :-)

      Comment


        #4
        10 bars ago

        Hi,

        I am trying to develop a strategy where EMA(10)>EMA(20)>EMA(50)>EMA(200) and the close is above EMA(10) for the last 10 bars, before submitting the Buy order.
        It works once and then it no longer advances.

        Regards
        Attached Files

        Comment


          #5
          Hello vtechdir,

          Thanks for your post and welcome to the forums!

          What type instruments and timeframe charts are you applying this to?
          Does the strategy become disabled after the first entry/exit?
          Do you see any error messages in the log tab of the control center related to the strategy?
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Hi Paul,

            Thank you for your prompt reply. It is a Daily strategy and I use it mainly for Forex (EUR/USD), however it can be used for any market traded security.
            I backtest it to ensure it does what I expect from 01/01/2014 to today. Visually on the chart I noticed several instances where all conditions will be true. Therefore the backtest does not pick up all the cases when the conditions are true and it stops after the first occurrence. The strategy is attached in the previous post.
            I could not find any information in the Log and the strategy does not get disabled.

            I suspect there is something wrong in the coded logic, maybe you can provide some help.

            Kind Regards

            Comment


              #7
              Hello vtechdir,

              Thanks for your reply.

              I did find an error in your code for the downside trades, one of the many "<" was actually a ">". When I changed that the downside trades showed.

              I took the liberty to add a few lines of code to your strategy to help visualize when the conditions that you have listed are true. I did this through the use of DrawDots. The dots will draw regardless of the the entry or exit and will draw as long as the conditions are true. In this way you have feedback that the strategy is functioning even if a trade has not taken place.

              In the example daily chart you can see various orange and lime green dots, these are where price closes above/below the EMA(10) which is your exit condition (if you were in a trade). The dark green dots show the 10 bars above EMA(10), which repeated 11 days in a row.

              I've also attached the modified code for your review.

              Please let me know if I can be of further assistance.
              Attached Files
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hi Paul,

                Excellent work, thank you very much.

                Regards
                Virgil

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by mjairg, 07-20-2023, 11:57 PM
                3 responses
                213 views
                1 like
                Last Post PaulMohn  
                Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                4 responses
                544 views
                0 likes
                Last Post PaulMohn  
                Started by GLFX005, Today, 03:23 AM
                0 responses
                3 views
                0 likes
                Last Post GLFX005
                by GLFX005
                 
                Started by XXtrader, Yesterday, 11:30 PM
                2 responses
                12 views
                0 likes
                Last Post XXtrader  
                Started by Waxavi, Today, 02:10 AM
                0 responses
                7 views
                0 likes
                Last Post Waxavi
                by Waxavi
                 
                Working...
                X