Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Q on how to get account data (positions) in an indicator

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

    Q on how to get account data (positions) in an indicator

    Hi NT,

    I am simply trying to create an indicator that prints the number of contracts I have in position. I've tried the two methods attached, and get the shown error messages, can you please help guide me?​
    Attached Files

    #2
    Hi NT,

    I am simply trying to create an indicator that prints the number of contracts I have in position. I've tried the two methods attached, and get the shown error messages, can you please help guide me?​

    Comment


      #3
      Hello Austiner87,

      You can find a sample that shows how to locate an account and subscribe to its events here:

      Comment


        #4
        Ok, I believe I am close/much closer, thank you, can you please help me with the called out code lines - they print nothing, but I think one of them should be printing the count/quantity of positions
        Attached Files

        Comment


          #5
          Hello Austiner87,

          When you say that they print nothing do you mean its showing 0 or you literally see no prints? If no prints are happening that means that code was not reached and not called. If there are no positions your second print should not happen because the loop won't loop over anything.

          Comment


            #6
            Hi NT,

            I am getting no prints at all, meaning that the code was not reached? but it appears the account data is bring subscribed to correctly:
            Attached Files

            Comment


              #7
              Hello Austiner87,

              You are getting prints based on your image but you have the same text in both of your prints so I don't know which that is. I would suggest trying the sample from the help guide, I see that working on my end.

              Code:
              if (State == State.DataLoaded)
              {
              
                  var myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
              
              
                 Print("Positions in State.DataLoaded:");
              
                 foreach (Position position in myAccount.Positions)
                 {
                    Print(String.Format("Position: {0} at {1} {2}", position.MarketPosition, position.AveragePrice, position.Quantity));
                 }
              
              }

              Comment


                #8
                Hi NT,

                I am still having trouble - I have matched your code exactly and it doesn't print.
                Attached Files

                Comment


                  #9
                  Hello Austiner87,

                  The print you are seeing is just the print that lets you know DataLoaded was called. You are not getting any prints from the loop meaning the count of the positions was 0. Do you have a realtime connection connected and are in a position on the sim account while testing this?

                  Comment


                    #10
                    Ok, the code you gave does work, but it only prints it once, and doesn't appear to do it on bar update, below.

                    Also, the reason I am doing this is so that I can reference the number of contracts in the Alerts menu, but I can't seem to do that? Having trouble plotting the Contracts to a Plot to reference in the Alerts - can you help? I promise I am trying a lot of things before responding back!
                    Attached Files

                    Comment


                      #11
                      Hello Austiner87,

                      If you wanted to do this from OnBarUpdate you would still find the account in OnStateChange and save it to a varaibe like the original code you had. The code I provided was jus a simple test from the help guide which has different text in the print inside the loop so you can easily see if that loop was being looped over.

                      You can remove the line
                      Code:
                      var myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
                      ​from OnBarUpdate. In OnStateChange it would be:

                      Code:
                      lock(Account.All)
                          myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");

                      To plot a value you also need to use the correct syntax, you just made an int variable and the builder won't be able to use that to get a value. You can see the following page which shows how to set a plot to a value.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      44 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      124 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      65 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      42 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      46 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X