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

Triggering strategy by movement of price up or down X number of points

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

    #31
    I really appreciate all your help!.

    I managed to get this strategy to work, following your recommendations. However, when it triggers a trade, I am getting an error. Here is the log and some screenshots of errors that happen when the limit order is first placed. Then when the limit order is filled, I get additional errors.

    1. When I enter a long limit order, I want the stop placed at the low of the previous bar that triggered the trade. Hence the use of Low[1]

    2. When I short, I want the stop placed at the high of the previous candle that triggered the trade. Use of High[1]


    Click image for larger version  Name:	image.png Views:	0 Size:	37.0 KB ID:	1243347


    After the limit order is filled, this is what is shown

    Click image for larger version  Name:	image.png Views:	0 Size:	232.6 KB ID:	1243348​Below is the code I am using.


    // Set 1
    .....
    {
    EnterLongLimit(0, true, Convert.ToInt32(Quantity), Open[0], @"MyEntry");
    }

    if (Position.MarketPosition == MarketPosition.Long)
    {
    ExitLongStopLimit(0, true, Convert.ToInt32(Quantity), 0, Low[1], @"MyStop", @"MyEntry");
    ExitLongLimit(0, true, Convert.ToInt32(Quantity), (High[0] + (TargetProfit * TickSize)) , @"TakeProfit", @"MyEntry");​
    }


    // Set 2
    ....
    {
    EnterShortLimit(0, true, Convert.ToInt32(Quantity), Low[0], @"MyEntry");
    }

    if (Position.MarketPosition == MarketPosition.Short)

    {

    ExitShortStopLimit(Convert.ToInt32(Quantity), 0, High[1], @"MyStop", @"MyEntry");
    ExitShortLimit(Convert.ToInt32(Quantity), (Low[0] + (TargetProfit * TickSize)) , @"TakeProfit", @"MyEntry");
    }

    }​
    Attached Files
    Last edited by Jdmtrader; 03-29-2023, 07:15 AM.
    Jdmtrader
    NinjaTrader Ecosystem Vendor - JDM Indicators

    Comment


      #32
      Hello Jeff,

      Thanks for your notes.

      This error message means that you are submitting a sell stop or sell stop limit order on the wrong side of the market (above the market price).

      Sell stop or sell stop limit orders must be submitted below the current market price.

      You would need to modify your script so that the sell stop or sell stop limit order in your script is submitting orders below the current market price.

      Debugging prints should be added to the script one line above the condition used to place Exit orders that print out the current market price (Close[0]) price and the price you are submitting the order to so that you understand how and at what price the strategy is placing orders.

      Below is a link to a forum post that demonstrates how to use prints to understand behavior.
      https://ninjatrader.com/support/foru...121#post791121

      Once you know how the strategy is placing orders from the debugging prints, you could modify the price of your Exit orders accordingly.

      Something you could consider is using GetCurrentBid() and GetCurrentAsk() to offset orders so that they are more likely to land on the correct side of the market.

      See these help guide pages for more information.
      GetCurrentBid(): https://ninjatrader.com/support/help...currentbid.htm
      GetCurrentAsk(): https://ninjatrader.com/support/help...currentask.htm

      Let me know if I may assist further.
      Brandon H.NinjaTrader Customer Service

      Comment


        #33
        Thanks. One other thing I have noted is that when I have an active limit order, when a new trade is triggered, it will cancel the limit order and create a new one.

        It seems there is no conditions that are checking to make sure there isnt an active limit order in place. I found another forum post that talked about this but its unclear to me how to put in a condition (like I have done for positions), to make sure to NOT take any new trades if there is an active limit order open.
        Jdmtrader
        NinjaTrader Ecosystem Vendor - JDM Indicators

        Comment


          #34
          Hello Jeff,

          Thanks for your notes.

          A bool could be used to control when trades are made by the strategy. You could create a bool named something like OkToTrade (initially set to false). You would create your order entry condition and set the bool to true. Then you would check if the bool is true and call your entry order method and set the bool to false.

          By doing this, the order entry method will only be called to place an order when the bool is true. When the bool is false, the order entry condition will not be called so the strategy will stop placing trades until the bool becomes true.​

          The example script I attached in post # 20 demonstrates how a bool could be used to control trades.

          Let me know if I may assist further.
          Brandon H.NinjaTrader Customer Service

          Comment


            #35
            Thanks. I dont see any example script though.
            Jdmtrader
            NinjaTrader Ecosystem Vendor - JDM Indicators

            Comment


              #36
              Hello Jeff,

              Thanks for your note.

              See the attached screenshot showing where the example script is on post # 20.

              I have also attached the example script below. This is an example script demonstrating how a bool could be used to control when trades are made.

              Please note that it would be up to you to come up with the exact custom logic in your script to accomplish your overall goal.
              Attached Files
              Brandon H.NinjaTrader Customer Service

              Comment


                #37
                Thanks. I notice that when my limit order is created, my stop and take profit are not created at the same time. I realize this is the way the script is setup and they arent triggered until there is an active position. However, is there a way to have my stop and take profit created at the same time as the entry limit order?
                Jdmtrader
                NinjaTrader Ecosystem Vendor - JDM Indicators

                Comment


                  #38
                  Hello Jeff,

                  Thanks for your note.

                  If you are running Calculate.OnBarClose, the script will only process logic and place trades at the end of each bar. Since the entry order is being submitted on one bar close, the stop and target exit orders would be placed on the close of the next bar.

                  If you want the strategy to process OnBarUpdate() and place trades more frequently, you could use Calculate.OnPriceChange or Calculate.OnEachTick.

                  Calculate.OnPriceChange means the strategy will process logic for each change in price. Calculate.OnEachTick means the strategy will process logic for each incoming tick.

                  Review this help guide page for more information about Calculate: https://ninjatrader.com/support/help.../calculate.htm

                  Let me know if you have further questions about this property.
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #39
                    Ok. So is it possible then to have my entry order, stop and take profit entered at the same time, not dependent on the limit order getting filled before the stop and take profit are created.
                    Jdmtrader
                    NinjaTrader Ecosystem Vendor - JDM Indicators

                    Comment


                      #40
                      Hello Jeff,

                      Thanks for your note.

                      No, the limit order must be filled before the stop and target orders are placed. Exit orders cannot be placed by a strategy unless an Entry order has been filled. This is because there must be an Entry filled for the Exit orders to exit.

                      Let me know if I may assist further.
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #41
                        Ok. But what if I use entry orders for the stop and take profit as well? I assume thats not possible? If so, is it because I am using a managed approach?
                        Jdmtrader
                        NinjaTrader Ecosystem Vendor - JDM Indicators

                        Comment


                          #42
                          Hello Jeff,

                          Thanks for you note.

                          Correct, that is not possible. Entry orders cannot be used as a Stop and Target to exit a market position. Entry orders will enter a position. Only Exit orders could be used as Stop and Target orders to exit a market position.

                          This is not because the Managed Approach is being used. For an Exit order to be placed by the strategy, you must have an Entry order filled. If you are not in a market position, the Exit order would have nothing to exit.

                          If you try to submit an Exit order and no Entry order has been filled, this will result in an error.

                          Let me know if I may assist further.
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #43
                            Ok. Just to be clear, I was referring to using entry orders to create stop market and take profit limit orders along with the limit entry and all orders. No position would be open at all when these were created.
                            Jdmtrader
                            NinjaTrader Ecosystem Vendor - JDM Indicators

                            Comment


                              #44
                              Hello Jeff,

                              Thanks for your note.

                              Entry orders cannot be used to create stop loss and profit target orders that would exit a position.

                              Exit orders, such as ExitLongStopMarket() and ExitLongLimit() would need to be used to create the stop loss and profit target orders to exit a position.

                              You could consider placing more Entry orders if you wish, but this would simply enter more positions if those Entry orders were hit. It would not exit from any positions. For example, say called EnterLongLimit() two times with one submitted at a higher price than the other, and both had a quantity of 1. If both Entry orders were hit, you would be in a total position of 2. 1 for the first Entry order and 1 for the second.

                              Entry orders can only be used to enter a market position. Exit orders need to be used to exit a market position.

                              See this help guide page for information about entering a position: https://ninjatrader.com/support/help...m#EntryMethods

                              See this help guide page for information about closing (exiting) a position: https://ninjatrader.com/support/help...CloseAPosition

                              Let me know if I may further assist you.
                              Brandon H.NinjaTrader Customer Service

                              Comment


                                #45
                                Thanks. When my stop loss and take profit orders get created, they are moving up and down dynamically as the price moves. Why would this happen? Could this be that ever bar update, it creating new stop and take profit orders? I assume that must be it.
                                Jdmtrader
                                NinjaTrader Ecosystem Vendor - JDM Indicators

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by junkone, Today, 11:37 AM
                                2 responses
                                12 views
                                0 likes
                                Last Post junkone
                                by junkone
                                 
                                Started by frankthearm, Yesterday, 09:08 AM
                                12 responses
                                43 views
                                0 likes
                                Last Post NinjaTrader_Clayton  
                                Started by quantismo, 04-17-2024, 05:13 PM
                                5 responses
                                35 views
                                0 likes
                                Last Post NinjaTrader_Gaby  
                                Started by proptrade13, Today, 11:06 AM
                                1 response
                                7 views
                                0 likes
                                Last Post NinjaTrader_Clayton  
                                Started by love2code2trade, 04-17-2024, 01:45 PM
                                4 responses
                                35 views
                                0 likes
                                Last Post love2code2trade  
                                Working...
                                X