• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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:
    https://csharp-station.com/Tutorial/CSharp/Lesson17

    This example from our help guide will give you an excellent framework to start from to make your own dropdown selections:
    https://ninjatrader.com/support/help...ned_parame.htm

    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 Mike661, Today, 07:22 AM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_PatrickG  
    Started by memonolog, Today, 07:56 AM
    0 responses
    6 views
    0 likes
    Last Post memonolog  
    Started by tdawson11, Today, 07:48 AM
    0 responses
    4 views
    0 likes
    Last Post tdawson11  
    Started by pstrusi, Today, 07:39 AM
    2 responses
    7 views
    0 likes
    Last Post pstrusi
    by pstrusi
     
    Started by vantojo, 06-19-2019, 04:06 PM
    6 responses
    34 views
    0 likes
    Last Post vantojo
    by vantojo
     
    Working...
    X