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

user choice as drop-down field for later use in a condition

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

    user choice as drop-down field for later use in a condition

    Hello all,

    how can I let the user make setting in the strategy property like this?

    Setting Foo --> A, B or C

    The user should be able to choose from a drop-down field. Only one choice should be possible and active which I want to use later as logic for a condition. Multi-choice shouldn't be possible.

    I tried by this, but obviously I am missing something because my strategy doesn't show any dropdown field

    Code:
    {
    public class ABC : Strategy
    {
    private Choice userSelection = Choice.A;
    
    // at the end I used this here
    #region Global Enums
    public enum Choice
    {
    A,
    B,
    C
    }
    #endregion
    Any help appreciated.
    Last edited by patricia70; 07-13-2021, 07:55 AM.

    #2
    Hello patricia70,

    Thank you for your post.

    You'd want to put the Enum in a custom namespace assigned to the Strategy, not in a region within the strategy itself.

    A simple example from our help guide that lets the user select from multiple moving averages using an Enum can be found here:



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

    Comment


      #3
      Appreciate the helpful answer which led to a working solution. Thank you Kate

      Comment


        #4
        Hello again,

        this is my actual code for my strategy ABC1

        Code:
        namespace NinjaTrader.NinjaScript.Strategies.myStrategies
        
        {
        public class ABC1 : Strategy
        {
        private const string myABC_CodeID = "myABC Strategy v1";
        private CustomEnumNamespace.Foo fooSel = CustomEnumNamespace.Foo.Foo1;
        private CustomEnumNamespace.Bar barSel = CustomEnumNamespace.Bar.BarB;
        
        private NinjaTrader.NinjaScript.Indicators.myIndicators.myIndi myABC1;
        public override string DisplayName
        {
        get { return myABC_CodeID; }
        }
        
        protected override void OnStateChange()
        {
        
        [...]
        
        namespace CustomEnumNamespace
        {
        public enum Foo
        {
        Foo1,
        Foo2,
        Foo3
        }
        
        public enum Bar
        {
        BarA,
        BarB
        }
        }
        I wanted to duplicate this strategy to create a forked version. Therefore I replaced this line
        Code:
        private const string myABC_CodeID = "myABC Strategy v1";
        by following line
        Code:
        private const string myABC_CodeID = "myABC Strategy v2";
        to have the new version properly set. Then I right-clicked onto the code and chose "Save As..." to save this updated version under a new filename called ABC2. Unfortunately the compilation will fail with the error:
        The namespace "CustomEnumNamespace" already contains a definition for 'Foo' and for 'Bar'
        By trial and error I found that I can remedy this circumstance by modifying the last part in my code inside the CustomEnumNamespace like this:
        Code:
        namespace CustomEnumNamespace
        {
        public enum FooNew
        {
        Foo1,
        Foo2,
        Foo3
        }
        
        public enum BarNew
        {
        BarA,
        BarB
        }
        }
        but this is not a nice solution and I would have to repeat this process for every future version I'll take. Is there a better way to fulfull this task? Is there any possibility to have this automagically done without resulting in conflicts? Maybe I could make use of my already existing const string myABC_CodeID somehow?

        looking forward to hear your comments and suggestions.

        Comment


          #5
          Hello patricia70,

          Thank you for your reply.

          You have basically 2 options here - rename the enums each time, or you can simply remove the enum code from the copy and reference it in the other script. Exporting would then require both scripts in the export, the one using the enum reference and the one actually holding the enum. I've made a simple example ExampleUseEnumInOtherIndicator that uses an enum found in SampleUniversalMovingAverage.

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

          Comment


            #6
            Originally posted by NinjaTrader_Kate View Post
            Hello Kate,

            I have another question related to this topic and hopefully you can shed some light into this matter. Looking at the provided SampleUniversalMovingAverage indicator. If we work with these enums, do you have to use switch and case for the condition? Why can't you use the classic if statements or is that also possible and I missed that? When should you use case and when if, is there a rule of thumb or best-practice ?

            thank you.

            Comment


              #7
              Hello patricia70,

              Thank you for your reply.

              They are somewhat interchangeable. Generally speaking, if you have more than 5 cases it's generally thought preferable to use a switch statement, otherwise if statements work fine. This publicly available page goes over some considerations: https://www.geeksforgeeks.org/switch-vs-else/

              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 Stanfillirenfro, Yesterday, 09:19 AM
              7 responses
              51 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by TraderCro, 04-12-2024, 11:36 AM
              4 responses
              69 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Mindset, Yesterday, 02:04 AM
              1 response
              15 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by agclub, 04-21-2024, 08:57 PM
              4 responses
              18 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by Irukandji, Today, 04:58 AM
              0 responses
              6 views
              0 likes
              Last Post Irukandji  
              Working...
              X