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

Deadlocks and Multi-threading

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

    Deadlocks and Multi-threading

    Hi,

    I've build an indicator which constantly gets deadlocks.
    I have rebuilt it many times now with more simplicity and i have been unable to resolve it.

    I have using 28 Series, and i have some methods that would benefit from running parallel operations.

    I am wondering if someone has an example script they can share that demonstrates how to to effectively deal with multi threading and read / write locks in ninjascript.

    Thanks,

    #2
    Hello yertle,

    Thank you for your post.

    While I do not have an example script to share, I do suggest reviewing the NinjaScript Best Practices (especially the performance practices) as well as the page about Multi-Threading in the help guide at the links below:You should be able to use some debugging tools to understand which parts of your indicator are more resource-intensive and resulting in deadlocks. I suggest commenting out portions of code and uncommenting them bit-by-bit until the deadlock behavior appears again. This could be a hint that one of the last portions that you uncommented could be the culprit contributing to the undesired behavior. We have more information about debugging at the links below:Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you,
      Today, I have been swapping all the arrays with circular buffers
      > i had the arrays updating with a loop, shifting values to the left, which may have been the problem.
      (but i did try swapping the arrays with data Series & still had issues (i had 100+ series though)

      Now that i have added the circular buffer code, i am having issues compiling in the ninjascript editor.
      (the code works on C#6.0)

      // should this object work in ninjascript? & is it the best way to handle alot of arrays used in calculations?

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;

      namespace NinjaTrader.Custom.Indicators.Enums
      {
      public class CircularBuffer<T>
      {
      private T[] buffer;
      private int head;
      private int tail;
      private int capacity;
      private bool isFull;

      public CircularBuffer(int capacity)
      {
      if (capacity < 1)
      throw new ArgumentException("Circular buffer cannot have negative or zero capacity.", nameof(capacity));
      this.capacity = capacity;
      this.buffer = new T[capacity];
      this.head = 0;
      this.tail = 0;
      this.isFull = false;
      }

      public void Add(T item)
      {
      buffer[head] = item;
      head = (head + 1) % capacity;
      if (isFull)
      {
      tail = (tail + 1) % capacity;
      }
      isFull = head == tail;
      }

      public T Get(int index)
      {
      if (index < 0 || index >= capacity)
      throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range.");
      if (index >= Size)
      throw new ArgumentException("Index is outside the bounds of the filled buffer.", nameof(index));

      int adjustedIndex = (tail + index) % capacity;
      return buffer[adjustedIndex];
      }

      public int Size => isFull ? capacity : (head - tail + capacity) % capacity;

      public T this[int index] => Get(index);

      public void Clear()
      {
      head = 0;
      tail = 0;
      isFull = false;
      }
      }
      }

      ​Thank you,
      Last edited by yertle; 03-08-2024, 06:04 AM.

      Comment


        #4
        Hello yertle,

        Thank you for your reply.

        What version of NinjaTrader are you using? This may be found at Control Center > Help > About. Versions prior to 8.1.2.0 were only capable of using C# features up to version 5.0. Now, as of 8.1.2.0 and newer, the compiler was updated to Roslyn and can take advantage of C# features up to version 8.0. If you need to update to the latest version of NinjaTrader Desktop please follow the steps below.
        • Login into the NinjaTrader Account Dashboard
        • Click "Download" in the bottom left-hand corner of the sidebar
        • Next, select "Download" in the "Most Recent Release" window on the main screen
        • Once the installer has been downloaded, ensure any previous version of NinjaTrader is closed before running the installation package.
          • Note: You may need to locate this in your "Downloads" folder in a Windows File Explorer
        I suggest testing this out on the latest version of NinjaTrader and seeing if you notice improved performance or not. Your question, "is it the best way to handle alot of arrays used in calculations​" falls into the territory of general C# questions and is not specific to NinjaTrader. We do not offer C# educational services nor hands-on debugging assistance, so it is up to you to decide if this logic suits your needs or not.

        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          thank you, I changed the code back to C#5.0 which fixed the issues
          - The latest version of NT was causing issues when modifying from VS.
          I found myself having to constantly reinstall NT throughout a coding session

          Comment


            #6
            Hello yertle,

            Just to confirm, you are not attempting to build or run the NinjaTrader project from Visual Studio, and you are only saving the one edited file (Ctrl + s) allowing NinjaTrader to automatically compile, is this correct?
            I installed the latest version of NT8. I have all updates of Windows 10 installed. I have also the latest version of Visual Studio. When I compile from the Ninjascript editor it works fine. When I use VS and want to build in VS, I am getting the following error in the NinjaScript Editor: \NETFramework,version=v4\6\1AssemblyAttr


            Are you launching NinjaTrader from the Visual Studio button on the NinjaScript Editor tool bar?

            What were the issues you were encountering?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              correct. i'm not building the project in VS.
              yes, i was launching VS from within Ninjatrader

              Main issue was,
              when i rename or add a new file in the solution explorer that will break Ninjatrader (latest version).

              Comment


                #8
                Hello yertle,

                You are referring to the NinjaScript Editor Explorer pane where you are renaming file/folders or adding files/folders, is this correct?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  the solution explorer within VS,
                  - adding a new, or renaming any file, from within VS and saving it will break NT.

                  Maybe Modifying it from within the Ninjascript editor is more stable.
                  - but its not an issue in prior versions.

                  also, it would be nice to have a option to turn the recompile chime/ sound on. (maybe i just couldn't find it)

                  Comment


                    #10
                    Hello yertle,

                    We recommend creating scripts and folders in the editor and making any changes to file names and folders in the NinjaScript Editor.
                    Removing scripts from an external editor is fine to do, but best habit is to make all filesystem related changes from within NinjaTrader.

                    The compile successfully sound can be found in Tools -> Options -> General -> Sounds.
                    (Be sure NinjaTrader is not muted in the Windows Sound Mixer)
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Ok thank you.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by llanqui, Today, 03:53 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post llanqui
                      by llanqui
                       
                      Started by burtoninlondon, Today, 12:38 AM
                      0 responses
                      10 views
                      0 likes
                      Last Post burtoninlondon  
                      Started by AaronKoRn, Yesterday, 09:49 PM
                      0 responses
                      15 views
                      0 likes
                      Last Post AaronKoRn  
                      Started by carnitron, Yesterday, 08:42 PM
                      0 responses
                      11 views
                      0 likes
                      Last Post carnitron  
                      Started by strategist007, Yesterday, 07:51 PM
                      0 responses
                      14 views
                      0 likes
                      Last Post strategist007  
                      Working...
                      X