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

MarketAnalyzer Columns use data from text file

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

    MarketAnalyzer Columns use data from text file

    My MarketAnalyzer column imports a small text file that contains some values needed for its' calculation. The problem is, when I load a MarketAnalyzer window with the 500 stocks in the S&P, each row does a fetch at the same time which results in some file in use errors. Is there any way to program my column in a way that it could import this file once and the value could be shared with each row in the MarketAnalyzer window?

    #2
    Hi swcooke thanks for your note.

    Unfortunately, loading files outside of NT does go out of the scope of support I can provide, so I would not be able to find a direct answer to this problem. I want to say you could do a using statement on the file like in this stack overflow answer:

    I'm writing a program in C# that needs to repeatedly access 1 image file. Most of the time it works, but if my computer's running fast, it will try to access the file before it's been saved back to...


    If the columns all have the same data, you could load it in just once into a static class. See this post I made on the subject (post #2 on this page):



    Please let me know if you have any further questions.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thanks for your reply. I am not experienced with Addons but I just gave the documentation a quick scan. I'm looking at these sample code blocks that would allow me to do something at startup and at shutdown of NT:

      Code:
                  protected override void OnWindowCreated(Window window)
                  {
                      if(window is ControlCenter){
                          DeactivateLicenseAsync();    
                      }
      
                  }
      And...

      Code:
                  protected override void OnWindowDestroyed(Window window)
                  {
                      if(window is ControlCenter){
                          DeactivateLicenseAsync();    
                      }
      
                  }
      Hypothetically, could an Addon read my text file into a variable at startup and then could my MarketAnalyzer columns call the Addon? Is this the kind of thing that is possible with Addons? In other words, can an Addon run like a service for the life of NT's session? If so, can a MarketAnalyzer column call values from the Addon?

      I'm just trying to learn if this is hypothetically possible before I start digging into it. If so, I could just put a timer in the Addon that would fetch the file if it's over an hour old or something. The Addon could just serve data when called by the MarketAnalyzer column. Possible?

      Comment


        #4
        Hello swooke,

        An AddOn could be built which starts operations in OnWindowCreated when the Control Center is created to start a task on start up. It is also possible to have other NinjaScripts access public properties of an AddOn. I have linked an example below. (This example demonstrates indicators and strategies, but the same concepts can be applied to other NinjaScripts.)



        What you are describing would be a plausible approach. The AddOn could read the file periodically, and your Market Analyzer Column can then request that information from the AddOn. You could also consider using try/catches to see if a a StreamReader has failed reading a file if it is already in use. If it fails, you can trap that occurence in the catch, and notify the script that you would need to attempt reading the file again. Our SampleStreamReader example demonstrates using a try/catch with a StreamReader.

        SampleStreamReader - https://ninjatrader.com/support/help...o_read_fro.htm

        Please let us know if you have any additional questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi Jim,

          Here is a quick AddOn that holds the value of a dictionary and returns it to a caller:

          Code:
          namespace NinjaTrader.NinjaScript.AddOns
          {
              public class sjSharesAddOn : NinjaTrader.NinjaScript.AddOnBase
              {
                  public static class Shares{
          
                      public static Dictionary<string,double> Dict = new Dictionary<string,double>(GetShares());
          
                      public static Dictionary<string, double> GetShares(){
                          Dictionary<string,double> result = new Dictionary<string,double>();
                          result.Add("MSFT",200);
                          return result;
                      }            
                  }
              }
          }
          Where could I learn how (and why) this works? I have read this already but still missing something. I haven't put this logic inside of an event like OnWindowCreated, yet it still works. Is this all I need? In other words, based on my specific (bare bones) example, when does my dictionary get created and become available? When is it destroyed?
          Last edited by swcooke; 01-27-2020, 08:44 AM.

          Comment


            #6
            Hello swcooke,

            What you are looking at is a standard C# structure, a class with a static property. To learn more about why this works or how it works you can search online for C# static examples or other information related to using static in C#. This type of information won't be in any NinjaScript documentation because this is not a NinjaScript concept but is just C#.

            MSDN provides a wealth of knowledge on this topic however it may be pretty dry to read, you can also search google for "C# static examples" for some other ideas. https://docs.microsoft.com/en-us/dot...-class-members

            The static class and property will be created at runtime so you won't need to instantiate the class or do anything else, you just need to use its static member one time for that to happen automatically. The property would be destroyed when you do that yourself or when NinjaScript is reloaded during a compile. Otherwise it would just remain in memory once accessed.

            Keep in mind using static in other areas like for an indicators property or variable would never be suggested, static is very powerful and should only be used in very specific scenarios like this with a specific intention.



            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by funk10101, Today, 12:02 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by GLFX005, Today, 03:23 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by nandhumca, Yesterday, 03:41 PM
            1 response
            13 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by The_Sec, Yesterday, 03:37 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by vecnopus, Today, 06:15 AM
            0 responses
            1 view
            0 likes
            Last Post vecnopus  
            Working...
            X