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
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
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
-
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
-
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
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
-
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 benmarkal, Yesterday, 08:44 AM
|
1 response
30 views
0 likes
|
Last Post Today, 03:22 PM | ||
Started by somethingcomplex, Yesterday, 10:36 PM
|
1 response
23 views
0 likes
|
Last Post Today, 03:12 PM | ||
Started by rayyyu12, Today, 12:47 AM
|
1 response
14 views
0 likes
|
Last Post Today, 03:06 PM | ||
Started by guyonabuffalo, Yesterday, 10:01 PM
|
1 response
11 views
0 likes
|
Last Post Today, 03:01 PM | ||
Started by thumper57, Yesterday, 04:30 PM
|
3 responses
12 views
0 likes
|
Last Post
by thumper57
Today, 02:52 PM
|
Comment