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

Entry at Crossover N Bars ago

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

    Entry at Crossover N Bars ago

    Hi,

    I have an issue with my automated strategy. I use CCI as my entry point, when it crosses the 0 line. The thing is I'd like my entry to be on the 5th candle after the crossover.

    I used a value of 5 as the look back period, but the strategy still taking orders on the crossover candle

    I appreciate your help
    Click image for larger version  Name:	Screenshot_17.png Views:	0 Size:	119.4 KB ID:	1212363
    Last edited by zakarianada; 08-17-2022, 06:58 AM.

    #2
    Hello zakarianada,

    Thank you for your post.

    Adjusting the lookback period for the crossover condition changes the number of bars back to check for the crossover. If you put a larger number here, then a larger span of bars will be checked for a crossover. This is noted in the help guide page here:



    One way to achieve what you are looking for is to check for the crossover in a different set of conditions. Once the crossover has been detected, save the bar index from the crossover to a custom int series and also set a bool from false to true. In your set of conditions for the entry, if the bool is true you then subtract the current bar index from the custom series. When the custom series bar index minus the current bar index equals -5, you can set the action to submit your entry and change the bool back to false. My colleague has created a similar example from the Strategy Builder in the following forum post:



    I went ahead and created another example using a condition if the WoodiesCCI crosses over the numeric value of 0. When that crossover happens, the bar index is saved to the custom series "CrossoverBar" and the bool "CrossoverDetected" is set to true. In the second set of conditions, if CrossoverDetected is true and if CrossoverBar from 5 bars ago minus the current bar = -5, then the strategy will EnterLong and set CrossoverDetected back to false.

    Please try importing the example and opening it in the Strategy Builder to review how the custom series, bool, and sets of conditions are configured. Using the same concepts, you should be able to adjust your strategy so that it achieves the desired outcome.

    Please let me know if I may be of further assistance.



    Attached Files
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you so much for your help, the file helped a lot. I do need further assistance.

      1. I'd like to add a pre condition: For LONGs, the CCI should have crossed up the -100:
      For this I beleive that I need to add a bool variable for crossing up the -100 line and set it to true?
      This CrossUp is valid until crossover of 0 line the first time

      2. Then the Usual Crossover of 0 lin + 5 bars + Price in the blue area (uptrend confirmed = LingReg12 above DEMA100) => Take a LONG
      If the conditions are not applied on the 5th bar, then no trades should be taken.

      Working on this for several hours and your help would be appreciated.
      Attached Files

      Comment


        #4
        Hello zakarianada,

        Thank you for your reply.

        For your first question, you could certainly achieve this using a bool variable. For example, you could add a bool like 'preCrossover' that defaults to false. In its own set of conditions that happens before Set 1 in your screenshot (you can add a set then click and drag them into a different order as needed), you can check if the CCI has crossed above the -100 line. If so, the 'preCrossover' should evaluate to true. Then in the conditions that set the LongCrossOverDetected to true, you could add a check for if preCrossover = true. To 'reset' the preCrossover bool, you could do something such as add an action that sets preCrossover to false once the LongCrossOverDetected is true or on a reversal once it has crossed below the -100 line again.

        For your second question, if there are other conditions that need to be true on the 5th bar other than just the LongCrossOverDetected = true, then they should also be added to the set of conditions and actions that trigger the long entry. You could confirm an uptrend by using IsRising from the Misc folder when adding a condition. Here are some screenshots of some example sets of conditions and actions:

        Set 1 - https://www.screencast.com/t/TnV2HRGB
        Set 2 - https://www.screencast.com/t/CGYIrzBalk1a
        Set 3 - https://www.screencast.com/t/3zXmweBEStr


        Are you saying something along the lines that if the uptrend reverses or one of the other conditions does not remain true for all 5 bars, no trade should be taken? If that is the case, please let me know and I could provide additional guidance on how to check the conditions over the course of the 5 bars after the crossover.

        Thank you for your patience.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank you for your reply.

          1. Yes, I'd like to NOT TO TAKE any trade where the preCrossover of -100 is true followed by the 0 line Crossover.
          2. In the next screenshot (conditions), does IsRising is the same as Default input[0]>Open[0] ? (I'm looking to take the entry where the 5th candle is green only)
          3. In the screenshot (Do the following), does making these variables false resets the logic to look again for the preCrossover of -100?

          NB: DayIsON is there to stop trading when max profit or loss is reached
          Attached Files

          Comment


            #6
            Hello zakarianada,

            Thank you for your response.

            IsRising checks to see if the current value is greater than the value from 1 bar ago. This is different than checking if the current price is higher than the open price of the candle. For more information about IsRising:



            Making the variables false will reset the logic so that it has to go re-evaluate the other sets of conditions that evaluate the bools to true. For example, after the conditions in Set 3 from your screenshot trigger the actions, your script will have to look for CrossUp100 to be True again to then trigger LongCrossOverDetected to be True, then once they are both true again Set 3 has a chance to be processed if the other conditions are also met.

            Although the NinjaScript team is here for guidance and to provide samples that demonstrate documented NinjaScript concepts, we are not full-stack programmers and we do not provide consulting services to help develop your strategy for you. The process of developing your strategy may take some testing and debugging to compare your expected/desired outcome of the strategy vs. the actual results when testing. I suggest testing your strategy using the Strategy Analyzer for a backtest, running the strategy in the Playback Connection, or using a simulated account in real-time to monitor the results and adjust your conditions and actions in the Strategy Builder as needed.

            Other tools for testing and debugging a strategy, even when developing in the Strategy Builder, include Trace Orders and Print() statements. Trace Orders may be enabled from the "Default Properties" page of the Strategy Builder by expanding "More Properties" and checking the box for Trace orders. With this enabled, information about orders will be printed to the NinjaScript Output window (Control Center > New > NinjaScript Output). This allows you to confirm when orders are placed or when they are ignored and why. For more information about Trace Orders:





            For more information about using prints to debug your code, please see the following post and link to a video showing how to add prints in the Strategy Builder:






            I hope these tools and resources are useful to aid you in the development of this strategy and future strategies. If you run into questions about the results from your testing, feel free to reach out and we would be glad to assist.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Great, this was very helpful. Thanks a bunch

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by kaywai, 09-01-2023, 08:44 PM
              5 responses
              601 views
              0 likes
              Last Post NinjaTrader_Jason  
              Started by xiinteractive, 04-09-2024, 08:08 AM
              6 responses
              22 views
              0 likes
              Last Post xiinteractive  
              Started by Pattontje, Yesterday, 02:10 PM
              2 responses
              17 views
              0 likes
              Last Post Pattontje  
              Started by flybuzz, 04-21-2024, 04:07 PM
              17 responses
              230 views
              0 likes
              Last Post TradingLoss  
              Started by agclub, 04-21-2024, 08:57 PM
              3 responses
              17 views
              0 likes
              Last Post TradingLoss  
              Working...
              X