Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop and reverse

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

    Stop and reverse

    How can I do a stop and reverse from inside a strategy which is currently long using
    ---> EnterLongLimit(1, EntryUP , "Long");
    The price is close to the stop
    ---> SetStopLoss("Long", CalculationMode.Ticks, S1, false);
    But I have a better exit at a new entry
    ---> EnterShortLimit(1, EntryDN , "Short");

    I can move the stop to EntryDN but the Entershort is ignored.
    I guess the solution is trivial but cant see it...

    pmaglio
    NinjaTrader Ecosystem Vendor - The Indicator Store

    #2
    Hello Pmaglio,

    This would violate internal order handling rules.


    Methods that generate orders (excluding market 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
    To work around this: Instead of using the set statements, you could submit a market order once your stop price has been reached. Your stop loss order is then only submitted when conditions evalute to true. Your EnterShortLimit order would not be ignored based on internal order handline rules as long as there is no order submitted with a Set statement.

    Example:

    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] <= Position.AvgPrice - TickSize * stopTicks)
    ExitLong();
    }
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      ..so let say I am inside the long condition and I want to exit and be short at the same time ;
      would it be correct ?
      if (Position.MarketPosition == MarketPosition.Long)
      {
      if (Close[0] <= Position.AvgPrice - TickSize * stopTicks)
      ExitLong(); EnterShort("signalname");
      }
      pmaglio
      NinjaTrader Ecosystem Vendor - The Indicator Store

      Comment


        #4
        That will likely not work either. The idea here:
        • Don't use use Set statements as you won't be able submit your EnterShortLimit order at the same time.
        • Separate your reversal logic from your standard stop loss logic. Entries will reverse a position so you don't need to call a specfic exit method if you're looking for reversals.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Ok, so without using SetProfitTarget() and SetStopLoss() I can reverse a position by just entering an inverse order ( ie: placing an EnterShort being long ).
          And if I want to exit at a desired profit target without reversing I must use ExitLong(), with the issue of being the exit values (on backtest data) on next bar surrounding. That can be far from the desired target....
          Is there a way of using SetProfitTarget() and SetStopLoss() ( to get exact fills at targets and stops on backtests ) and when a reverse is needed cancel those orders ? ie: something like CancelSetProfitTarget(), etc ? so we can use an inverse order to reverse position ?
          pmaglio
          NinjaTrader Ecosystem Vendor - The Indicator Store

          Comment


            #6
            Unfortunately there's no way to cancel orders submitted by set methods. There's not one exact way you need to follow to solve this.

            What you may consider doing is using ExitLongLimit() for your profit target order. When you run into a condition for reversal you could then issue CancelOrder() commands to this order. Then within the fill state = canceled area of this order you could submit your reversal order EnterShortStop() . An additional bool flag here would be good to ensure you are only submitting your short order for your reversals and not anytime the ExitLongLimit order is canceled.

            This reference sample helps with using CancelOrder() and setting up the needed IOrder objects:
            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


            For your questions on fill prices, please see the following page which details how fills are determined in backtests:
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks Ryan, that works exact as I want...
              About the example code SampleCancelOrder, where cancelling an EnterLongLimit() order makes enter an EnterLong() on OnOrderUpdate()....
              ...Why cancelling the order manually doesnt fires the market order ?
              ..adding this line to the code shows same result on manually cancelled order and by CancelOrder() instruction...
              protectedoverridevoid OnOrderUpdate(IOrder order)
              {
              // Checks for all updates to entryOrder.
              if (entryOrder != null && entryOrder.Token == order.Token)
              {
              Print(CurrentBar+
              " "+order.OrderState );
              pmaglio
              NinjaTrader Ecosystem Vendor - The Indicator Store

              Comment


                #8
                pmaglio, the NS strategy will not interact with manual orders, it's only viewing it's own individual orders - if you want to move strategy orders manually, you should consider calling ATM's for exit management.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  pmaglio, the NS strategy will not interact with manual orders, it's only viewing it's own individual orders - if you want to move strategy orders manually, you should consider calling ATM's for exit management.

                  But when I move an active limit order, the coded detects it and order.OrderState changes from PendingChange to Accepted and finally working. ( The market order is never placed because status is never Cancelled )
                  BUT, if I manually cancel the limit order, I get printed the "Cancelled Status" on the Output Window, so it tries to place the Market Order but I get this error ;
                  **NT** Error on calling 'OnOrderUpdate' method for strategy 'SampleCancelOrder/ae1d78483924405ab83a12bd6b45234e':
                  Why ?
                  pmaglio
                  NinjaTrader Ecosystem Vendor - The Indicator Store

                  Comment


                    #10
                    pmaglio, to be clear : we not recommend getting manually into the strategy generated orders, as the NinjaScript strategy would only know about it's own, individually placed orders.

                    I could not reproduce the issue here - is this all info you see in the log for this issue?

                    Were you running with TraceOrders = true? If yes, does it provide any insight into the error returned?
                    BertrandNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by andrewtrades, Today, 04:57 PM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by chbruno, Today, 04:10 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post chbruno
                    by chbruno
                     
                    Started by josh18955, 03-25-2023, 11:16 AM
                    6 responses
                    436 views
                    0 likes
                    Last Post Delerium  
                    Started by FAQtrader, Today, 03:35 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post FAQtrader  
                    Started by rocketman7, Today, 09:41 AM
                    5 responses
                    19 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X