NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

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

Reply
 
Thread Tools Display Modes
Old 01-17-2011, 12:32 PM   #1
gg80108
Senior Member
 
Join Date: Dec 2008
Location: Castle Pines, Co
Posts: 621
Thanks: 1
Thanked 2 times in 2 posts
Default global namespace error already contains defination

I used the "save as" and copied and indicator.. The copy wont compile,, I attached the error and the part the code that is probable causing the error.. How do I fix this?
Attached Images
File Type: jpg namespace.JPG (81.1 KB, 30 views)
gg80108 is offline  
Reply With Quote
Old 01-17-2011, 12:58 PM   #2
ETFVoyageur
Senior Member
 
Join Date: Dec 2010
Posts: 470
Thanks: 0
Thanked 4 times in 4 posts
Default

Been there, done that. The problem is the enum. This is the global namespace pollution issue -- best answer is "Don't Do That!" You have no business putting names in the global namespace. It is wrong.

Solution: wrap the enum in a unique namespace, perhaps the same name as the indicator. Add a "using" line for the new namespace, so that the Magic Code at the end will be able to find the enum.

That should take care of you.

--EV
ETFVoyageur is offline  
Reply With Quote
Old 01-17-2011, 01:04 PM   #3
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Thanks for helping out here, EV. Yes, issue is likely with the scope of your enum.

You only need enums declared once per namespace and it will still be available from the copied, new indicator. You should be able to delete the enum declaration completely and it will compile.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 01-17-2011, 01:13 PM   #4
ETFVoyageur
Senior Member
 
Join Date: Dec 2010
Posts: 470
Thanks: 0
Thanked 4 times in 4 posts
Default

Quote:
Originally Posted by NinjaTrader_RyanM View Post
Thanks for helping out here, EV. Yes, issue is likely with the scope of your enum.

You only need enums declared once per namespace and it will still be available from the copied, new indicator. You should be able to delete the enum declaration completely and it will compile.
Technically, that is correct. However, I have some problems with just deleting one of them
  • It is bad practice to put names in the global namespace to begin with. Just quit doing that! Wrap them in a namespace. That's what namespaces are for.
  • Since they are in separate indicators, he will be up the proverbial creek if someone wants his indicator that fails to have the definition, but does not want the one that keeps the definition.
  • Keeping just one of them establishes a strong linkage between the two indicators -- which is a Bad Idea. What happens if one of the indicators later finds a need to change the enum?
Please, folks ... V7 forces enums for parameters up to a high level. Please wrap them in a namespace, rather than letting them pollute the global namespace. That pollution is just plain bad software engineering.

Example: his first enum is MovingAverageType -- hardly a unique name. What happens if I make one of the same -- it will build fine for me, since I do not have his indicator installed. A problem for anyone who wants to install both of our indicators, however. Please, just use namespaces and put nothing in the global namespace.

--EV
Last edited by ETFVoyageur; 01-17-2011 at 01:18 PM.
ETFVoyageur is offline  
Reply With Quote
Old 01-17-2011, 01:23 PM   #5
gg80108
Senior Member
 
Join Date: Dec 2008
Location: Castle Pines, Co
Posts: 621
Thanks: 1
Thanked 2 times in 2 posts
Default

I didn't create the original indicators I using and modifying.. Can u give me a quick example as to how to wrap them,, maybe a sample code like the ones ninja has to demonstrate various things.. I am interested in exporting my code to some buddies, and seems like this is an issue if we all go to the same code thats available in the public domain
gg80108 is offline  
Reply With Quote
Old 01-17-2011, 01:25 PM   #6
ETFVoyageur
Senior Member
 
Join Date: Dec 2010
Posts: 470
Thanks: 0
Thanked 4 times in 4 posts
Default

Quote:
Originally Posted by gg80108 View Post
I didn't create the original indicators I using and modifying.. Can u give me a quick example as to how to wrap them,, maybe a sample code like the ones ninja has to demonstrate various things.. I am interested in exporting my code to some buddies, and seems like this is an issue if we all go to the same code thats available in the public domain
Sure -- here is some demo code I use as a model

--EV

Code:
#region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;

    using RwbDemoModel;
#endregion

namespace RwbDemoModel {
    // This namespace is to prevent polluting the global namespace
    public enum DemoEnum {value1, value2}
}
ETFVoyageur is offline  
Reply With Quote
Old 01-17-2011, 05:19 PM   #7
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,213
Thanks: 24
Thanked 1,229 times in 1,000 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by gg80108 View Post
I used the "save as" and copied and indicator.. The copy wont compile,, I attached the error and the part the code that is probable causing the error.. How do I fix this?
Separate the enum into a unique namespace.

Example:

Code:
// NT standard "Using Declarations section is here
using _nsDataBoxTest;

namespace _nsDataBoxTest
{
    public enum MovAvgType
    {
        EMA,
        SMA,
        HMA,
        None
    }
}

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator

// rest of the code goes here
koganam is offline  
Reply With Quote
Old 01-24-2011, 07:07 AM   #8
gg80108
Senior Member
 
Join Date: Dec 2008
Location: Castle Pines, Co
Posts: 621
Thanks: 1
Thanked 2 times in 2 posts
Default

If this is in the indicator,, if I want to call this indicator in a strategy,, I have to add
using _nsDataBoxTest;
to the
Using Declarations section in my strategy also?
gg80108 is offline  
Reply With Quote
Old 01-25-2011, 11:27 PM   #9
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,213
Thanks: 24
Thanked 1,229 times in 1,000 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by gg80108 View Post
If this is in the indicator,, if I want to call this indicator in a strategy,, I have to add
using _nsDataBoxTest;
to the
Using Declarations section in my strategy also?
Technically, if the Strategy is calling the indicator, then the indicator knows its NameSpace requirements, and should just use them. However, I do not remember testing this directly, so you may just have to try it and see. I am pretty sure I have done it in a Strategy test; I just cannot remember, as my trading methods are now pretty set, and will not change until they stop working.
koganam is offline  
Reply With Quote
Old 01-26-2011, 02:48 AM   #10
Mindset
Senior Member
 
Join Date: Mar 2008
Location: UK West Sussex
Posts: 665
Thanks: 9
Thanked 9 times in 7 posts
Default Enum vs Namespace

So if I understand correctly, if I place my Enums in a namespace they will only work in that indicator and not others that use the same enum? ie they are not global?

So the MAType example would only be available to that script and not others?
Mindset is offline  
Reply With Quote
Old 01-26-2011, 09:09 AM   #11
gg80108
Senior Member
 
Join Date: Dec 2008
Location: Castle Pines, Co
Posts: 621
Thanks: 1
Thanked 2 times in 2 posts
Default

I found that these issues show up if u do the "save as" on an indicator with enums/namespace and then try to compile.. For us non geeks not sure why NT doesn't take care of this when it allows u to " save as", guess NT thought that just changing the strategy name would be good enough?

I did find that if I do a "save as" that has some enums wrapped in a namespace,, that I have to change the namespace name in the copy,, which is better then having to alter all the enum names if there are several enums..
Also found that I have to add the namespace in the declaration using of a strategy if I want to use the indicator in a strategy
gg80108 is offline  
Reply With Quote
Old 01-26-2011, 09:22 AM   #12
ETFVoyageur
Senior Member
 
Join Date: Dec 2010
Posts: 470
Thanks: 0
Thanked 4 times in 4 posts
Default

As to the saving -- it actually works quite well

There are two cases -- either (a) you do not want the namespace changed when you do Save As ... , such as when you want to use the enum across several of your own indicators or (b) you do want the namespace changed, such as when you want the enum unique to that indicator.

In the case of (b) which is what you seemed to want -- just follow my convention. I name the namespace the same as my indicator. Then when Save As ... does its text substitution it updates the namespace name as well. This convention is pretty well guaranteed to not collide, since you cannot have two indicators of the same name anyway.

In the case of (a) pick any namespace name you like that is unlikely to get picked by someone else, and not the same as your indicator name. Then Save As ... will not touch it.

Put another way, here is my suggestion:
  • If you have things to share between scripts of yours, (enums, classes, struct definitions, static library functions, etc) put them in a namespace that is unique to you, but does not match any script name. Save As... will not bother this namespace name.
  • If you have things that are unique to a single script (e.g. enum properties) , put them in a namespace whose name matches the script name. That is pretty well guaranteed to not collide with anything else, including your own other scripts, because the script itself must be a unique name. By making the namespace name and the script name match, Save As... will update both.

--EV
Last edited by ETFVoyageur; 01-26-2011 at 09:40 AM.
ETFVoyageur is offline  
Reply With Quote
Old 01-26-2011, 09:39 AM   #13
gg80108
Senior Member
 
Join Date: Dec 2008
Location: Castle Pines, Co
Posts: 621
Thanks: 1
Thanked 2 times in 2 posts
Default

Enclosed is my save as results,, less then trouble free.. Am I doing something wrong setting this up?
Think ur wisdom just dawned on me "I name the namespace the same as my indicator. Then when Save As ... does its text substitution it updates the namespace name as well."
Attached Images
File Type: jpg copy.JPG (193.5 KB, 23 views)
Last edited by gg80108; 01-26-2011 at 09:50 AM.
gg80108 is offline  
Reply With Quote
Old 01-26-2011, 09:53 AM   #14
ETFVoyageur
Senior Member
 
Join Date: Dec 2010
Posts: 470
Thanks: 0
Thanked 4 times in 4 posts
Default

See my previous posting.

In your case you have two indicators with the same namespace declaring the same enums. You can only declare the same enum once per namespace.

By the way, please try to pick a namespace name that is likely to be unique to you. Something like "Avgtype" is too likely to collide with one in use by some other indicator that a user may have also installed.

As I suggested in my last post, I suggest using the indicator name as the namespace name too.

--EV
ETFVoyageur is offline  
Reply With Quote
Old 10-28-2011, 08:14 AM   #15
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Updating this thread here as we have lately seen issues following this approach. If you export as an assembly, your enums declared in their own namespace may not import correctly on another machine. Official recommendation on enum placement is shown in this sample:
http://www.ninjatrader.com/support/f...ead.php?t=3420
NinjaTrader_RyanM 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
Compile error message with namespace tradervick Strategy Development 6 01-12-2011 03:00 PM
Polluting the global namespace ETFVoyageur Indicator Development 4 01-06-2011 04:27 AM
namespace trend Version 7 Beta General Questions & Bug Reports 1 10-21-2010 08:48 AM
Global Namespace Error when saving ThatManFromTexas Indicator Development 6 05-07-2010 07:45 AM
Z-ordering of global drawing tools error rtrader Version 7 Beta General Questions & Bug Reports 10 05-03-2010 02:51 PM


All times are GMT -6. The time now is 06:41 PM.