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

DataTable Class

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

    DataTable Class

    Msdn DataTable Class:

    DataTable Examples:
    Store data in memory with a DataTable. Add rows, call Compute and Merge, and set PrimaryKey.


    I’d like to use a data table as a multidimensional dynamic array inside ninja 7 but have been unable to implement it. Does anyone have any experience with this or another data structure that will store vars like excel? ( In columns and rows and is dynamic )
    Initializing and storing vars in the table is no problem. Reading the data is a problem. The MSDN recommended syntax doesn’t work inside ninja.
    Code:
    using System.Data;
            protected override void OnBarUpdate()
            {
    			// Declare DataTable
    			DataTable table = new DataTable();
    			// Add columns 
    			table.Columns.Add("Dosage", typeof(int));
    			table.Columns.Add("Drug", typeof(string));
    			table.Columns.Add("Patient", typeof(string));
    			table.Columns.Add("Date", typeof(DateTime));
    
    			// Add five DataRows.
    			table.Rows.Add(25, "Indocin", "David", DateTime.Now);
    			table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    			table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    			table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    			table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    			
    
    			// no matter what syntax I try I'm unable to read the data
    			//	Print(table.Rows[0][0]);
    	
            }
    Thanks for any pointers or advice!
    Westsider

    #2
    Any errors being reported?

    Also, did you try DotNetPerls method for looping through the data to write it?

    DataTable is very dynamic in that if you want data out of it, you need to get the type for it as well.

    Code:
    table.Rows[0].Field<string>(0)
    Last edited by Calonious; 04-26-2016, 09:36 AM.

    Comment


      #3
      Hi westsider,

      Calonious has the right idea. The item likely needs to be converted to a string.

      Are you getting any error messages in the Log tab of the Control Center?
      If so, what do these say?
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by Calonious View Post
        Any errors being reported?

        Also, did you try DotNetPerls method for looping through the data to write it?

        DataTable is very dynamic in that if you want data out of it, you need to get the type for it as well.

        Code:
        table.Rows[0].Field<string>(0)
        Thanks for looking at my issue!. Yes I did try every method on DotNetPerls including looping through. In the case of
        Code:
        foreach (DataRow row in table.Rows)
        				{
        					// ... Write value of first field as integer.
        					Print( table.Rows[0].Field<string>(0) );
        				}
        I get an error -- CS1061 - click for info
        'System.Data.DataRow' does not contain a definition for 'Field' and no extension method 'Field' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?)

        Comment


          #5
          I assume you have added the System.Data and System.Data.DataSetExtensions
          reference to your NT Editor correct?

          Field is an extension in the latter reference. If you don't wanna use that, then try using Item[string] instead to get the cell value -
          https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
          Last edited by Calonious; 04-26-2016, 10:01 AM.

          Comment


            #6
            Ahh - Sorry, Didn't realize I needed System.Data.DataSetExtensions - when I add this I get the error:

            The type or namespace name 'DataSetExtensions' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

            I'm assuming I need add this assembly reference but haven't done this before.

            Comment


              #7
              Found it here and have added it successfully.

              C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\ System.Data.DataSetExtensions.dll

              Comment


                #8
                Are you still getting compile errors now? Or are you able to move forward to testing?

                Comment


                  #9
                  Moving forward now. Thanks very much for the help.

                  I was missing the compiler directive. Here is the complete code if someone in the future wants to use a datatable;

                  Code:
                  // add these references
                       using System.Data;
                       using System.Data.DataSetExtensions;
                  
                  			// Declare DataTable
                  			DataTable table = new DataTable();
                  			// Add columns 
                  			table.Columns.Add("Dosage", typeof(int));
                  			table.Columns.Add("Drug", typeof(string));
                  			table.Columns.Add("Patient", typeof(string));
                  			table.Columns.Add("Date", typeof(DateTime));
                  
                  			// Add five DataRows.
                  			table.Rows.Add(25, "Indocin", "David", DateTime.Now);
                  			table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
                  			table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
                  			table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
                  			table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
                  			
                                          // Loop thru to read the data
                  			foreach (DataRow row in table.Rows)
                  				{
                  					Print(row.Field<int>(0));
                  				}

                  Comment


                    #10
                    Thanks so much Calonious. I was stuck on that one for a while !!! I really appreciate the help.

                    Here is the full loop and print Syntax to read each column and row

                    Code:
                    foreach (DataRow row in table.Rows)
                    	{
                    		Print("Dose: " +row.Field<int>(0) +" Drug: " + row.Field<string>(1)+
                    			"Patient: " +row.Field<string>(2) +" Date: " + row.Field<DateTime>(3));			
                    	}

                    Comment


                      #11
                      Well, I can't get this to work. It compiles just fine, but when I run the script I get the following error message on the Output window: "AssemblyResolveEvent handlers cannot return Assemblies loaded for reflection only."

                      Any suggestions as to how to fix that ?
                      Last edited by spottysallrite; 04-15-2018, 05:00 PM. Reason: clarification

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Perr0Grande, Today, 08:16 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post Perr0Grande  
                      Started by elderan, Today, 08:03 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post elderan
                      by elderan
                       
                      Started by algospoke, Today, 06:40 PM
                      0 responses
                      10 views
                      0 likes
                      Last Post algospoke  
                      Started by maybeimnotrader, Today, 05:46 PM
                      0 responses
                      12 views
                      0 likes
                      Last Post maybeimnotrader  
                      Started by quantismo, Today, 05:13 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post quantismo  
                      Working...
                      X