if (Position.MarketPosition == MarketPosition.Long)
{
SetStopLoss(CalculationMode.Price, Close[0]-2*ATR(3)[0];
}
if (Position.MarketPosition == MarketPosition.Flat)
{
if (//conditions)
{
EnterLong();
}
}
if (Position.MarketPosition == MarketPosition.Short)
{
SetStopLoss(CalculationMode.Price, Close[0]+2*ATR(3)[0]);
}
if (Position.MarketPosition == MarketPosition.Flat)
{
if (//Conditions )
{
EnterShort();
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
SetStopLoss not triggering
Collapse
X
-
SetStopLoss not triggering
I have set my StopLoss as follow but it is not triggering
Thanks.Code:Tags: None
-
Hello,
Thank you for the post.
Generally you would need to call SetStopLoss prior to the entry to set up the order. If this has not been called earlier in OnBarUpdate or in State.Configure, you likely need to add in a call to SetStopLoss prior to the entries.
Can you confirm if you change the logic to the following, do you see it place the stop?
I look forward to being of further assistance.Code:if (Position.MarketPosition == MarketPosition.Flat) { if (//conditions) { SetStopLoss(CalculationMode.Price, Close[0]-2*ATR(3)[0]; EnterLong(); } else if (//Conditions ) { SetStopLoss(CalculationMode.Price, Close[0]+2*ATR(3)[0]); EnterShort(); } }
-
Hello,
Thank you for testing that.
It sounds like the stop is working for you if you got some stops placed, it just seems they are not at the price or time you had expected, is this correct?
Would you be able to provide a more complete sample of the logic?
Without seeing your script I can only guess what may be happening, I tried the following and can see that the stop is both placed consistently. Could you try a very basic test like the following to see if the stop is placed for you after entering?
If the result is different than the current script, this is likely to do with the logic being used and in that case a more complete sample would be helpful.
Code:protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Flat) { SetStopLoss(CalculationMode.Price, Close[0]-2*ATR(3)[0]); EnterLong(); } }
I look forward to being of further assistance.
Comment
-
Jesse,
Thanks for the detailed explanation. I tried the SetStopLoss on the following simple script without getting it to execute.
Also, where in the script can I add a "DrawText" so it will print after the SetStopLoss executes?Code:{ if (Position.MarketPosition == MarketPosition.Flat) { if (BarsSinceExit()!=0 && SMA(12)[0]>SMA(20)[0]) { SetStopLoss(CalculationMode.Price, Close[0]-2*ATR(3)[0]); EnterLong(); } } if (Position.MarketPosition == MarketPosition.Flat) { if (BarsSinceExit()!=0 && SMA(12)[0]<SMA(20)[0] ) { SetStopLoss(CalculationMode.Price, Close[0]+2*ATR(3)[0]); EnterShort(); } } }
Code:DrawText(""+CurrentBar, Math.Round(TradeProfit,2).ToString(), 0, Low[0] - (TickSize *5), Color.Cyan);
Comment
-
Hello,
Not a problem I went ahead and moved this into the correct forum, I just wanted to make sure we were talking about the same thing.
I tried the code in NT7, I do see this placing stops or placing a stop that is not filled and exiting on close. Are you seeing no stops placed at all? http://screencast.com/t/XyHVF0CS
If you are seeing no Filled stops, potentially they are being placed at prices that will not fill, are you only seeing exit on close and entries? Can you provide an image of the chart you are seeing?
I look forward to being of further assistance.
Comment
-
Hello,
It appears in the image there were stops placed and filled, I can also see that this is the case when running the script.
Are you expecting different placement of the stops or different fills? Could you tell me what you are expecting in this case because it appears the stop is in fact working.
For the question related to DrawText, you could check the Execution information from OnExecution:
I look forward to being of further assistance,
Comment
-
In the script I have the Stop Loss that's supposed to exit the long position when the price crosses below the ATR trailing stop line or to exit the short position when the price crosses above the ATR trailing stop line. This is not occurring as the stops when executed, are mostly random or at the end of day.
I was expecting Stops every time the price crosses the ATR trailing stop line. Did I go wrong with the logic or the location of the SetStopLoss? Thanks again for your time.
Comment
-
Hello,
Thank you for the reply.
I believe this may be a confusion of the methods needed for the concept.
You said:
If you want to exit at a specific condition you could call ExitLong or ExitShort. The SetStopLoss would need to be set before the entry and it would be some amount of Price, Percent or Ticks away from the price. That means depending on the market price, it could fill now or later.exit the long position when the price crosses below the ATR trailing stop line or to exit the short position when the price crosses above the ATR trailing stop line
I believe to correct the logic, rather than using SetStopLoss, use ExitLong or ExitShort in the condition for exit. You could leave the other entry logic alone, mainly as long as Exit is not called in the same OnBarUpdate as an Enter it should just exit the position at that point.
I look forward to being of further assistance.
Comment
-
If you refer to something like the following, then my ExitLong condition is not consistent with the ATR trailing stop because it isn't executing. What would be the proper syntax for both the ExitLong and ExitShort?
Code:if (Position.MarketPosition == MarketPosition.Flat) { if (Condition ) { EnterLong(); } } if (Position.MarketPosition == MarketPosition.Long) { if(Close[0]<(Close[0]-2*ATR(3)[0])) { ExitLong(); } }
Comment
-
Hello,
Try using a Print to see whats going on:
If no exit is happening at all, it is likely the value is not equating as true. This type of print would just show if the Position is long, and if so there should be a print right after "Is Long" showing the Close price < Close[0]-2*ATR(3)[0]Code:if (Position.MarketPosition == MarketPosition.Long) { Print("Is Long"); Print(Close[0] + " < " + (Close[0]-2*ATR(3)[0])); if(Close[0]<(Close[0]-2*ATR(3)[0])) { ExitLong(); } }
If the value of the close is less than the calculated value and still no exit happens, I would suggest trying TraceOrders: http://ninjatrader.com/support/helpG...ub=traceorders
I look forward to being of further assistance.
Comment
-
The two print statements to the output window are consistent with the values, yet still no exit happens.
The TraceOrders = true; will printout a buy order consistent with the strategy but no exits based on Close[0]<(Close[0]-2*ATR(3)[0]). The only exit is at the close of the day as shown in TraceOrders.
I agree with your statement, but why is that? I also tried the logic as part of an indicator instead of a strategy and tried BackColor whenever it was true, but nothing happened either.HTML Code:If no exit is happening at all, it is likely the value is not equating as true
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