Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

using ENUM in strategy from Indicator

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

    using ENUM in strategy from Indicator

    Hi I have indicator with ENUM and I want to have strategy that selects ENUM from indicator.
    If we take an example of SampleUniversalMovingAverage


    it has enum for MA.

    If I want to create a strategy that also selects ENUM for MA without me having to define all logic of enum from indicator.

    I understand that in strategy I still need to create ENUM,but how do i modify switch statement so only SMA or EMA or HMA is selected without defining values. Because values are defined in indicator already.




    private CustomEnumNamespace.UniversalMovingAverage maType = CustomEnumNamespace.UniversalMovingAverage.SMA;


    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;
    }
    }
    }​


    public CustomEnumNamespace.UniversalMovingAverage MAType
    {
    get { return maType; }
    set { maType = value; }
    }​

    namespace CustomEnumNamespace
    {
    public enum UniversalMovingAverage
    {
    EMA,
    HMA,
    SMA,
    WMA,
    }
    }​





    Last edited by tkaboris; 05-16-2024, 07:55 AM.

    #2
    Hello tkaboris,

    You want to call the SampleUniversalMovingAverage indicator from your strategy?

    The MAType and Period public variables will need the [NinjaScriptProperty] attribute added so that these are added as overload parameters.


    Once added, you can call the script and supply these values as parameters.

    Code:
    private SampleUniversalMovingAverage myIndy;
    In State.DataLoaded:

    Code:
    myIndy = SampleUniversalMovingAverage(CustomEnumNamespace.UniversalMovingAverage.EMA, 14);

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Something is going on with forum. I cant post anything for last 4 hours, it returns 403 or 401 error

      Comment


        #4
        right but what if I just want user to select wchich MA via enum in strategy?
        Do i need to create a swithc for that in strategy? if so how will that look like?

        Comment


          #5
          Hello tkaboris,

          An enum is just a value. If you want to trigger logic based on that value, you would have to do that in the script.

          To see what this would look like, have a look at lines 52 to 86 in the SampleUniversalMovingAverage reference sample linked below.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            I am lost at this switch, could you please correct me of how i need to define in example

            Code:
            //
            //
            // Copyright (C) 2024, NinjaTrader LLC <www.ninjatrader.com>.
            // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
            //
            #region Using declarations
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.ComponentModel.DataAnnotations;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows;
            using System.Windows.Input;
            using System.Windows.Media;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Gui;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Gui.SuperDom;
            using NinjaTrader.Data;
            using NinjaTrader.NinjaScript;
            using NinjaTrader.Core.FloatingPoint;
            using NinjaTrader.NinjaScript.Indicators;
            using NinjaTrader.NinjaScript.DrawingTools;
            #endregion
            
            //This namespace holds strategies in this folder and is required. Do not change it.
            namespace NinjaTrader.NinjaScript.Strategies
            {
                public class SampleMACrossOverCopy : Strategy
                {
                    private SMA smaFast;
                    private SMA smaSlow;
                    private SampleUniversalMovingAverage myIndy;
                    private CustomEnumNamespaceMA.UniversalMovingAverage    maType    = CustomEnumNamespaceMA.UniversalMovingAverage.SMA;
            
                    protected override void OnStateChange()
                    {
                        if (State == State.SetDefaults)
                        {
                            Description    = NinjaTrader.Custom.Resource.NinjaScriptStrategyDescriptionSampleMACrossOver;
                            Name        = "SampleMACrossOverCopy";
                            Fast        = 10;
                            Slow        = 25;
                            // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
                            // See the Help Guide for additional information
                            IsInstantiatedOnEachOptimizationIteration = false;
                        }
                        else if (State == State.DataLoaded)
                        {
                            smaFast = SMA(Fast);
                            smaSlow = SMA(Slow);
            
                            smaFast.Plots[0].Brush = Brushes.Goldenrod;
                            smaSlow.Plots[0].Brush = Brushes.SeaGreen;
            
                            AddChartIndicator(smaFast);
                            AddChartIndicator(smaSlow);
                            myIndy = SampleUniversalMovingAverage(CustomEnumNamespace.UniversalMovingAverage.EMA, 14);
                        }
                    }
            
                    protected override void OnBarUpdate()
                    {
                        if (CurrentBar < BarsRequiredToTrade)
                            return;
                        
                        
                        switch (maType)
                        {
                            // If the maType is defined as an EMA then...
                            case CustomEnumNamespaceMA.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 CustomEnumNamespaceMA.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 CustomEnumNamespaceMA.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 CustomEnumNamespaceMA.UniversalMovingAverage.WMA:
                            {
                                // Sets the plot to be equal to the WMA's plot
                                Value[0] = (WMA(Period)[0]);
                                break;
                            }
                        }
                        
            
                        if (CrossAbove(smaFast, smaSlow, 1))
                            EnterLong();
                        else if (CrossBelow(smaFast, smaSlow, 1))
                            EnterShort();
                    }
            
                    #region Properties
                    [Range(1, int.MaxValue), NinjaScriptProperty]
                    [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
                    public int Fast
                    { get; set; }
            
                    [Range(1, int.MaxValue), NinjaScriptProperty]
                    [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
                    public int Slow
                    { get; set; }
                    #endregion
                }
            }
            
            namespace CustomEnumNamespaceMA
            {
                public enum UniversalMovingAverage
                {
                    EMA,
                    HMA,
                    SMA,
                    WMA,
                }
            }​
            }
            Last edited by tkaboris; 05-16-2024, 02:29 PM.

            Comment


              #7
              Hello tkaboris,

              What are you needing assistance with?

              Is there an error message you would like assistance with?

              Is the value of maType not what you expect?

              Where have you set the maType variable's value?
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              30 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              124 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              64 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              41 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X