Go Back   NinjaTrader Support Forum > NinjaTrader 7 > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 04-26-2016, 09:23 AM   #1
westsider
Member
 
Join Date: Oct 2011
Location: Playa Del Rey, CA
Posts: 47
Thanks: 8
Thanked 5 times in 5 posts
Default DataTable Class

Msdn DataTable Class:
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
DataTable Examples:
http://www.dotnetperls.com/datatable

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
westsider is offline  
Reply With Quote
Old 04-26-2016, 09:33 AM   #2
Calonious
Senior Member
 
Join Date: Apr 2015
Location: Denver, CO
Posts: 156
Thanks: 23
Thanked 98 times in 70 posts
Default

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 at 09:36 AM.
Calonious is offline  
Reply With Quote
The following 2 users say thank you to Calonious for this post:
Old 04-26-2016, 09:41 AM   #3
NinjaTrader_ChelseaB
NinjaTrader Customer Service
 
NinjaTrader_ChelseaB's Avatar
 
Join Date: Dec 2012
Location: Denver, CO
Posts: 8,612
Thanks: 126
Thanked 1,888 times in 1,760 posts
Default

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?
NinjaTrader_ChelseaB is offline  
Reply With Quote
Old 04-26-2016, 09:48 AM   #4
westsider
Member
 
Join Date: Oct 2011
Location: Playa Del Rey, CA
Posts: 47
Thanks: 8
Thanked 5 times in 5 posts
Default

Quote:
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?)
westsider is offline  
Reply With Quote
Old 04-26-2016, 09:55 AM   #5
Calonious
Senior Member
 
Join Date: Apr 2015
Location: Denver, CO
Posts: 156
Thanks: 23
Thanked 98 times in 70 posts
Default

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 at 10:01 AM.
Calonious is offline  
Reply With Quote
Old 04-26-2016, 10:18 AM   #6
westsider
Member
 
Join Date: Oct 2011
Location: Playa Del Rey, CA
Posts: 47
Thanks: 8
Thanked 5 times in 5 posts
Default

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.
westsider is offline  
Reply With Quote
Old 04-26-2016, 10:21 AM   #7
westsider
Member
 
Join Date: Oct 2011
Location: Playa Del Rey, CA
Posts: 47
Thanks: 8
Thanked 5 times in 5 posts
Default

Found it here and have added it successfully.

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\ System.Data.DataSetExtensions.dll
westsider is offline  
Reply With Quote
Old 04-26-2016, 10:25 AM   #8
Calonious
Senior Member
 
Join Date: Apr 2015
Location: Denver, CO
Posts: 156
Thanks: 23
Thanked 98 times in 70 posts
Default

Are you still getting compile errors now? Or are you able to move forward to testing?
Calonious is offline  
Reply With Quote
Old 04-26-2016, 10:35 AM   #9
westsider
Member
 
Join Date: Oct 2011
Location: Playa Del Rey, CA
Posts: 47
Thanks: 8
Thanked 5 times in 5 posts
Default

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));
				}
westsider is offline  
Reply With Quote
Old 04-26-2016, 11:10 AM   #10
westsider
Member
 
Join Date: Oct 2011
Location: Playa Del Rey, CA
Posts: 47
Thanks: 8
Thanked 5 times in 5 posts
Default

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));			
	}
westsider is offline  
Reply With Quote
Old 04-15-2018, 04:59 PM   #11
spottysallrite
Junior Member
 
Join Date: Jun 2014
Location: thousand oaks, CA
Posts: 28
Thanks: 3
Thanked 1 time in 1 post
Default

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 at 05:00 PM. Reason: clarification
spottysallrite is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Indicator class? hozman General Development 3 12-18-2015 01:24 PM
partial class blar58 Indicator Development 1 09-13-2013 02:09 PM
Class hirarchie tbmuc General Development 1 03-05-2012 07:34 AM
Trade Class annettes Strategy Development 1 03-04-2010 05:14 AM
TradeCollection Class astrolobe Strategy Development 1 09-21-2009 05:39 AM


All times are GMT -6. The time now is 07:16 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.