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

Access Instrument Commission from Ninjascript NT8

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

    Access Instrument Commission from Ninjascript NT8

    Hi,

    It has been probably asked before, but just to be sure I ask again.

    Is it possible to access an Instrument's Commission value? Screenshot attached.

    Something similar like this for the Risk data:
    Code:
    Risk.Get("NinjaTrader Brokerage Default").ByMasterInstrument[Instrument.MasterInstrument].MaxPositionSize
    Something like this:
    Code:
    Commission.Get("NinjaTrader Brokerage Lifetime").ByMasterInstrument[Instrument.MasterInstrument].PerUnitCommission
    Worst case I can add a new parameter to my indicator for this, but I would prefer to get it automagically.

    Thanks in advance,

    G.
    Attached Files

    #2
    Hello Gorkhaan,

    There are no supported methods for getting commissions in NinjaScript.

    However, I was able to find some unsupported code that might be what you need.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the quick reply. I managed to find this and some similar posts by google, but none of them helped.
      Maybe a feature request can be sent to the devs? :-)

      I stick to an indicator parameter for now.

      Thanks!

      Comment


        #4
        Hello Gorkhaan,

        I've submitted your request to call commissions for an account/instrument through NinjaScript.

        Once I have a tracking ID for this request, I will post in this thread for future reference.

        I appreciate your patience.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Gorkhaan,

          Thank you for your patience.

          I have added a vote for you to an existing feature request for this, which may be tracked under the number SFT-1645. Should this be included in a later release, you can find it in the release notes.

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

          Comment


            #6
            Hello there,

            Please add an additional vote for me to the feature request SFT-1645. Would be very usefull to allow custom code to set commission (with complexe commision/taxes schema) while backtesting.

            Thanks

            Comment


              #7
              Hello Logicwar,

              I've added your vote to SFT-1645.

              Thanks for your input.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hi Gorkhaan,

                You can try this from within a Strategy.

                Code:
                MasterInstrument mi = Bars.Instrument.MasterInstrument;
                double commPerUnit = this.Account.Commission.ByMasterInstrument[mi].PerUnit;
                Steve L
                NinjaTrader Ecosystem Vendor - Ninja Mastery

                Comment


                  #9
                  Dayum, that actually worked. Too bad that the NinjaScript Editor IDE did not come up with the "Commission" method from my account object. Weird.
                  Here is a demo indicator for future reference. Note: There is an undocumented way to select an Account via dropdown list shown below. Enjoy.

                  Thanks for your help Steve!

                  Code:
                      public class AccessCommissionsAndRiskFromIndicatorNT8 : Indicator
                      {
                          private Account account;
                          private int maxContractSize = 0;
                          protected override void OnStateChange()
                          {
                              if (State == State.SetDefaults)
                              {
                                  Description                                    = @"Enter the description for your new custom Indicator here.";
                                  Name                                        = "AccessCommissionsAndRiskFromIndicatorNT8";
                                  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;
                                  SelectedAccount                                = "Sim101";
                              }
                              else if (State == State.Configure) {
                                  lock (Account.All) {
                                      account = Account.All.FirstOrDefault(a => a.Name == SelectedAccount);
                                  }
                  
                                  maxContractSize = Risk.Get("NinjaTrader Brokerage Default").ByMasterInstrument[Instrument.MasterInstrument].MaxPositionSize;
                  
                                  // NinjaScript Editor does not come up with "Commission" method for my account object. Sad. But it still works.
                                  Print("Instrument Commission PerUnit: " + account.Commission.ByMasterInstrument[Bars.Instrument.MasterInstrument].PerUnit);
                  
                                  // Sometimes Ninjatrader is unable to access this data and it just does not work. Restarting ninjatrader fixes it.
                                  Print("Instrument Risk Profile MaxContractSize: " + maxContractSize);
                              }
                          }
                  
                          protected override void OnBarUpdate()
                          {
                              // Press F5 to reload ninjascript, and check Ninjascript Output window.
                          }
                  
                          #region Properties
                          [TypeConverter(typeof(NinjaTrader.NinjaScript.AccountNameConverter))]
                          [Display(Name="SelectedAccount", Order=10, GroupName="Properties")]
                          public string SelectedAccount
                          { get; set; }
                          #endregion
                      }

                  Comment


                    #10
                    Hi Gorkhaan,

                    Glad you got it to work.

                    The reason is doesn't appear in the intellisense of your editor is intentional. It could change over time and is not an "officially" supported feature.

                    But there's no harm in using it as long as you're aware of that.
                    Steve L
                    NinjaTrader Ecosystem Vendor - Ninja Mastery

                    Comment


                      #11
                      Hello Steve, Gorkhaan,

                      I can't get the proposed solution to work with NT version 8.0.25.0. May be I'm doing something wrong or the risk of that "not officially supported" feature to stop working actually materialized.

                      Could you please confirm if it's still working for you. If it stopped working, did you find a workaround?

                      Thank you
                      ASCENDO Trading
                      NinjaTrader Ecosystem Vendor - ASCENDO Trading

                      Comment


                        #12
                        Hello Steve, Gorkhaan,

                        I made a mistake in my previous message. I think I was testing with an account that does not have a commission template applied. It is actually still working with version 8.0.25.0.

                        For those who would like to use it for a drawing tool you can modify the code as follow:
                        Code:
                        maxContractSize = Risk.Get("NinjaTrader Brokerage Default").ByMasterInstrument[AttachedTo.Instrument.MasterInstrument].MaxPositionSize;
                        
                        // NinjaScript Editor does not come up with "Commission" method for my account object. Sad. But it still works.
                        Print("Instrument Commission PerUnit: " + selectedAccount.Commission.ByMasterInstrument[AttachedTo.Instrument.MasterInstrument].PerUnit);
                        
                        // Sometimes Ninjatrader is unable to access this data and it just does not work. Restarting ninjatrader fixes it.
                        Print("Instrument Risk Profile MaxContractSize: " + maxContractSize);
                        Thanks for sharing this information.
                        ASCENDO Trading
                        NinjaTrader Ecosystem Vendor - ASCENDO Trading

                        Comment


                          #13
                          Hi

                          Does anyone know if this is still working or changed recently?

                          I'm using the suggested code in State.Configure for a strategy but just getting: Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

                          Code:
                          MasterInstrument mi = Bars.Instrument.MasterInstrument;
                          double commPerUnit = this.Account.Commission.ByMasterInstrument[mi].PerUnit;​

                          Comment


                            #14
                            Hello timcjpfx,

                            The error means that something you used is null.

                            Using Bars from State.Configure is not supposed to work so that may be part of the problem. This type of code would only be suggested in later states like DataLoaded or when bar data is processing. This is mentioned in the help guide page for Bars: https://ninjatrader.com/support/helpGuides/nt8/bars.htm

                            Warning: The Bars object and its member should NOT be accessed within the OnStateChange() method before the State has reached State.DataLoaded

                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Jesse

                              I had tried both State.Configure and DataLoaded but get the same error.
                              When I debug it, the Account.Commission is null (see screenshot).

                              Any ideas?


                              Click image for larger version

Name:	Screenshot 2022-11-17 163505.png
Views:	279
Size:	113.3 KB
ID:	1224065

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GussJ, 03-04-2020, 03:11 PM
                              15 responses
                              3,271 views
                              0 likes
                              Last Post xiinteractive  
                              Started by Tim-c, Today, 02:10 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Taddypole, Today, 02:47 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Taddypole  
                              Started by chbruno, 04-24-2024, 04:10 PM
                              4 responses
                              51 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              10 responses
                              403 views
                              1 like
                              Last Post beobast
                              by beobast
                               
                              Working...
                              X