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

account information and updating account balance

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

    account information and updating account balance

    I am trying to collect information on my account I was wondering how to get this information. This is what I have done;

    private void OnExecutionUpdate(object sender, ExecutionEventArgs e)
    {
    Print( e.Time);
    Print(e.Quantity);
    Print(e.Price);
    Print(e.MarketPosition);
    Print(e.Operation);
    Print(e.OrderId);
    Print(e.IsSod);
    Print(e.StatementDate );
    Print(account.Get(AccountItem.CashValue,Currency.U sDollar));
    }

    The picture below will give the correct information, but the account balance is the same.
    the start account balance and the end account balance are equal.
    when I start a new trade it will show the updated price.

    How do I get the updated account balance when closing a trade?

    I am planning to have a trade recorder for myself for analyisis
    Attached Files

    #2
    Thank you for your question. Since OnExecutionUpdate only updates during executions, I would recommend using the AccountItemUpdate and PositionUpdate feeds to instead update when your Account and Position information change, respectively. Attached is a code sample that should do something like what you are describing. Code samples we provide are for educational purposes, and are not intended for live trading, and are not guaranteed to accomplish any user goal or to be maintained.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the example.

      I do have a last question on the event Handlers?

      I used the example code and I want to know why Account Update prints out 2 times.

      Secondly Here is the State Code

      if (State == State.Configure)
      {
      bDidConfigure = true;
      lock (Account.All)
      {
      account = Account.All.FirstOrDefault(a => a.Name == AccountName);

      }

      if (account != null)
      {
      Print("in account executrion");
      account.ExecutionUpdate += OnExecutionUpdate;
      account.PositionUpdate += OnPositionUpdate;
      account.AccountItemUpdate += OnAccountItemUpdate;

      }

      }
      else if (State == State.Terminated)
      {
      account.ExecutionUpdate -= OnExecutionUpdate;
      account.PositionUpdate -= OnPositionUpdate;
      account.AccountItemUpdate -= OnAccountItemUpdate;
      }


      Why would I get an object error?

      The following 2 pictures are given.

      Is there a way to check how many event handlers are activated and what type of event handler are running in the indicator?
      Attached Files

      Comment


        #4
        The answer to both your questions have to do with the state machine, and realtime events occurring.

        With respect to the 2nd picture, in my code example I ended up needing to do these three things to avoid the message you saw :

        Code:
        private bool DidConfigure;
        Code:
        else if (State == State.Configure)
        {
            // ...
            DidConfigure = true;
        Code:
        else if (State == State.Terminated && DidConfigure)
        In other words, I check to make sure we reached State.Configure before handling anything in State.Terminated. This is because State.Terminated gets reached when you select a NinjaScript item in, for example, an Indicator or Strategy selection screen, even if you don't actually add or enable the indicator or strategy. So in your code, there's a path where
        • State.Configure is never reached
        • State.Terminated is reached
        • Therefore your account object is never set up.


        This is also why you see several things printed twice (but see below for Realized PnL)

        We can avoid both of these problems by decorating the getter for the strategy's single Sim101 instance. We can leave the teardown code exactly the same. This will force exactly one subscription (thus preventing most double messages), and prevent Sim101 from ever being null.

        All that said, you will still see 2 listings for RealizedPnL. This is because 2 account update events are actually occurring. I will submit another example in my next post where I limit us to one update message even though 2 events are actually occurring.
        Attached Files
        Last edited by NinjaTrader_JessicaP; 09-25-2018, 11:15 AM.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          With all the above in mind, the attached script uses a simple state machine to filter down to one RPnL message per position change
          • A switch PrintFirstUpdate is initialized to false
          • This switch is reset to false every Position update
          • Printing the first RPnL update sets this switch to true until the next Position update

          Please let us know if there are any other ways we can help.
          Attached Files
          Jessica P.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by gentlebenthebear, Today, 01:30 AM
          3 responses
          14 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by PhillT, Today, 02:16 PM
          2 responses
          6 views
          0 likes
          Last Post PhillT
          by PhillT
           
          Started by Kaledus, Today, 01:29 PM
          3 responses
          10 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by frankthearm, Yesterday, 09:08 AM
          14 responses
          47 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by PaulMohn, Today, 12:36 PM
          2 responses
          17 views
          0 likes
          Last Post PaulMohn  
          Working...
          X