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

Put a order limit after X bars-down and X bars-up

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

    Put a order limit after X bars-down and X bars-up



    Hello,

    I am currently seeking to create a condition using strategy builder , that involves two consecutive bearish bars, followed by a green candle .

    My objective is to place a buy limit order at the highest point of the previous two bearish bars once the condition is true and waiting what will happened , so if the 4th bars is coming green it will execute the order and that's fine , and if the 4th is coming red it should delete the buy limit order .

    is it possible to use NBarsdown or NBarsup indicator or how exactly to do it ?

    THANK YOU



    #2
    Hello mariorac19,

    Thanks for your post.

    To detect if a bar is an up bar (green), you could create a condition that compares if the Close[0] price is greater than the Open[0] price.

    To detect if a bar is a down bar (red), you could create a condition that compares if the Close[0] price is less than the Open[0] price.

    In the Strategy Builder you would need to create multiple conditions in the same Set to check if there are 2 consecutive down bars followed by 1 up bar. The first condition would check if the Close[2] price is less than the Open[2] price. This checks if the bar 2 bars prior to the current bar is a down bar. The second condition would check if the Close[1] price is less than the Open[1] price. This checks if the prior bar is a down bar. The third condition could check if the Close[0] price is greater than the Open[0] price. This checks if the current bar is an up bar.

    See this help guide page about creating price comparison conditions: https://ninjatrader.com/support/help...ataComparisons

    Conditions: https://ninjatrader.com/support/help...on_builder.htm
    Actions: https://ninjatrader.com/support/help...t8/actions.htm

    Note that Entry() method orders are canceled at the end if a bar if they are not filled or re-submitted.

    From the Managed Approach help guide: "By default, orders submitted via Entry() and Exit() methods automatically cancel at the end of a bar if not re-submitted"

    Managed Approach: https://ninjatrader.com/support/help...d_approach.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your response.

      I managed to solve the initial problem, but now I'm facing another issue. I want to know how to use the strategy builder effectively to create a strategy that prevents opening any orders if the previous bar is green, and there are three successive red bars before it (as shown in the attached image).
      Click image for larger version

Name:	example.png
Views:	153
Size:	47.3 KB
ID:	1261790


      In the example provided, I noticed that the bar before the current one is green, and the last three bars are red. However, the strategy still opened a buy order, which is not the desired outcome. I would like to avoid getting any order in such cases. Is it possible to achieve this? If so, could you please guide me step by step on how to do it? I would appreciate your assistance.
      Attached Files

      Comment


        #4
        Hello mariorac19,

        Thanks for your notes.

        You could consider creating a bool variable in the Inputs and Variables screen of the Strategy Builder and add custom logic to the strategy to control when trades are placed.

        For example, you could create a bool called something like 'OkToTrade', then create price comparison conditions that check if the current bar is a down bar, checks if the previous bar is a down bar, checks if the bar 2 bars ago is a down bar, and checks if the bar 3 bars ago is a green bar. Then, set the bool to false.

        Then, you would add a condition that checks if the OkToTrade bool is true and call your entry order method.

        By doing so, the order will only be placed when the bool is true and the bool will not be true when the current bar is red, the previous bar is red, the bar 2 bars ago is red, and the bar 3 bars ago is green. Note that you will need to create your entry order conditions in your script and set the bool to true. Otherwise, it would remain false.

        Conditions: https://ninjatrader.com/support/help...on_builder.htm
        Actions: https://ninjatrader.com/support/help...t8/actions.htm
        Brandon H.NinjaTrader Customer Service

        Comment


          #5


          Thank you for your response.

          1st Question :

          I would like to add the following logic: each opened trade should have a fixed stop loss (set at 10 ticks) and a trailing stop that activates after reaching 20 ticks, then moves every 15 ticks thereafter. Is it possible to implement this using a boolean or a variable? If there is a step-by-step guide on how to create this in the strategy builder, that would be greatly appreciated.

          2nd Question :

          Is it possible to use ATM strategy setting to manage my trade opened before using the strategy on the chart ? mean the strategy that i was apply on the chart will look for the condition to open the trade , and once done , the ATM strategy on the menu ( default ) can take the rest of management of trade ( apply sl , trailing, tp .... ) is it possible ?

          Thank you.



          Last edited by mariorac19; 08-06-2023, 09:32 AM.

          Comment


            #6
            Hello mariorac19,

            Thanks for your notes.

            1. Yes, it would be possible to implement trailing stop logic in a Strategy Builder strategy.

            You can use exit order methods and add coding logic in the Strategy Builder to accomplish this goal. Please note that you would not be able to use the exit methods and the stops in the stop and targets window as that creates a violation of the managed approach internal order handling rules, linked here: https://ninjatrader.com/support/help...d_approach.htm

            We do not have a step-by-step guide for implementing trailing stop logic. However, my colleague Chelsea has created educational examples of strategy builder breakeven and trailing stop in the strategy builder here that you could review:



            2. It would not be possible to use an ATM Strategy template to control protective orders in a Strategy Builder strategy. However, this would be possible to accomplish in an unlocked strategy by manually programming your strategy to use Atm Strategy Methods for placing/handling orders.

            To unlock a Strategy Builder strategy, click the 'Unlock code' button.

            We have a sample script called SampleAtmStrategy that you could view that comes default with NinjaTrader and details the use of Atm Strategy Methods. To view the sample strategy's code, open a New > NinjaScript Editor window, open the Strategies folder, and double-click on the SampleAtmStrategy file.

            See the help guide documentation below for more information.

            Atm Strategy Methods: https://ninjatrader.com/support/help...gy_methods.htm
            Using Atm Strategies in NinjaScripts: https://ninjatrader.com/support/help...strategies.htm
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              thnks for the answer and for the documentations too , i would like to know if is there any video that explain clear how to use an ATM Strategy template to control protective orders in a Strategy Builder strategy .if yes it ill be more helpful fo us .
              thanks

              Comment


                #8
                Hello marioac19,

                Thanks for your note.

                Atm Strategy templates cannot be used in a Strategy Builder strategy. This is a limitation of the Strategy Builder.

                From my previous post: "2. It would not be possible to use an ATM Strategy template to control protective orders in a Strategy Builder strategy."

                To use Atm Strategy templates in a NinjaScript strategy, you must unlock the script from the Strategy Builder using the 'Unlock code' button and manually program the NinjaScript strategy in the NinjaScript Editor window to use Atm Strategy Methods.

                We do not have a video available demonstrating the use of Atm Strategy methods. We do have a documented sample script called SampleAtmStrategy that you could view that comes default with NinjaTrader. This reference sample details the use of Atm Strategy Methods. To view the sample strategy's code, open a New > NinjaScript Editor window, open the Strategies folder, and double-click on the SampleAtmStrategy file.

                See the help guide documentation below for more information.

                Atm Strategy Methods: https://ninjatrader.com/support/help...gy_methods.htm
                Using Atm Strategies in NinjaScripts: https://ninjatrader.com/support/help...strategies.htm
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  i understand what you mean , and i did unlocked the script and add all modification needed to it , but still have one problem to execut the order , cause even i made all changes needed still not show any order when test it in playback or even on live data .

                  by the way in this code of SampleAtmStrategy :

                  Code:
                  isAtmStrategyCreated = false;  // reset atm strategy created check to false
                                  atmStrategyId = GetAtmStrategyUniqueId();
                                  orderId = GetAtmStrategyUniqueId();
                                  AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) => {
                                      //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
                                      if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
                                          isAtmStrategyCreated = true;​
                  if i want to put my owen long strategy as follow :

                  Code:
                  EnterLongStopMarket(Convert.ToInt32(Quantity), (MAX2[0] + (1 * TickSize)) , @"B")
                  should i change Low[0] by (MAX2[0] + (1 * TickSize)) , otherwase if the Low[0] is the point of entry on the strategy or isn't

                  thanks

                  Comment


                    #10
                    Hello marioac19,

                    Thanks for your notes.

                    Currently, the AtmStrategyCreate() code you shared is submitting a Buy Limit order to the current Low price and uses an ATM Strategy template called "AtmStrategyTemplate".

                    You must have an ATM Strategy template created and saved named "AtmStrategyTemplate" for this code to work.

                    If you want to use an ATM Strategy template that you have already created and saved with a different name, you would need to change "AtmStrategyTemplate" in the AtmStrategyCreate() method to the name of your saved ATM Strategy template you want to use.

                    If you want to submit a Buy Stop Market order to the MAX2[0] + (1 * TickSize) price, you would need to change the OrderType from OrderType.Limit to OrderType.StopMarket and change the LimitPrice argument Low[0] to 0, and set the StopPrice argument from 0 to MAX2[0] + (1 * TickSize).

                    You need to define a StopPrice argument instead of a LimitPrice argument because you are wanting to submit a stop market order, not a limit order.

                    Please refer to this help guide page for details about using AtmStrategyCreate() in a NinjaScript strategy: https://ninjatrader.com/support/help...tegycreate.htm

                    See this help guide page about creating ATM Strategy templates: https://ninjatrader.com/support/help..._templates.htm

                    I suggest that you study the SampleAtmStrategy script and the Atm Strategy Methods help guide documentation to gain a good understanding of how these methods work and how the reference sample functions. Once you have a good understanding of how the methods work and how the script functions you could create your own custom NinjaScript using Atm Strategy Methods.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11

                      in this case i should not use an input of quantity , tp and sl on scipt more then the one on the part use for atm strategy right ?
                      cause when i start the script i get 2 order opened and one of them try to modify the point of entry in the close of candle

                      Comment


                        #12
                        Hello marioac19,

                        Thanks for your notes.

                        The AtmStrategyCreate() method does not have an argument available to specify a quantity. This could be seen in the AtmStrategyCreate() syntax shown below from the help guide.

                        AtmStrategyCreate(OrderAction action, OrderType orderType, double limitPrice, double stopPrice, TimeInForce timeInForce, string orderId, string strategyTemplateName, string atmStrategyId, Action<Cbi.ErrorCode, string> callback)

                        To submit the Buy Stop Market order you referenced in post # 9, the code might look something like this:

                        AtmStrategyCreate(OrderAction.Buy, OrderType.StopMarket, 0, MAX2[0] + (1 * TickSize), TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) => {
                        //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
                        if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
                        isAtmStrategyCreated = true;​
                        });


                        Note that OrderType is set the OrderType.StopMarket since you want to submit a stop market order, the limitPrice argument is set to 0 since you are not submitting a limit order, and the stopPrice argument is set to the value you want to place your stop market order at.​

                        If you are wanting to use an ATM Strategy template you created that is not saved under the name "AtmStrategyTemplate", you would need to replace "AtmStrategyTemplate" with the name of the ATM Strategy template you created and saved.
                        Brandon H.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by port119, 04-27-2024, 02:43 PM
                        3 responses
                        30 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by sastrades, 01-31-2024, 10:19 PM
                        13 responses
                        203 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by reynoldsn, Today, 04:40 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post reynoldsn  
                        Started by Philippe56140, 04-27-2024, 02:35 PM
                        6 responses
                        54 views
                        0 likes
                        Last Post bltdavid  
                        Started by ETFVoyageur, Yesterday, 06:05 PM
                        7 responses
                        45 views
                        0 likes
                        Last Post ETFVoyageur  
                        Working...
                        X