I'm attempting to use NT methods in a nested class and I'm getting an error: Cannot access a non-static member of out type " " via nested type " ". Is there a way to make NT methods static or is there another workaround? Thanks.
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!
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
NinjaTrader
Accessing non static members
Collapse
X
-
Accessing non static members
Hello,
I'm attempting to use NT methods in a nested class and I'm getting an error: Cannot access a non-static member of out type " " via nested type " ". Is there a way to make NT methods static or is there another workaround? Thanks.Tags: None
-
Hello CaptainAmericaXX,
Thanks for your message. Using inheritance and other more advanced uses of C# may interfere with how our NinjaScript wrapper works for generating indicator code and other advanced uses of the C# language can carry other complications.
As such it can extend beyond our scope of services to provide further direction with advanced C# concepts with NinjaScript. That being said, we provide some direction for creating static variables/methods and using partial classes on our Code Breaking Changes page of the HG.
Also available is an example my colleague Chelsea has posted where these partial classes are created in a NinjaScript in the AddOns folder.
Please let us know if we can be of further assistance.JimNinjaTrader Customer Service
-
Thanks Jim,
I'm too cheap to pay a consultant $150 an hour so I'll just have to keep learning on my own.
Those examples you gave me we for NT 8. I'm still plugging along with NT 7. Do you have any suggestions for 7?
btw my understanding is limited, but I have a basic understanding of static members and partial classes. I promise I won't sue NT if you give me just a little more advice and I end up breaking something.Last edited by CaptainAmericaXX; 05-20-2019, 09:12 AM.
Comment
-
Hello CaptainAmericaXX,
Thanks for the reply,
The information above shows how the UserDefinedMethods approach can be used in NinjaTrader 8. This script comes shipped with NinjaTrader 7 and can be opened from the Control Center under Tools > Edit NinjaScript > Indicator...
Documentation page - https://ninjatrader.com/support/help...ed_methods.htm
Please let us know if there is anything else we can do to help.JimNinjaTrader Customer Service
Comment
-
Thanks Jim,
I am familiar with the UserDefinedMethods file and have created a few dozen methods, but I'd like the option of using the NT methods so I don't have to recreate the wheel. I believe I'm on to something, but I can't figure out the last part. I can at least get the Intellisence to open up the NT methods (I'm using LowestBar()), but I'm getting an error cs1519 Invalid token ('(' & ',') in class, struct, or interface member declaration. Here's the code:
Code:partial class Indicator { public class TestClass { private Indicator parent; public TestClass(Indicator parent) { this.parent = parent; } Indicator.TestClass testClass = new Indicator.TestClass(); testClass.parent.LowestBar(Low, 30); } }
Comment
-
Hello CaptainAmericaXX,
We can leave this thread open for any input from other community members, but we could not comment further on this topic as it involves more advanced C# which would be outside of what we can support with NinjaScript.
Unsupported does not mean this is not possible, it just means that the concepts go beyond what we can provide direction for.
If there is another NinjaScript topic we can assist with, please do not hesitate to open new thread.JimNinjaTrader Customer Service
Comment
-
Ok so using a slightly modified version of what I wrote above I am able to at least get a few bars running on my chart, at least until the method runs and then I get the error: Object ref not set to an instance of an object. I created an instance at the beginning of the method. I'm not sure where else I need to create the instance.
Code:public class LL_Up { private A1WaveFinder parent; public LL_Up() { } public LL_Up(A1WaveFinder parent) { this.parent = parent; } public void Find_Lower_Low(IDataSeries open, IDataSeries close, IDataSeries low, IDataSeries price_Valley) { A1WaveFinder.LL_Up llup = new A1WaveFinder.LL_Up(); if(UpBar(open,close) && low[1] > low[0]){ llup.parent.Print("Working"); //Adding the Print() is causing the crash! } } protected override void OnBarUpdate() { if(CurrentBar < 50) return; LL_Up lowerLow_Up1 = new LL_Up(); if(lowerLow_Up1 != null){ lowerLow_Up1.Find_Lower_Low(Open, Close, Low, Price_Valley); } }
Sorry the code is a mess. The copy and paste didn't work very well.
Comment
-
Originally posted by CaptainAmericaXX View PostOk so using a slightly modified version of what I wrote above I am able to at least get a few bars running on my chart, at least until the method runs and then I get the error: Object ref not set to an instance of an object. I created an instance at the beginning of the method. I'm not sure where else I need to create the instance.
Code:public class LL_Up { private A1WaveFinder parent; public LL_Up() { } public LL_Up(A1WaveFinder parent) { this.parent = parent; } public void Find_Lower_Low(IDataSeries open, IDataSeries close, IDataSeries low, IDataSeries price_Valley) { A1WaveFinder.LL_Up llup = new A1WaveFinder.LL_Up(); if(UpBar(open,close) && low[1] > low[0]){ llup.parent.Print("Working"); //Adding the Print() is causing the crash! } } protected override void OnBarUpdate() { if(CurrentBar < 50) return; LL_Up lowerLow_Up1 = new LL_Up(); if(lowerLow_Up1 != null){ lowerLow_Up1.Find_Lower_Low(Open, Close, Low, Price_Valley); } }
Sorry the code is a mess. The copy and paste didn't work very well.
Comment
-
Give this a try ....
Code:public class LL_Up { private A1WaveFinder parent; public LL_Up(A1WaveFinder instance) { parent = instance; } public void Find_Lower_Low() { if (parent.UpBar(parent.Open, parent.Close) && parent.Low[1] > parent.Low[0]) parent.Print("Working"); } } protected override void OnBarUpdate() { if (CurrentBar < 50) return; // you must pass 'this' on every new object creation LL_Up llup = new LL_Up(this); // if a new lower low is found, "Working" is printed llup.Find_Lower_Low(); }
the method Find_Lower_Low().
I mean, that method could be coded much easier directly within the scope of
your A1WaveFinder strategy, rather than being embedded in a nested class.
Nevertheless, I hope it helps your understanding of how to call things inside
a nested class that are 'owned' by the parent strategy class (such as Print).
Good luck!Last edited by bltdavid; 05-21-2019, 09:45 AM.
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by usjavaburn, Today, 12:59 AM
|
0 responses
3 views
0 likes
|
Last Post
by usjavaburn
Today, 12:59 AM
|
||
Started by heatherjmarshalls, Today, 12:56 AM
|
0 responses
2 views
0 likes
|
Last Post Today, 12:56 AM | ||
Started by Nicholewatkinsi, Yesterday, 10:53 PM
|
0 responses
6 views
0 likes
|
Last Post Yesterday, 10:53 PM | ||
Started by dward123, 01-02-2024, 09:59 PM
|
4 responses
176 views
0 likes
|
Last Post
by Lancer
Yesterday, 09:27 PM
|
||
Started by ETFVoyageur, Yesterday, 04:00 PM
|
2 responses
19 views
0 likes
|
Last Post
by ETFVoyageur
Yesterday, 08:09 PM
|
Comment