Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Counting Losing trades
Collapse
X
-
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?Tags: None
-
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
-
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
-
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
-
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
-
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
-
-
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
-
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
-
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
-
Morning JC,Originally posted by NinjaTrader_JC View PostHello 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.
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
-
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
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
377 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
110 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
575 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
580 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment