Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entering Position Based on Percentage of Account

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

    Entering Position Based on Percentage of Account

    Let's say that you want to risk 3% of your current account balance per trade. How would I code that?

    Thanks much!

    #2
    Hi trend747,

    If you want to access programatically the value of your account, you can use GetAccountValue();


    double myPositionSize = GetAccountValue(AccountItem.CashValue) * .03;

    This is a real-time only value. To do the same in a backtest requires manually calculating an account balance with TradePerformance class:
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      This is only for backtesting purposes

      Code:
       protected override void OnBarUpdate()
              {
      
      
      if (moneymanagement == 1 )
      
      {	
      double startingcapital = 1000000;
      			
      			
      			
      DefaultQuantity = (int)((startingcapital-Performance.AllTrades.Count)*0.01  /(Instrument.MasterInstrument.PointValue * StopValue))  ;
      		}
      }
      The $ risk should be 1% of my current capital (starting capital +- performance)



      doesn't seem like the performance variable is doing anything, and if i try to print the performance it does nothing. what is my mistake?

      Comment


        #4
        Do you try only this code or would the strategy actually be entering trades so the Performance.AllTrades.Count could be populated?

        Comment


          #5
          yep this is embedded in a actual strategy which is working even after adding the code, but the contract size is still not varying as expected

          Set Order Quantity is set to "by strategy"

          Comment


            #6
            That's the issue then, you define / set DefaultQuantity and do not choose to set the strategy size by it.

            Comment


              #7
              mmmh

              but the strategy is exactly doing what i want when i choose "by strategy", the contract size is varying with the stopsize. => constant dollar risk per trade is working.

              when choosing by defaultquantiy i have to choose a fix number

              Comment


                #8
                I guess I don't understand why you attempt to set a defaultquantity here for this process, this is meant to provide a basis to enter a custom quantity from the UI or program a hard coded default into the script. If you want to adapt on the fly with the script then I would suggest staying with calculating in a custom variable in OnBarUpdate() and feeding this to your Entry methods.

                Comment


                  #9
                  Old thread I know, but...

                  I would like to get a tip or at least a direction on what to think of and use in an Unmanaged Strategy in NinjaTrader 8 regarding only putting, for example 2% of cash value.
                  My strategy is building up Position.Quantities and I have to limit the amount.

                  Kennet

                  Comment


                    #10
                    Hello Kennet,

                    Thank you for your post.

                    You can pull the cash value and calculate 2 percent of the CashValue in the following manner:
                    Code:
                    			cash = Account.Get(AccountItem.CashValue, Currency.UsDollar);
                    			
                    			twoPercent = Convert.ToInt32(cash * .02);
                    For information on pulling Account values in NinjaTrader 8 please visit the following link: http://ninjatrader.com/support/helpG.../en-us/get.htm

                    Please let me k now if you have any questions.

                    Comment


                      #11
                      Thank you!
                      And hpw do I get the total cost for a trade?

                      Comment


                        #12
                        Hello Kennet,

                        Thank you for your response.

                        You can refer to the details at the following link for information on pulling the cost of each trade: http://ninjatrader.com/support/forum...841#post493841

                        Please let me know if you have any questions.

                        Comment


                          #13
                          Additional questions on this topic

                          Where would you insert the code in the body of the script? I would like to do the same thing to my strategy.

                          Comment


                            #14
                            Hello CoopersFX,

                            Thank you for your post and welcome to the NinjaTrader Support Forum!

                            You would use the calculation to determine the quantity of the order. This means you would take the output of the calculation as your quantity for your entry or exit order method. The code would likely need to be placed where you place your entry or exit method as an action for a condition.

                            Please let me know if you have any questions.

                            Comment


                              #15
                              It wouldn't work for trading FX would it? Since you have to trade in even lots. Is there a way to round down to the nearest micro lot?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              572 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              331 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              549 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              550 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X