protected override void OnBarUpdate()
{
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, StopInitial);
}
// If a long position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is between ProTgt1BE and ProfTgt2, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + ProfTgt1BE * TickSize
&& Close[0] < Position.AvgPrice + ProfTgt2 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
// Once the price gets above ProfTgt2, set stop loss to Stop2 level
if (Close[0] >= Position.AvgPrice + ProfTgt2 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + Stop2 * TickSize);
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Multiple stoploss price modifications
Collapse
X
-
Multiple stoploss price modifications
Using your example for the stoploss price modification to breakeven, I attempted to create a multiple price modification for the stoploss at increasing profit targets. Due to my embarassingly slow progress in NS code, I can't figure out how to resolve a fairly obvious problem. I have set up the first modification to move the stop to breakeven at profit target BE and then have a second profit target 2 where it is again adjusted. It works to that extent but has an obvious problem if the price reverses back below my profit target 2 and satisfies my lower profit target BE 'if' conditions. I can think of many ways to solve this from the logic standpoint, I just don't know where to begin from the NS coding aspect. (set a "condition=" type variable, etc.) Thanks for your help.
Code:Tags: None
-
Hi billr,
You are correct in that there are many way to do this. One suggestion is you may want to consider using a bool statement.
For example:
Check for a bool true before the 1st modification
Set the bool to false after the 1st modification
just be sure to reset the bools once you're done.TimNinjaTrader Customer Service
-
Hi Bill, I would do the following changes adding a double variable ( tmpStop );
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, StopInitial);
tmpStop = 0;
}
// If a long position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is between ProTgt1BE and ProfTgt2, set stop loss to breakeven
if (High[0] > Position.AvgPrice + ProfTgt1BE * TickSize
&& High[0] < Position.AvgPrice + ProfTgt2 * TickSize)
{
tmpStop = Math.Max(tmpStop, Position.AvgPrice );
SetStopLoss(CalculationMode.Price, tmpStop);
}
// Once the price gets above ProfTgt2, set stop loss to Stop2 level
if (Close[0] >= Position.AvgPrice + ProfTgt2 * TickSize)
{
tmpStop = Math.Max(tmpStop, Position.AvgPrice + Stop2 * TickSize);
SetStopLoss(CalculationMode.Price, tmpStop);
}
}
Comment
-
Thanks Tim. That is what I had in mind but I am not sure how to do the details of this. I have to admit I find the NS documentation difficult to navigate. Many of the the very basic but important details seem to elude me. All my searches usually lead to 'no result'. Is there an online manual somewhere? I tend to check in the NT7 documentation. Is there a v6.5 that has more complete documentation on syntax and rules of NS basics.
Using your reply for instance, where can I find the ABC's of how to set that framework up? Syntax, what is required in the variables and parameters areas, etc. Thanks again.
Comment
-
Hello pmaglio,
thanks for your suggestion, including the one to use High[] instead of Close[]. (I used Close only because this strat was going to be run in renko charts, but I will make this change too) My lack of knowlege in NS has kept me from trying your suggested logic. For instance, I don't know what, if anything, is required elsewhere when introducing the tmpStop=0. Do I need to add something to the Variable section or Parameters? I also don't know what is happening or where to use Math.Max. So as you can see I am a rank amateur at this. But thank you for the lead on how to do this. I think you may have given me hints on how to solve several problems. Thanks again.
Comment
-
Hi billr,
Here's what I suggest, create an indicator with the wizard "Tools>New NinjaScript>Indicator" and create a bool type in the third step. From there you can view the code and see how the bool statement is used in the "Variables" region, as well as the "Properties" region.
More on basic syntax here: http://www.ninjatrader-support.com/H...ml?BasicSyntaxTimNinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
578 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
334 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 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
554 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
551 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment