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

how to get account balance

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

    how to get account balance

    I was wondering in NT8 how to get the account balance. before executing my trade

    #2
    Hello,

    Thank you for the question.

    From an Indicator, you would need to first get a reference to an account as indicators are not associated with an account at all. After that, you could use the Account methods such as Get to access AccountItems.

    The general syntax for Get would be the following:

    Code:
    Account a = Account.All.First(t => t.Name == "Sim101");
    double value = a.Get(AccountItem.CashValue, Currency.UsDollar);
    Print(value);
    If you need to access an Account from an indicator, I would suggest reviewing the Addon samples and helpguide for further details: https://ninjatrader.com/support/help...ount_class.htm

    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Code:
                  
              protected override void OnBarUpdate()
              {
                  Account a = Account.All.First(t => t.Name == "Sim101");
                  double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar);            
                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= Balance*0.015)            
                  {
                      ExitLong(0,10000,"EURUSD","");
                      ExitShort(1,10000,"GBPUSD","");                    
                  }  
              }
      I'm working on a multi-instrument strategy that would close all positions whenever
      Unrealized Profit >= 1.5% of Account Balance
      For some reason, the comparison only works the first time.
      After it closes less than 1.5% of Account Balance?
      Last edited by johnnybegoode; 12-13-2018, 07:20 PM.

      Comment


        #4
        Hello johnnybegoode,

        From what you provided I don't see anything specific however you mentioned multi-instrument so you may need to instead use the Positions object instead of Position so you can access the multiple instruments positions. You may also need to use more conditions here to determine which BarsInProgress is running the code. We have some samples of using BarsInProgress in the help guide along with using the Positions object. I will link them below.

        I would also likely suggest using a Print to gain a better understanding of the problem.
        Code:
               protected override void OnBarUpdate()
                {
                    Account a = Account.All.First(t => t.Name == "Sim101");
                    double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar);                        
                    double unrealized = Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]);
        
                    Print(BarsInProgress + " " + unrealized + " >= " + Balance*0.015);
        
                    if (unrealized >= Balance*0.015)            
                    {
                        ExitLong(0,10000,"EURUSD","");
                        ExitShort(1,10000,"GBPUSD","");                    
                    }  
                }



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I think

          Code:
          [LIST][*][B][B](PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])[/B][/B][/LIST]
          does not work for multi-instrument PnL
          It does not look at overall instrument PnL,
          but only at either one of the instrument.
          How do I get PnL of all open positions (multiple-instruments)?
          Last edited by johnnybegoode; 12-14-2018, 08:02 PM.

          Comment


            #6
            Code:
            if (BarsInProgress == 0)
                        {                    
                        if (
                            (
                            (PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])
                            +PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[1]))
                            >= ProfitTarget
                            )
                            )            
                        {
            Q: I added (BarsInProgress == 0)
            Am I right that
            Close[0] is looking at EURUSD
            Close[1] is looking at GBPUSD

            and that is no Bars Ago.
            Last edited by johnnybegoode; 12-16-2018, 02:55 AM.

            Comment


              #7
              Hello johnnybegoode,

              You are correct, PositionAccount does not represent multiple instruments you would need to instead use PositionsAccount. This is noted in the help guide page for PositionAccount, it notes you would need to use PositionsAccount (plural PositionS) for multi instruments. This holds true for many objects in NinjaScript such as Close Open High Low Position, there are plural versions of each of these items which instead apply toward multi-series scripts: Closes Opens Highs Lows Positions AccountPositions etc..



              Regarding your next post, this also comes back to the plural usage of the series. You have used Close (not plural) and supplied a BarsAgo [0] and [1]. If you are referring to a specific series, you instead would use the Plural series which also has an index for the BarsInProgress: Closes[0][0] and Closes[1][0]


              Closes[0][0] EURUSD
              Closes[1][0] GBPUSD

              You can find more of the specifics like this in the following page which outlines some of the common multi-series syntaxes you will use:




              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Q1: PositionsAccount does not have the method "GetUnrealizedProfitLoss()"

                So I tried

                Code:
                if (BarsInProgress == 0)
                            {                    
                            if (
                                (
                                (PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[0][0])
                                +PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[1][0]))
                                >= ProfitTarget
                                )
                                )            
                            {
                but it does not close.

                Q2: Indicators

                Code:
                            else if (State == State.DataLoaded)
                            {                
                                RSI1                = RSI(Closes[0][0], 14, 3);  // for EURUSD
                                RSI2                = RSI(Closes[1][0], 14, 3);  // for GBPUSD
                            }
                Error (Above): CS1502 and CS1503

                Code:
                            else if (State == State.DataLoaded)
                {
                RSI1 = RSI(BarsArray[0], 14, 3); // for EURUSD
                RSI2 = RSI(BarsArray[1], 14, 3); // for GBPUSD
                }
                Does using BarsArray works?
                Does that mean I could not specific Close, Open, High, Low etc?


                ....

                Code:
                        protected override void OnBarUpdate()
                        {
                            if (State != State.Realtime)
                                return;
                             // Set 1
                            if ((RSI1.Avg[0] > 70)     // for EURUSD
                                 && (RSI2.Avg[0] < 30) // for GBPUSD
                                 && (BarsInProgress == 0))
                This should looks ok right?
                Last edited by johnnybegoode; 12-18-2018, 01:09 AM.

                Comment


                  #9
                  Hello johnnybegoode,

                  Q1: PositionsAccount does not have the method "GetUnrealizedProfitLoss()"
                  It does, how were you trying to use it? You could refer to the help guide for a sample on how to access the position object while using PositionsAccount which would then have the same methods as the PositionAccount or Position objects:

                  Code:
                  PositionsAccount[1].GetUnrealizedProfitLoss()

                  Q2: Indicators
                  else if (State == State.DataLoaded)
                  {
                  RSI1 = RSI(Closes[0][0], 14, 3); // for EURUSD
                  RSI2 = RSI(Closes[1][0], 14, 3); // for GBPUSD
                  }
                  You are trying to supply a price to the indicator here but that is not what the indicator would need for input, indicators take a Series. This is another item I would suggest reviewing in the help guide in the link below. I had touched on this in some detail in the last post about the difference between the plural and non-plural object naming, when using a multi-instrument series the double brackets make how you use the indicator different. You would need to remove the second bracket with the BarsAgo so you supply just the series.



                  Code:
                  RSI1 = RSI(Closes[0], 14, 3); // for EURUSD
                  RSI2 = RSI(Closes[1], 14, 3); // for GBPUSD
                  You can also use BarsArray, you are just simply not supplying the correct object in your first example using Closes. You can use either of these it would be up to your design.


                  The syntax you provided in general looks correct, but this would be a question that you should determine the answer to yourself by confirming the results. You can use Prints here to Print the values used in the condition to confirm it works as you are expecting.


                  Code:
                  Print(RSI1.Avg[0] + " &gt; 70 " +  RSI2.Avg[0] + " &lt; 30 BarsInProgress: " + BarsInProgress);
                  if ((RSI1.Avg[0] &gt; 70)     // for EURUSD
                                   &amp;&amp; (RSI2.Avg[0] &lt; 30) // for GBPUSD
                                   &amp;&amp; (BarsInProgress == 0))
                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    I went with

                    Code:
                        if (                
                                    (
                                    (PositionsAccount[0].GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[0][0])
                                     +PositionsAccount[1].GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[1][0]))
                                    >= ProfitTarget
                                    ) 
                    
                                    )            
                                 {    
                                         PositionsAccount[1].Close();                        
                                         PositionsAccount[0].Close();                                             
                                 }
                    How do I close all and have the strategy continue to run?
                    It stop placing orders after closing.
                    *If I execute PositionsAccount[0] first, it stops before closing PositionsAccount[1].

                    Code:
                    [B][B]ExitLong(0,10000,"EURUSD","");[/B][/B]
                    [B][B]ExitShort(1,10000,"GBPUSD","");[/B][/B]
                    Exit would continue to run but I was looking for a universal close all positions regardless.

                    Comment


                      #11
                      Hello johnnybegoode,

                      In this case that would be expected if you are using the Position object to close the overall position directly as that will disable the script.

                      To exit the position, you could use an exit order with the Position.Quantity as one way of doing this. Generally, you would need to check the direction of your position and submit the correct order to exit that position.

                      Code:
                      if(Position.MarketPosition == MarketPosition.Long)
                      {
                           ExitLong(Position.Quantity);
                      }
                      I look forward to being of further assistance.



                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Under what circumstances do somebody needs to use PostionAccount over Position,
                        the description on the reference is somewhat ambiguous.

                        Position
                        Represents position related information that pertains to an instance of a strategy.

                        PostionAccount
                        Represents position related information that pertains to real-world account (live or simulation).

                        Comment


                          #13
                          Hello johnnybegoode,

                          This would really be dependent on your logic. These two objects represent two separate positions which are your account position and the position of a single instance of a strategy.

                          Your account position does not necessarily need to match the strategies position, you could have an Account Position of 2 long, but you have two separate strategies which put it in that position and each strategies Position would be 1 long. This is generally used for advanced use cases where the strategy needs to track the live account position which may differ from its virtual position.

                          Strategies are not aware of the actual account position by default or what other strategies are doing in their virtual performance. In advanced scenarios how you see fit, this can be used to access the actual account position if your strategy were to require that type of logic. Generally using the Position object is used as it references this strategies performance opposed to the overall account position which is cumulative.

                          I look forward to being of further assistance.
                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            I'm trying to create a strategy that adjusts its order quantity based on the Balance, but the balance
                            does not update and remains at its initial value of 1000
                            as seen on the attached output screenshot.


                            Code:
                                    protected override void OnBarUpdate()
                                    {        
                                         Account a = Account.All.First(t => t.Name == "Sim101");
                                        double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar);
                                        double QuantityRisk = 0.1;
                                        double InitialDeposit = 1000;
                                        double QuantityD = (Balance * (QuantityRisk/100)/InitialDeposit*100);
                                        double QuantityE = (Math.Round(QuantityD, 2));
                                        double QuantityF = QuantityE * 100000; // 1 Lot = 100000
                                        int Quantity = Convert.ToInt32(QuantityF);
                            
                                             Print("@@@@@@@@ Balance = " + Balance);
                                            Print("@@@@@@@@ QuantityD = " + QuantityD);
                                            Print("@@@@@@@@ QuantityE = " + QuantityE);
                                            Print("@@@@@@@@ QuantityF = " + QuantityF);
                                            Print("@@@@@@@@ Quantity = " + Quantity);
                            // ....
                            // i.e.
                                     if ((RSI1.Avg[0] > 80)
                                     EnterShort(0,Quantity,"EURUSD");
                            //...
                            //etc
                            Attached Files

                            Comment


                              #15
                              Hello johnnybegoode,

                              In this case because you are referencing the account by using the Addon methods, you need to be more specific. You are using the Playback connection which does not use Sim101, it uses Playback101. You can see the account name listed in the image you provided in the control center row. You could use the following instead:

                              Code:
                              Account a = Account.All.First(t => t.Name == "Playback101");

                              I look forward to being of further assistance.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by CortexZenUSA, Today, 12:53 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by usazencortex, Today, 12:43 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post usazencortex  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              168 responses
                              2,265 views
                              0 likes
                              Last Post sidlercom80  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              3 responses
                              12 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X