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

wrapping order methods in a class that resides in a separate cs file

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

    wrapping order methods in a class that resides in a separate cs file

    Hi,

    I want to wrap order methods in my own code (in a separate .cs file) because there're other data to be updated at entering/exiting the market; something like in below. But the compiler complains 'EnterLong() not in the context' (CS0103). What qualifier should I add before EnterLong()?

    Thank you for the help.

    Regards,
    Steven


    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.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.Gui.Tools;​

    public void K2W_EnterLong()
    {
    EnterLong();
    curNbrContracts++;
    entryBar= currentBar;

    }​

    #2
    Hello i019945nj,

    Thank you for your post.

    NinjaScript methods only exist inside the class that inherits from Strategy, so you will need to pass in the strategy instance to the constructor of that class.

    For example,
    Code:
    public void K2W_EnterLong(MyCustomStrategy strat)
    
    {
    strat.EnterLong();
    
    curNbrContracts++;
    
    entryBar= currentBar;
    }

    As a suggestion, it might be better to define the strategy instance as shown below, so you can use the strategy variable in any other method from the class.

    Code:
    private MyCustomStrategy strategy
    
    public MyCustomClass (MyCustomStrategy strat)
    
    {
    strategy = strat;
    }

    In the strategy, you can pass the instance like this:

    Code:
    yourClassVariable.K2W_EnterLong(this);
    Please let us know if you have any other questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Thank you indeed, Gaby.

      One further question. I am designing a system of "magic wands," where each wand (that is, a Line) has its own functional role (i.e., long and short, long only, short only) at cross-over, and can use different position management mechanism and scaling in/out strategy. As such, can all wands (note that many can be at work at the same time) can share one strategy instance? Would orders/positions/... of one Line interfere with others in this case?

      Or each Line should have its own strategy instance so as to work independently?

      Thank you again for your valuable input.

      Regards,
      Steven

      Comment


        #4
        Thank you indeed, Gaby.

        One further question. I am designing a system of "magic wands," where each wand (that is, a Line) has its own functional role (i.e., long and short, long only, short only) at cross-over, and can use different position management mechanism and scaling in/out strategy. As such, can all wands (note that many can be at work at the same time) can share one strategy instance? Would orders/positions/... of one Line interfere with others in this case?

        Or each Line should have its own strategy instance so as to work independently?

        Thank you again for your valuable input.

        Regards,
        Steven

        Comment


          #5
          Just in case of confusions - I have one strategy called GBB, where many Lines are created, each of which can employ different approach to handle stop loss, profit trailing, ....

          Would it suffice to just pass to each Line (a separate class) GBB's strategy instance to make all this work?


          Regards,
          Steven

          Comment


            #6
            Hello,

            A single strategy can place as many trades as it needs to based on the conditions you make, so it's not a problem for all your "wands"/lines to share a single strategy instance.

            You can use a custom class to control the conditions. Make sure to pass the instance of the strategy into that class, so it can use the strategy instance to place orders or use other properties/methods of the strategy.

            Please let us know if you have any other questions.
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Hi Gaby,

              Grateful.

              RE "pass the instance of the strategy into that class," - please refer to below my current code segment -

              Would it suffice to simply use inheritance like:
              public class _MagicWand: GGB

              Or I need to add a new class member like "private GGB ggbInstance;" and "new GGB()" in the constructor of class '_MagicWand'?

              Thank you.

              Steven


              <<<< Current Code >>>

              (in GGB.cs)
              ==========
              public class GGB : Strategy
              {
              protected override void OnStateChange()
              {
              ...
              }
              protected override void OnBarUpdate()
              {​
              }​
              ...


              }

              (in K2WLib.cs)
              public class _MagicWand
              {
              private DrawingTools.Line line;
              private int maxNbrContracts; //maximum # of contracts I can trade; version 1.0: defualt 1
              private int curNbrContracts; //current # of contracts I have
              private int curMarketPosition; //my current market direction; _Flat=0; _LONG=1, _SHORT=2
              ...

              public void K2W_EnterLong()
              {
              EnterLong();
              curNbrContracts++;
              entryBar= currentBar;

              }​

              }

              Comment


                #8
                Hello,

                You should not attempt to use custom inheritence. Instead, we recommend passing the instance of the strategy to the class as previously mentioned.

                Code:
                protected override void OnBarUpdate()
                
                {
                
                myCustomClass.K2W_EnterLong();
                
                }

                With your sample, it would look as below:

                Code:
                public class GGB : Strategy
                {
                
                private MagicWand myCustomClass;
                
                protected override void OnStateChange()
                
                {
                
                if(State == State.DataLoaded)
                {
                myCustomClass = new MagicWand (this);
                }
                
                }
                
                
                protected override void OnBarUpdate()
                {
                myCustomClass.K2W_EnterLong();
                }
                
                }
                
                
                
                public class MagicWand
                {
                
                private GGB myStrategy;
                
                public MagicWand(GGB strat)
                {
                myStrategy = strat;
                }
                
                public void K2W_EnterLong()
                {
                myStrategy .EnterLong();
                }
                
                }

                Please note that NinjaTrader does not normally allow underscores in class names, so rename _MagicWand to MagicWand to avoid any issues.
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  Clear completely. Thank you Gaby.

                  Steven

                  Comment


                    #10
                    Hi Gaby,

                    if below is not a problem (meaning 'Strategy' is a type/class), why declaring a class member (in a separate file) of type Strategy is a problem?

                    Cheers,
                    Steven


                    <<< No problem>>
                    -------------------------
                    public class GGB : Strategy
                    {
                    private MagicWand myCustomClass;

                    protected override void OnStateChange()

                    {

                    if(State == State.DataLoaded)
                    {
                    myCustomClass = new MagicWand (this);
                    }

                    }

                    <<<compiler error CS0246 "could not find class or namespace 'Strategy' ...>>>
                    --------------------------------------------------------------------------------------------------------
                    public class _MagicWand
                    {
                    //three types of the Wand: longshort, long (only), short (only);
                    //when maxContracts > 1, put (maxContracts - 1) contracts at 3 ticks from (above for long, below for short) the stop loss price;
                    private Strategy myStrategy;

                    ...
                    }​​

                    Comment


                      #11
                      Hello,

                      Most likely you are missing a using statement in order for you to declare the strategy instance that way.

                      In this case, a partial class may be better to use so you won't have to make a new instance.

                      Please see this forum post with an example script:

                      https://forum.ninjatrader.com/forum/...40#post1142340

                      If you have any other questions, please let me know.
                      Gaby V.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks, Gaby. Shall check on that.

                        In below code, 'myStrategy' is a reference to GGB which is a Strategy. Yet the compiler complains that it cannot find the definition of 'MarketPosition'. Where is this 'MarketPosition' defined?

                        if (myStrategy.Position.MarketPosition == myStrategy.MarketPosition.Flat) {

                        K2W_EnterLong();

                        }

                        Thanks and Best Regards,
                        Steven​

                        Comment


                          #13
                          Hello,

                          What using statements do you have at the top of your file?

                          MarketPosition is from NinjaTrader.Cbi.Position class.

                          Please let me know if you have any further questions.
                          Gaby V.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Gaby,

                            These are what I have on top of the file. I suppose they are all the same with general strategy files. 'Cbi' is there. Incidentally where can I find the associated files listed in the using?

                            Cheers,
                            Steven

                            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.Gui.Tools;
                            using NinjaTrader.Data;
                            using NinjaTrader.NinjaScript;
                            using NinjaTrader.Core.FloatingPoint;
                            using NinjaTrader.NinjaScript.Indicators;
                            using NinjaTrader.NinjaScript.DrawingTools;
                            #endregion

                            Comment


                              #15
                              Hello,

                              Position is a class in the namespace position is a class in the NinjaTrader.Cbi namespace.

                              using NinjaTrader.Cbi;


                              using NinjaTrader.Cbi.Position;


                              Please let me know if you have any other questions.

                              Gaby V.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by usglucofreeze, Today, 01:19 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post usglucofreeze  
                              Started by f.saeidi, Today, 01:12 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post f.saeidi  
                              Started by NinjaTrader_ChelseaB, 03-14-2017, 10:17 AM
                              227 responses
                              34,318 views
                              7 likes
                              Last Post rare312
                              by rare312
                               
                              Started by f.saeidi, Today, 12:11 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post f.saeidi  
                              Started by Graci117, Yesterday, 11:40 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Graci117  
                              Working...
                              X