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

Displaying account equity and margin in Market Analyzer

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

    Displaying account equity and margin in Market Analyzer

    To make the Market Analyzer display a truely comprehensive overview of my trading positions, I'd like to see my account equity and margin on their too.

    That would make Market Analyzer all I need to have on the screen when I'm not actively working on it. Currently I always have to grab the mouse or the keyboard and change windows to check up on it.

    I can see that I could put an extra column in there with the account equity showing in every row, but that's a really naff way of doing it.

    #2
    Thanks for the feedback adamus - from a strategy you could grab those AccountItems in the link and perhaps share those via text files with an indicator running in the MA?

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Just as another column?

      I'd love for them to be in the Total line. Don't suppose that's possible, right? How could I put an indicator in there where all the instruments add up to give the account equity? Impossible.

      Comment


        #4
        Yes, as a column, you could look into coding your own type for this task (see the bin .> custom > MA folder) but unfortunately this is not supported by us.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I just tried writing a Market Analyzer column script and the lack of documentation means I would have to play around for ages to work out what it does. Since you don't support it, wouldn't it be easier to write an indicator for this instead?

          Maybe if a NinjaTrader user knows who can help, I'd appreciate it.

          This time instead of trying to show the account equity, I want to show the nearest stop order to the market. I have the following, but it doesn't show any values:

          Code:
          namespace NinjaTrader.MarketAnalyzer
          {
              public class StopOrder : NinjaTrader.MarketAnalyzer.Column
              {
                  private string accountName = Connection.SimulationAccountName;
                  // holds the position for the actual instrument
                  private    Position position = null;
                  private Order stopOrder = null;
          
                  protected override void Initialize()
                  {
                      CalculateOnBarCloseConfigurable    = false;
                      DataType = typeof(double);
                      RequiresBars = true;
                      ShowInTotalRow = false;
                      Value = 0;
                  }
          
                  protected override void OnConnectionStatus(ConnectionStatusEventArgs e)
                  {
                      if (e.Status == ConnectionStatus.Connected 
                          || e.OldStatus == ConnectionStatus.Connecting)
                      {
                          lock (e.Connection.Accounts)
                              foreach (Account account in e.Connection.Accounts)
                                  if (account.Name == AccountName)
                                      lock (account.Positions)
                                          foreach (Position positionTmp in account.Positions)
                                              if (positionTmp.Instrument.IsEqual(
                                                          Instrument))
                                                  position = positionTmp;
                      }
                      else if (e.Status == ConnectionStatus.Disconnected)
                      {
                          if (position != null && position.Account.Connection == e.Connection)
                          {
                              position = null;
                              stopOrder = null;
                              Value = 0;
                          }
                      }
                  }
          
                  protected override void OnPositionUpdate(Cbi.PositionUpdateEventArgs e)
                  {
                      if (e.Position.Account.Name == AccountName 
                          && e.Position.Instrument.IsEqual(Instrument))
                      {
                          position = null;
                          stopOrder = null;
                          Value = 0;
                          if (e.Operation != Operation.Remove)
                          {
                              position = e.Position;
                              lock (e.Position.Account.Orders)
                              {
                                  foreach (Order order in e.Position.Account.Orders)
                                      if (order.Account.Name == AccountName 
                                          && order.Instrument.IsEqual(Instrument)
                                          && order.OrderState != OrderState.Cancelled
                                          && order.OrderState != OrderState.Filled
                                          && order.OrderState != OrderState.Rejected
                                          && order.OrderType == OrderType.Stop
                                          && (order.OrderAction == OrderAction.BuyToCover
                                          || order.OrderAction == OrderAction.Sell))
                                      {
                                          stopOrder = order;
                                          Value = stopOrder.StopPrice;
                                          return;
                                      }
                              }
                          }
                      }
                  }
                  
                  protected override void OnBarUpdate()
                  {
                      if (stopOrder != null)
                          Value = stopOrder.StopPrice;
                  }
          
                  #region Properties
                  /// <summary>
                  /// </summary>
                  [Description("Selected account")]
                  [GridCategory("Parameters")]
                  [Gui.Design.DisplayName("Selected account")]
                  [TypeConverter(typeof(Gui.Design.AccountNameConverter))]
                  public string AccountName
                  {
                      get { return accountName; }
                      set { accountName = value; }
                  }
                  #endregion
          
              }
          }
          I notice also that the Market Analyzer RealizedProfitAndLoss column does pick up recent realized PnL - presumably only 'realizations' that happen since the column was initialized?

          Comment


            #6
            Yes, an indicator would be an option as well - the realized PnL is tied to the account, so it take into consideration previous trades from the current session as well.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Hi Bertrand,
              thanks for that.

              The first problem I have is accessing the account object. I can't see how you do it. It doesn't look like anyone else is doing it either on the forum.

              Comment


                #8
                code example to get Account

                I thought I posted this yesterday but there's no trace of the message.

                I am trying to get the Account in my indicator so that I can access orders and positions in my indicator. I've put a parameter on the indicator so I can specify which account name I want (which is the text type accountName variable)

                Code:
                [FONT=Courier New]private void findStopOrder()
                {
                    if (Bars.MarketData != null)
                        lock (Bars.MarketData.Connection.Accounts)
                            foreach (Account account in Bars.MarketData.Connection.Accounts)
                                if (account.Name == accountName)
                                    lock (account.Orders)
                                        foreach (Order order in account.Orders)
                                            if (order.OrderState != OrderState.Cancelled
                                                && order.OrderState != OrderState.Filled
                                                && order.OrderState != OrderState.Rejected
                                                && order.OrderType == OrderType.Stop
                                                && (order.OrderAction == OrderAction.BuyToCover
                                                    && (stopOrder == null 
                                                        || order.StopPrice < stopOrder.StopPrice)
                                                || (order.OrderAction == OrderAction.Sell
                                                    && (stopOrder == null 
                                                        || order.StopPrice > stopOrder.StopPrice)
                                                )))
                                                stopOrder = order;
                }
                [/FONT]
                What I want to do is grab the Account and keep it but I am worried about it getting out of sync and losing the correct reference is I disconnect and reconnect, for instance.

                Is there a method to check if the Account is still valid or if my variable needs refreshing?

                Comment


                  #9
                  adamus,

                  Unfortunately we are not able to support this. Grabbing anything for accounts is not supported, especially from an indicator.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Josh,

                    one thing I need to code is a strategy that doesn't trade, but merely does a sanity check that the positions and orders on the markets I follow are logical for the strategies running.

                    Today for instance after a connection loss lasting an hour, one of my strategies reversed in theory but restarting the strategy and checking it in the middle of the night, I failed to notice that the long position now had buy exit orders against it because I hadn't manually reversed the position.

                    With multiple strategies on multiple markets, my manual checking is quickly going to become a massive liability.

                    Yet without documentation and support for how to code against the NinjaTrader API to manage the account, it's going make the whole programming really difficult.

                    Is there no way you can help on this? What about paid support?

                    Comment


                      #11
                      adamus,

                      Checking against the account is unfortunately not supported. You could try contacting some NinjaScript Consultants though. Have you considered using the "Sync account position" option?
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        account value in an indicator

                        Is it really necessary to use a Consultant just to get a value??
                        That seems perverse when it's so easy in a strategy.
                        Is it possible to make GetAccountValue() method open to indicators?

                        Comment


                          #13
                          Thanks for the provided feedback Mindset, we will add to our list.
                          BertrandNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by alifarahani, Today, 09:40 AM
                          6 responses
                          36 views
                          0 likes
                          Last Post alifarahani  
                          Started by Waxavi, Today, 02:10 AM
                          1 response
                          17 views
                          0 likes
                          Last Post NinjaTrader_LuisH  
                          Started by Kaledus, Today, 01:29 PM
                          5 responses
                          14 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by Waxavi, Today, 02:00 AM
                          1 response
                          12 views
                          0 likes
                          Last Post NinjaTrader_LuisH  
                          Started by gentlebenthebear, Today, 01:30 AM
                          3 responses
                          17 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X