Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position Sizing based on margin!

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

    Position Sizing based on margin!

    Hello!

    I need help putting together the correct ninjascript for my position sizing concept.

    - I would like to use 2% of my account value per trade (see below).
    - Divide that amount by the distance to my exit.
    - My exit is set at 3x ATR 20 period (see below)

    I have highlighted in red where I believe I should be setting the "qtyToTrade".

    Please help! Thank you.

    double qtyToTrade;
    int positionSize;
    qtyToTrade = AccountSize *
    0.02 / Margin [0];
    positionSize = (
    int)qtyToTrade;


    // Condition set 1
    if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1)
    && SMA(
    10)[0] > SMA(40)[0])

    {
    EnterLong(positionSize,
    "enter long");
    SetStopLoss(CalculationMode.Price,Position.AvgPric e - ATR(
    20)[0]*3);
    }

    #2
    Originally posted by antonio_zeus View Post
    Hello!

    I need help putting together the correct ninjascript for my position sizing concept.

    - I would like to use 2% of my account value per trade (see below).
    - Divide that amount by the distance to my exit.
    - My exit is set at 3x ATR 20 period (see below)

    I have highlighted in red where I believe I should be setting the "qtyToTrade".

    Please help! Thank you.

    double qtyToTrade;
    int positionSize;
    qtyToTrade = AccountSize *
    0.02 / Margin [0];
    positionSize = (
    int)qtyToTrade;


    // Condition set 1
    if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1)
    && SMA(
    10)[0] > SMA(40)[0])

    {
    EnterLong(positionSize,
    "enter long");
    SetStopLoss(CalculationMode.Price,Position.AvgPric e - ATR(
    20)[0]*3);
    }
    You have only a Stop Loss; no Profit Target. From your description, it reads as if you are equating your margin to the size of your risk, so why not just use it? Place your position size calculation in the same block as your entry; calculating it just before your entry.

    Comment


      #3
      koganam

      thanks for the reply. Im a bit of noob in programming, can you give me an example on how to complete the suggestion below?

      I see what you are saying I just cannot think on how to code that in....

      Thank you.

      Comment


        #4
        BUMP bump
        BUMP bump
        BUMP bump

        Comment


          #5
          Originally posted by antonio_zeus View Post
          koganam

          thanks for the reply. Im a bit of noob in programming, can you give me an example on how to complete the suggestion below?

          I see what you are saying I just cannot think on how to code that in....

          Thank you.
          Code:
          #region Variables
          private bool InTrade = false;
          #endregion
          Code:
          [FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Condition set 1[/COLOR][/SIZE][/FONT]
          [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (CrossAbove(Close, MAEnvelopes([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1.5[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]3[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]20[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]).Upper, [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New]&& SMA([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]10[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] > SMA([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]40[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] && !InTrade)[/SIZE][/FONT]
           
          [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
          [SIZE=2][FONT=Courier New][COLOR=#0000ff]double[/COLOR][FONT=Courier New][SIZE=2][SIZE=2][COLOR=#000000] qtyToTrade = 0;[/COLOR][/SIZE][/SIZE][/FONT]
          [FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][SIZE=2] positionSize = 0;[/SIZE][/SIZE][/FONT][/FONT][/SIZE][/SIZE][/FONT]
          [FONT=Courier New][SIZE=2][SIZE=2][FONT=Courier New][FONT=Courier New][SIZE=2]double StopLoss = Instrument.MasterInstrument.Round2TickSize(ATR([FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]20[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]*[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]3)[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];[/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][/FONT][/SIZE][/SIZE][/FONT]
           
          [SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]qtyToTrade = (ATR([FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]20[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] >= TickSize) ? ([/SIZE][/FONT][/SIZE][/FONT]AccountSize * [/SIZE][/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0.02[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] / [COLOR=red][B]StopLoss) : DefaultQuantity;[/B][/COLOR][/SIZE][/SIZE][/FONT]
          [SIZE=2][FONT=Courier New][SIZE=2]positionSize = ([/SIZE][/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2])qtyToTrade;[/SIZE][/SIZE][/FONT]
           
          SetStopLoss(CalculationMode.Price, Close[0] - StopLoss[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
          [/SIZE][/FONT]EnterLong(positionSize, [/FONT][/SIZE][/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"enter long"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT][/SIZE][/FONT]
          [FONT=Courier New][SIZE=2]InTrade = true;[/SIZE][/FONT]
          [FONT=Courier New][SIZE=2][SIZE=2][FONT=Courier New]}[/FONT][/SIZE][/SIZE][/FONT]
           
          [FONT=Courier New][SIZE=2]if (Position.MarketPosition == MarketPosition.Flat && InTrade) InTrade = false;[/SIZE][/FONT]
          Your probably want to specify and incorporate a minimum StopLoss size. While unlikely that your ATR(20) will be less than one Tick, you still have to account for that unlikely boundary condition, where your ATR(20) is zero, in order for your code to be robust.
          Last edited by koganam; 04-16-2013, 12:14 AM.

          Comment


            #6
            hi koganam

            im giving this a try but are receiving errors. If you can help me break it down I would GREATLY appreciate it.

            1. What does this do -> Intrade = true; and !InTrade
            2. Did you mean to do the following? (I highlighted what seems a bit odd to me)

            qtyToTrade = (ATR(20)[0] >= TickSize) ? (AccountSize * 0.02 / StopLoss) : DefaultQuantity;
            positionSize = (int)qtyToTrade;

            Comment


              #7
              Originally posted by antonio_zeus View Post
              hi koganam

              im giving this a try but are receiving errors. If you can help me break it down I would GREATLY appreciate it.

              1. What does this do -> Intrade = true; and !InTrade
              2. Did you mean to do the following? (I highlighted what seems a bit odd to me)

              qtyToTrade = (ATR(20)[0] >= TickSize) ? (AccountSize * 0.02 / StopLoss) : DefaultQuantity;


              positionSize = (int)qtyToTrade;
              1. What are the exact errors?
              2. InTrade is a Boolean variable that it set when in a trade, and not set when not in a trade. It serves to ensure that positions are only entered when not already in a trade.
              3. What is wrong with the qtyToTrade statement. Does it throw an error?
              Last edited by koganam; 04-14-2013, 06:46 AM. Reason: Corrected spelling.

              Comment


                #8
                Hi koganam

                sorry for the late reply... here is where I am now.
                - I would like to use 2% of my account value per trade (see below).
                - Divide that amount by the distance to my exit.
                - My exit is set at 3x ATR 20 period (see below)

                What do I put as my StopLoss? I get errors for the below script because originally i did not define StopLoss and it is claiming the following errors

                The name StopLoss does not exist in the current context

                also note that technically my stoploss for longs will be Close[0] - ATR and for shorts Close[0] + ATR

                StopLoss = Close[0] - ATR(20)[0]*3;
                qtyToTrade = ((ATR(
                20))[0] >= TickSize) && (AccountSize * 0.02 / StopLoss);
                positionSize = (
                int)qtyToTrade;

                Comment


                  #9
                  I see StopLoss defined in Koganams post dated 03-01-2013, 12:41 PM.

                  Are you using it outside it's scope?

                  Move all your variable declarations to the varaibles region, and only do assignments in the code.



                  Originally posted by antonio_zeus View Post
                  Hi koganam

                  sorry for the late reply... here is where I am now.
                  - I would like to use 2% of my account value per trade (see below).
                  - Divide that amount by the distance to my exit.
                  - My exit is set at 3x ATR 20 period (see below)

                  What do I put as my StopLoss? I get errors for the below script because originally i did not define StopLoss and it is claiming the following errors

                  The name StopLoss does not exist in the current context

                  also note that technically my stoploss for longs will be Close[0] - ATR and for shorts Close[0] + ATR

                  StopLoss = Close[0] - ATR(20)[0]*3;
                  qtyToTrade = ((ATR(
                  20))[0] >= TickSize) && (AccountSize * 0.02 / StopLoss);
                  positionSize = (
                  int)qtyToTrade;

                  Comment


                    #10
                    sledge!

                    thanks for your reply... okay I didnt catch that.

                    Putting that now gives me a new error - if you look at my script maybe this will help

                    Strategy\COVENANT.cs Operator '&&' cannot be applied to operands of type 'bool' and 'double' CS0019

                    the error applies to

                    qtyToTrade = ((ATR(20))[0] >= TickSize) && (AccountSize * 0.02 / StopLoss);

                    double qtyToTrade;
                    int positionSize;
                    double StopLoss = Instrument.MasterInstrument.Round2TickSize(ATR(20)[0]*3);
                    qtyToTrade = ((ATR(
                    20))[0] >= TickSize) && (AccountSize * 0.02 / StopLoss);
                    positionSize = (
                    int)qtyToTrade;

                    Comment


                      #11
                      Originally posted by antonio_zeus View Post
                      Hi koganam

                      sorry for the late reply... here is where I am now.
                      - I would like to use 2% of my account value per trade (see below).
                      - Divide that amount by the distance to my exit.
                      - My exit is set at 3x ATR 20 period (see below)

                      What do I put as my StopLoss? I get errors for the below script because originally i did not define StopLoss and it is claiming the following errors

                      The name StopLoss does not exist in the current context

                      also note that technically my stoploss for longs will be Close[0] - ATR and for shorts Close[0] + ATR

                      StopLoss = Close[0] - ATR(20)[0]*3;
                      qtyToTrade = ((ATR(
                      20))[0] >= TickSize) && (AccountSize * 0.02 / StopLoss);
                      positionSize = (
                      int)qtyToTrade;
                      I am not sure that I understand your question. I gave you exact code. Instead of using the code, you modified it as you saw fit. After doing so, you got errors. Are you asking me why you got errors when you modified the code that I wrote? If not, then what exactly are you asking?

                      It is one thing if you get errors from my unmodified code, and bring those to my attention. I am really not sure what you are expecting me to say if you modify the code and get errors. I have to presume that you know why you modified the code, so you should know how to fix it if your modifications are giving you errors.

                      Comment


                        #12
                        koganam

                        very sorry to not have just copied and pasted your code. I did do this now in a new strategy.

                        Here are the errors I get. I am also attaching the ninjascript.

                        Strategy\COVENANT.cs The best overloaded method match for 'NinjaTrader.Strategy.StrategyBase.Add(NinjaTrader .Indicator.IndicatorBase)' has some invalid arguments CS1502 - click for info line 44 column 4

                        Strategy\COVENANT.cs The best overloaded method match for 'NinjaTrader.Strategy.StrategyBase.Add(NinjaTrader .Indicator.IndicatorBase)' has some invalid arguments CS1502 - click for info line 44 column 4

                        Strategy\COVENANT.cs Argument '1': cannot convert from 'NinjaTrader.Gui.Chart.Plot' to 'NinjaTrader.Indicator.IndicatorBase' CS1503 - click for info line 44 column 8

                        Strategy\NTForumHelpCovenant.cs The name 'InTrade' does not exist in the current context CS0103 - click for info line 49 column 91

                        now the same error - The name 'InTrade' does not exist - occurs for line 61, 64, 72, 73.
                        Attached Files

                        Comment


                          #13
                          Originally posted by antonio_zeus View Post
                          koganam

                          very sorry to not have just copied and pasted your code. I did do this now in a new strategy.

                          Here are the errors I get. I am also attaching the ninjascript.

                          Strategy\COVENANT.cs The best overloaded method match for 'NinjaTrader.Strategy.StrategyBase.Add(NinjaTrader .Indicator.IndicatorBase)' has some invalid arguments CS1502 - click for info line 44 column 4

                          Strategy\COVENANT.cs The best overloaded method match for 'NinjaTrader.Strategy.StrategyBase.Add(NinjaTrader .Indicator.IndicatorBase)' has some invalid arguments CS1502 - click for info line 44 column 4

                          Strategy\COVENANT.cs Argument '1': cannot convert from 'NinjaTrader.Gui.Chart.Plot' to 'NinjaTrader.Indicator.IndicatorBase' CS1503 - click for info line 44 column 8
                          These are errors from a different file, COVENANT.cs. I cannot speak to those. You can either correct the errors in that file, or remove the file, so that it is not in the compilation.

                          Strategy\NTForumHelpCovenant.cs The name 'InTrade' does not exist in the current context CS0103 - click for info line 49 column 91

                          now the same error - The name 'InTrade' does not exist - occurs for line 61, 64, 72, 73
                          That is because of imprecision. Computers are stupid counting machines that only do what they are told.

                          If you look at the post for the code that I gave you, there were 2 code sections. You ignored the one where InTrade is declared. Therefore, in your file, InTrade is not declared. Declare InTrade properly in the Variables section, as in the code that I gave you.

                          Comment


                            #14
                            hi koganam

                            very sorry again! I am very new to programming.. learning every day new things. I really appreciate your help.

                            Okay, so I put in the variables section the following code you gave me:
                            #region Variables
                            privatebool InTrade = false;

                            #endregion

                            but I still get errors. I closed and fixed the "COVENANT" strategy.. these errors have to do with the InTrade again.

                            Again, thank you very much for your help... I am actually learning a few things from your responses.

                            Strategy\COVENANT.cs The best overloaded method match for 'NinjaTrader.Strategy.StrategyBase.Add(NinjaTrader .Indicator.IndicatorBase)' has some invalid arguments CS1502 - click for info 44 4

                            Strategy\COVENANT.cs Argument '1': cannot convert from 'NinjaTrader.Gui.Chart.Plot' to 'NinjaTrader.Indicator.IndicatorBase' CS1503 - click for info 44 8

                            Strategy\NTForumHelpCovenant.cs The name 'Intrade' does not exist in the current context CS0103 - click for info 63 4

                            Strategy\NTForumHelpCovenant.cs The name 'myInput0' does not exist in the current context CS0103 - click for info 74 26

                            Strategy\NTForumHelpCovenant.cs The name 'myInput0' does not exist in the current context CS0103 - click for info 75 19


                            Attached Files

                            Comment


                              #15
                              Originally posted by antonio_zeus View Post
                              hi koganam

                              very sorry again! I am very new to programming.. learning every day new things. I really appreciate your help.

                              Okay, so I put in the variables section the following code you gave me:
                              #region Variables
                              privatebool InTrade = false;

                              #endregion

                              but I still get errors. I closed and fixed the "COVENANT" strategy.. these errors have to do with the InTrade again.

                              Again, thank you very much for your help... I am actually learning a few things from your responses.

                              Strategy\COVENANT.cs The best overloaded method match for 'NinjaTrader.Strategy.StrategyBase.Add(NinjaTrader .Indicator.IndicatorBase)' has some invalid arguments CS1502 - click for info 44 4

                              Strategy\COVENANT.cs Argument '1': cannot convert from 'NinjaTrader.Gui.Chart.Plot' to 'NinjaTrader.Indicator.IndicatorBase' CS1503 - click for info 44 8

                              Strategy\NTForumHelpCovenant.cs The name 'Intrade' does not exist in the current context CS0103 - click for info 63 4

                              Strategy\NTForumHelpCovenant.cs The name 'myInput0' does not exist in the current context CS0103 - click for info 74 26

                              Strategy\NTForumHelpCovenant.cs The name 'myInput0' does not exist in the current context CS0103 - click for info 75 19


                              I cannot speak to anything regarding COVENANT.cs, as it has nothing to do with what I coded. I have already told you that, and also suggested that you remove the file, or otherwise correct whatever errors you are being told exist. As long as that file exists with errors, you will not be able to compile any of your classes.

                              C# is case sensitive. InTrade is not the same as Intrade. It seems to have been my error. I have corrected the original code.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              630 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
                              566 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