protected override void OnBarUpdate()
{
double sl;
int atr_Period = 100;
double point = 0.01;
sl = ATR(atr_Period)[0] * ATR_Mult * point;
SetStopLoss("",CalculationMode.Ticks,sl,false);
if (entryLCondition) { EnterLong(Lots,""); }
if (entrySCondition) { EnterShort(Lots,""); }
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
SetStopLoss problem
Collapse
X
-
SetStopLoss problem
My stop losses are opening and closing positions immediately. How do I fix this?
Code:Tags: None
-
I'm running it on GBP/JPY 5 minute chart. Sometimes it works properly. Sometimes it still closes immediately. The calculated value on 1/6/2009 at 4:45 was 0.795 according to my log. Calculating by hand, that's the value it's supposed to be (79.5 pips).Originally posted by NinjaTrader_Josh View Posttexasnomad,
I would suggest you print out what value that ATR calculation is giving you. When you use SetStopLoss() with CalculationMode.Ticks it would use integer values. So a value of 1 would be a very very close stop loss of 1 tick.
This is my currently updated code:
Code:protected override void OnBarUpdate() { double sl; int atr_Period = 100; double atr = ATR(atr_Period)[0]; sl = atr * ATR_Mult; Print(sl.ToString() + Time[0].ToString()); SetStopLoss("",CalculationMode.Ticks,sl,false); }
Comment
-
Now I'm passing in 79.5. It's still doing the same thing sometimes. I don't understand why it works on some trades and not on others.Originally posted by NinjaTrader_Josh View Posttexasnomad,
You definitely don't want to be passing in 0.795 into SetStopLoss(). That means you are using 0.795 of one tick as your stop loss which is really tiny. You should adjust the value to be in the proper scale you want.
Code:protected override void OnBarUpdate() { double sl; int atr_Period = 100; double point = 0.01; double atr = ATR(atr_Period)[0]; sl = atr * ATR_Mult / point; Print(sl.ToString() + Time[0].ToString()); SetStopLoss("",CalculationMode.Ticks,sl,false); }
Comment
-
Hi,
I think I have the same problem, trading CL.
When I add the "BreakevenCode" as below
the StopLoss is only about 2 cents above the entry price. I definitely know that the TickSize in CL is 0.01 and the variable BreakEvenShort is an integer (e.g. 50, but certainly not 2).Code:else if (Position.MarketPosition == MarketPosition.Short) { if (Close[0] > Position.AvgPrice - BreakEvenShort * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice); } }
For Long Positions, everything works fine....
Comment
-
Hi Josh,
here´s my output:
03.12.2009 15:18:00 Entered internal PlaceOrder() method at 03.12.2009 15:18:00: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Sell' FromEntrySignal=''
03.12.2009 15:18:00 Cancelled pending exit order, since associated position is closed: Order='NT-05685/Back101' Name='Profit target' State=Working Instrument='CL 01-10' Action=Sell Limit price=80,05 Stop price=0 Quantity=1 Strategy='MACDwithExit' Type=Limit Tif=Gtc Oco='NT-03464' Filled=0 Fill price=0 Token='9d2174dd41f245938e50b35b6e282038' Gtd='01.12.2099 00:00:00'
03.12.2009 15:18:00 Cancelled pending exit order, since associated position is closed: Order='NT-05684/Back101' Name='Stop loss' State=Working Instrument='CL 01-10' Action=Sell Limit price=0 Stop price=76,65 Quantity=1 Strategy='MACDwithExit' Type=Stop Tif=Gtc Oco='NT-03464' Filled=0 Fill price=0 Token='9f5dccc985954ddea6a2ebe0dd37aea8' Gtd='01.12.2099 00:00:00'
03.12.2009 15:19:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=76,84 Currency=0 Simulated=False
03.12.2009 15:19:00 Amended stop order: Order='NT-05688/Back101' Name='Stop loss' State=Working Instrument='CL 01-10' Action=BuyToCover Limit price=0 Stop price=76,84 Quantity=1 Strategy='MACDwithExit' Type=Stop Tif=Gtc Oco='NT-03466' Filled=0 Fill price=0 Token='e9594661c88e4c19a8fc2f624d02b477' Gtd='01.12.2099 00:00:00'
SL adjusted to Breakeven, Short
03.12.2009 15:19:00 Entered internal PlaceOrder() method at 03.12.2009 15:19:00: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Sell' FromEntrySignal=''
03.12.2009 15:19:00 Ignored PlaceOrder() method at 03.12.2009 15:19:00: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Sell' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
03.12.2009 15:19:00 Cancelled pending exit order, since associated position is closed: Order='NT-05689/Back101' Name='Profit target' State=Working Instrument='CL 01-10' Action=BuyToCover Limit price=73,84 Stop price=0 Quantity=1 Strategy='MACDwithExit' Type=Limit Tif=Gtc Oco='NT-03466' Filled=0 Fill price=0 Token='5b683aa349c84af6913e603f794e9b4f' Gtd='01.12.2099 00:00:00'
03.12.2009 15:19:00 Cancelled OCO paired order: BarsInProgress=0: Order='NT-05689/Back101' Name='Profit target' State=Cancelled Instrument='CL 01-10' Action=BuyToCover Limit price=73,84 Stop price=0 Quantity=1 Strategy='MACDwithExit' Type=Limit Tif=Gtc Oco='NT-03466' Filled=0 Fill price=0 Token='5b683aa349c84af6913e603f794e9b4f' Gtd='01.12.2099 00:00:00'
03.12.2009 15:20:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=40 Currency=0 Simulated=False
03.12.2009 15:20:00 Entered internal PlaceOrder() method at 03.12.2009 15:20:00: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Sell' FromEntrySignal=''
I have set back slippage to zero, so the stop loss for sell orders is exactly equal to the entry price. My code is the same as below. Sorry, still don´t get it..
Comment
-
This is my current code. You can see from the screenshots that sometimes it's working, sometimes it isn't. This doesn't make any sense to me.Originally posted by NinjaTrader_Josh View PostAnything in Control Center logs? Please make sure you submit whole numbers. Decimal places for ticks does not make sense. You want to either submit 79 or 80.
Code:protected override void OnBarUpdate() { double sl; int atr_Period = 100; double point = 0.01; double atr = ATR(atr_Period)[0]; sl = Math.Round(atr * ATR_Mult / point); Print(sl.ToString() + Time[0].ToString()); SetStopLoss("",CalculationMode.Ticks,sl,false); }
Comment
-
And no.. nothing in the logs folder. My prints look fine. Here's an example where it immediately entered and exited. It's passing 44 to the SetStopLoss function.
44 10/13/2009 12:45:00 PM
I also tried casting double sl to int in the SetStopLoss parameters. It made no difference.
SetStopLoss("",CalculationMode.Ticks,(int)sl,false );
Comment
-
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
649 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
370 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 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
573 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
576 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment