Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Special StopLoss order placement. How could this be coded?

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

    Special StopLoss order placement. How could this be coded?

    OnBarUpdate() call, current market price is at 1683.75 right now.
    At this very instant, let's say two prices are calcualted and known. 1695.50 and 1680.50.

    I want to achieve the following in code:
    - When price moves up to 1695.50, enter long and 1680.50 acts as the stop loss.
    - When price moves down to 1680.50, enter short and 1695.50 acts as the stop loss.

    I tried using various order types but could not get it to work.
    Please someone give me a hand.

    #2
    I would do it this way.
    ExitLongStop(1680.50);
    ExitShortStop(1695.50);

    //these will be ignored until a position is established

    if (Close[0] >= 1695.5)
    {
    EnterLong("long");
    }

    if (Close[0] <= 1680.50)
    {
    EnterShort("short")
    }


    of course you would replace numbers with variables

    Comment


      #3
      That worked like a charm

      That worked great! Thanks a lot! I had no idea you could place the Exits first.

      Comment


        #4
        Both the ExitLongStop() and ExitShortStop() do not return an object if IOrder. They return null instead. This means I can't use CancelOrder() to cancel them. How do I achieve that?

        Basically what I want to do is
        - set both exits
        - if the price moves up and enters long, cancel ExitShortStop
        - if the price moves down and enters short, cancel ExitLongStop

        Comment


          #5
          Originally posted by jiminssy View Post
          Both the ExitLongStop() and ExitShortStop() do not return an object if IOrder. They return null instead. This means I can't use CancelOrder() to cancel them. How do I achieve that?

          Basically what I want to do is
          - set both exits
          - if the price moves up and enters long, cancel ExitShortStop
          - if the price moves down and enters short, cancel ExitLongStop

          There is no need to cancel the exits since they will be ignored until a corresponding long/short position is established.

          Comment


            #6
            Thanks for the assist Ragdoll.

            jiminssy, generally developing in the managed approach you want to be aware of the internal order handling rules which would have been the reason for your unexpected outcomes seen likely -



            To debug the orders under the hood and see if and when something is ignored, please run with the TraceOrders option - http://www.ninjatrader.com/support/h...raceorders.htm

            Enter and Exit methods can both return an IOrder, if you assigned one to them on first placement - this could then be used in your CancelOrder() call.



            Further helpful example would be found here -

            The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

            When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until

            Comment


              #7
              Hi Bertrand,

              Thank you for the reply. However I am experiencing null being returned from ExitLongStop() method. Following is what I've done to capture just that.

              if (ExitLongStop(prices.lowest) == null)
              {
              Print("Order received for exiting long was null");
              }

              I am observing the print statement appear in output window. I need to have the reference to the IOrder to later cancel the stop. One thing to note though is as Ragdoll suggested, I am calling the Exit methods before the Enter methods. Would this have an affect?

              I know that Ragdoll also mentioned there is no need to cancel the order but as my strategy runs across multiple days, I would like to clean orders myself in the code and manage the orders as clean as possible if I can.

              Comment


                #8
                Hi Jiminssy,

                If you put a TraceOrder command in your initialize section it will print information in the output window . There you will see that the stop orders have not been executed until a corresponding Long or Short order has been executed. Unless you want to cancel a working stop order there will not be anything to cancel since the stop orders were never executed.

                Hope this helps.

                Comment


                  #9
                  Originally posted by jiminssy View Post
                  Hi Bertrand,

                  Thank you for the reply. However I am experiencing null being returned from ExitLongStop() method. Following is what I've done to capture just that.

                  if (ExitLongStop(prices.lowest) == null)
                  {
                  Print("Order received for exiting long was null");
                  }

                  I am observing the print statement appear in output window. I need to have the reference to the IOrder to later cancel the stop. One thing to note though is as Ragdoll suggested, I am calling the Exit methods before the Enter methods. Would this have an affect?

                  I know that Ragdoll also mentioned there is no need to cancel the order but as my strategy runs across multiple days, I would like to clean orders myself in the code and manage the orders as clean as possible if I can.
                  Explicitly declare IOrders and assign your Exit orders to them. That way you have explicit objects to use as the targets of your cancellations.

                  Comment


                    #10
                    Sorry I am not quite following you guys. What I am saying is, ExitLongStop() method should return an object of IOrder. Like Koganam mentioned, even if I do something like

                    IOrder exitOrder = ExitLongStop(price);
                    CancelOrder(exitOrder);

                    This is throwing NullPointerException as ExitLongStop(price) is returning "null" and not an actual IOrder object. This is proven in my example below when I see the print statement in output window.

                    if (ExitLongStop(prices.lowest) == null)
                    {
                    Print("Order received for exiting long was null");
                    }

                    So, my question is how do you cancel an exit order when you don't receive any IOrder object. I know the exit order is sitting some where in the system because I do see the exit executing when the price actually moves to that mark.

                    Comment


                      #11
                      Are you trying to cancel the active stop order or the opposite order?

                      Comment


                        #12
                        So here is what I am doing so far with your code suggestion:

                        - let's say there are three prices: 3(highest), 2, 1(lowest).
                        - Right now the market is at price 2.
                        - I placed long stop order at price 1.
                        - I placed short stop order at price 3.

                        Here is what I want to do:
                        - When market moves to price 3, go long and cancel short stop at price 3. If ever market moves down to 1, I have long stop there and loss is stopped.
                        - When market moves to price 1, go short and cancel long stop at price 1. If ever market moves up to 3, I have short stop there and loss is stopped.

                        I want to cancel those stops because they are unnecessary in my strategy and I like cleaning things up as my strategy might be running across multiple days I feel that it wouldn't hurt to clean things up.

                        Comment


                          #13
                          Originally posted by jiminssy View Post
                          So here is what I am doing so far with your code suggestion:

                          - let's say there are three prices: 3(highest), 2, 1(lowest).
                          - Right now the market is at price 2.
                          - I placed long stop order at price 1.
                          - I placed short stop order at price 3.

                          Here is what I want to do:
                          - When market moves to price 3, go long and cancel short stop at price 3. If ever market moves down to 1, I have long stop there and loss is stopped.
                          - When market moves to price 1, go short and cancel long stop at price 1. If ever market moves up to 3, I have short stop there and loss is stopped.

                          I want to cancel those stops because they are unnecessary in my strategy and I like cleaning things up as my strategy might be running across multiple days I feel that it wouldn't hurt to clean things up.

                          So your trying to clean up the opposite stop. The reason you are getting a null on that stop is because it doesn't exist. The stops are not executed unless there is a corresponding long or short position. In other words when the price goes to three the stop at one gets instantiated. Prior to that it was ignored and not instantiated. Therefore the stop at three doesn't exist and you get the null exception. You are trying to remove something that was never created.
                          Last edited by Ragdoll; 10-05-2013, 10:49 AM.

                          Comment


                            #14
                            I appreciate all the help Ragdoll, thank you very much.

                            See that's where I am getting confused. Let's consider the following scenario.

                            - Current price is at 2.
                            - two stops placed at 3 and 1.
                            - Price moves to 3, enter long, and according to you, due to this long order, stop at price 1 gets executed internally in NT.
                            - At this point stop at price 3 is still waiting on its corresponding short order and I have no way to "cancel" that "waiting-to-create-order" logic. (I tested this by enter short and stop kicked in when market moved against me)

                            - next day, I want to perform the similar strategy.
                            - Current price is at 5
                            - two stops placed at 6 and 4.
                            - Price moves to 4, enter short, and which stop is it going to execute then? stop at 3 that was placed yesterday? or stop at 6?

                            To me ExitLongStop() and ExistShortStop() clearly are some kind of logic initiated that we should be able to also take back. Just not sure how that is done in NT even after going through the documentation and testing things around myself...

                            The doc says,

                            ExitLongStop()

                            Method Return Value
                            An IOrder read-only object that represents the order. Reserved for experienced
                            programmers, additional information can be found within the Advanced Order Handling
                            section.
                            It says it returns an object of IOrder.

                            Comment


                              #15
                              Sorry it just hit me, why not just test it out myself lol. Will get back with what I find.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              371 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X