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

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?
        BertrandNinjaTrader Customer Service

        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.
            BertrandNinjaTrader Customer Service

            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.
                BertrandNinjaTrader Customer Service

                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 Christopher_R, Today, 12:29 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post Christopher_R  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              166 responses
                              2,235 views
                              0 likes
                              Last Post sidlercom80  
                              Started by thread, Yesterday, 11:58 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post thread
                              by thread
                               
                              Started by jclose, Yesterday, 09:37 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,415 views
                              0 likes
                              Last Post Traderontheroad  
                              Working...
                              X