Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Dynamic Position Size

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

    Dynamic Position Size

    Hi,

    I'm trying to configure a strategy where the size of the entry is determined by the account size at that point (multi instrument portfolio/basket test).

    However when I use the EnterShort or EnterLong method it does not allow me to pass this variable into it; it must be EnterLong (int, string) or just EnterLong (int) it seems.

    I would like, for example, to have something like EnterLong (AccountSize*.02) where it the size of the entry would be 2% of the account size (QTY purchased would be obtained by currency value of 2% of account divided by price per contract).

    How can I do this?

    Thanks,

    Ciaran

    #2
    Ciaran, you would have to convert the double result returned from the AccountSize * 0.02 into an integer value to pass to the Enter() methods. This code should work:
    Code:
    int qtyToTrade = (int) AccountSize * 0.02 / Close[0];
    If that doesn't work, you could use any of the Math.Round() functions to round to an integer value.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Pyramiding Positions and Stop Loss

      Hi Austin,

      Thanks for that; the sample code didn't work but I used a cast to turn the double value into an acceptable integer value.

      However, my stop loss is not functioning correctly. In the screenshot attached you will see from the performance chart at the end of my backtest that even though the SMA 50 crosses below the SMA 100 a short position is not entered. It appears as if the short is entered but a stop loss is immediately triggered canceling the position. I can't understand why this is happening since the logic in my code is the same for a short sale as for a long sale? I think it may have something to do with using the ATR indicator as a method for setting my stop loss. When I go short I want to set the stop loss at a distance of twice the 20 day ATR above the entry point (or below the entry point for long trades). However in the logic I'm not sure if I should have this as AvgPrice + ATR or - ATR. I've attached a second screenshot with my code if you could take a look?

      I'm also trying to use " if (Close [0] > Position.AvgPrice - ATR(20) [0])" on my long trades (or < for short trades) to enter an additional short position if the price moves by 1 x 20 day ATR in my favour after the initial entry but this doesn't appear to be working either. Is my code correct?

      Many thanks for your help,

      Regards,

      Ciaran
      Attached Files

      Comment


        #4
        Ciaran, you will have to start debugging your script with Print() statements printing out every value you are using to see where the variables start going against your expectations. The Print() method puts the output in the output window available from Tools -> Output Window.

        For example, you can put Print() statements inside each code block, like this:
        Code:
        if (CrossAbove(SMA(50), SMA(100), 1)
        {
            Print("inside 50 crossabove 100 code block");
            EnterLong(...);
            SetStopLoss(...);
            
            if (Close > AvgPrice + ATR)
            {
                Print("inside the close is greater than the avgPrice plus ATR block, which is inside the crossabove block");
                EnterLong(...);
                SetStopLoss(...);
            }
        }
        AustinNinjaTrader Customer Service

        Comment


          #5
          Problem with stop loss

          Hi Austin,

          I've begun debugging my code as suggested. It seems that the code I have written for placing stop losses is not working as I intend it to.

          The message I get in the output window is - "**NT** Calculated stop order price for strategy 'TEST1' was smaller/equal 0. No stop order placed."

          The code I have written is -

          EnterLong (positionSize);
          SetStopLoss(CalculationMode.Price, Position.AvgPrice - ATR(20)[0]*2);

          So for example on one trade where I entre a long position at a price of 1.2657 I would like a stop loss to be placed at a distance of twice the 20 day ATR which is .014 in this instance. This would place the stop loss at a distance of .028 from the price which would be at a price of approximately 1.23. My code above is not achieving this for me but instead is not placing any stop loss as you can see from the error code above;

          I would be grateful if you could assist me rectifying this.

          Thanks,

          Comment


            #6
            ciaranfryan,

            If you want a Set() method to be applied to the entry order you need to call Set() before the entry order. This also means when you do this you cannot do Position.AvgPrice. You will need to use another criterion. After the entry order is filled should you want to adjust the price again based on actual fill price you can do that later, but the important part is the initial submission which needs to come before the Enter() method.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Setting Stop Loss Related to Price of Instrument

              Thanks Josh;

              Can you suggest a possible alternative criterion for referencing the price of the instrument when setting the stop loss? I want to set the stoploss @ the market price + or - ATR(20)[0]*2 (depending on whether I'm going short or long).

              I have tried a number of different "Position." criterion none of which seem to work for this.

              Thanks,

              Ciaran

              Comment


                #8
                Hello,

                You would need to use Close[0] to get the current market price.

                Let me know if I can be of further assistance.
                BrettNinjaTrader Product Management

                Comment


                  #9
                  Repeating Ninjascript Errors

                  Thanks Brett,

                  Separately; I have suddenly started getting the compile errors CS1518 and CS1022 in lines 106 and 113 of the code I am working on. Since these lines are a section of code which I do not edit/interfere with in the ninjascript I thought something may have become corrupted. To troubleshoot this I created a new very basic strategy using the ninjatrader strategy development wizard to see if that would work but I got the exact same errors in the exact same lines of code in that strategy also.

                  Would you be able to advise me what might be suddently causing these errors in every strategy I create?

                  I have attahced two screenshots of two different strategies in which I'm getting this error. One strategy has custom code in it and the other was created entirely in the wizard.

                  Many thanks,

                  Ciaran
                  Attached Files

                  Comment


                    #10
                    Hello,

                    If you are wondering why you receive an error when compiling only one indicator, it is because NinjaTrader compiles all indicators and strategies- not just one. You have a coding error in TEST1.CS.

                    Please attach TEST1.CS file to the forum or send it to support at NinjaTrader dot com and reference this forum post so that I can assist with debugging the coding error if you are unable to find it.

                    I look forward to assisting you further.
                    BrettNinjaTrader Product Management

                    Comment


                      #11
                      Debugging NinjaScript

                      Thanks Brett,

                      I have attached the TEST1.cs code as a .txt file; I hope this is sufficient.

                      Any assistance you can provide as to the difficulty I'm having debugging the code would be much appreciated.

                      Thanks,

                      Ciaran
                      Attached Files

                      Comment


                        #12
                        Hello,

                        Please look closely to this section of code:

                        // Condition set 2
                        if (CrossBelow(SMA(50), SMA(100), 1))
                        {
                        {
                        Print (
                        "set stop loss going short code block");
                        SetStopLoss(CalculationMode.Price, Close[
                        0] + ATR(20)[0]*2);
                        }
                        Print(
                        "enter short code block");
                        EnterShort(positionSize);
                        }


                        Here is where the error is. You have a Parenthesis ) with no opening parenthesis that is ending the OnBarUpdate before your ready to do so.

                        Let me know if I can be of further assistance.

                        BrettNinjaTrader Product Management

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        633 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        364 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
                        567 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