Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing Stop using Advanced Order Management

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

    Trailing Stop using Advanced Order Management

    I've recently changed my strategy code from using SetStopLoss() and SetProfitTarget() to Advanced Order Management using the Exit procedures so that I can get Signal Names in the Ninja output. As an aside, I think a nice enhancement to SetStopLoss() and SetProfitTarget() would be an overload that includes a signalName.

    I've been unable to get ExitLongStop() to move my stops properly. I'm able to set an initial stop, but sometimes (not all the time) when I call ExitLongStop() to move my initial stop, the ExitLongStop() procedure returns a null. Here's the pertinent code snippet:


    private IOrder longOrder = null; // order for long position
    private IOrder stopOrder = null; // order for stop
    private double stopLoss = 0.0;

    protected override void OnBarUpdate()
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (stopLoss < MyStopLogic())
    {
    stopLoss = MyStopLogic();
    SetStopPrice(stopLoss, "MyStop", "Entry1");
    }
    }
    }


    protected override void OnOrderUpdate(IOrder order)
    {
    if (longOrder != null && longOrder.Token == order.Token)
    {
    if (order.OrderState == OrderState.Filled)
    {
    stopLoss = MyInitialStop();
    if(stopOrder == null)
    SetStopPrice(stopLoss, "InitialStop", "Entry1");
    }
    }
    }


    private void SetStopPrice(double dStop, string strExit, string strEntry)
    {
    stopOrder = ExitLongStop(0,true,1,dStop, strExit, strEntry);
    }


    What conditions would cause the back-tester to return a null from ExitLongStop()? I'm using liveUntilCancelled so do I have to first cancel my Initial stop before setting a new stop? If I use ExitLong(), I do not get null orders for stop orders executed after the initial stop and the back-tester exits the open position immediately and properly.

    Thanks,

    David
    dbw451

    #2
    Hi dbw451, did you run this with TraceOrders = true? What is the exact error message you are getting?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks Bertrand. No I did not have TraceOrders turned on. The message is:

      Ignored PlaceOrder() method: Action=Sell OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1287.50 SignalName='TrailtoBE' FromEntrySignal='Entry1' Reason='This was an exit order but no position exists to exit'

      I'm not sure why Ninja doesn't think a position exists because I'm looking at the Strategy Analyzer chart and there is definitely an open position. I use an entry name of "Entry1" in all my order procedure calls.

      Regards,
      David
      dbw451

      Comment


        #4
        Hi David, please post the complete code including the 'TrailToBE' section...are you entering a target that could have exited the open position? Then you would need to cancel the Exit order, since those are not OCO automatically (other than the Set() stop and targets).
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hi Bertrand,
          I created a new, minimal strategy which captures my order logic to isolate my trailing stop problem and post for you to this thread. In preparing the strategy, I've identified an order handling issue with my stop and target orders when my position is exited on close. I'm going to start a separate thread asking about exit on close.

          I have not seen in my new strategy the trailing stop problem that I have in my original strategy. If I can identify the problem in the new strategy, I'll post the strategy with a specific example to this thread. I'm sure my issues have to do with my incomplete understanding of how I need to manage orders in Ninja.

          Regards,

          David
          dbw451

          Comment


            #6
            Sounds good David.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I'm still having this trailing stop issue and I'm unable to replicate in a simplified test strategy. However I have more information. I've determined that the problem occurs the next trade after an entry order is placed and then a "Cancelled custom managed order" occurs. These canceled trades are timeouts if the entry price is not hit within one bar. There is a detailed example of the problem in this snapshot:



              I've been working on this problem for two days and have not been able to figure it out. Any suggestions would be most appreciated.

              Regards,

              David
              dbw451

              Comment


                #8
                David,

                Unfortunately we cannot debug your code for you. I suggest you remove the complexity and slowly add them back. You are using many custom functions. Ensure they actually return values and do what you want. You need to take it step by step and isolate the issue.

                As a note, you are using tick time frames and as such you should not rely on timestamps of those bars to determine distinct bars. Very easily you can have several bars with the same timestamp. Instead you should use CurrentBar to distinguish bars.
                Josh P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by jaybedreamin, Today, 05:56 PM
                0 responses
                7 views
                0 likes
                Last Post jaybedreamin  
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                18 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                4 views
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                12 views
                0 likes
                Last Post Javierw.ok  
                Started by timmbbo, Today, 08:59 AM
                2 responses
                13 views
                0 likes
                Last Post bltdavid  
                Working...
                X