NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 03-27-2012, 07:03 PM   #1
coolmoss
Senior Member
 
Join Date: Dec 2007
Posts: 365
Thanks: 36
Thanked 28 times in 24 posts
Default Why is setstop resetting when I don't think it should be?

I'm trying to understand why a stop loss order (set via SetStopLoss) is getting reset twice on the same bar (I'm printing times out to the output.txt file). Strategy is calc on bar close. The only place in which the stop is modified is in this block of code:

Code:
if(Position.MarketPosition == MarketPosition.Short)
						{
							if (Position.MarketPosition == MarketPosition.Short && Low[0] <= theoEntry - oneDone*TickSize && stopControl)
								{
								Print("stop updated at One & Done tick increment in the money"+ Time[0].ToString());
								stopPrice = theoEntry + entryOffset*TickSize;
								SetStopLoss(CalculationMode.Price,stopPrice);// one & done stop move
								stopControl = false; //this can only be reset to true if flat at end of bar on primary series
								}
							// MomDot stop loss adjustment	
							if (Position.MarketPosition == MarketPosition.Short && Close[0] > rhMomDot().Plot0[0] && High[0] + entryOffset*TickSize < stopPrice && Close[0] > Open[0])
								{
								Print("stop updated at momdot"+ Time[0].ToString());
								SetStopLoss(CalculationMode.Price,High[0] + entryOffset*TickSize);//momdot stop move
								//stopPrice = High[0] + entryOffset*TickSize;
								}
							else
								{
								SetStopLoss(CalculationMode.Price,stopPrice);//reset stop loss price to One&Done or Initial Stop
								Print("stop RESET to stopPrice after momdot exit not elected"+ Time[0].ToString());
								}
}
The strategy is in a state where the position is short, and the above code is the only code that runs when strategy is short. The output.txt shows the stop being reset to a new price, followed by the "Amended stop order" lines, then immediately resets again to the price it was previously at.

I'm totally baffled.
coolmoss is offline  
Reply With Quote
Old 03-28-2012, 04:19 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
Default

coolmoss, the strategy runs on historical and realtime data, correct?

Do you reset the stoploss then to a tick default setting for example when you're flat with the strategy? This would be needed as otherwise an historically used / set stop value could be reused for a subsequent entry.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-28-2012, 07:28 AM   #3
coolmoss
Senior Member
 
Join Date: Dec 2007
Posts: 365
Thanks: 36
Thanked 28 times in 24 posts
Default

I do reset stopPrice to a new value when flat AND a new entry signal appears. And that works okay.

What is happening in this instance, is I'm changing the stop loss (via SetStopLoss) to a new value, and output.txt shows that new value in amended order. But then, on the same bar, with the position unaltered since beginning of bar, and calc'd on bar close, the stop gets changed again. It looks like this:

stop updated at momdot3/27/2012 9:14:55 AM
3/27/2012 9:14:55 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.28 Currency=0 Simulated=False
3/27/2012 9:14:55 AM Amended stop order: Order='NT-00177/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00116-746' Filled=0 Fill price=0 Token='a7b113f0dc2946b280b9f1e7facdca85' Gtd='12/1/2099 12:00:00 AM'
3/27/2012 9:14:55 AM Amended stop order: Order='NT-00179/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00117-746' Filled=0 Fill price=0 Token='1bc5252517c04ccdad658c4fb2a0bb8e' Gtd='12/1/2099 12:00:00 AM'
3/27/2012 9:14:55 AM Amended stop order: Order='NT-00181/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00118-746' Filled=0 Fill price=0 Token='a4f4756b46784ab295915bc7ea69acc2' Gtd='12/1/2099 12:00:00 AM'
3/27/2012 9:14:55 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.34 Currency=0 Simulated=False
3/27/2012 9:14:55 AM Amended stop order: Order='NT-00177/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00116-746' Filled=0 Fill price=0 Token='a7b113f0dc2946b280b9f1e7facdca85' Gtd='12/1/2099 12:00:00 AM'
3/27/2012 9:14:55 AM Amended stop order: Order='NT-00179/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00117-746' Filled=0 Fill price=0 Token='1bc5252517c04ccdad658c4fb2a0bb8e' Gtd='12/1/2099 12:00:00 AM'
3/27/2012 9:14:55 AM Amended stop order: Order='NT-00181/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00118-746' Filled=0 Fill price=0 Token='a4f4756b46784ab295915bc7ea69acc2' Gtd='12/1/2099 12:00:00 AM'
3/27/2012 9:16:23 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.34 Currency=0 Simulated=False

You can see at first the price is changed to 22.28, then in the same bar it's changed back to 22.34 (what it had just been). No where else in the code is there is SetStopLoss statement except where a condition exists that position is flat.

This is only running on historical data, I'm not using this strategy for real time.
coolmoss is offline  
Reply With Quote
Old 03-28-2012, 07:36 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
Default

coolmoss, looks like you have 2 conditions for the bar update that would be called sequentially allowing for a SetStopLoss modification - the first one leasing to 22.28 as stoploss and second one to the 22.34.

I would suggest debugging your code to ensure only the intended condition is hitting and allowing the stop loss to be moved, this could be achieved for example via bool flags to streamline the code / flow control as you desire.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-28-2012, 08:29 AM   #5
coolmoss
Senior Member
 
Join Date: Dec 2007
Posts: 365
Thanks: 36
Thanked 28 times in 24 posts
Default

Bertrand,

I thought that's what I was doing with the Print statements. If you note the section with an IF and an Else, they both have Print statements, and in the output.txt window, only one of those statements prints for each bar. I thought that meant only one or the other of the IF/Else was executing.

I have Print statements at every location in the code where I have a SetStopLoss, yet not one of those Print statements executes at the portion of the output.txt window where the stop loss is getting reset incorrectly.

I guess I would have this question then: if the stop loss is not being reset by one of my SetStopLoss, what else could be resetting it?

Thanks much for your assistance.
coolmoss is offline  
Reply With Quote
Old 03-28-2012, 08:40 AM   #6
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
Default

coolmoss, I would recommend you remove code sections until you experience first of all a state that aligns with your expectations when running the script, only then include more complex logic. The stop loss would only be moved again if you call this in your OnBarUpdate() code, there's no way around. In the print you set please also incluce a timestamp and CurrentBar, so you can easily align the prints triggered with the corresponding trades.
NinjaTrader_Bertrand is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing SetStop value coolmoss General Programming 1 03-09-2012 05:17 AM
Resetting Defaults kenb2004 Strategy Development 3 01-05-2011 11:15 AM
Resetting Variables richp6 Strategy Development 4 12-30-2010 09:10 AM
Resetting Sim101 cgeorgan Version 7 Beta General Questions & Bug Reports 1 08-11-2010 09:10 AM
Resetting a variable trader413 Strategy Development 8 09-14-2009 10:12 AM


All times are GMT -6. The time now is 02:53 PM.