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

Basic C# question - how to variablize a function call

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

    #16
    Originally posted by Cheech View Post
    ...
    As you might be able to tell I'm using terminology from a different era of computer technology (I really hate to tell you how far back it was when I started but, it was when "programming" was done by wiring patch panels and the first real computer filled a large room and now there is much more processing power and memory in your phone or calculator, and yeah, I'm an old dude) so I can not describe it in OO terms like the original poster was able to do.


    Thanks
    Hm. You may not be quite as old as you think, though I never did hit the patch panel era, the first computer that I used did fill a whole room and had, I think 1MB of memory. We programmed in FORTRAN, and did all kinds of fancy gymnastics when we needed more than 1MB, as happened to one of my folks when he tried to use the computer to do some structural analysis of a cantilevered truss. Now my phone has more computing power and storage than that!

    Anyhow, this is a skeleton of how I would go about it. You will have to fill out the parts where I have indicated some etc stuff, but at least the first 2 Plots show up, if you use any of the valid defined MA's (the only valid ones I used are SMA, EMA and WMA). It is just a demo, which you will have to expand to your 17 types, but the entire skeleton is exposed.

    Have fun!

    Code:
    //this goes outside the class. Best to put it right under the "using declarations" region, right at the top of the file:
     
    using CheechGazillions;
    namespace CheechGazillions
    {
     public enum MAType
     {
      SMA,
      EMA,
      WMA,
      Type1,
      Type2,
      Type3
      //etc
     }
    }
    Code:
            #region Variables
     
      //define periods for MA's
      private int mA1Period = 5;
      private int mA2Period = 8;
      private int mA3Period = 13;
      private int mA4Period = 21;
     
      //defaults for MATypes
      private MAType mA1Type = MAType.SMA;
      private MAType mA2Type = MAType.SMA;
      private MAType mA3Type = MAType.SMA;
      private MAType mA4Type = MAType.SMA;
     
      //define named instances for each of the MA's as wach type  
      private SMA mA1SMA;
      private EMA mA1EMA;
      private WMA mA1WMA;
      // etc for each MAType
     
      private SMA mA2SMA;
      private EMA mA2EMA;
      private WMA mA2WMA;
      // etc for each MAType
     
      private SMA mA3SMA;
      private EMA mA3EMA;
      private WMA mA3WMA;
      // etc for each MAType
     
      private SMA mA4SMA;
      private EMA mA4EMA;
      private WMA mA4WMA;
      // etc for each MAType
     
            #endregion
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Square, "Plot0"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Square, "Plot1"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Yellow), PlotStyle.Square, "Plot2"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Square, "Plot3"));
                Overlay    = true;
            }
     
            protected override void OnStartUp()
      {
       switch (mA1Type) {
        case MAType.WMA:
         this.DisposeAllMA1();
         this.mA1WMA = WMA(Input, this.mA1Period);
         break;
        case MAType.EMA:
         this.DisposeAllMA1();
         this.mA1EMA = EMA(Input, this.mA1Period);
         break;
        case MAType.Type1:
         // you need to put the correct stuff here, using the 
         //same syntax style for Type1, which in this sample
         //has not been defined. Look at the EMA etc examples.
         break;
        //etc for the rest of the 17 types
        default:
        case MAType.SMA:
         this.DisposeAllMA1();
         this.mA1SMA = SMA(Input, this.mA1Period);
     
         break;
       }
       //repeat the switch structure for mA2Type, mA3Type and mA4Type
     
       switch (mA2Type) {
        case MAType.WMA:
         this.DisposeAllMA2();
         this.mA2WMA = WMA(Input, this.mA2Period);
         break;
        case MAType.EMA:
         this.DisposeAllMA2();
         this.mA2EMA = EMA(Input, this.mA2Period);
         break;
        case MAType.Type1:
         // you need to put the correct stuff here, usig the 
         //same syntax style for Type1, which in this sample
         //has not been defined. Look at the EMA etc examples.
         break;
        //etc for the rest of the 17 types
        default:
        case MAType.SMA:
         this.DisposeAllMA2();
         this.mA2SMA = SMA(Input, this.mA2Period);
     
         break;
       }
     
       //etc
      }
     
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
       if (CurrentBar < this.BarsRequired) return;
     
       switch (mA1Type) {
        case MAType.WMA:
         Plot0.Set(this.mA1WMA[0]);
         break;
        case MAType.EMA:
         Plot0.Set(this.mA1EMA[0]);
         break;
        case MAType.Type1:
         // you need to put the correct stuff here, using the 
         //same syntax style for Type1, which in this sample
         //has not been defined. Look at the EMA etc examples.
         break;
        //etc for the rest of the 17 types
        default:
        case MAType.SMA:
         Plot0.Set(this.mA1SMA[0]);
     
         break;
       }
       //repeat the switch structure for mA2Type(Plot1)), mA3Type(Plot2) and mA4Type(Plot3)
       switch (mA2Type) {
        case MAType.WMA:
         Plot1.Set(this.mA2WMA[0]);
         break;
        case MAType.EMA:
         Plot1.Set(this.mA2EMA[0]);
         break;
        case MAType.Type1:
         // you need to put the correct stuff here, using the 
         //same syntax style for Type1, which in this sample
         //has not been defined. Look at the EMA etc examples.
         break;
        //etc for the rest of the 17 types
        default:
        case MAType.SMA:
         Plot1.Set(this.mA2SMA[0]);
     
         break;
       }
     
       //etc
     
       //You now have all Plots set. The rest is just using those Plots, to examine x-overs etc..
      }
     
     
      #region Functions
      private void DisposeAllMA1()
      {
       if (this.mA1SMA != null) this.mA1SMA.Dispose();
       if (this.mA1EMA != null) this.mA1EMA.Dispose();
       if (this.mA1WMA != null) this.mA1WMA.Dispose();
       //etc for the rest of the 17 MATypes
       return;
      }
      private void DisposeAllMA2()
      {
     
       if (this.mA2SMA != null) this.mA2SMA.Dispose();
       if (this.mA3EMA != null) this.mA3EMA.Dispose();
       if (this.mA4WMA != null) this.mA4WMA.Dispose();
       //etc for the rest of the 17 MATypes
       return;
      }
     
      //repeat for MA3 and MA4
     
      #endregion
            #region Properties
            [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]  // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Plot0
            {
                get { return Values[0]; }
            }
            [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]  // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Plot1
            {
                get { return Values[1]; }
            }
            [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]  // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Plot2
            {
                get { return Values[2]; }
            }
            [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]  // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Plot3
            {
                get { return Values[3]; }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public int MA1Period
            {
                get { return this.mA1Period; }
                set { this.mA1Period = Math.Max(1, value); }
            }
     
            [Description("")]
            [GridCategory("Parameters")]
            public int MA2Period
            {
                get { return this.mA2Period; }
                set { this.mA2Period = Math.Max(1, value); }
            }
     
            [Description("")]
            [GridCategory("Parameters")]
            public int MA3Period
            {
                get { return this.mA3Period; }
                set { this.mA3Period = Math.Max(1, value); }
            }
     
            [Description("")]
            [GridCategory("Parameters")]
            public int MA4Period
            {
                get { return this.mA4Period; }
                set { this.mA4Period = Math.Max(1, value); }
            }
     
            [Description("")]
            [GridCategory("Parameters")]
            public MAType MA1Type
            {
                get { return this.mA1Type; }
                set { this.mA1Type = value; }
            }
     
            [Description("")]
            [GridCategory("Parameters")]
            public MAType MA2Type
            {
                get { return this.mA2Type; }
                set { this.mA2Type = value; }
            }
     
            [Description("")]
            [GridCategory("Parameters")]
            public MAType MA3Type
            {
                get { return this.mA3Type; }
                set { this.mA3Type = value; }
            }
     
            [Description("")]
            [GridCategory("Parameters")]
            public MAType MA4Type
            {
                get { return this.mA4Type; }
                set { this.mA4Type = value; }
            }
     
            #endregion
    For completeness, I would reorganize the GUI config dialog, and include descriptions and name text for the parameters.
    Last edited by koganam; 11-11-2012, 03:07 PM.

    Comment


      #17
      Well, I'm not going to play "I'm older than you are (hence more obsolete)" but when we moved the EAM equipment out of the "computer room" it was to make space for a new, shinny 4K (the largest one available at the time) 1401 with the advanced Autocoder programming language. By today's standards this would be like doing dentistry with a hammer, chisel, and a pair of pliers.

      Things got much better with the 360 series with BAL (Basic Assembler Language) which later became 360, 370 Assembler which was the language I used the most and occasionally dipping in to COBOL (ugh) and Fortran (great for scientific work) . I never made the transition from procedure oriented languages to OO which appears that you have done nicely. All the Inheritance and Encapsulation stuff gives me a headache, and I no longer derive any pleasure reading 4-5" thick .Net or C# manuals. Besides I usually fall asleep after about 8 pages and occasionally waking up to see that I've drooled all over them.

      I looked at the code you provided and it's appears to be really well done. However, not to slight your efforts, I have that part of the indicator working already (but will revisit my code using yours as a model). The part that was causing me difficulty at the time I made this blog entry was to be able to use the CrossAbove/Below to check for crossing for each MA pair without having to hard code each of the possible combinations. I actually have managed to get that part coded up too and have it working now although it would probably cause a real C# programmer to barf. It appears that there is still part of my programming brain that hasn't gone to the "happy hunting ground", ... yet.

      Not sure that you want to put anymore effort into this but if you're willing I can post the Cross code and you can critique or improve it as you see fit. Regardless, I thank you for your efforts and what you have done.
      Last edited by Cheech; 11-11-2012, 07:25 PM.

      Comment


        #18
        Originally posted by Cheech View Post
        Well, I'm not going to play "I'm older than you are (hence more obsolete)" but when we moved the EAM equipment out of the "computer room" it was to make space for a new, shinny 4K (the largest one available at the time) 1401 with the advanced Autocoder programming language. By today's standards this would be like doing dentistry with a hammer, chisel, and a pair of pliers.

        Things got much better with the 360 series with BAL (Basic Assembler Language) which later became 360, 370 Assembler which was the language I used the most and occasionally dipping in to COBOL (ugh) and Fortran (great for scientific work) . I never made the transition from procedure oriented languages to OO which appears that you have done nicely. All the Inheritance and Encapsulation stuff gives me a headache, and I no longer derive and pleasure reading 4-5" thick .Net or C# manuals. Besides I usually fall asleep after about 8 pages and occasionally waking up to see that I've drooled all over them.

        I looked at the code you provided and it's appears to be really well done. However, not to slight your efforts, I have that part of the indicator working already (but will revisit my code using yours as a model). The part that was causing me difficulty at the time I made this blog entry was to be able to use the CrossAbove/Below to check for crossing for each MA pair without having to hard code each of the possible combinations. I actually have managed to get that part coded up too and have it working now although it would probably cause a real C# programmer to barf. It appears that there is still part of my programming brain that hasn't gone to the "happy hunting ground", ... yet.

        Not sure that you want to put anymore effort into this but if you're willing I can post the Cross code and you can critique or improve it as you see fit. Regardless, I thank you for your efforts and what you have done.
        Yikes! You are the ancient Mariner, for sure. The 1401 today is like doing dentistry with a stone and sharpened nail. "Hammer and chisel" is way too modern a description.

        Sure. Let's take a look at your code.

        OOP is really not far removed from Modular/Structured programming. Just think of the object as a collection of modules that one can call from outside of the collection. Essentially a data structure that breaks the paradigm that a function can only return at most a single value, and instead behaves somewhat like a function that can return multiple values as and when required. (Considerably more powerful than a Procedure, as we used to call code that could handle multiple returns, when we used procedural programming. To quote the book, an object is just a data structure, combined with the code to manipulate the data.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by frankthearm, Yesterday, 09:08 AM
        13 responses
        45 views
        0 likes
        Last Post frankthearm  
        Started by PaulMohn, Today, 12:36 PM
        2 responses
        16 views
        0 likes
        Last Post PaulMohn  
        Started by Conceptzx, 10-11-2022, 06:38 AM
        2 responses
        53 views
        0 likes
        Last Post PhillT
        by PhillT
         
        Started by Kaledus, Today, 01:29 PM
        1 response
        4 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by yertle, Yesterday, 08:38 AM
        8 responses
        37 views
        0 likes
        Last Post ryjoga
        by ryjoga
         
        Working...
        X