Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set DefaultQuantity using dollar amount

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

    Set DefaultQuantity using dollar amount

    I use a strategy on a stock list. I need to set position quantity on each stock using dollar amount. For example each position size should be (20000$/stock price) rounded to nearest hundred. How can I do it?

    Can I get information about stock on which strategy is used from Initialize method to use code like this:

    DefaultQuantity = RoundToHundreds(20000/stock price);

    ?

    #2
    Hello nysetrader,
    Thanks for writing in and I am happy to assist you.
    You can write a code as shown below to round to nearest 100;

    Code:
    DefaultQuantity = (int)((20000 / pr) / 100) * 100;
    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thank you.
      But actually the question is how to get price of the stock?
      In your code I need to initialize "pr" with stock price value and I can't find how to do it.

      Comment


        #4
        Hello nysetrader,
        Sorry for the confusion. pr will be the price of the stock.

        Code:
        DefaultQuantity = (int)((20000 / pr) / 100) * 100; // pr= stock price
        Also, we recommend to use the OnStartUp event to calculate the position size and not at Initialize. http://www.ninjatrader.com/support/h...?onstartup.htm

        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Originally posted by nysetrader View Post
          Thank you.
          But actually the question is how to get price of the stock?
          In your code I need to initialize "pr" with stock price value and I can't find how to do it.
          If COBC is false, then the price of the stock at any time is Close[0]; if COBC is true, you will have to use OnMarketDate() to get the exact price of the stock at any given time, as Close[0] will just be the price at which the last bar closed, which is very unlikely to be the price at any given time.

          Comment


            #6
            Hello nysetrader,
            Since you are accessing it once during startup please use this code.

            Code:
            protected override void OnStartUp()
            {
                DefaultQuantity = (int)((20000 / BarsArray[0].LastPrice) / 100) * 100;
            }
            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

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