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

How To Prevent a StopLoss from Adjusting On Every OnBarUpdate

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

    #16
    Hello coopgrafik,

    Your script will only have the stop loss fill and the exit order will be ignored, as you cannot use set methods and exit orders on the same order.

    This violates the internal order handling rules for the managed position.
    https://ninjatrader.com/support/help...antedPositions

    "Methods that generate orders to exit a position will be ignored if:
    •A position is open and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
    •A position is open and an order submitted by a set method (SetStopLoss() for example) is active"

    Enable the TraceOrders and you will get a message that the order was ignored, which would tell you what happened with the order.
    Please enable TraceOrders in State.Configure, then please provide the output from the output window.

    Are you testing just the stop loss and this is not filling when the price falls 7 ticks below the entry?
    Last edited by NinjaTrader_ChelseaB; 08-04-2021, 03:09 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      Originally posted by NinjaTrader_ChelseaB View Post
      Are you testing just the stop loss and this is not filling when the price falls 7 ticks below the entry?
      I mean... I sent you my code so you can see exactly what andwhere I am I as I try to integrate your notes.

      When we began you can see I was using these three methods in unison and they are working with the exception that my stop loss, if moved my hand during a trade, would reset back to the coded position at the top of every OnBarUpdate.
      • SetProfitTarget();
      • SetStopLoss();
      • EnterLong();
      You told me I needed to use ExitLongStopMarket() and then followed on with the fact that it needs to be wrapped in the OnExecutionUpdate event listener. I have followed all the directions and not one single recommendation has worked for me.

      You are now bring up new issues and asking questions that I clearly don't have the answers for. I have humbly come here for help. My original request was, "Why does my stop loss keep snapping back to it's original place." As an expert with NinjaScript and knowing that I don't have a fluency with this library I think it would be helpful if you could use your expertise to holistically look at what I'm trying to accomplish and make a recommendation from that perspective.

      The code you sent over does not have a profit target so clearly I would have no way to tell if SetProfitTarget() is a method I could NOT use along with ExitLongStopMarket(). You are aware that I am using SetProfitTarget as I'm included sample code in my posts a coupe of time. This is nothing new.

      No where in the documentation on ExitLongStopMarket does it state that it's not compatible with SetProfitTarget. This is typical with the documentation. Never does it remotely suggest how these methods work together. Everything is so fragmented that it's impossible to tell what to do. Even the simplest note like SetStopLoss needs to be placed a line above EnterLong is just something I had to learn scouring through the forums. that's a critical piece of information and the expectation is that a new user will just figure that out on their own.


      What I'm trying to do is extremely basic. Ultimately I'm looking to place an ATM style split order (scalp + runner) with protection: stop loss and target. I feel like this is what everyone wants. We're all trying to emulate the way we actually trade.

      Again, if you can holistically look at what I'm trying to accomplish and make a recommendation that would be helpful. If there is example code for a split order with protection that would be extremely helpful.

      You are the expert. I don't know what I don't know but I have made my living as a developer and also own an advertising software company. I do understand software but this library and it's documentation leaves me with a lot of questions and dead ends.

      I respect your time and appreciate you help.








      Last edited by coopgrafik; 08-04-2021, 03:41 PM.

      Comment


        #18
        Hello coopgrafik,

        I've added in a profit target with ExitLongLimit() to show how ExitLongLimit() is called.

        From the help guide on the Internal Order Handling rules I have linked in post #16:
        "Methods that generate orders to exit a position will be ignored if:
        •A position is open and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
        •A position is open and an order submitted by a set method (SetStopLoss() for example) is active"
        "Set() methods that generate orders to exit a position will be ignored if:
        •A position is open and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
        •A position is open and an order submitted by a non market order exit method (ExitLongLimit() for example) is active"

        This is stating that if you attempt to use an exit order after an set method that will be ignored. If you attempt to use a set method after an exit order, it will be ignored.

        You cannot use SetProfitTarget, SetTrailStop, or SetStopLoss or any set method if an exit order is working or vice versa. Entries also have their own handling rules listed above.

        Take a look at the ProfitChaseStopTrailExitOrdersExample_NT8 that was linked in post #8. This is a demonstration that places both the stop and the target using ExitLongStopMarket and ExitLongLimit() and be used to learn how to code something this. This also shows logic on how to move these, however you don't have to use that logic to move them, you can place them all them to sit and remove the logic that changes the prices.


        I am not able to debug your code, so I must guide with questions and answer any of your direct questions. Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one on one educational support over the phone in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

        We can provide demonstration example scripts for you to import and learn from. Links to educational material in the help guide and videos. We are also able to guide you through the debugging process to assist or building process of a specific script you are wanting to work out.

        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.


        What I can do is answer any questions that you may have.

        Set methods are updated and controlled by NinjaTrader and updated anytime OnBarUpdates. This means that they will move back to their NinjaTrader calculated positions.
        Exit methods, however, will not move back to their NinjaTrader calculated positions. These do use the managed approach and have rules in respect to the position and other working orders. The strategy position is specifically updated from the OnExecutionUpdate event. This means that the strategy will not have the position open for the exit order to attach to and will be ignored.

        Enabling TraceOrders will allow to see when orders are being ignored and why.
        Below is a link to a forum post that demonstrates using TraceOrders and prints to understand what is happening with an order.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #19
          Chelsea,

          I'm been pecking away at this on and off for the last week and I'm just not able to get this working in tow directions. Can you look at the code below and let be know why the order is not firing off at all? I can get this to work long or short but when I try to build this in a way where the strategy will go long under the right conditions and the later (not at the same time) short under the correct conditions the thing just kind of does nothing. I have tried to put unique names for everything hoping that would help but no dice.

          Any ideas?


          Code:
          protected override void OnBarUpdate()
          {
          
          if (BarsInProgress != 0)
          return;
          
          if (CurrentBars[0] < 10)
          return;
          
          
          if ((Position.MarketPosition == MarketPosition.Flat)
          
          && ( Conditions ))
          {
          
          EnterLong(DefaultQuantity, "scalpLong");
          EnterLong(DefaultQuantity, "runnerLong");
          
          }
          
          
          if ((Position.MarketPosition == MarketPosition.Flat)
          
          && ( Conditions ))
          {
          
          EnterShort(DefaultQuantity, "scalpShort");
          EnterShort(DefaultQuantity, "runnerShort");
          
          }
          
          }
          
          
          protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
          {
          
          
          if (execution.Order.Name == "scalpLong" && execution.Order.OrderState == OrderState.Filled)
          {
          
          ExitLongStopMarket(0, true, 1, execution.Order.AverageFillPrice - 17 * TickSize, "exitStopScalpLong", "scalpLong");
          ExitLongLimit(0, true, 1, execution.Order.AverageFillPrice + 7 * TickSize, "exitLimitScalpLong", "scalpLong");
          
          }
          
          
          if (execution.Order.Name == "runnerLong" && execution.Order.OrderState == OrderState.Filled)
          {
          
          ExitLongStopMarket(0, true, 1, execution.Order.AverageFillPrice - 17 * TickSize, "exitStopRunnerLong", "runnerLong");
          ExitLongLimit(0, true, 1, execution.Order.AverageFillPrice + 200 * TickSize, "exitLimitRunnerLong", "runnerLong");
          
          }
          
          if (execution.Order.Name == "scalpShort" && execution.Order.OrderState == OrderState.Filled)
          {
          
          ExitLongStopMarket(0, true, 1, execution.Order.AverageFillPrice - 17 * TickSize, "exitStopScalpShort", "scalpShort");
          ExitLongLimit(0, true, 1, execution.Order.AverageFillPrice + 7 * TickSize, "exitLimitScalpShort", "scalpShort");
          
          }
          
          
          if (execution.Order.Name == "runnerShort" && execution.Order.OrderState == OrderState.Filled)
          {
          
          ExitLongStopMarket(0, true, 1, execution.Order.AverageFillPrice - 17 * TickSize, "exitStopRunnerShort", "runnerShort");
          ExitLongLimit(0, true, 1, execution.Order.AverageFillPrice + 200 * TickSize, "exitLimitRunnerShort", "runnerShort");
          
          }
          
          }
          
          }
          }

          Comment


            #20
            Hello coopgrafik,

            You will need to use prints and TraceOrders to understand the behavior.

            Below is a link to a forum post that demonstrates how to use prints to understand behavior.


            Start by enabling TraceOrders. Then print the time of the bar outside and above the condition that is not evaluating as true when you expect. Next add all of the values in the condition to the print.

            The prints and TraceOrders will tell you why any condition or any order is or is not submitted.


            Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one on one educational support over the phone in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

            That said, through email we are happy to answer any questions you may have about NinjaScript if you decide to code this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

            You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #21
              Chelsea,

              Before you typed 3 paragraphs of how you can't help me did you look at the code? This is literally the same code you provided to me in your previous post. I have simply added additional calls to go short as well as long. I am not asking you to debug my code. As you can see this is not even a real algo as there are no conditions.I'm asking for help with the methods you recommended I use. I'm searching for an explanation on how all of these methods work together.

              I have looked the the docs and they are extremely bare.

              OnExecutionUpdate code example from the docs.

              Code:
              // Example
              protected override void OnExecutionUpdate(Executio n execution, string executionId, double price, int  quantity, MarketPosition marketPosition, string o rderId, DateTime time)
              {
                Print(execution.ToString());
              }

              ExitLongStopMarket code example from the docs.


              Code:
              // Exits position
              ExitLongStopMarket(stopPrice);

              This is it. This is what you give your customers yet you expect me to somehow understand how these methods work? Nothing talks about how an stoploss is bound to an order. There are no examples of anything. The docs list the arguments and then show only the most basic usage of that method without any context of how they are supposed to work together. EXAMPLE: SetStopLoss and SetProfitTarget has to be above EnterShort or they won't work. This critical fact is not in the docs. This is just one example of many where the docs fail to demonstrate real world usage.

              I first started coding in 1980: basic and assembler. Then HTML in the 90s. A full LAMP stack in the 2000s and finally into a MEAN stack. I have made my living as a developer. I own a creative agency that has won awards for the projects I have built. I'm also founder a ad tech company that servers banner ads for the movies and build that platform from the ground up. I know how to code.

              Please stop insisting that I need to take a class to learn C#. I'm confused and offended by that. I could do 200 hours on codecademy and learn every single aspect of C# but at the end I will still have no idea how NinjaTrader proprietary scripting language deal with advanced order handling BECAUSE THIS HAS NOTHING TO DO WITH C#. NinjaScript is a proprietary library of methods. NinjaTrader created it, not Microsoft so if I ask a question on how the methods work or why they are not working I feel like that it a completely fair question.


              Comment


                #22
                Hello coopgrafik,

                I did look at the code you suggested. The is psuedo code and there are a lot of unknowns. I do not know what is causing the issue, because I have no idea what the conditions are evaluating as, what orders may already be working that would violate the internal handling rules, what the entries per direction is. TraceOrders would tell us exactly what is wrong if the order method is being called.

                If you only want to know if the order methods calls are right, I can say yes, the method calls are correct. However, there can be many other reasons an order is not submitted or filled.

                The way to understand what is happening is with prints. If this was my script I would first be going to the TraceOrders and prints to understanding of what is going wrong.

                To move forward, we need the output from TraceOrders and prints. There are many many threads on this forum where I have assisted other community members on adding prints, getting output, analzying the output, understanding what is wrong, and advising how to correct the script for the behavior the coder is wanting. And I am very happy to help. But to do so, you would need to follow this direction to provide me the information to assist.

                I am not expecting you to understand how all of the methods work and I can answer any direct questions you have about method (instead of whats wrong with this code without doing any debugging yourself).

                I cannot advise what is wrong without seeing the output. The information is how we understand what is wrong.

                If you have been programming since 1980, you may find that enabling TraceOrders and adding prints will be very simple for you, and easy to save the output from the window to a text file, as these are not advanced. Like any programming in any language, debugging can sometimes be a majority of the development process.

                As per your example:
                EXAMPLE: SetStopLoss and SetProfitTarget has to be above EnterShort or they won't work. This critical fact is not in the docs. This is just one example of many where the docs fail to demonstrate real world usage.
                This actually depends on the direction of the entry and follows the rules the brokerage have set. A buy stop order must have the stop price above the current ask, and a sell stop below the current bid. If the entry is a market order, then this would be based off the current ask and current bid at the time of the fill.

                The rules are the same if you were to call you broker and ask to place a stop market order over the phone as these are brokerage trading rules.

                Below is a link to investopdia, an educational site, on how a stop market order works. (See under the heading 'Buy-Stop')



                If the order is on the wrong side of the market, the output from TraceOrders and a print of the order object in OnOrderUpdate() would immediately identify this as the issue.


                Please let me know if you want to debug this and figure what is wrong and how to correct it. I am very happy to assist you through the process.
                Last edited by NinjaTrader_ChelseaB; 08-17-2021, 03:44 PM.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CortexZenUSA, Today, 12:53 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by CortexZenUSA, Today, 12:46 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by usazencortex, Today, 12:43 AM
                0 responses
                5 views
                0 likes
                Last Post usazencortex  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                168 responses
                2,265 views
                0 likes
                Last Post sidlercom80  
                Started by Barry Milan, Yesterday, 10:35 PM
                3 responses
                12 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X