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

Write lock may not be acquired with read lock held error in strategy

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

    Write lock may not be acquired with read lock held error in strategy

    Hi,

    I'm getting the following error in my unmanaged strategy:

    "Write lock may not be acquired with read lock held. This pattern is prone to deadlocks. Please ensure that read locks are released before taking a write lock. If an upgrade is necessary, use an upgrade lock in place of the read lock."

    This strategy has been working for several days under Playback connection while under development, and this error just started appearing. Below is code snippet showing where it's occurring. This is in OnExecutionUpdate:

    ...
    (ShortEntryOrder1 != null) && (ShortEntryOrder1 == execution.Order))

    if ((execution.Order.OrderState == OrderState.Filled) ||
    (execution.Order.OrderState == OrderState.PartFilled) ||
    (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    // book keeping code
    ...
    if (execution.Order.Name == ShortTag1)
    {
    // more book keeping code, e.g. set target and stop values, ensure above/below current bid ask, etc.
    ...
    // Submit exit orders for partial fills
    if (execution.Order.OrderState == OrderState.PartFilled)
    {
    string _ocoString = string.Format("unmamangedexitoco{0}", DateTime.Now.ToString("hhmmssffff"));
    try
    {
    TargetOrder1 = SubmitOrderUnmanaged(
    0,
    OrderAction.BuyToCover,
    OrderType.Limit,
    execution.Order.Filled,
    execution.Order.AverageFillPrice - (targetTicks * TickSize),
    0,
    _ocoString,
    TargetTag1);
    }
    catch (Exception e)
    {
    ERROR CAUGHT HERE
    Debugger.Break();
    if (State >= State.Terminated) return;
    }

    try
    {
    StopOrder1 = SubmitOrderUnmanaged(
    0,
    OrderAction.BuyToCover,
    OrderType.StopMarket,
    execution.Order.Filled,
    0,
    LastSetStop1Price,
    _ocoString,
    StopTag1);
    }
    catch (Exception e)
    {
    Debugger.Break();
    if (State >= State.Terminated) return;
    }

    Short1OrderFilled += execution.Order.Filled;
    }
    else
    if (execution.Order.OrderState == OrderState.Filled && SumFilledOrder1 == execution.Order.Filled)
    {
    ... same as PartFilled with adjustments for quantities already filled

    Apologies for lack of indentions, your portal seems to strip them out.

    Any ideas what might be causing this and what to look at?

    Thanks.

    #2
    Hello silverm3170,

    This error can sometimes be associated with threading issues.

    Does this script use the ChartControl or do custom rendering in OnRender()?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Does anyone from the Ninjatrader support staff have any insights on this? I searched the archives and found incidents related to rendering, but this is occurring during order submission.

      Thanks.

      Comment


        #4
        Hi Chelsea,

        Sorry my view hadn’t refreshed and I didn’t notice you had replied already. I don’t explicitly use OnRender and I don’t do anything specific with ChartControl. The exception occurred during the SubmitOrderUnmanaged call in OnExecuteOrder. Would you expect OnRender or ChartControl related threading issues to present there? Or might this be an order related threading issue?

        Thanks,
        silverm3170

        Comment


          #5
          Hello silverm3170,

          Have a look at the other forum posts that involve this error.


          May I have you provide an export of the script so that I may review the code and look for anything threading related?

          To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
          1. Click Tools -> Export -> NinjaScript Add-on...
          2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
          3. Click the 'Export' button
          4. Enter a unique name for the file in the value for 'File name:'
          5. Choose a save location -> click Save
          6. Click OK to clear the export location message
          By default your exported file will be in the following location:
          • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
          Below is a link to the help guide on Exporting NinjaScripts.
          http://ninjatrader.com/support/helpG...nt8/export.htm
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi Chelsea,

            I figured out the problem. I was trying to force-exit a position before it hit the target or stop, similar to "exit on session close", and I inadvertently sent it down the initial order creation path that always creates a target and stop oco pair. Here I just wanted to close the position and cancel the existing target and stop orders, but ended up creating new target and stop orders and assigning them to the existing and uncanceled target and stop order objects. What I'm trying to do is similar to ExitShort("MyShort") in a managed strategy. So now I have a different question:

            - I've created three orders, let's call them "Short1", "Target1" and "Stop1". "Target1" and "Stop1" are an oco pair, and all three were created with SubmitOrderUnmanaged.
            - at some point in the future I want to close "Short1" prematurely before it hits "Target1" or "Stop1". What would be the recommended way to do this? The UnmanagedOCOBracketExample doesn't cover this (I switched to unmanaged because the initial entry, "Short1" in this instance, may be a bracket oco order with a "Long1" counterpart. Otherwise I would have carried on with managed). How do I identify the order that needs to be closed and take action only on it and it's target/stop pair? Crucial if I have multiple orders in flight, e.g. "Short2", "Short3", etc.

            Should I use the initial entry order object to manage this? Most of your examples show this being nulled out as soon as the order is filled, and that's what I've been doing.

            Thanks,
            silverm3170

            Comment


              #7
              Hello,

              " at some point in the future I want to close "Short1" prematurely before it hits "Target1" or "Stop1". "

              You could modify the target price so it's behind the market and fills immediately.

              You could cancel the stop, and then in OnOrderUpdate detect the order is OrderState.Cancelled, and then submit a market order.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thank you!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by futtrader, 04-21-2024, 01:50 AM
                4 responses
                41 views
                0 likes
                Last Post futtrader  
                Started by Option Whisperer, Today, 09:55 AM
                1 response
                11 views
                0 likes
                Last Post bltdavid  
                Started by port119, Today, 02:43 PM
                0 responses
                1 view
                0 likes
                Last Post port119
                by port119
                 
                Started by Philippe56140, Today, 02:35 PM
                0 responses
                3 views
                0 likes
                Last Post Philippe56140  
                Started by 00nevest, Today, 02:27 PM
                0 responses
                2 views
                0 likes
                Last Post 00nevest  
                Working...
                X