// Add these fields
private int consecutiveLosses = 0;
private int maxConsecutiveLosses = 3; // Adjustable parameter
// Add to OnExecutionUpdate after checking if a position was closed
if (execution.Order.OrderState == OrderState.Filled &&
(execution.Order.OrderAction == OrderAction.BuyToCover ||
execution.Order.OrderAction == OrderAction.Sell))
{
// Check if this was a loss
if (execution.Order.ProfitLoss < 0)
{
consecutiveLosses++;
Print($"Loss detected - consecutive losses: {consecutiveLosses}");
if (consecutiveLosses >= maxConsecutiveLosses)
{
Print("Max consecutive losses reached - pausing trading");
// Skip trading until next session
}
}
else
{
// Reset on a win
consecutiveLosses = 0;
}
}
// Reset at start of new session
private DateTime lastSessionDate = DateTime.MinValue;
// Add to OnBarUpdate
if (lastSessionDate.Date != Time[0].Date)
{
lastSessionDate = Time[0].Date;
consecutiveLosses = 0;
Print("New session - resetting consecutive loss counter");
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to implement Consecutive Loss Prevention?
Collapse
X
-
How to implement Consecutive Loss Prevention?
Hi there, I want my strategy tto stop trading for the day after 3 consecutive losses. ChatGPT suggests this, but I haven't had much luck with the GPTs (Claude and Chat) on this front, to be honest (not a developer). Can anyone point me in the right direction?
Code:Tags: None
-
Hello money0101,
While our support cannot assist with AI generated code you can use the Prints it added to debug what that logic is doing when its run. You would need to open the NinjaScript output window to see what those prints say when that code is run.
AI disclaimer:
From our experience at this time, ChatGpt and other AI models are not adequate at generating valid NinjaScript code that function as the user has intentioned. We often find that these tools generate code that will call non-existent properties and methods, use improper classes or inheritance, and may have incorrect logic. In addition any code that does compile may not work efficiently or correctly based on the NinjaScript coding guidelines. Using these tools for general NinjaScript learning information may also provide incorrect responses. We highly encourage that you create a new NinjaScript yourself using the NinjaScript Editor and avoid any AI based coding tools.
While it would not be within our support model to assist with scripts generated or checked by ai tools, we would be happy to provide insight for any direct specific inquiries you may have if you would like to create this script yourself. Our support is able to assist with finding resources in our help guide as well as simple examples, and we are happy to provide guidance on how you can debug the script while running it on your machine. To start learning NinjaScript I would suggest the following link as a starting point.
https://support.ninjatrader.com/s/ar...language=en_US
You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
20 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
119 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
63 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
41 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
45 views
0 likes
|
Last Post
|

Comment