Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Counting Losing trades

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

    Counting Losing trades

    I have a strategy where I need to count the number of losing trades. I presume I would do this in OnExecution, but what is the best way to do this?

    #2
    Hello GKonheiser,

    You may use the "TradeCollection" to see how many losing trades without having to count manually inside of OnBarUpdate. For example:

    Code:
    protected override void OnBarUpdate()
    {
    		Print("Number of Losing Trades: "+Performance.AllTrades.LosingTrades.Count);
    }
    JCNinjaTrader Customer Service

    Comment


      #3
      Hi JC,

      My Strategy tries going long or short at diverent levels thought the day untill it brecheds the set number of lossers for each level, so I need to do a manual count once the position is closed bases on each level,so what would be the best way to do that. So in the OnExecuiton I would like to say :-

      if (execution.Order.OrderState != OrderState.PartFilled)
      {
      longEntryPP = null;
      if(longEntry == losing trader)
      {
      badTradeCount++
      }
      }

      But Im not sure how to correctly say If(longEntry == losing trade)

      Comment


        #4
        Hello GKonheiser,

        To see if it is a losing trade you would have to see if the Exit price is less than the Entry price (For a Long position). So you may either use a variable to get the Entry orders price or you may use the "Position.AvgPrice" to get a price to check against to see if it is a losing trade or not. Note that for Short orders it would be the opposite so if Exit price is greater than Entry Price.

        Let me know if that works for you.
        JCNinjaTrader Customer Service

        Comment


          #5
          If the position is already closed how do I reference the e exit price of the order?

          for the entry I would use Position.AvgPrice , I would of thought that Position.ExitPrice would work but no?

          Comment


            #6
            Hello GKonheiser,

            There is no Position.ExitPrice, but you would want to compare the Entry price to your Exit orders price inside of the OnExecution() event because this is going to be where you can get the Exit price of your trade.

            If you want to get the exit price of a trade that has already been completed you will want to get the trade variable and then you can return the exit price. Here is a post that you may view that gives an example of this.
            JCNinjaTrader Customer Service

            Comment


              #7
              When I try and use double " exitPrice = lastTrade.Exit.Price; " I get a compile error

              Comment


                #8
                Hello GKonheiser,

                Could you let me know what the compile error states in the "Error" column?
                JCNinjaTrader Customer Service

                Comment


                  #9
                  The name 'lastTrade' does not exist in the current context CS0103 - click for info 587 25

                  Here is the code snipit:-

                  if ((stopLongEntryPP != null && stopLongEntryPP == execution.Order) || (targetLongEntryPP != null && targetLongEntryPP == execution.Order))
                  {
                  if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                  {

                  exitPrice = lastTrade.Exit.Price;

                  if(Position.AvgPrice > exitPrice)
                  {
                  badTradeCountPP++;
                  }

                  stopLongEntryPP = null;
                  targetLongEntryPP = null;
                  }
                  }

                  *********
                  Ive just worked out that "lastTrade" should be the string of the last order, am I right? But when I try to overload the string of the last order "longEntryPP", .exit is not an option ??
                  Last edited by GKonheiser; 11-20-2013, 01:39 AM.

                  Comment


                    #10
                    Could I do this?

                    if ((stopLongEntryPP != null && stopLongEntryPP == execution.Order) || (targetLongEntryPP != null && targetLongEntryPP == execution.Order))
                    {
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                    {



                    if(Position.AvgPrice > execution.Order.AvgFillPrice)
                    {
                    badTradeCountPP++;
                    }

                    stopLongEntryPP = null;
                    targetLongEntryPP = null;
                    }
                    }

                    Also I am using Position.AvgPrice after the iOrder is set to null is that a problem?

                    Comment


                      #11
                      Hello GKonheiser,

                      Thanks for the snippets. This is because you are not declaring "lastTrade" as a "Trade" object like in the example, but yes using a count like that would work for what you would like.

                      You may want to use the "execution.Price" instead of the average. Note that you may not want to set your stop and target orders to null if the order is only PartFilled.

                      Using Position.AvgPrice after IOrder is null should not cause an issue. If you are in a position then it will return the average price no matter the IOrder, and if you are not in a position it should return 0.
                      JCNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_JC View Post
                        Hello GKonheiser,

                        Thanks for the snippets. This is because you are not declaring "lastTrade" as a "Trade" object like in the example, but yes using a count like that would work for what you would like.

                        You may want to use the "execution.Price" instead of the average. Note that you may not want to set your stop and target orders to null if the order is only PartFilled.

                        Using Position.AvgPrice after IOrder is null should not cause an issue. If you are in a position then it will return the average price no matter the IOrder, and if you are not in a position it should return 0.
                        Morning JC,

                        When I am using Position.AvgPrice in the code below will it give me the price I want, ie the entry price of iorder longEntryPP ? and execution.Order.AvgFillPrice would definatly be for the exit of the position??

                        I don't understand how it knows what is the entry price and the exit price??

                        I hope im making sense

                        Here is the code,

                        if (longEntryPP != null && longEntryPP == execution.Order)
                        {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                        // Stop order
                        stopLongEntryPP = ExitLongStop(0, true, execution.Order.Filled, pp - stop * TickSize, "Stop of Long PP", "Long Entry PP");


                        // Target order
                        targetLongEntryPP = ExitLongLimit(0, true, execution.Order.Filled, r1 - exitAllowance * TickSize, "Target of Long PP", "Long Entry PP");

                        // Resets the entryOrder object to null after the order has been filled
                        if (execution.Order.OrderState != OrderState.PartFilled)
                        {
                        longEntryPP = null;
                        }
                        }
                        }

                        if ((stopLongEntryPP != null && stopLongEntryPP == execution.Order) || (targetLongEntryPP != null && targetLongEntryPP == execution.Order))
                        {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                        {



                        if(Position.AvgPrice > execution.Order.AvgFillPrice)
                        {
                        badTradeCountPP++;
                        }

                        stopLongEntryPP = null;
                        targetLongEntryPP = null;
                        }
                        }

                        Comment


                          #13
                          Hello GKonheiser,

                          A NinjaScript strategy will keep its own virtual position when order are submitted and filled inside of this strategy, so it does keep track if you are in a long position or short position and at what prices it is filled.

                          So the what the Postion.AvgPrice is going to return is the average entry price of your Position. If you are trading 1 Contract it will be exact entry price, but if trading multiple contracts it may vary a bit but should be fairly close to see if the trade was losing.

                          Here is a link to our Help Guide that goes over the Position.AvgPrice that you may view as reference.



                          Let me know if you have any questions.
                          JCNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          666 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          377 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          110 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          575 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          580 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X