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

How to: Create Properties with names as values. Part 1/2

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

    How to: Create Properties with names as values. Part 1/2

    Ever wanted to have your Property give the Trader a dropdown list of values like "Open, Close ..." to choose from rather than asking them to type a number? This is how you do it.

    Step 1: Create an Enumerated type
    public
    enum MAType {
    Simple = 0,
    Exponential = 1,
    Weighted = 2
    }
    Tip A: Its always good to assign the value 0 to something that will be the default. If you are using these as Flags, make it = "None"
    Tip B: You don't have to put any numbers after the name, it is just my preference.
    Tip C: You can improve the documentation/intellisense by adding a "///" Prefix prior to the enum value. eg:
    ///
    <summary>Simple Moving Average </summary><returns></returns>
    Simple = 0,

    Tip D: If you plan on creating Values that you can compare & blend together with logic, use the [Flags] Attribute & assign a value that is represented by single bit. eg 1,2,4,8,16... Zero should indicate no flags are set.

    [Flags]
    public enum StopType {
    None = 0x00,
    SAR = 0x01,
    CrossOver = 0x02
    };

    if (myStop && StopType.SAR) { ...}

    Tip E: Declare it Outside of your class.
    If you don't, you will need to also change the Property Category to prevent it being a public parameter which means it will not be visible to any other parts of Ninja, ie Strategy etc. (I'll make another post on this)


    #region My Global Enums
    publicenum SymbolType {
    Circle = 1,
    Square = 2, ...
    }
    #endregion
    // Note: enums are before this line
    //This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {

    Step 2: Create a Property with its value
    As this is really just a number. You don't have to do anything special to serialise it. It will save & restore its settings just fine.
    [Description("This Property does ...")]
    [Category(
    "Parameters")]
    [Gui.Design.DisplayName(
    "Moving Average")]
    public MAType MovAvgName
    {
    get { return movAvgName; }
    set { movAvgName = value; }
    }

    <Exceeded post limit, continued in the reply>
    Last edited by David Lean; 11-23-2009, 03:48 PM.

    #2
    How to: Create Properties with names as values. Part 2/2

    Step 3: Declare a local variable to hold the property value

    #region Variables
    // Wizard generated variables
    private MAType movAvgName = MAType.Simple;

    Step 4: Use it in your code.
    Typically you will use either a switch statement to distinguish between a your many types. A common error is to forget to prefix the value (".Simple") with the enum type ("MAType.")
    switch (movAvgName) {
    case MAType.Simple: dataMA.Set(SMA(Input,iPeriod)[0]);
    break;
    case MAType.Weighted: dataMA.Set(WMA(Input,iPeriod)[0]);
    break;
    }
    OR an if statement with a bit mask to AND or OR the result to filter out flags.
    if ((showLine & 0x08) != 0) { // Mask 8 (0000 1000) to show SD line
    PlotD.Set(Stochastics(StocD, StocK, StocSmooth)[0]);
    }
    Note: One "&" or one "|" does bit manipulation. Two "&&" or "||" does logic comparisions.

    enjoy Dave.
    Disclaimer: Using Enumerated datatypes as Indicators is not supported by Ninja. This means it might break in a future release &/or might not work with other parts of Ninja. Test it well & remember to regresion test it with each new Ninja release.
    That said, enums are a core part of the C# language. Its hard to see how they could bite you.

    Comment


      #3
      David,

      Thank you for this thorough writeup. We appreciate you taking the time and sharing with the community.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Nice write up.

        One thing to keep in mind though is to not pollute the name space with your type names. Your public enum MAType effectively becomes a global name which is fine, but can cause some name collisions. Best to use a unique name by having a prefix.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by techgetgame, Today, 11:42 PM
        0 responses
        7 views
        0 likes
        Last Post techgetgame  
        Started by sephichapdson, Today, 11:36 PM
        0 responses
        1 view
        0 likes
        Last Post sephichapdson  
        Started by bortz, 11-06-2023, 08:04 AM
        47 responses
        1,612 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        9 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        19 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Working...
        X