Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unexpected order was submitted

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

    Unexpected order was submitted

    I'm trying to understand how my strategy submitted an unexpected order. My strategy triggers OnBarUpdate() on each tick, and places orders as follows:

    At the start of each hourly bar, an expected minimum price (predLow) and expected maxiumum price (predHigh) are calculated for the that hour and, in this case, an order to open a long position was subsequently submitted using the instruction

    Code:
    entryOrder = EnterLongLimit(Convert.ToInt32(orderQuantity), predLow, longSignalName);
    Here the order was submitted and executed as expected. From the NT log,

    2023-09-28 10:03:42.912 Execution Execution='978189250560' Instrument='ES 12-23' Account='XXXXXX' Exchange=Globex Price=4344.25 Quantity=1 Market position=Long Operation=Operation_Add Order='1293702408' Time='2023-09-28 10:03'
    After the buy order executed, my strategy placed a limit sell order, as intended, using OnExecutionUpdate:

    Code:
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity,
    MarketPosition marketPosition, string orderId, DateTime time)
    {
         if (execution.Order.Name == longSignalName)
         {
              ExitLongLimit(predHigh, longSignalName);
         }
         else if (execution.Order.Name == shortSignalName)
         {
              ExitShortLimit(predLow, shortSignalName);
         }
    }
    From the NT log,

    2023-09-28 10:03:42.913 NinjaScript NinjaScript strategy 'HourlyLongWithTryES/301101426' submitting order

    2023-09-28 10:03:42.988 Order Order='88ea8cf512de4d71bc389ed344ffb215/XXXXXX' Name='Sell' New state='Submitted' Instrument='ES 12-23' Action='Sell' Limit price=4358 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:03:42.989 Position Instrument='ES 12-23' Account='XXXXXX' Average price=4344.25 Quantity=1 Market position=Long Operation=Operation_Add

    2023-09-28 10:03:43.025 Order Order='1293688293/XXXXXX' Name='Sell' New state='Accepted' Instrument='ES 12-23' Action='Sell' Limit price=4358 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:03:43.025 Order Order='1293688293/XXXXXX' Name='Sell' New state='Working' Instrument='ES 12-23' Action='Sell' Limit price=4358 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Before the position could reach its expected exit price, however, its unrealized profit became more negative than was allowed, causing a stop loss condition that resulted in the submission of a market sell order using the following code:

    Code:
    if (PositionAccount.MarketPosition == MarketPosition.Long)
    {
         if (PositionAccount.GetUnrealizedProfitLoss(Performan ceUnit.Currency, GetCurrentBid()) <= stopLossCurrency * PositionAccount.Quantity ||
         DateTime.Now > currentBarCloseDateTime.AddSeconds(barCloseImminen tSeconds))
         {
              ExitLong();
              CancelOrder(entryOrder); // Cancel entry order if it exists
         }
    }
    else if (PositionAccount.MarketPosition == MarketPosition.Short)
    {
         if (PositionAccount.GetUnrealizedProfitLoss(Performan ceUnit.Currency, GetCurrentAsk()) <= stopLossCurrency * PositionAccount.Quantity ||
         DateTime.Now > currentBarCloseDateTime.AddSeconds(barCloseImminen tSeconds))
         {
              ExitShort();
              CancelOrder(entryOrder); // Cancel entry order if it exists
         }
    }
    Here are the corresponding NT log entries:

    2023-09-28 10:54:52.722 NinjaScript NinjaScript strategy 'HourlyLongWithTryES/301101426' submitting order

    2023-09-28 10:54:52.770 Order Order='68e7da7e73654381a9d42325ca3da018/XXXXXX' Name='Sell' New state='Submitted' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:52.829 Order Order='1293846762/XXXXXX' Name='Sell' New state='Accepted' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:52.830 Order Order='1293846762/XXXXXX' Name='Sell' New state='Working' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:52.832 NinjaScript NinjaScript strategy 'HourlyLongWithTryES/301101426' submitting order

    2023-09-28 10:54:52.871 Order Order='1293846762/XXXXXX' Name='Sell' New state='Filled' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=1 Fill price=4340.5 Error='No error' Native error=''

    2023-09-28 10:54:52.877 Execution Execution='978256471040' Instrument='ES 12-23' Account='XXXXXX' Exchange=Globex Price=4340.5 Quantity=1 Market position=Short Operation=Operation_Add Order='1293846762' Time='2023-09-28 10:54'
    Now here's the puzzling bit. After the market sell order executed, another market sell order was unexpectedly submitted, opening an unwanted short position:

    2023-09-28 10:54:52.926 Order Order='d2da5d4b75a449158dfae401874c0304/XXXXXX' Name='Sell' New state='Submitted' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:52.932 Position Instrument='ES 12-23' Account='XXXXXX' Average price=0 Quantity=0 Market position=Flat Operation=Remove

    2023-09-28 10:54:52.940 Order Order='1293688293/XXXXXX' Name='Sell' New state='Cancel submitted' Instrument='ES 12-23' Action='Sell' Limit price=4358 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:52.963 Order Order='1293760154/XXXXXX' Name='Sell' New state='Accepted' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:52.963 Order Order='1293760154/XXXXXX' Name='Sell' New state='Working' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:52.964 Order Order='1293760154/XXXXXX' Name='Sell' New state='Cancel submitted' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:53.010 Order Order='1293760154/XXXXXX' Name='Sell' New state='Filled' Instrument='ES 12-23' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=1 Fill price=4340.5 Error='No error' Native error=''

    2023-09-28 10:54:53.011 Execution Execution='978256504064' Instrument='ES 12-23' Account='XXXXXX' Exchange=Globex Price=4340.5 Quantity=1 Market position=Short Operation=Operation_Add Order='1293760154' Time='2023-09-28 10:54'

    2023-09-28 10:54:53.069 Order Order='1293688293/XXXXXX' Name='Sell' New state='Cancelled' Instrument='ES 12-23' Action='Sell' Limit price=4358 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:54:53.111 Position Instrument='ES 12-23' Account='XXXXXX' Average price=4340.5 Quantity=1 Market position=Short Operation=Operation_Add
    Then, as intended by my strategy, this short position was closed 30 seconds before the hour's end:

    2023-09-28 10:59:30.573 NinjaScript NinjaScript strategy 'HourlyLongWithTryES/301101426' submitting order

    2023-09-28 10:59:30.617 Order Order='1c670be211a14afba7f4653532c7abd2/XXXXXX' Name='Buy to cover' New state='Submitted' Instrument='ES 12-23' Action='Buy to cover' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:59:30.655 Order Order='1293813521/XXXXXX' Name='Buy to cover' New state='Accepted' Instrument='ES 12-23' Action='Buy to cover' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:59:30.655 Order Order='1293813521/XXXXXX' Name='Buy to cover' New state='Working' Instrument='ES 12-23' Action='Buy to cover' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    2023-09-28 10:59:30.696 Order Order='1293813521/XXXXXX' Name='Buy to cover' New state='Filled' Instrument='ES 12-23' Action='Buy to cover' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=1 Fill price=4342.25 Error='No error' Native error=''

    2023-09-28 10:59:30.697 Execution Execution='978265035776' Instrument='ES 12-23' Account='XXXXXX' Exchange=Globex Price=4342.25 Quantity=1 Market position=Long Operation=Operation_Add Order='1293813521' Time='2023-09-28 10:59'

    2023-09-28 10:59:30.723 Position Instrument='ES 12-23' Account='XXXXXX' Average price=0 Quantity=0 Market position=Flat Operation=Remove
    My question, then, is: Why was a second market sell order submitted after the first one had executed? Does my strategy need to first check that the position is flat before proceeding? Or should it be doing something else to ensure that no unintended orders are submitted?


    #2
    Hello Red_70,

    Thanks for your post.

    This could possibly be caused by calling ExitLong() and CancelOrder() within the same condition in your script. Calling ExitLong() and CancelOrder() in the same script could have cause two orders to be submitted. One to exit the long position and the other to cancel the entry order. If both orders become filled, this could cause you to end up in a 1 Short position (One sell order could fill to exit the position and the other sell order could fill causing you to enter a short position).

    So I may investigate this matter further, please write in to support[at]ninjatrader[dot]com and attach your full Log and Trace files to your email so I may review them and look into the matter. In the subject of your email include ATTN: BrandonH and in the body of the email include a link to this forum post.

    Follow the steps below to manually attach your log and trace files to your response so I may investigate this matter further.
    • Open your NinjaTrader folder under, "Documents" (sometimes called, "My Documents")
    • Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
    • Send the 2 compressed folders as attachments to this email.
    • Once complete, you can delete these compressed folders.
    I look forward to assisting further.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    51 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    127 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X