Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position Sizing based on margin!

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

    #16
    koganam!

    we are almost there... down to only two errors now and this has to do with lines 74 and lines 75.

    did I forget to add myInput0 under variables? Its pretty far into the code....

    Appreciate your help... as always!


    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


      #17
      Originally posted by antonio_zeus View Post
      koganam!

      we are almost there... down to only two errors now and this has to do with lines 74 and lines 75.

      did I forget to add myInput0 under variables? Its pretty far into the code....

      Appreciate your help... as always!


      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
      Well, yes, you have not declared myInput0, so you cannot expose the Properties of a variable that does not exist. It is obvious that you are not using myInput0, so I would just delete the Property definition.

      Comment


        #18
        hi koganam

        that worked.. but funny enough, it doesnt work in buying the correct amount of contracts. For example, the purpose is to use 2% of my account value (1,000,000 which means $20,000) divided by the stop loss distance... which should be around 4-5 contracts.

        When I applied the strategy to lets say CT (cotton futures) it buys 2800 contracts!

        your code looks like it should do this..

        any ideas? I also included the actual strategy so you can test it too if you wish.



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

        {
        double qtyToTrade = 0;
        int positionSize = 0;
        double StopLoss = Instrument.MasterInstrument.Round2TickSize(ATR(20)[0]*3);

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

        SetStopLoss(CalculationMode.Price, Close[
        0] - StopLoss);
        EnterLong(positionSize,
        "enter long");
        InTrade =
        true;
        }

        if (Position.MarketPosition == MarketPosition.Flat && InTrade) InTrade = false;
        Attached Files

        Comment


          #19
          Originally posted by antonio_zeus View Post
          hi koganam

          that worked.. but funny enough, it doesnt work in buying the correct amount of contracts. For example, the purpose is to use 2% of my account value (1,000,000 which means $20,000) divided by the stop loss distance... which should be around 4-5 contracts.

          When I applied the strategy to lets say CT (cotton futures) it buys 2800 contracts!

          your code looks like it should do this..

          any ideas? I also included the actual strategy so you can test it too if you wish.
          Unfortunately, it is not a matter of postulating what it should be: it is a matter of what it is. Have you done the actual math using the values that you can read? What is the value of the StopLoss? Have you divided that into your Risk Amount? In other words, you have to detemine why you are not getting what you expect. Is it because your values are wrong (unlikely), or because your logic is wrong?

          For one thing, futures are leveraged instruments with margins that are a very small portion of the value of the contract. Does your logic take that into account. If not, how do you propose to take it into account? The only way is to re-examine your logic, in the light of your unexpected results. The math does not lie. Incidentally, the figures that you give would indicate a StopLoss value of $7.14. Is that realistic? If not, where is the defect in your logic? If you expect 4 - 5 contracts to cover $20,000, it means that you are saying your risk should be $5,000 - $4,000 per contract. Really?

          You will need to Print values to the "Output Window" so that you can see what values your code is generating and using. Welcome to the real world of coding, where most of our time is spent debugging what we thought was perfectly correct when we first wrote our design, only to find out that the stupid computer literally does what we tell it to do, not what we intend it to do; unless what we tell it exactly matches what we intend.

          Comment


            #20
            Ummm, you should be hiring someone like Koganam.


            Originally posted by antonio_zeus View Post
            hi koganam

            that worked.. but funny enough, it doesnt work in buying the correct amount of contracts. For example, the purpose is to use 2% of my account value (1,000,000 which means $20,000) divided by the stop loss distance... which should be around 4-5 contracts.

            When I applied the strategy to lets say CT (cotton futures) it buys 2800 contracts!

            your code looks like it should do this..

            any ideas? I also included the actual strategy so you can test it too if you wish.



            [/SIZE][/FONT][/SIZE][/FONT]

            Comment


              #21
              hey koganam

              You are right... the equation is incorrect. Its simply dividing $20,000 by the distance of price to the ATR.

              Ill need to work on this... because my original goal was to divide $20,000 by the distance in ATR from price BUT as a value of the contract...

              ex: if the distance between my cotton graph is 7.24 (this is not dollars it is cents) and the contract is worth 50000lbs cotton then 50000 * 7.24 / 100 = $3620 per contract

              $20,000 of my acct size divided by the actual value of each contract from my trailing stop $3620 = 5.525 contracts.. put that into an integer and get 5 contracts to buy or sell.

              Comment


                #22
                Originally posted by antonio_zeus View Post
                hey koganam

                You are right... the equation is incorrect. Its simply dividing $20,000 by the distance of price to the ATR.

                Ill need to work on this... because my original goal was to divide $20,000 by the distance in ATR from price BUT as a value of the contract...

                ex: if the distance between my cotton graph is 7.24 (this is not dollars it is cents) and the contract is worth 50000lbs cotton then 50000 * 7.24 / 100 = $3620 per contract

                $20,000 of my acct size divided by the actual value of each contract from my trailing stop $3620 = 5.525 contracts.. put that into an integer and get 5 contracts to buy or sell.
                Glad that I could help you straighten out the logic. Now all you need to do is code the arithmetic. That should be trivial.
                Last edited by koganam; 08-29-2014, 10:58 AM. Reason: Corrected spelling.

                Comment


                  #23
                  hey koganam

                  really appreciate your help... with what I mentioned on my previous post.. is that possible?

                  Setting up a value of "loss" per contract and dividing 2% of account portfolio by that? Im not asking you to do it, im more interested to know if its actually possible in the current ninjatrader framework.

                  Thank you!

                  Comment


                    #24
                    Originally posted by antonio_zeus View Post
                    hey koganam

                    really appreciate your help... with what I mentioned on my previous post.. is that possible?

                    Setting up a value of "loss" per contract and dividing 2% of account portfolio by that? Im not asking you to do it, im more interested to know if its actually possible in the current ninjatrader framework.

                    Thank you!
                    Absolutely. It is rather elementary arithmetic. Just do the exact thing that you described. You need to input the contract value, so that you can multiply and divide, just as you detailed. I do not believe that NT will give you the contract value, so you will have to have it as an input, either as an enum, or as a double or integer, as the case may be.

                    Comment


                      #25
                      Hi All,

                      The Title of the thread suggests that Margin would be used in position sizing calculation.

                      How do we get access to the Margin through the InstrumentManager? Or via Account ?

                      Comment


                        #26
                        Originally posted by tornadoatc View Post
                        Hi All,

                        The Title of the thread suggests that Margin would be used in position sizing calculation.

                        How do we get access to the Margin through the InstrumentManager? Or via Account ?
                        Margins are set by the exchange. You will have to input them yourself.

                        Comment


                          #27
                          hi koganam

                          one thing I did realize.. based on my logic from yesterday, as in using the distance in entry vs. trailing ATR stop, and converting that distance into a dollar value per contract.

                          Now does ninjatrader have anything built in for that? Because technically speaking, every contract is different and some are in cents, as others are in dollars... Just curious to know your experience with this as it seems NT only has very specific options available. Ex: always buying 5 contracts (fixed) as opposed to basing it on my goals (variable)

                          Comment


                            #28
                            Originally posted by antonio_zeus View Post
                            hi koganam

                            one thing I did realize.. based on my logic from yesterday, as in using the distance in entry vs. trailing ATR stop, and converting that distance into a dollar value per contract.

                            Now does ninjatrader have anything built in for that? Because technically speaking, every contract is different and some are in cents, as others are in dollars... Just curious to know your experience with this as it seems NT only has very specific options available. Ex: always buying 5 contracts (fixed) as opposed to basing it on my goals (variable)
                            NT has the TickSize which determines the units in which the contract is measured. Margins are set by the exchange and can be changed whenever the exchange wishes. That means that you will have to arrange input fields to input that kind of information. NT is not the issue: the issue is determining what information you need to provide so that NT can process that information. Almost all my strategies use variable contract amounts based on criteria that I determine, and some of those conditions are quite complex. As an example, in one of my strategies:
                            1. Enters with a number of contracts.
                            2. Adds on if conditions are met.
                            3. However, if there is no add on, and the first trade is a loss, it enters the next trade with fewer contracts, and no add on.
                            4. If that trade is a success, the next trade returns to default processing as described in (1) and (2).
                            5. If the daily loss limit is reached, with or without addon trades, no further trades are taken, but the signals are still marked on the chart, so that I can see what would have happened. So far, just about every time that I hit the daily loss limits, further trading would have resulted in bigger losses. That is why it is so easy for me to shut down when I hit the loss limit: I have objective evidence that continuing does not allow me to recover.
                            Last edited by koganam; 08-29-2014, 11:01 AM. Reason: Corrected spelling.

                            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