Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order canceled that kills strategy

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

    Order canceled that kills strategy

    I've been running into weird exception errors that keep killing my strategy at random times. I found I can make it crash more often if I have bad logic that submits multiple entries in 1 bar (ex: Entry, 2seconds later Stop Loss, then immediate entry.) Gives me an error stating it can't reuse the OCOID hash(?). I am getting this error at random times. I'll post the error log. I would like to know why it's fighting me. I may not be as savvy at reading the logs, so if anyone can see what needs to be poked, that would be good. Sorry for the dump. Let me know if you have any questions though.

    Getting a lot of those "Foriegn Key constraint failed" errors.

    Logs:
    2025-04-07 22:36:15:033|1|32|Order='aa68735dea6944dfa35aa3207 0f3322e/Playback101' Name='Stop loss' New state='Working' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=6047.75 Quantity=1 Type='Stop Market' Time in force=GTC Oco='a608ab3f466a421495494969446e115b' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:15:039|1|32|Order='aa68735dea6944dfa35aa3207 0f3322e/Playback101' Name='Stop loss' New state='Filled' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=6047.75 Quantity=1 Type='Stop Market' Time in force=GTC Oco='a608ab3f466a421495494969446e115b' Filled=1 Fill price=6047.75 Error='No error' Native error=''
    2025-04-07 22:36:15:040|1|32|Order='c1937a754a9d48e9818644443 ac49400/Playback101' Name='Profit target' New state='Cancel submitted' Instrument='MES 03-25' Action='Sell' Limit price=6072.75 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='a608ab3f466a421495494969446e115b' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:15:040|1|32|Order='c1937a754a9d48e9818644443 ac49400/Playback101' Name='Profit target' New state='Cancelled' Instrument='MES 03-25' Action='Sell' Limit price=6072.75 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='a608ab3f466a421495494969446e115b' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:15:040|1|8|Execution='c0fa4ea3627841fe86f3e4 f6f4e6615f' Instrument='MES 03-25' Account='Playback101' Exchange=Default Price=6047.75 Quantity=1 Market position=Short Operation=Operation_Add Order='aa68735dea6944dfa35aa32070f3322e' Time='1/6/2025 9:42 AM'
    2025-04-07 22:36:15:040|1|64|Instrument='MES 03-25' Account='Playback101' Average price=0 Quantity=0 Market position=Flat Operation=Remove
    2025-04-07 22:36:15:050|3|524288|Error on executing DB command: code = Constraint (19), message = System.Data.SQLite.SQLiteException (0x800027AF): constraint failed
    FOREIGN KEY constraint failed
    at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
    at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
    at System.Data.SQLite.SQLiteDataReader.NextResult()
    at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCo mmand cmd, CommandBehavior behave)
    at System.Data.SQLite.SQLiteCommand.ExecuteReader(Com mandBehavior behavior)
    at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(C ommandBehavior behavior)
    at NinjaTrader.NinjaScript.StrategyBase.DbAdd()
    at NinjaTrader.Cbi.DB.DBThread()
    2025-04-07 22:36:22:656|1|16|NinjaScript strategy 'ESMAStrategyManaged/355543122' submitting order
    2025-04-07 22:36:22:663|1|32|Order='4dec519fcc7344b5ba31f2227 a27c6f0/Playback101' Name='ShortEntry' New state='Submitted' Instrument='MES 03-25' Action='Sell short' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:22:664|1|32|Order='4dec519fcc7344b5ba31f2227 a27c6f0/Playback101' Name='ShortEntry' New state='Accepted' Instrument='MES 03-25' Action='Sell short' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:22:664|1|32|Order='4dec519fcc7344b5ba31f2227 a27c6f0/Playback101' Name='ShortEntry' New state='Working' Instrument='MES 03-25' Action='Sell short' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:22:669|1|32|Order='4dec519fcc7344b5ba31f2227 a27c6f0/Playback101' Name='ShortEntry' New state='Filled' Instrument='MES 03-25' Action='Sell short' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=1 Fill price=6026.5 Error='No error' Native error=''
    2025-04-07 22:36:22:669|1|8|Execution='140fbc9812934a32870c55 5cd824d8b9' Instrument='MES 03-25' Account='Playback101' Exchange=Default Price=6026.5 Quantity=1 Market position=Short Operation=Operation_Add Order='4dec519fcc7344b5ba31f2227a27c6f0' Time='1/6/2025 11:00 AM'
    2025-04-07 22:36:22:669|1|16|NinjaScript strategy 'ESMAStrategyManaged/355543122' submitting order
    2025-04-07 22:36:22:677|1|32|Order='f0bcec6c705e4addbc053c1e7 b732710/Playback101' Name='Stop loss' New state='Submitted' Instrument='MES 03-25' Action='Buy to cover' Limit price=0 Stop price=5901.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='140fbc9812934a32870c555cd824d8b9' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:22:677|1|32|Order='f0bcec6c705e4addbc053c1e7 b732710/Playback101' Name='Stop loss' New state='Rejected' Instrument='MES 03-25' Action='Buy to cover' Limit price=0 Stop price=5901.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='140fbc9812934a32870c555cd824d8b9' Filled=0 Fill price=0 Error='Order rejected' Native error='Buy stop or buy stop limit orders can't be placed below the market.'
    2025-04-07 22:36:22:677|0|32|Playback101, Buy stop or buy stop limit orders can't be placed below the market. affected Order: BuyToCover 1 StopMarket @ 5901.25
    2025-04-07 22:36:22:677|0|4|Strategy 'ESMAStrategyManaged/355543122' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
    2025-04-07 22:36:22:677|1|4|Disabling NinjaScript strategy 'ESMAStrategyManaged/355543122'
    2025-04-07 22:36:22:678|1|32|Order='07db07cff52b4da08123a52e8 4e82b6b/Playback101' Name='Profit target' New state='Rejected' Instrument='MES 03-25' Action='Buy to cover' Limit price=5884.25 Stop price=0 Quantity=1 Type='Limit' Time in force=GTC Oco='140fbc9812934a32870c555cd824d8b9' Filled=0 Fill price=0 Error='Unable to submit order' Native error='Order '07db07cff52b4da08123a52e84e82b6b' can't be submitted: The OCO ID ‘140fbc9812934a32870c555cd824d8b9’ cannot be reused. Please use a new OCO ID.'
    2025-04-07 22:36:22:678|0|32|Playback101, Order '07db07cff52b4da08123a52e84e82b6b' can't be submitted: The OCO ID ‘140fbc9812934a32870c555cd824d8b9’ cannot be reused. Please use a new OCO ID. affected Order: BuyToCover 1 Limit @ 5884.25
    2025-04-07 22:36:22:678|1|64|Instrument='MES 03-25' Account='Playback101' Average price=6026.5 Quantity=1 Market position=Short Operation=Operation_Add
    2025-04-07 22:36:22:689|1|32|Order='bba850753eb94aabac99bb0f1 abb3e89/Playback101' Name='StopCancelClose' New state='Submitted' Instrument='MES 03-25' Action='Buy to cover' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:22:689|1|32|Order='bba850753eb94aabac99bb0f1 abb3e89/Playback101' Name='StopCancelClose' New state='Accepted' Instrument='MES 03-25' Action='Buy to cover' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:22:689|1|32|Order='bba850753eb94aabac99bb0f1 abb3e89/Playback101' Name='StopCancelClose' New state='Working' Instrument='MES 03-25' Action='Buy to cover' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    2025-04-07 22:36:22:702|1|32|Order='bba850753eb94aabac99bb0f1 abb3e89/Playback101' Name='StopCancelClose' New state='Filled' Instrument='MES 03-25' Action='Buy to cover' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=1 Fill price=6027.25 Error='No error' Native error=''
    2025-04-07 22:36:22:702|1|8|Execution='7152eb76d56b406ea79887 87a687203d' Instrument='MES 03-25' Account='Playback101' Exchange=Default Price=6027.25 Quantity=1 Market position=Long Operation=Operation_Add Order='bba850753eb94aabac99bb0f1abb3e89' Time='1/6/2025 11:00 AM'
    2025-04-07 22:36:22:702|1|64|Instrument='MES 03-25' Account='Playback101' Average price=0 Quantity=0 Market position=Flat Operation=Remove
    2025-04-07 22:36:25:076|3|524288|Error on executing DB command: code = Constraint (19), message = System.Data.SQLite.SQLiteException (0x800027AF): constraint failed
    FOREIGN KEY constraint failed
    at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
    at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
    at System.Data.SQLite.SQLiteDataReader.NextResult()
    at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCo mmand cmd, CommandBehavior behave)
    at System.Data.SQLite.SQLiteCommand.ExecuteReader(Com mandBehavior behavior)
    at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(C ommandBehavior behavior)
    at NinjaTrader.NinjaScript.StrategyBase.DbAdd()
    at NinjaTrader.Cbi.DB.DBThread()


    #2
    Hello alphatango,

    The OCO error is due to the order rejection, you need to fix the rejection to avoid that. If a target or stop that is paired in OCO is rejected the OCO has already been used (rejected) so the other order shows an OCO error.

    Error='Order rejected' Native error='Buy stop or buy stop limit orders can't be placed below the market.'

    You need to change the offset or price being used.


    The DB error may mean you need to repair or reset the db. If you see a problem after doing both of those items that likely means the logic used is at fault. The only known reason you would get DB errors would be if you apply a strategy in realtime and switch to playback and use the same instance, or the reverse. Strategies generally need to be applied to the connected account only.

    Comment


      #3
      Okay. That much makes sense. The question is, why will it work for 80% of the day and suddenly the calculations for PT/SL come out wrong.

      I have print statements for the Output to show me what's going on. None of these are improperly setup. This is NT platform grabbing some random number. You can see the "Short Entry - Close=..." This is a print statement because I am trying to nail down what the heck is going on. This is the calculation
      Output:
      1/6/2025 11:00:00 AM: Short entry - Close=6026.25, PT=6016.25, SL=6036.25
      1/6/2025 11:00:01 AM: OnOrderUpdate processed. Order ShortEntry Submitted
      1/6/2025 11:00:01 AM: OnOrderUpdate processed. Order ShortEntry Accepted
      1/6/2025 11:00:01 AM: OnOrderUpdate processed. Order ShortEntry Working
      1/6/2025 11:00:01 AM: OnOrderUpdate processed. Order ShortEntry Filled
      1/6/2025 11:00:01 AM: OnOrderUpdate processed. Order Stop loss Submitted
      Strategy 'ESMAStrategyManaged/355543122' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
      Disabling NinjaScript strategy 'ESMAStrategyManaged/355543122'

      NT is picking its own Stoplosses and Profit targets. How do I make it not do that? Looks like it picked 5901. No idea how it got that number. It's nowhere close to the entry at 6026.25. It's been doing this to me and killing my algos for a while now. It's taken me a ton of time to drill down what platform issues I'm having. How do I make it stop doing that?
      Last edited by alphatango; 04-08-2025, 10:14 AM.

      Comment


        #4
        Hello alphatango,

        Are you using SetProfitTarget and SetStopLoss or are you using your own orders and variables? The print you are using won't relate to the targets if you are printing the close, most of the options are based on the average entry price and the settings you selected.

        Comment


          #5
          Yes.

          Code:
           longEntryOrder = EnterLongLimit(1, true, 5, entryPrice, "LongEntry");
           SetProfitTarget("LongEntry", CalculationMode.Ticks, profitTargetTicks, false);
           SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);​

          Comment


            #6
            Hello alphatango,

            One problem with the code provided is the order that you have the tatgets being set, those should go before the entry so that may be causing a old price to be used when they are submitted. Do you still see a problem if you change the code like the following?

            SetProfitTarget("LongEntry", CalculationMode.Ticks, profitTargetTicks, false);
            SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);​​
            longEntryOrder = EnterLongLimit(1, true, 5, entryPrice, "LongEntry");

            Comment


              #7
              Thanks for the reply Jesse. I'm willing to try anything. I didn't know if the code would grump at me if I Set a Profit Target for an order that didn't exist yet. I just am unsure who needs what first on the code side of things. I'll definitely try it because here we go again.

              Code:
              2025-04-11 08:48:16:186|1|4|Enabling NinjaScript strategy 'ESMAStrategyManaged/355543122' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
              2025-04-11 08:49:01:268|1|16|NinjaScript strategy 'ESMAStrategyManaged/355543122' submitting order
              2025-04-11 08:49:01:277|1|32|Order='4efe80ab9fc14f299084302c1 234684a/Playback101' Name='FadeLong' New state='Submitted' Instrument='MES 03-25' Action='Buy' Limit price=0 Stop price=0 Quantity=20 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
              2025-04-11 08:49:01:277|1|32|Order='4efe80ab9fc14f299084302c1 234684a/Playback101' Name='FadeLong' New state='Accepted' Instrument='MES 03-25' Action='Buy' Limit price=0 Stop price=0 Quantity=20 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
              2025-04-11 08:49:01:277|1|32|Order='4efe80ab9fc14f299084302c1 234684a/Playback101' Name='FadeLong' New state='Working' Instrument='MES 03-25' Action='Buy' Limit price=0 Stop price=0 Quantity=20 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
              2025-04-11 08:49:01:285|1|32|Order='4efe80ab9fc14f299084302c1 234684a/Playback101' Name='FadeLong' New state='Filled' Instrument='MES 03-25' Action='Buy' Limit price=0 Stop price=0 Quantity=20 Type='Market' Time in force=GTC Oco='' Filled=20 Fill price=5947.75 Error='No error' Native error=''
              2025-04-11 08:49:01:285|1|8|Execution='5b7b0c3cea2646008c7570 3b19217c68' Instrument='MES 03-25' Account='Playback101' Exchange=Default Price=5947.75 Quantity=20 Market position=Long Operation=Operation_Add Order='4efe80ab9fc14f299084302c1234684a' Time='12/30/2024 8:15 AM'
              2025-04-11 08:49:01:285|1|16|NinjaScript strategy 'ESMAStrategyManaged/355543122' submitting order
              2025-04-11 08:49:01:292|1|32|Order='b715025b17394358b8aa44f20 1ffb57b/Playback101' Name='Stop loss' New state='Submitted' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=6016.5 Quantity=20 Type='Stop Market' Time in force=GTC Oco='5b7b0c3cea2646008c75703b19217c68' Filled=0 Fill price=0 Error='No error' Native error=''
              2025-04-11 08:49:01:293|1|32|Order='b715025b17394358b8aa44f20 1ffb57b/Playback101' Name='Stop loss' New state='Rejected' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=6016.5 Quantity=20 Type='Stop Market' Time in force=GTC Oco='5b7b0c3cea2646008c75703b19217c68' Filled=0 Fill price=0 Error='Order rejected' Native error='Sell stop or sell stop limit orders can't be placed above the market.'
              2025-04-11 08:49:01:293|0|32|Playback101, Sell stop or sell stop limit orders can't be placed above the market. affected Order: Sell 20 StopMarket @ 6016.5
              2025-04-11 08:49:01:293|0|4|Strategy 'ESMAStrategyManaged/355543122' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
              2025-04-11 08:49:01:293|1|4|Disabling NinjaScript strategy 'ESMAStrategyManaged/355543122'
              2025-04-11 08:49:01:293|1|32|Order='87e27c22d4254094a3f9916a5 1ee8cc9/Playback101' Name='Profit target' New state='Rejected' Instrument='MES 03-25' Action='Sell' Limit price=5957.75 Stop price=0 Quantity=20 Type='Limit' Time in force=GTC Oco='5b7b0c3cea2646008c75703b19217c68' Filled=0 Fill price=0 Error='Unable to submit order' Native error='Order '87e27c22d4254094a3f9916a51ee8cc9' can't be submitted: The OCO ID ‘5b7b0c3cea2646008c75703b19217c68’ cannot be reused. Please use a new OCO ID.'
              2025-04-11 08:49:01:293|0|32|Playback101, Order '87e27c22d4254094a3f9916a51ee8cc9' can't be submitted: The OCO ID ‘5b7b0c3cea2646008c75703b19217c68’ cannot be reused. Please use a new OCO ID. affected Order: Sell 20 Limit @ 5957.75
              2025-04-11 08:49:01:293|1|64|Instrument='MES 03-25' Account='Playback101' Average price=5947.75 Quantity=20 Market position=Long Operation=Operation_Add
              2025-04-11 08:49:01:304|1|32|Order='b664011fa69440a89e7cbac2a cd211db/Playback101' Name='StopCancelClose' New state='Submitted' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=0 Quantity=20 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
              2025-04-11 08:49:01:305|1|32|Order='b664011fa69440a89e7cbac2a cd211db/Playback101' Name='StopCancelClose' New state='Accepted' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=0 Quantity=20 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
              2025-04-11 08:49:01:305|1|32|Order='b664011fa69440a89e7cbac2a cd211db/Playback101' Name='StopCancelClose' New state='Working' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=0 Quantity=20 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
              2025-04-11 08:49:01:311|1|32|Order='b664011fa69440a89e7cbac2a cd211db/Playback101' Name='StopCancelClose' New state='Filled' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=0 Quantity=20 Type='Market' Time in force=GTC Oco='' Filled=20 Fill price=5947.5 Error='No error' Native error=''
              2025-04-11 08:49:01:313|1|8|Execution='ec8cb72900f446df8de0e8 307b00197f' Instrument='MES 03-25' Account='Playback101' Exchange=Default Price=5947.5 Quantity=20 Market position=Short Operation=Operation_Add Order='b664011fa69440a89e7cbac2acd211db' Time='12/30/2024 8:15 AM'
              2025-04-11 08:49:01:314|1|64|Instrument='MES 03-25' Account='Playback101' Average price=0 Quantity=0 Market position=Flat Operation=Remove
              2025-04-11 08:49:12:852|1|2|Simulation account 'Playback101' reset
              ​
              It handled these setups "the prior day" (playback) but today it says nope, I'm reusing OCOID's or something? It's just super frustrating to have NT fighting me like this. There's no reason for this error that I can see other than glitches in the system. I'll defer to whatever you say though. Maybe you see something I don't. Someone with intimate knowledge of NT8 and ninjascript could probably tell me in a second and that would be GREATLY appreciated .

              Comment


                #8
                Hello alphatango,

                Putting the set methods first primes them, those orders are not submitted until the entry fills. Calling them after can result in old prices being used.

                The OCO errors are not relevant, its the other rejection:


                2025-04-11 08:49:01:293|1|32|Order='b715025b17394358b8aa44f20 1ffb57b/Playback101' Name='Stop loss' New state='Rejected' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=6016.5 Quantity=20 Type='Stop Market' Time in force=GTC Oco='5b7b0c3cea2646008c75703b19217c68' Filled=0 Fill price=0 Error='Order rejected' Native error='Sell stop or sell stop limit orders can't be placed above the market.'


                Your order is above market, you need to increase the offset to make sure the order is placed on the correct side of the market for the direction you entered.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                41 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                124 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                64 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                41 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