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

Getting AccountItem Values from my custom Strategy

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

    Getting AccountItem Values from my custom Strategy

    I tried to get the AccountItem Values for my live account with Rithmic without success. The CashValue does not seem to be up to date and the different kinds of Margins return zero. Why is this so?

    #2
    Hello mbesha,
    Thanks for your post.

    Do you see these values on the Accounts tab of your Control Center?

    Can we see your code where you are trying to access those account values?
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Hi JoshG

      I can only see the Cash Value on the Accounts tab.

      My code is a simple modification of what is in the HelpGuide and the relevant part is as follows:


      public class MarginChecker : Strategy
      {
      #region Variables
      private Account myAccount;
      #endregion

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"checks the margin values of the account.";
      Name = "MarginChecker";
      Calculate = Calculate.OnPriceChange;
      EntriesPerDirection = 20; //Default Position Limits from NT
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = false;
      ExitOnSessionCloseSeconds = 0;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 0;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;

      // Find live account
      lock (Account.All)
      myAccount = Account.All.FirstOrDefault(a => a.Name == "XXXXXXX");

      // Subscribe to static events. Remember to unsubscribe with -= when you are done
      Account.AccountStatusUpdate += OnAccountStatusUpdate;

      if (myAccount != null)
      {
      // Print some information about our account using the AccountItem indexer
      Print("STATE_DEFAULTS-------------------------------------------");
      Print("" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString());
      Print("Account Name: " + myAccount.Name);
      Print("Item 1: " + myAccount.Get(AccountItem.BuyingPower, Currency.UsDollar));
      Print("Item 2: " + myAccount.Get(AccountItem.CashValue, Currency.UsDollar));
      Print("Item 3: " + myAccount.Get(AccountItem.NetLiquidation, Currency.UsDollar));
      Print("Item 4: " + myAccount.Get(AccountItem.NetLiquidationByCurrency , Currency.UsDollar));
      Print("Item 5: " + myAccount.Get(AccountItem.PositionMargin, Currency.UsDollar));
      Print("Item 6: " + myAccount.Get(AccountItem.TotalCashBalance, Currency.UsDollar));
      Print("Item 7: " + myAccount.Get(AccountItem.ExcessInitialMargin, Currency.UsDollar));
      Print("Item 8: " + myAccount.Get(AccountItem.ExcessPositionMargin, Currency.UsDollar));
      Print("-------------------------------------------");

      // Subscribe to events. Remember to unsubscribe with -= when you are done
      myAccount.AccountItemUpdate += OnAccountItemUpdate;
      myAccount.OrderUpdate += OnOrderUpdate;
      }
      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.Terminated)
      {
      // Unsubscribe to events
      myAccount.AccountItemUpdate -= OnAccountItemUpdate;
      myAccount.OrderUpdate -= OnOrderUpdate;

      Account.AccountStatusUpdate -= OnAccountStatusUpdate;
      }
      }

      Comment


        #4
        mbesha,

        If you are not getting the different Margin values on your Accounts tab I would not expect to get them through NinjaScript either.

        In regards to the CashValue that you see in your Print() -- that would only get called once inside State.SetDefaults, so I would not expect it to stay in sync. You would need to use OnBarUpdate() or any of the other event driven methods to get a more up to date CashValue.
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Hi JoshG

          With regard to the Margin values, are there any plans to have them included in future releases?

          As for the Cash Value, it is not a simple matter of today's value being different from yesterday's value for instance. The value I get through NinjaScript is weeks if not months off. The Accounts Tab shows the latest value but whatever value is coming through NinjaScript is totally different.

          Comment


            #6
            mbesha,

            With regard to the Margin values, are there any plans to have them included in future releases?
            Like the Accounts tab, the Margin Values are already accessible from within the platform if your broker supports them.


            The value I get through NinjaScript is weeks if not months off.
            Let me know if relocating your CashValue print to OnBarUpdate() does not give you something more expected.
            Josh G.NinjaTrader Customer Service

            Comment


              #7
              Hi JoshG

              I take it then that Rithmic does not support these Margin values.

              With regard to the Cash Value, there is no difference even when I print using OnBarUpdate().

              Comment


                #8
                mbesha,

                Yeh, I suspect Rithmic does not support that information if you do not see it on your Accounts tab.

                With regard to the Cash Value, there is no difference even when I print using OnBarUpdate().
                I'm able to see a difference when I Print that value from OnBarUpdate() in my strategy. Can we see your OnBarUpdate() code that does not update with the current CashValue?
                Josh G.NinjaTrader Customer Service

                Comment


                  #9
                  Hi JoshG

                  This is my code for OnBarUpdate()

                  protected override void OnBarUpdate()
                  {
                  //Add your custom strategy logic here.
                  if(State != State.Realtime || myAccount == null)
                  return;

                  //For printing out the Cash Value
                  Print("ONBARUPDATE-------------------------------------------");
                  Print("" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString());
                  Print("Account Name: " + myAccount.Name);
                  Print("Cash Value: " + myAccount.Get(AccountItem.CashValue, Currency.UsDollar));
                  }

                  Comment


                    #10
                    mbesha,

                    I do not see anything that sticks out as an error. Something is not correct though here if the prints for CashValue are not updating. Are there errors on the Logs tab of your Control Center?

                    I was able to get the following snippet to lock onto my SIM101 account and print the cash value as it changed. Please modify this and see if it works with your live account.

                    Code:
                    public class SampleStrat : Strategy
                    {
                        private Account myAccount;
                        protected override void OnStateChange()
                        {
                            if (State == State.SetDefaults)
                            {
                                Calculate = Calculate.OnPriceChange;
                                lock (Account.All)
                                      myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
                            }
                        }
                        protected override void OnBarUpdate()
                        {            
                            Print("Cash Value: " + myAccount.Get(AccountItem.CashValue, Currency.UsDollar));
                        }
                    }
                    Josh G.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi JoshG

                      There are no errors in the logs. The code you recommended still yields the same results with my live account. I have a feeling this could be an issue specific to a Rithmic connection to a live account.

                      Comment


                        #12
                        mbesha,

                        It looks like you should expect to see CashValue, Relaized PnL, and Unrealized PnL while connected to a Rithmic account. If you want to see what all would be expected for account values please see the grid of Account values by data provider in the following link:

                        Josh G.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi JoshG

                          As you mentioned, I should expect to see those values but unfortunately I am not. And if the Ninjascript is in order, what then could be the problem? Could the fact that I use another trading platform to place and sometimes manage my orders be a factor here? Please help me to get to the bottom of this.

                          Comment


                            #14
                            mbesha,

                            Managing your orders in multiple platforms can cause all kinds of weird things to happen so that could certainly be it. Does this occur when you place an order from NinjaTrader and ONLY manage the order from NinjaTrader?
                            Josh G.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by trilliantrader, Today, 08:16 AM
                            0 responses
                            3 views
                            0 likes
                            Last Post trilliantrader  
                            Started by AttiM, 02-14-2024, 05:20 PM
                            9 responses
                            174 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by funk10101, Today, 08:14 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post funk10101  
                            Started by adeelshahzad, Today, 03:54 AM
                            1 response
                            13 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by RookieTrader, Today, 07:41 AM
                            1 response
                            6 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Working...
                            X