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

Setting NinjaScriptProperty values

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

    Setting NinjaScriptProperty values

    What is the proper way to set the default values for NinjaScriptProperty properties, that will be input or selected by someone in the UI?
    • For a single default value, that the user can change
    • For a True / False condition
    • For a drop-down list of values to select from
    The following produces an error:
    [NinjaScriptProperty]
    [Display(Name="ATM Strategy", Description="ATMStrategy for executing orders", Order=0, GroupName="Strategy")]
    public string AtmStrategy
    { get; set {AtmStrategy = "Strategy1";} }

    #2
    Hello epete,

    Thank you for your post.

    You've got three different scenarios here.

    First, for a user defined variable that the user can just type in:

    public class TestPropertiesIndicator : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "TestPropertiesIndicator";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    TestInput = 10;
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="TestInput", Description="Test Input", Order=1, GroupName="Parameters")]
    public int TestInput
    { get; set; }

    #endregion

    In the above example, we set up the Properties for the input in the Properties region, and we assign a default value for the variable in the State.SetDefaults section of OnStateChange().

    This example is for an integer value, however, this could be adapted for other data types, such as a String. This leads in to your second question, about a true/false selection in the properties.

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "MyCustomIndicator";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    IsThisTrue = true;
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    }

    #region Properties
    [NinjaScriptProperty]
    [Display(Name="IsThisTrue", Description="Is This True?", Order=1, GroupName="Parameters")]
    public bool IsThisTrue
    { get; set; }

    #endregion

    In the above example, we have a very similar scenario to the first, where we again set the Properties for the input in the Properties region, and we assign a default value for the variable in the State.SetDefaults section of OnStateChange(). The main difference is that since this property is a bool, it will show the user a true/false dropdown in the Properties for the indicator.

    Lastly, the third scenario is a bit different and requires the use of an enum.

    This publicly available link goes into some detail on what enums are and how they work. I heartily recommend reviewing this if you are new to enums:
    Enums are strongly typed constants and unique types that allow you to assign symbolic names to integral values. This lesson teaches C# enum.


    This example from our help guide will give you an excellent framework to start from to make your own dropdown selections:


    So, for an example:

    // Create a variable that stores the user's selection for a moving average
    private CustomEnumNamespace.UniversalMovingAverage maType = CustomEnumNamespace.UniversalMovingAverage.SMA;
    private int period = 14;

    protected override void OnStateChange()
    {
    if(State == State.SetDefaults)
    {
    // Adds a plot for the MA values to be stored in
    AddPlot(Brushes.Orange, "MA");

    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    Name = "Sample universal moving average";
    }
    }

    protected override void OnBarUpdate()
    {
    // We use a switch which allows NinjaTrader to only execute code pertaining to our case
    switch (maType)
    {
    // If the maType is defined as an EMA then...
    case CustomEnumNamespace.UniversalMovingAverage.EMA:
    {
    // Sets the plot to be equal to the EMA's plot
    Value[0] = (EMA(Period)[0]);
    break;
    }

    // If the maType is defined as a HMA then...
    case CustomEnumNamespace.UniversalMovingAverage.HMA:
    {
    // Sets the plot to be equal to the HMA's plot
    Value[0] = (HMA(Period)[0]);
    break;
    }

    // If the maType is defined as a SMA then...
    case CustomEnumNamespace.UniversalMovingAverage.SMA:
    {
    // Sets the plot to be equal to the SMA's plot
    Value[0] = (SMA(Period)[0]);
    break;
    }

    // If the maType is defined as a WMA then...
    case CustomEnumNamespace.UniversalMovingAverage.WMA:
    {
    // Sets the plot to be equal to the WMA's plot
    Value[0] = (WMA(Period)[0]);
    break;
    }
    }

    }

    #region Properties
    // Creates the user definable parameter for the moving average type.
    [Display(GroupName = "Parameters", Description="Choose a Moving Average Type.")]
    public CustomEnumNamespace.UniversalMovingAverage MAType
    {
    get { return maType; }
    set { maType = value; }
    }


    In the above example, which you can find the full code for in the help guide link above, we still set properties for our enum, however, we have to declare it as a variable at the beginning of the script, and in OnBarUpdate we must have a switch case that handles the assignment of the values from the dropdown to the plot value.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by bmartz, 03-12-2024, 06:12 AM
    4 responses
    31 views
    0 likes
    Last Post bmartz
    by bmartz
     
    Started by Aviram Y, Today, 05:29 AM
    4 responses
    12 views
    0 likes
    Last Post Aviram Y  
    Started by algospoke, 04-17-2024, 06:40 PM
    3 responses
    28 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by gentlebenthebear, Today, 01:30 AM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by cls71, Today, 04:45 AM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X