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

Trailing Pending Orders

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

    Trailing Pending Orders

    Hello, Good morning,


    I'm developing a strategy with Long/ShortStop Orders and I'm trying to move (trailing) the orders following the prize.

    I have a problem to get the distance (in ticks) between the price (Ask/Bid) and the pending orders. The formula (difference) do not work fine!

    ================================================== ========

    This is the code about the place and modifying orders, in "OnBarUpdate()" and CalculateOnBarClose = true;

    Show you img for alternative view;


    Code:
    
    #region PENDING ORDER Type 3
        //    PENDING ORDERS (type 3)
                
        if (entrytype == 3)
        {        
            if (Position.MarketPosition == MarketPosition.Flat)
                {                
                    if (entryOrderBuy == null && TradeEntry == 1)
                    {
                        entryOrderBuy = EnterLongStop(0, true, 1, GetCurrentAsk() + OffSetOrder * TickSize, "buy order");
                    }
                    
                    if (entryOrderSell == null && TradeEntry == -1)
                    {
                        entryOrderSell = EnterShortStop(0, true, 1, GetCurrentBid() - OffSetOrder * TickSize, "sell order");
                    }                
                }
                
                
                
        // MODIFIED ORDER            
    
                
            if (entryOrderBuy != null &&  (entryOrderBuy.StopPrice - GetCurrentAsk()) > OffSetOrder * TickSize)
                    
                {
                    entryOrderBuy = EnterLongStop(0, true, 1, GetCurrentAsk() + OffSetOrder * TickSize, "buy order");
                    
                    //Print (entryOrderBuy.StopPrice - GetCurrentAsk() );
                    //Print (GetCurrentAsk());
                }
                
                
            if (entryOrderSell != null &&  (GetCurrentBid() - entryOrderSell.StopPrice) > OffSetOrder * TickSize)
                    
                {
                    entryOrderSell = EnterShortStop(0, true, 1, GetCurrentBid() - OffSetOrder * TickSize, "sell order");
                    
                    //Print (entryOrderBuy.StopPrice - GetCurrentAsk() );
                    //Print (GetCurrentAsk());
                }            
                
        }
                
        #endregion
    Then in OnOrderUpdate(IOrder order) I Print the value only when the Order is still pending but I get very bad values, finally the Strategy work but not well!

    This is the code to Print:

    Code:
    protected override void OnOrderUpdate(IOrder order)
            {        
                
                if (entryOrderBuy != null && entryOrderBuy == order)  
                {
                    if (order.OrderState != OrderState.Filled)
                    Print ("Moving pending buy order: STOP PRICE = " + entryOrderBuy.StopPrice + " | ASK PRICE = " + GetCurrentAsk()+ " | DIFF. PRICE = " +(entryOrderBuy.StopPrice - GetCurrentAsk()) );
                }
                
                if (entryOrderSell != null && entryOrderSell == order)  
                {
                    if (order.OrderState != OrderState.Filled)
                    Print ("Moving pending sell order: STOP PRICE = " + entryOrderSell.StopPrice + " | BID PRICE = " + GetCurrentBid()+ " | DIFF. PRICE = " +(GetCurrentBid() - entryOrderSell.StopPrice) );
                }
    ... end this is the output:

    Code:
    Moving pending sell order: STOP PRICE = 0,7284 | BID PRICE = 0,7285 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending sell order: STOP PRICE = 0,7284 | BID PRICE = 0,7285 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending sell order: STOP PRICE = 0,7285 | BID PRICE = 0,7286 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending sell order: STOP PRICE = 0,7285 | BID PRICE = 0,7286 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending sell order: STOP PRICE = 0,7285 | BID PRICE = 0,7286 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0 | ASK PRICE = 0,7284 | DIFF. PRICE = -0,7284
    Moving pending buy order: STOP PRICE = 0 | ASK PRICE = 0,7284 | DIFF. PRICE = -0,7284
    Moving pending buy order: STOP PRICE = 0 | ASK PRICE = 0,7284 | DIFF. PRICE = -0,7284
    Moving pending buy order: STOP PRICE = 0,7282 | ASK PRICE = 0,7281 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7282 | ASK PRICE = 0,7281 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7282 | ASK PRICE = 0,7281 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7281 | ASK PRICE = 0,728 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7281 | ASK PRICE = 0,728 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7281 | ASK PRICE = 0,728 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7277 | ASK PRICE = 0,7276 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7277 | ASK PRICE = 0,7276 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7277 | ASK PRICE = 0,7276 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7276 | ASK PRICE = 0,7275 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7276 | ASK PRICE = 0,7275 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7276 | ASK PRICE = 0,7275 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending sell order: STOP PRICE = 0,7283 | BID PRICE = 0,7284 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending sell order: STOP PRICE = 0,7283 | BID PRICE = 0,7284 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending sell order: STOP PRICE = 0,7283 | BID PRICE = 0,7284 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0 | ASK PRICE = 0,7282 | DIFF. PRICE = -0,7282
    Moving pending buy order: STOP PRICE = 0 | ASK PRICE = 0,7282 | DIFF. PRICE = -0,7282
    Moving pending buy order: STOP PRICE = 0 | ASK PRICE = 0,7282 | DIFF. PRICE = -0,7282
    Moving pending buy order: STOP PRICE = 0,7281 | ASK PRICE = 0,728 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7281 | ASK PRICE = 0,728 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,7281 | ASK PRICE = 0,728 | DIFF. PRICE = 9,9999999999989E-05
    Moving pending buy order: STOP PRICE = 0,728 | ASK PRICE = 0,7279 | DIFF. PRICE = 9,9999999999989E-05
    And finally this is a screen of the Chart:



    Questions:

    1. Why with differencial of 1 point I get difference ans so bad values?
    Ex1: Moving pending sell order: STOP PRICE = 0,7283 | BID PRICE = 0,7284 | DIFF. PRICE = 9,9999999999989E-05
    Ex2: Moving pending sell order: STOP PRICE = 0,7283 | BID PRICE = 0,7284 | DIFF. PRICE = 0,0001000000000001
    ... and sometimes why the StopPrice=0 ?..I have set more condition to Print only with StopOrder Active!

    2. How can I get perfect values (on candle close: false or true):
    Difference between order and price: 1,2,3,4,5 ...point

    3. How can you see I have used "
    order.OrderState != OrderState.Filled" to work with pending order "active" (no filled). I have try OrderState.PartFilled but is never active, do not work. Could you tell me something about? When there is a stop/limit order active, the OrderState.PartFilled is True??


    Many thanks for your support
    Last edited by ClauTrade; 01-04-2017, 07:05 AM.

    #2
    Hello ClauTrade, and thank you for your questions. While we may only answer questions about how Ninja operates, and while direct development and debugging are outside of the scope of the support we may provide, I may answer some of the questions you have. Should you require more in depth development and debugging help, please let us know so we can help put you in touch with a NinjaScript consultant.

    With respect to the 9.99... x 10^-5 numbers, and the 0.0001 + ε numbers, you can filter and convert these to 0.0001 with this code :

    Code:
    [FONT=Courier New]// publicly available C# documentation
    // https://msdn.microsoft.com/en-us/library/system.math.round.aspx
    Print("... | DIFF. PRICE = " + Math.Round(entryOrderBuy.StopPrice - GetCurrentAsk(), 4));[/FONT]
    To your second question, the 2nd argument to Math.Round is the number of decimal places to round to. You can pass 0 in here if you would like whole numbers.

    To your third question, the trade desk makes the decision whether to PartFill an order or not, and for this reason I would recommend handling this state in OnExecution (NT8 OnExecutionUpdate) rather than OnOrderUpdate. This state is not related to whether an order away from the price is active. I recommend printing out your order states for debugging purposes. I also recommend reviewing the order state definitions here.

    NT7 : http://ninjatrader.com/support/helpG...nt7/iorder.htm
    NT8 : http://ninjatrader.com/support/helpG...n-us/order.htm

    Orders away from the current price are usually in the Working state.

    Please let us know if there are any other ways we can help.
    Last edited by NinjaTrader_JessicaP; 01-05-2017, 09:46 AM.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Many Thanks Jessica, great exhaustive answers!

      So, I confirm the code to convert/filter work fine, this is the new code:

      Code:
      protected override void OnExecution(IExecution execution)
              {
                  
                  
                  if (entryOrderBuy != null && entryOrderBuy == execution.Order)  
                  {
                      if (execution.Order.OrderState == OrderState.Filled)
                       Print (Time[0].ToString() +"| LongStopOrder = " +  entryOrderBuy.StopPrice + " | ASK PRICE = " + GetCurrentAsk()+ " | DIFF.  PRICE = " + Math.Round(entryOrderBuy.StopPrice - GetCurrentAsk(), 4));
                      
                   }
                  
                  if (entryOrderSell != null && entryOrderSell == execution.Order)  
                  {
                      if (execution.Order.OrderState == OrderState.Filled)
                       Print (Time[0].ToString() +"| ShortStopOrder = " +  entryOrderSell.StopPrice + " | BID PRICE = " + GetCurrentBid()+ " |  DIFF. PRICE = " + Math.Round(GetCurrentBid()- entryOrderSell.StopPrice,  4) );
                  }



      This is part of the output:

      20/12/2016 12:26:29| ShortStopOrder = 0,7211 | BID PRICE = 0,7212 | DIFF. PRICE = 0,0001
      20/12/2016 12:58:51| ShortStopOrder = 0,7215 | BID PRICE = 0,7216 | DIFF. PRICE = 0,0001
      20/12/2016 13:10:01| LongStopOrder = 0 | ASK PRICE = 0,7219 | DIFF. PRICE = -0,7219
      20/12/2016 13:20:03| LongStopOrder = 0,7217 | ASK PRICE = 0,7216 | DIFF. PRICE = 0,0001
      20/12/2016 14:30:27| LongStopOrder = 0,7211 | ASK PRICE = 0,721 | DIFF. PRICE = 0,0001
      20/12/2016 15:29:16| LongStopOrder = 0,7221 | ASK PRICE = 0,722 | DIFF. PRICE = 0,0001
      20/12/2016 17:09:08| LongStopOrder = 0,7234 | ASK PRICE = 0,7233 | DIFF. PRICE = 0,0001
      20/12/2016 19:46:02| LongStopOrder = 0,7237 | ASK PRICE = 0,7236 | DIFF. PRICE = 0,0001


      I have find that it print Order.StopPrice=0 when the order is closed by Strategy (not stop loss)... I'll check the code but I do not understand and my question is:

      How in "OnExecution(IExecution execution)" the strategy Print an Order with Price=0 ?? There are several filter for Print only when the order is active!

      This is the simply code in OnBarUpdate() to close the trades by strategy:

      Code:
          
          //     EXIT BY STRATEGY
                  
                  if (Position.MarketPosition == MarketPosition.Long && TradeExit == 1)
                  {
                          entryOrderBuy = ExitLong("buy order");
                          entryOrderBuy     = null;                        
                  }
                  
                  if (Position.MarketPosition == MarketPosition.Short && TradeExit == -1)
                  {
                          entryOrderBuy = ExitShort("sell order");
                          entryOrderSell     = null;                        
                  }

      Comment


        #4
        I believe what is occurring is that the order is being closed with a market order. To know for certain what is happening, you may want to simply

        Print(execution.order.ToString())

        This will let you know exactly what kind of order is being processed, all of its price values, and many more things about this order.

        If you could add this into your code, and then run it over the same time period in the Market Replay connection, if you then see any more unusual output please don't hesitate to reach out so we may assist further.
        Jessica P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bortz, 11-06-2023, 08:04 AM
        47 responses
        1,602 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        8 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  
        Working...
        X