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

SetStopLoss not send to the broker

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

    SetStopLoss not send to the broker

    Hi

    when i write this code :

    // Condition Buy
    if (CrossAbove(Stochastics(5,Stoch, 3).K, survente, 1)
    {
    EnterLong(DefaultQuantity,
    "Achat");
    SetProfitTarget(
    "Achat", CalculationMode.Ticks, target);
    SetStopLoss(
    "Achat", CalculationMode.Ticks, stop, true);
    }

    My profit order is sent to the broker, the state in NT is green "working"

    but the StopLoss is yellow and "initialzed" and i cannot see it on the broker plateforme... so the time NT send the order to the broker i have a slippage of many ticks, so how it is possible to have a stoploss order state green and "working" sent to the broke like the profit ?

    Can you help ?

    #2
    Hello,

    You have the "Simulated" bool set to true in your SetStopLoss method. The simulated Stop will be held on your local PC and not send to the broker until the stop is triggered.

    If you would like to send the Stop to the broker, please change this to false:

    Code:
    SetStopLoss("Achat", CalculationMode.Ticks, stop, [B]false[/B]);
    MatthewNinjaTrader Product Management

    Comment


      #3
      I have another problem, when i change the EnterLong by an EnterLongLimit like that:

      // Condition Buy
      if (CrossAbove(Stochastics(5,Stoch, 3).K, survente, 1)
      {
      EnterLongLimit(DefaultQuantity, Close[
      0], "STSAchat");
      SetProfitTarget(
      "STSAchat", CalculationMode.Ticks, target);
      SetStopLoss(
      "STSAchat", CalculationMode.Ticks, stop, true);
      }
      // Condition Sell
      if (CrossBelow(Stochastics(5,Stoch, 3).K, surachat, 1)
      {
      EnterShortLimit(DefaultQuantity,Close[
      0], "STSVente");
      SetProfitTarget(
      "STSVente", CalculationMode.Ticks, target);
      SetStopLoss(
      "STSVente", CalculationMode.Ticks, stop, true);
      }

      when for example the position is long and a bearish signal is sent, it is not reversing the position... but if the entry order are not limit (EnterLong and EnterShort), it is reversing... i don't understand why the nature of the entry order is changing the reversal position...

      Comment


        #4
        Hello,

        There are some Internal Order Handling Rules that Reduce Unwanted Positions
        that can be envoked that will prevent you from entering multiple orders at the same time. For example:

        Methods that generate orders to enter a position will be ignored if:
        • A position is open and an order submitted by an exit method (ExitLongLimit() 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 and the order is used to open a position in the opposite direction
        • The strategy position is flat 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
        • The entry signal name is not unique
        There are ways to avoid these however - can you please clarify if you're completing your strategy by hand through code or through the Strategy Wizard?
        MatthewNinjaTrader Product Management

        Comment


          #5
          i'm completing my strategy by hand through code.

          Comment


            #6
            I would recommend using OnExecution or OnOrderUpdate to set your SetStop and SetTarget to prevent these orders from being ignored at the OnBarUpdate call when the entry condition is true.

            Please see our Reference Sample on Using OnOrderUpdate() and OnExecution() methods to submit protective orders
            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()


            I'd also suggest enabling the TraceOrders property for additional debugging to understand exactly why the order are being ignored or canceled.

            More information on TraceOrders can be found below:

            MatthewNinjaTrader Product Management

            Comment


              #7
              Hi Matthew

              i wrote the code with OnExecution and OnOrderUpdate but the problem is when the strategy is long and there is a sell condition, the strategy stay long untill the stop or the target is hit.

              With the previous script OnBarUpdate, it is reversing if i'm long and a sell condition appears.

              you will find enclosed my code if you can help.

              Thx.
              Attached Files

              Comment


                #8
                Hi Thomas79,

                This will require some advanced order handling to work around, and you have to move away from "set" statements completely.

                Set orders are mainly for convenience and your strategy will require more control with orders. Once set, they cannot become unset (they'll be applied to any position matching signal name), they can't be cancelled, and these working orders will always prevent submission of a limit reversal order according to internal order handling rules.

                Here is what's required for limit order reversal while having protective orders:
                Use the previously linked OnOrderUpdate()/ OnExecution reference sample for help submitting protective orders with the same timing as set orders. Instead of set, your target order needs to be submitted with either ExitLongLimit() or ExitShortLimit().

                If you want a reversal with a limit order, you must first cancel the working exit order, and then submit your entry order. This sample can help with CancelOrder()
                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


                This can be a bit tricky to code and knowledge of advanced topics like IOrders and advanced event handlers is needed. These pages can help you more with that:

                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Ryan

                  if i well understand, if the position is long and there is a selling signal

                  i have to:

                  1 - cancel the exitlongstop() and the exitlonglimit() orders
                  2 - exit the long position with a exitlong() order (because i want to exit at market not with a limit)
                  3- and then send an entershortlimit() order

                  is it all right ?

                  but i have to call all this handling in the OnBarUpdate or in OnOrderUpdate ?

                  Thx

                  Comment


                    #10
                    Hello Thomas79,
                    Yes, but you have to make sure the orders are cancelled and the position is flat before submitting the Sell limit order.

                    You can put the Exit orders and the order cancellation order in OnOrderUpate (or OnExecution) and put the entry order in OnBarUpdate (after making sure that you are flat etc).




                    Please let me know if I can assist you any further.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Internal order handling rules will not apply to market orders like ExitLong() and ExitShort(). This means you do not have to cancel working exit limit orders since this would already be handled by these market exit orders.

                      Cancelling is needed when you want to replace the working exit limit order with an entry limit order for reversal.
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        ok Ryan

                        so i have just to say

                        if position long and selling condition appears, exitlong() order and entershortlimit() order

                        and that on the OnBarUpdate ?

                        Comment


                          #13
                          That's the idea but you can't have both orders in the same place. They need to be submitted in a sequence. This is ultimately the sequence you should go for and it could be done all in OnBarUpdate or using advanced handlers depending on your experience with them:
                          Market sell condition > Submit ExitLong() > ExitLong filled > Submit EnterShortLimit() order.

                          Since part of this sequence depends on the exit long order filled, OnExecution() is the best place to check for this.

                          If you wanted to work only in OnBarUpdate() you would still need to code a sequence with bool flags (or user variables), and a MarketPosition check.
                          Ryan M.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by love2code2trade, 04-17-2024, 01:45 PM
                          4 responses
                          36 views
                          0 likes
                          Last Post love2code2trade  
                          Started by alifarahani, Today, 09:40 AM
                          2 responses
                          13 views
                          0 likes
                          Last Post alifarahani  
                          Started by junkone, Today, 11:37 AM
                          3 responses
                          15 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by pickmyonlineclass, Today, 12:23 PM
                          0 responses
                          1 view
                          0 likes
                          Last Post pickmyonlineclass  
                          Started by frankthearm, Yesterday, 09:08 AM
                          12 responses
                          44 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Working...
                          X