Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

account PositionUpdate + any allready applied

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

    account PositionUpdate + any allready applied


    Hi everyone,

    I am looking at the SamplePositionDisplay indicator from the ecosystem by Jim,
    and I'd like to not have to choose an account in the properties.
    this way the values rendered would be of any account the chart is applied on, from the chart trader.

    I deleted the AccountName property.
    but I am challenged whith this :

    lock (Account.All)
    account = Account.All.FirstOrDefault(a => a.Name == AccountName);

    tried this :

    lock (Account.All)
    account = Account.All.FirstOrDefault(a => a.Name == "");

    but now the code is not finding any account obviously.
    How could we render the default account from the chart ?

    #2
    Hi Amedeus, thanks for writing in.

    There is an example here that gets the selected account in ChartTrader:


    The Account is set up in an initialization step of the SamplePositionDisplay script, so you will need to remove and re-add the indicator if you switch accounts. As an alternative you can create an Account selector and set up the Account object in State.Configure:
    Hi Ninja experts, I'm developing an indicator which will place trades using CreateOrder(). For this I need to choose an account for trade (like sim101). What I need is to add the account selector in the indicator parameter settings so that I can choose an account while applying indicator on a chart. I have tried the following


    Kind regards,
    -ChrisL

    Comment


      #3
      hey Chris ! thank you,

      it looks as such in State.DataLoaded :
      Code:
      ChartControl.Dispatcher.InvokeAsync((Action)(() =>
      {
      //You have to put the stuff below within this ChartControl.Dispatcher.InvokeAsync((Action)(() =>, because you are trying to access something on a different thread.
      accSelector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
      Print(accSelector.SelectedAccount.ToString());
      
      AccountName = accSelector.SelectedAccount.ToString();
      
      // Find our account
      lock (Account.All)
      account = Account.All.FirstOrDefault(a => a.Name == AccountName);
      
      // Subscribe to account item updates
      if (account != null)
      {
      account.AccountItemUpdate += OnAccountItemUpdate;
      account.PositionUpdate += OnPositionUpdate;
      
      foreach (Position pos in account.Positions)
      if (pos.Instrument == Instrument)
      position = pos;
      
      realizedPnL = account.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar);
      cashValue = account.Get(AccountItem.CashValue, Currency.UsDollar);
      }
      
      }));
      the print works perfectly, I had to move everything following inside the Dispatcher.InvokeAsync for it to render.

      We can switch sim accounts and reload the scripts, without the remove/re-add alternative, and the rendering renders, wich is cool.

      But when selecting the live account, only the print works, not the rendering... Is this expected ?
      Last edited by Amedeus; 04-01-2022, 03:02 PM.

      Comment


        #4
        more precisely : with the live account it does render, but without any values.

        I moved it in Configure too, there was no change in behavior.
        Last edited by Amedeus; 04-01-2022, 03:16 PM.

        Comment


          #5
          Hi Amedeus, the Account object is likely null due to the name being incorrect. It is possible that the DisplayName and the Name property of the live account are different. What do you see when you print out:
          for(int i = 0; i < Account.All.Count; i++)
          Print(Account.All[i]);

          Im afraid the Account selector example will not work if the DisplayName is different than the name that prints out from Account.All. You would need to set up an Enum public property and fill it with all the account names:
          Last edited by NinjaTrader_ChrisL; 04-01-2022, 03:19 PM.

          Comment


            #6
            thanks Chris,

            yes the Display name is different than the print.

            You would need to set up an Enum public property and fill it with all the account names
            Does this not mean having the user choose the account in the properties, exactly like initially in the SamplePositionDisplay indicator ?
            Last edited by Amedeus; 04-01-2022, 04:35 PM.

            Comment


              #7
              Hi Amedeus, I just tested for myself and I realized the code is checking a.Name == AccountName not "a.DisplayName" so this should work properly. If you print out the AccountName string before initializing it you should get the correct name to then identify it in Account.All.

              Comment


                #8
                hey Chris, thank you,

                yes it does print the live AccountName.
                Yet it does not render it, nor its values... unlike sim accounts (as soon as I switch account on chart trader, then reload the script).

                Have you tested positive with a live account ? or just assumed it should, since the print works ?

                (edit) when I chose the live account in the parameters of the initial SamplePositionDisplay, it did render the values... that makes it more of a mystery : should work.
                Last edited by Amedeus; 04-04-2022, 10:58 AM.

                Comment


                  #9
                  Hi Amedeus, I did a test with prints and I made sure the live account was not null. I would need you to post a reduced version of your script so I can have a look at it.

                  Kind regards,
                  -ChrisL

                  Comment


                    #10
                    Hey Chris, thanks a lot again,

                    here you go, have a look a this.
                    on my end, this does render any sim account (I tested with 2) with their values.
                    however, it does render the live account without any values.

                    is it the same with you ?
                    prints are ready.
                    Attached Files

                    Comment


                      #11
                      Hi Amedeus, you will need to use either the Account selector property that I made or do some processing on the Account string. If you detect the account does not start with "Sim" or "Replay" (using String.StartsWith()) then iterate the Account.All list and find the corresponding account. The code that is accessing the account name through the Chart Trader module is showing the DisplayName of the account.

                      Kind regards,
                      -ChrisL

                      Comment


                        #12

                        hey Chris, thank you very much.

                        I kinda understand what you suggest with processing the accounts, but I did not understand why the code below ...
                        Code:
                        accSelector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
                        AccountName = accSelector.SelectedAccount.ToString();
                        Print("AccountName post selector : " + AccountName );
                        ... would print the right live account number (not DisplayName), but not allow the script to render its values.
                        Why process all accounts if the selector does its job ?

                        It happens that in the script, after the selector, we still were using the example from the HelpGuide and the PisitionDisplay indicator : this syntax
                        Code:
                        // Find our account
                        lock (Account.All)
                        account = Account.All.FirstOrDefault(a => a.Name == AccountName);
                        Is this not re-processing Account.All, thus re-allocating AccountName ? just erasing the selectors jobs...


                        if we change the code above for the code below, the script works as planned
                        Code:
                        lock (accSelector.SelectedAccount)
                        account = accSelector.SelectedAccount;

                        here is the full snippet allowing the script to choose from what's on the chart, just to have it in one post.
                        Code:
                        ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                        {
                        //You have to put the stuff below within this ChartControl.Dispatcher.InvokeAsync((Action)(() =>, because you are trying to access something on a different thread.
                        accSelector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
                        
                        lock (accSelector.SelectedAccount)
                        account = accSelector.SelectedAccount;
                        
                        // Subscribe to account item updates
                        if (account != null)
                        {
                        account.AccountItemUpdate += OnAccountItemUpdate;
                        account.PositionUpdate += OnPositionUpdate;
                        }
                        }));
                        please let us know if you see something wrong here (especially with : lock (accSelector.SelectedAccount)), my unexpert eye may not. If not, then it's all good, problem solved!

                        Thank you again and have a great day.

                        Comment


                          #13
                          Hi Amedeus, The code that gets the account name directly from ChartTrader is seeing the DisplayName only. If you try out the Account selector property you will see that the "Name" property will be shown, not the DisplayName.

                          Comment


                            #14

                            hey Chris,

                            thanks for the precision, I need it.

                            I still dont understand why this below makes account be null :
                            (hiding behind the hand gesture, lol)

                            Code:
                            // unmentionned is us finding our targetted account with the selector wich would print the Name
                            // then we assign the Name of the Account to AccountName -> we skipped any relation with ChartTrader thanks to accSelector
                            AccountName = accSelector.SelectedAccount.ToString();
                            lock (Account.All)
                            // here we do not mention anything coming from ChartTrader and choose an Account from Account.All with the name given by accSelector
                            account = Account.All.FirstOrDefault(a => a.Name == AccountName);
                            // yet if we were to print account, it would return null...
                            I understand your pointing out, I can see that account is null.
                            but where did we ask ChartTrader anything ?

                            Comment


                              #15
                              Hi Amedeus, the code "accSelector = Window.GetWindow(ChartControl.Parent)..." is getting the ChartTrader Account selector, which gets the DisplayName property. The actual AccountSelector property will directly get the Account.Name property. Please compare the two examples I linked in post #2 it will show you the difference I am speaking of. The only reason an Account object will be null after using the Account.All.FirstOrDefault command the name is wrong, so printing out the name will explain this.

                              Kind regards,
                              -ChrisL

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              627 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              359 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              562 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              568 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X