Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy to Buy/Sell on Trend Change

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

    Strategy to Buy/Sell on Trend Change

    I'm working on a NinjaTrader 8 strategy and need some assistance. I want to create a strategy that buys or sells a contract when a specific chart indicates a trend change, and then closes the order once it achieves a certain number of points point profit.

    Could someone provide guidance or an example of how to code this? Any help with the trend detection logic and setting up the profit target would be greatly appreciated!

    Thanks in advance!

    #2
    Hello maaxk,

    You could create this type of strategy in the builder by making price conditions for the trend you are looking for. The way you make the conditions depends on how you determine a trend is happening. If you just mean if the price has increased or deceased for a number of bars in a row you would do that by checking multiple price conditions in one set. For example if the current bar close is greater than the previous, if the previous bar close is greater than the one before that and so on until you have included the amount of bars you wanted to check are increasing or decreasing.

    We have a webinar that goes over the basic use of the strategy builder and how to make conditions, that would be a good starting point if you are wanting to start creating strategies.

    For profit targets and stop losses the builder has a specific page that lets you define those targets and their offsets. That is also explained in the video linked below.



    Comment


      #3
      Hello Jesse Thanks for the quick response.

      I have already watched this video but its old and some of the features are modified. also that is a general video that gives understanding of strategy building and it is really good but my question is:
      For instance the 30/20 chart is in the sell trend and as soon as i run the strategy on the chart it should wait and see when the trend is changed, lets say the chart is in sell trend so the candles are all red, as soon as it makes the first red candle the strategy should fill and order for 2 contract in that trend and then sell it after 2.5 points are achieved then again it will wait for the next trend and repeat the process.

      How will i do that exactly. Thanks!

      Comment


        #4
        Hello maaxk,

        I am unsure what a 30/20 chart is, you would need to be more specific here.

        Also what do you consider a trend? Is that prices that are increasing or decreasing over a period of bars or what specifically do you consider as the trend? Whatever your trend is you would need to make price conditions in the builder for that situation. I had provided one example in the previous reply of a "increasing trend" that is just when the price increases over a number of bars. You would make a number of conditions and use BarsAgo to compare one bars price with the bar before it for the total number of bars you wanted to compare.

        Comment


          #5
          Hello Jesse,

          Thank you for your assistance.

          To clarify, the "30/20 chart" refers to the MD Trend 30020 BBs mdMaxPrime chart that displays candles in different colors: red candles indicate a downtrend (price is decreasing), and green candles indicate an uptrend (price is increasing).

          For my strategy, I want to place an order when the chart creates the first green candle after a series of red candles, indicating a trend change from down to up. Specifically, the strategy should:
          1. Detect the first green candle after a series of red candles.
          2. Place a buy order for 2 contracts.
          3. Close the position once a 2-point profit is achieved.

          Could you provide guidance on how to set up these conditions and logic in the Strategy Builder? Any specific details or steps to achieve this would be greatly appreciated.

          Thanks!

          Comment


            #6
            Hello maaxk,

            In regard to the chart you are using, that sounds like a custom item so I would have no details on that specifically. If that is an indicator that is doing that it would need to expose a Plot for the builder to be able to use it.

            To determine the first green candle after some red candles you would have to make price conditions for that. That is exactly like the example that I provided except it would have 1 additional condition checking that the current price is the opposite direction. For example if the Current bar is greater than the previous, if the previous is less than the bar before it, if the bar before that is less than 1 more before that and so on for however many red bars you wanted in a row.

            There is an image showing how to make price conditions in the following link. Your set would end up with multiple conditions. For example if you wanted 3 red bars followed by a green bar ou would have 4 total price conditions in your set. You would use the BarsAgo field when configuring each condition to specify which bars you are comparing in each specific condition. The above example would be comparing barsago 0 with 1, 1 with 2, 2 with 3, 3 with 4.

            Comment


              #7
              Hello,

              Thank you for your detailed explanation. I understand the basic concept of creating price conditions to identify patterns such as a green candle following a series of red candles. I see that the approach involves setting multiple conditions to compare the current bar with previous bars, and using the BarsAgo field to specify the comparisons.

              However, I would appreciate a bit more detail on a few aspects to ensure I’m implementing this correctly:
              1. Defining the Conditions: Could you provide more specifics on how to configure the conditions for different scenarios? For instance, how exactly do I set up the conditions for identifying a green candle following three consecutive red candles?
              2. Using BarsAgo: I’m particularly interested in understanding the BarsAgo field better. How do I determine the correct BarsAgo values to use, and are there any common pitfalls or best practices when setting these?
              3. Custom Indicators: If the chart indicator I’m using is custom and doesn’t expose a Plot, are there alternative ways to integrate or modify it to work with the conditions you mentioned?

              Any additional clarification or examples you can provide would be very helpful.

              Thank you again for your assistance!

              Comment


                #8
                Hello maaxk,

                For your first question you mentioned that you understand creating price conditions, that would be what you need to do in order to create different scenarios. You would define 4 total price conditions in your set if you wanted to check 3 red bars in a row and then 1 green bar. That is the example that I provided in the last reply. Are you asking how to make price conditions?

                For bars ago the current processing bar is always 0 bars ago. To get 1 bar previous to that would be 1 bars ago and so on. The script will process bars starting at bar 0 the left most bar and move forward 1 bar at a time. To reference past data you would use the number of bars ago from now to access that data. For example on bar 4 you could access bar 3,2,1 by using the bars ago 1 2 and 3.

                If an indicator does not expose a plot it cannot be used with the builder. You would need to edit the indicator to expose data by using a plot.

                Comment


                  #9
                  Hello Jesse,

                  Thank you for your previous responses. I now have a clearer understanding of the concepts, but I still need some additional clarification to fully implement my strategy. Here are my specific questions:
                  1. Defining the Conditions for Arbitrary Candle Sequences:
                    • I understand how to set up conditions for a fixed number of bars. However, I need guidance on creating a strategy where the number of green candles before a red candle can vary (e.g., 50, 100, or any number).
                    • Specifically, how can I configure the Strategy Builder to detect a red candle after a varying number of green candles? Is there a way to make this adaptable to different numbers of candles without manually setting each condition?
                  2. Using BarsAgo Effectively:
                    • I’m looking for a more detailed explanation of how to use BarsAgo in this context. If I want to check if the last 50 candles were green and the current candle is red, how should I set up these conditions in the Builder?
                    • Could you provide a step-by-step example or clarification on how to reference multiple bars ago to compare the current bar with a long sequence of previous bars?
                  3. Handling Custom Indicators:
                    • Since the chart indicator I’m using (MD Trend 30020 BBs mdMaxPrime) is custom and doesn’t expose a Plot, what are my options?
                    • Are there alternative methods to integrate or work with this indicator within the Strategy Builder, or would I need to modify the indicator to expose data for use in the Strategy Builder?
                  4. Order Management and Historical Data:
                    • How can I set up the strategy to place an order when the condition is met, wait for a specific number of ticks or candles, and then close the position once a profit target is achieved?
                    • Additionally, how can I ensure that the strategy keeps a record of all trades and historical data for analysis?

                  I appreciate any additional details or examples you can provide to help me implement this strategy effectively.

                  Thank you again for your assistance!

                  Comment


                    #10
                    Hello maaxk,

                    That is not possible in the builder you would have to manually code the strategy to make it variable in that way. In the builder you would need to define the specific price conditions that you wanted to check.

                    To use bars ago in the builder that is also not variable, you need to specify the exact bars ago in your price conditions.

                    If your indicator does not have a plot it cannot be used for signals, you would have to contact the developer to have them add a plot that can be used for signals.

                    To place orders at a later time you would have to make variables and additional sets which use those variables to trigger entries. When your intial condition becomes true you can set a variable to true to trigger another set. To wait a number of bars would require using math which in the builder would be a complicated process using a series to count. There is a link on how to use series below.



                    The strategy automatically keeps track of trades it has made, you don't need to add anything for that.

                    Comment


                      #11
                      Hello Jesse,

                      Thank you for your response and for outlining the limitations of the Strategy Builder. I appreciate the insights provided. I have a few additional points and questions based on your feedback:
                      1. Indicator Plot Exposure:
                        • You mentioned that if the indicator does not have a plot, it cannot be used for signals. However, the indicator I’m using does expose a plot, but it might be different from the tutorial you referenced. I will attach a screenshot of the data series from the chart I am using for your reference. Could you please guide me on how to work with this plot in the Strategy Builder or clarify any specific steps I should follow? Please find the attached file for the screenshot of the data series of the chart i am using:
                          Click image for larger version

Name:	image.png
Views:	636
Size:	77.1 KB
ID:	1313988
                      2. Handling Variable Candle Sequences:
                        • The scenario I’m aiming for involves detecting the first red candle after a trend of green candles (or vice versa) without specifying an exact number. Essentially, I need the strategy to be dynamic and adapt to the market’s conditions. For example, if the market is in an uptrend and then switches to a downtrend, the strategy should place an order when the trend changes, regardless of the number of candles.
                        • Could you provide more guidance on how to implement such a variable condition in the Strategy Builder, or suggest how I might approach this with custom coding if necessary?
                      3. Placing Orders with Timing and Conditions:
                        • To clarify, I need the strategy to place an order when the trend changes (e.g., from green to red candles) and then manage the order based on profit targets. You mentioned using variables and additional sets to handle timing and conditions. Could you elaborate on how to use these techniques effectively or provide a high-level example?
                      4. Series and Counting Bars:
                        • The link you provided regarding incrementing or counting bars is helpful. However, I would appreciate any additional tips or best practices for implementing this in the Strategy Builder, especially in the context of waiting for a certain number of bars or ticks before placing an order.

                      I look forward to your guidance on these points. Thank you for your continued assistance!

                      Comment


                        #12
                        Hello Jesse,

                        I was following up on the query.
                        Thanks!

                        Comment


                          #13
                          Hello maaxk,

                          In the image you pictured the data series window, that won't give us any information about indicators. You would need to look in the charts data box after applying the image to see if any plots are listed.

                          What you are wanting to do is not available in the builder, to make variable conditions you need to manually code the strategy so you can use C# loops.

                          Placing orders is an action that you can do when your condition is true. Once your price condition becomes true it will submit an order. If you want to do more complex tasks like counting a number of bars you would have to refer to the series counter example link that I posted, that is the only way to count a number in the builder. I won't be able to provide exact steps on how to create what you are asking but you can learn different concepts like making a counter if you wanted to delay an action.



                          Comment


                            #14
                            Hello Jesse,

                            Thank you for your detailed response.

                            I have attached a screenshot of the applied data series, as mentioned. Could you please review the databox in the screenshot and advise if there’s any relevant information regarding the plots?
                            Click image for larger version

Name:	image.png
Views:	617
Size:	485.3 KB
ID:	1314125​Now if you see in the screenshot you can see the red and green candles which i was referring to as series, so the black candle is an indication that the trend is changing.
                            Additionally, could you confirm if there is any possibility of achieving my goal using the builder? If not, I would appreciate a step-by-step guide on how to code the strategy in C# to handle variable conditions, counting bars, and submitting orders. Specifically, I’m looking for guidance on:
                            1. Writing C# Code for Strategy: How to code the conditions and actions required for my strategy.
                            2. Testing the C# Code: Best practices for testing and validating the strategy to ensure it behaves as expected.

                            Thank you for your assistance.

                            Comment


                              #15
                              Hello maaxk,

                              In the image the databox is not present, you would need to open the databox to see if the indicator you want to use has plots. You should create a new empty chart, apply only that indicator and then open the databox to see if plots are in the databox.

                              The candles in the chart are a bar type, that is not an indicator and does not have plots. You can apply the strategy to that chart to use the OHLC data if you wanted.

                              In regard to providing specific steps for manual coding, I would not be able to walk through writing out code for a strategy but we can answer general questions you may have when developing items yourself. If you need more hands on assistance with learning or wanted something created for you then you can reach out to a third party educator or developer for that type of assistance. If you would like to learn more about that I can have another member of our team provide some resources.

                              The following resources can be used for learning manually coded NinjaScript and also debugging code.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              601 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              347 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              559 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              558 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X