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

Trade Count

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

    Trade Count

    Performance.AllTrades.Count I have read counts a trade only when it's closed. Is there a way to count the # of trades executed?

    Or if not does Position.Quantity count the shares traded as soon as the trades executed? And I could limit the # of trades to say 15 max trades (each trade 200 shares) with...

    if (Position.Quantity > (200*15))
    //exit or halt trading
    Last edited by zachj; 11-03-2013, 08:34 PM.

    #2
    Hello zachj,

    Thank you for your post.

    There is no method to count the executions made, but you can develop your own count for the executions using the OnExecution() method to track the executions.

    For information on OnExecution() please visit the following link: http://www.ninjatrader.com/support/h...nexecution.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      Doesn't the Position.Quantity calculate the shares right on execution or if not when does it do the calculation?

      If it's right on execution it seems this would be effective to control the trade count?

      Comment


        #4
        Hello zachj,

        Thank you for your response.

        That would in fact be an effective way to track the executions.

        Comment


          #5
          Hello zachj,

          To expand upon my last reponse; the Position.Quantity will not reflect exactly each execution. Unless you only trade 1 lot per execution, the executions could contain multiple quantity.

          A good way to check this would be the following:
          Code:
          if(Position.Quantity > previousQty)
          Print("Position size has increased"); // does not necessary mean one execution
          previousQty = Position.Quantity;
          An order of two lots could have two executions for example. So you would need to figure this and compare against the quantity you trade per order.

          Please let me know if I may be of further assistance.

          Comment


            #6
            I see what your saying, this is just for backtesting, it's 200 shares per execution and I think in Backtesting it just always fills the full order from what I've seen. The print statements seem to confirm this. I'm just having trouble getting it to stop filling orders once the max # of shares(positions) dictated has been reached. It definately seems to be exiting the positions very quick but it's not stopping it from entering more than 10 positions. See any possible issues with the strategy below?


            if ((Position.Quantity) >= 2000 ) //10 positions
            {
            StopStrategy();
            return;
            }
            //Entry Conditions here

            private void StopStrategy()
            {
            //If our Long Limit order is still active we will need to cancel it.
            if (entryOrder != null)
            {
            CancelOrder(entryOrder);
            }

            //If we have a position we will need to close the position
            if (Position.MarketPosition == MarketPosition.Long))
            {
            ExitLong();
            }
            }

            Comment


              #7
              Hello zachj,

              Thank you for your response.

              Are all 10 positions held at the same time? Do you exit the positions in any other manner besides the StopStrategy() method?

              I look forward to your response.

              Comment


                #8
                I have attached an image of the trades so you can see the entry and exit times. I do not have any other exits.

                I can see how multiple trades are being entered simultaneously and it might not pick up the 10 trade condition right away for instance how it shows 6 trades being entered at 9:46am. But 42 trades occur on that day all the way up to 11am it should kick in I would think.

                Also the Output print shows that 1800 shares traded(9 positions) at 9:46am and it stays at 1800 the rest of the day. So it's restricting the shares trades but trades are still being entered up to as far as 11am?
                Attached Files

                Comment


                  #9
                  Hello zachj,

                  Thank you for your response.

                  The code is doing exactly what it is being told here, which is cancel the entryOrder IOrder object when we reach 2000 quantity. However, nothing is there to prevent further trades. If you wish to halt the strategy and cancel all order as well as close all positions please visit the following link for a reference sample on this item: http://www.ninjatrader.com/support/f...ead.php?t=4804

                  Please let me know if I may be of further assistance.

                  Comment


                    #10
                    Yes I posted the code in previous post that I'm using, it's exactly from the halt strategy sample you just provided a link for. I used StopStrategy() and CancelOrder(entryOrder). That should halt it?

                    Comment


                      #11
                      Hello zachj,

                      Thank you for your response.

                      I see but the Position.Quantity will then be zero and there fore "if ((Position.Quantity) >= 2000 ) //10 positions" is now false. You can use Disable() if you wish to disable the strategy as well. You may instead wish to set a bool that is true until the StopStrategy() method is called, and then it is false - only enter new orders if this bool is true.

                      For information on Disable() please visit the following link: http://www.ninjatrader.com/support/h...t7/disable.htm

                      Please let me know if I may be of further assistance.
                      Last edited by NinjaTrader_PatrickH; 11-05-2013, 12:46 PM.

                      Comment


                        #12
                        I put the bool conditions all in blue below, do I have them in the correct locations?

                        private bool tradeCount = true;

                        if(Bars.FirstBarOfSession)
                        {
                        tradeCount = true;
                        }


                        if ((Position.Quantity) >= 2000 && tradeCount == true) //10 positions
                        {
                        StopStrategy();
                        return;
                        }
                        //Entry Conditions here

                        private void StopStrategy()
                        {
                        //If our Long Limit order is still active we will need to cancel it.
                        if (entryOrder != null)
                        {
                        CancelOrder(entryOrder);
                        }

                        //If we have a position we will need to close the position
                        if (Position.MarketPosition == MarketPosition.Long))
                        {
                        ExitLong();
                        }
                        tradeCount == false;
                        }

                        Comment


                          #13
                          Hello zachj,

                          Thank you for your response.

                          Yes, these are in the correct locations but you will also need to add tradeCount == true to your entry conditions so that when it is set to false the entries are no longer taken.

                          Comment


                            #14
                            Holy mackerel the bool actually did do it. I've been struggling with this for a while. Nice work! thanks

                            Comment


                              #15
                              Follow up to this. Again trying to have the strategy stop for the day if the PnL goes too low. Have it set for 1000 loss as shown below. It definately is not entering any new positions and is limiting the loss but the loss always exceeds 1000. The recent backtest I ran over a month I have a few days that are about -2k. I'm noticing that it will exit several positions when the limit is reached but many positions will ride out till the end of the day. Any idea what might be going on here? I did try using Disable() but all it did was prevent any new trades and didn't exit the positions that were open until the backtest period ended which was like a week later.

                              if (unrlzdPnl <= -1000)
                              {
                              StopStrategy();
                              return;
                              }

                              //entry conditions here

                              private void StopStrategy()
                              {
                              //If our Long Limit order is still active we will need to cancel it.
                              if(entryOrder1 != null) // I have several entryOrders as it's multi-instrument
                              {
                              CancelOrder(entryOrder1);
                              }
                              //If we have a position we will need to close the position
                              if (Position.MarketPosition == MarketPosition.Long)
                              {
                              ExitLong(200);
                              }

                              tradeCount = false; //this is the bool to stop from entering any new trades
                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by funk10101, Today, 09:43 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post funk10101  
                              Started by pkefal, 04-11-2024, 07:39 AM
                              11 responses
                              37 views
                              0 likes
                              Last Post jeronymite  
                              Started by bill2023, Yesterday, 08:51 AM
                              8 responses
                              44 views
                              0 likes
                              Last Post bill2023  
                              Started by yertle, Today, 08:38 AM
                              6 responses
                              26 views
                              0 likes
                              Last Post ryjoga
                              by ryjoga
                               
                              Started by algospoke, Yesterday, 06:40 PM
                              2 responses
                              24 views
                              0 likes
                              Last Post algospoke  
                              Working...
                              X