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

Programmatically Access Risk while Running in Strategy Analyzer

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

    Programmatically Access Risk while Running in Strategy Analyzer

    In my strategy code, I'm trying to get access to Initial Margin for my instrument. Works fine in a chart, but in Strategy Analyzer the Strategy.Account.Risk property is null.

    It appears that NinjaTrader is using an account named Backtest when running in Strategy Analyzer, while in a Chart window the Sim101 account is being used. I have a risk template assigned to the Sim101 account and I can access initial margin so long as I'm running in the chart.

    I've also noticed that I can access my Sim101 account in Strategy Analyzer using Account.All. And indeed I can pull the risk. Is this the only way to solve this problem?

    Is it possible to somehow assign a risk template to the Backtest account? Seems like I must be missing something.

    Thanks.

    #2
    Hello BarzTrading,

    Accessing the risk is not documented or officially supported, so this would be used at your own discretion.

    Backtest orders are not actually placed to an account and do not affect the account position or performance, but yes the Backtest account is what is 'selected' in the Strategy Analyzer.

    Accessing the Sim101 account information from a backtest is possible, but not recommended as this will not relate to the strategy being tested.

    The help guide has example code of using Account.All.FirstOrDefault() to find an account by name.


    "Is it possible to somehow assign a risk template to the Backtest account?"

    The answer for this would be no, as the orders are not placed to an account.


    But you can create logic to control when a strategy stops trading. Below is a link to an example of a daily loss limit that may give you some ideas.
    Hello, I've updated the DailyLossLimit and DailyLosLimitMultiTrade examples that were posted on the forum for NinjaTrader 7 for NinjaTrader 8. These are often requested and I felt they are good examples to have for NT8. DailyLossLimitExample_NT7 - http://ninjatrader.com/support/forum...241#post451241 (http://ninjatrader
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Chelsea, for the confirmation that programmatic access to the Risk property is not officially supported.

      Is this code officially supported?

      Code:
      double initialMargin = 0;
      var acct = Account.All.Where(a => a.Name == "Sim101").FirstOrDefault();
      if (sim101 != null)
      {
          initialMargin = acct.GetAccountItem(AccountItem.InitialMargin, Currency.UsDollar).Value;
      }
      ​Specifically, is access to
      Code:
      Account.All
      supported. Is
      Code:
      acct.GetAccountItem(...)
      supported? Obviously, I would have to ensure that the Sim101 account available with a valid risk template assigned. Or do further checks.

      Aside from this, there are still workarounds which, for my purposes right now, would still be sufficient, including:
      • Accessing the Risk object and checking for null.
      • Making a copy of a risk template's XML file and parse it myself.
      But, if I can, I'd prefer to use supported code to minimize the likelihood of future failures.

      Thanks,

      Comment


        #4
        Hello BarzTrading,

        You can find an account object and use <Account>.Get(). That is supported. Is that what you are referring to?


        Note, the backtest orders are not going to affect that value.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          The following does not work for me in a chart backtest or in Strategy Analyzer. Is this expected? All I get are zeroes.

          Code:
          var acct = Account.All.Where(a => a.Name == "Sim101").FirstOrDefault();
          double imGetAccountItem = acct.GetAccountItem(AccountItem.InitialMargin, Currency.UsDollar).Value;
          double imGet = acct.Get(AccountItem.InitialMargin, Currency.UsDollar);
          Print(String.Format("{0}, GETACCTITEM: {1}, IM={2}", CurrentBar, acct.Name, imGetAccountItem));
          Print(String.Format("{0}, GET: {1}, IM={2}", CurrentBar, acct.Name, imGet));​
          The only way I can actually get the Initial Margin value is through the Risk property, which I understand is not supported.

          Is there no officially supported way to programmatically get the Initial Margin of an instrument in a backtest? Even when the Account has a risk template assigned?


          Comment


            #6
            Hello BarzTrading,

            The value returned should be the same value you see on the Accounts tab of the Control Center.
            https://ninjatrader.com/support/help...gHourTemplates
            https://ninjatrader.com/support/helpGuides/nt8/index.html?using_the_risk_window.htm#Understanding SessionDefinitions


            May I have a screenshot of the Accounts tab of the Control Center to show these are not matching the output window?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              I think I've conflated a couple of things: the initial margin property of an account with the initial margin for a single specific instrument. The value I'm interested in is the initial margin for a specific instrument. It is the value you see associated with the symbol when you look at the details of the Risk Template.

              I'm actually not interested in the initial margin property of the account. I understand now why <Account>.Get(...) was returning zero. It's not what I wanted.

              Attached Files

              Comment


                #8
                Hello BarzTrading,

                There is not a supported way of getting the risk template settings in NinjaScript. The open feature request tracking demand for this is tracked with ID # SFT-1645.

                I will add your vote to this request.

                Please note, we receive many requests and cannot reasonably implement all requested features or changes. Interest is tracked internally and if enough interest is tracked, it would be weighed against how feasible it would be to make those changes to consider implementing, so we cannot offer an ETA or promise of fulfillment.

                When new features are implemented, they will be listed in the Release Notes page of the Help Guide. The ID number may be different than the internal feature request tracking ID, but the description of the feature will let you know if that feature has been implemented.

                Release Notes - https://ninjatrader.com/support/help...ease_notes.htm


                There is a thread that has some undocumented / unsupported code you may find helpful.
                https://forum.ninjatrader.com/forum/...-to-a-strategy
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the help, Chelsea. I have several options I can work with. Thanks for adding my vote to SFT-1645.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by geddyisodin, 04-25-2024, 05:20 AM
                  8 responses
                  58 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by jxs_xrj, 01-12-2020, 09:49 AM
                  4 responses
                  3,285 views
                  1 like
                  Last Post jgualdronc  
                  Started by Option Whisperer, Today, 09:55 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post Option Whisperer  
                  Started by halgo_boulder, 04-20-2024, 08:44 AM
                  2 responses
                  22 views
                  0 likes
                  Last Post halgo_boulder  
                  Started by mishhh, 05-25-2010, 08:54 AM
                  19 responses
                  6,189 views
                  0 likes
                  Last Post rene69851  
                  Working...
                  X