NinjaTrader Support Forum  

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 04-06-2008, 10:50 AM   #1
Sidhartha
Senior Member
 
Join Date: Mar 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
Default Going from a Stop Loss to a Trailing Stop within a strategy

Hi All,

I'm writing my first NinjaScript strategy... I'm wanting to set a firm price level stop on an opened position initially (either long or short) and then change the stop to a trailing stop after a specified length of time. Is there a way of doing this...??

As I understand it if I have a stop set using SetStopLoss() it will always take precedence over another instruction to then use the SetTrailStop() method...??

So am I right in thinking that once I have called SetStopLoss() and subsequent calls to SetTrailStop() will be ignored...? If this is the case, could you point to how I can go from a firm stop to a trailing stop...?

Many Thanks
Sidhartha is offline  
Reply With Quote
Old 04-06-2008, 01:44 PM   #2
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

This is possible in NT6.5 with the use of IOrder objects and the OnOrderUpdate() method. Please see this reference sample: http://www.ninjatrader-support.com/v...ead.php?t=3917

The idea is you don't use the Set() methods anymore and do everything manually. You can modify your stop prices whenever you decide.
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-07-2008, 04:24 AM   #3
Sidhartha
Senior Member
 
Join Date: Mar 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks Josh.

I've looked at the code in this example, and it looks ok. Famous last words etc.

One question, instead of using the methods you outline here, could I not just continue to call (with a new price) the SetStopLoss() method on each OnBarUpdate()....?? Would this not simulate a trailing stop if the price I passed to it was trailing...??

Thanks
Sidhartha is offline  
Reply With Quote
Old 04-07-2008, 07:07 AM   #4
wayneFH
Senior Member
 
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
Default

Sidartha,

FYI, I tried various ways of implementing a trailing stop.

Problem with those other options Josh mentioned is that:

1. NT won't let you enter another stop with the current stop still working.
2. So you have to cancel your stop loss and THEN enter the trailing stop. DANGEROUS, in my opinion because if software/hardware or network fails, you're left without a stop.

A safer way to handle trailing stops is simply to change your original stop loss. NT handles that very well as a change order than cancels and replaces the stop order all in one shot. So it's less risk you're left without a stop.

If you originally call SetStopLoss(5) for a 5 pip stop loss, you can later call it SetStopLoss(-5) to raise it to a 5.

What I do is check the Profit or Loss of the position on every tick and raise the Stop loss accordingly.

In other words, you can implement you're on trailing stop. In fact, when I Did this, I found a far more profitable logic for the trailing stop than the simple logic built into NT.

Hopefully this helps.

Sincerely,
Wayne
wayneFH is offline  
Reply With Quote
Old 04-07-2008, 07:12 AM   #5
Sidhartha
Senior Member
 
Join Date: Mar 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks Wayne.

So if I understand you right, you are saying that repeatedly calling the SetStopLoss() method for an open position has the same effect of trailing the stop loss...?? And in fact it's potentially better than the methods outlined by Josh because it simply amends the existing Stop...?

Many Thanks
Sidhartha is offline  
Reply With Quote
Old 04-07-2008, 08:03 AM   #6
wayneFH
Senior Member
 
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
Default

Correct. Try it. You'll see in the logs that it cancels the order and replaces it. I verify everything in the logs.

It also seems simpler than dealing with the IOrder interface.

Try it both ways. I was pleasantly surprise and happy that SetStopLoss() accepts negative numbers so that properly handles changing the stop higher and higher.

Sincerely,
Wayne
wayneFH is offline  
Reply With Quote
Old 04-08-2008, 12:03 AM   #7
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

There should be no cancellation of the stop order in the IOrder interface methodology. The stop order is simply modified just like how you are modifying your SetStopLoss(). They should both behave the same.

Since there are many ways to tackle a problem, by all means, take whichever approach feels more comfortable to you.
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-08-2008, 05:48 AM   #8
wayneFH
Senior Member
 
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
Default

Josh, in 6.5.0.10, the SetStopLoss() method returns a void.

So is it even possible to modify it with the IOrder interface?

I think that's the other reason I did this way. Maybe you need to change the software to return an IOrder on the SetStopLoss() SetProfitTarget(), and SetTrailStop().

I couldn't figure out any way to "cancel" or change them which is quite unfortunate if that's the case.

Sincerely,
Wayne
wayneFH is offline  
Reply With Quote
Old 04-08-2008, 07:22 AM   #9
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Set() methods can not be cancelled. If you require flexibility of cancellation, instead of using the Set() methods, use Exit() methods but trigger their placement from OnOrderStatus() monitoring for filled event from the associated entry order. This acomplishes the same thing as a Set() method. One exception is SetTrailStop() in which case you would need to self code this logic in OnBarUpdate() or OnMarketData() for real-time only adjustments.
NinjaTrader_Ray is offline  
Reply With Quote
Old 04-08-2008, 07:28 AM   #10
wayneFH
Senior Member
 
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks. Those are exactly the reason why, it's far easier to simply use the SetStopLoss() and raise it using custom trailing stop logic. It take far fewer lines of code to write and debug.

Suggestion for next version. Why not return the IOrder from the Set() methods? Since they return void now, it won't impact anyone's existing code to return the IOrder so developers have more control.

Just a feature suggestion.

Sincerely,
Wayne
wayneFH is offline  
Reply With Quote
Old 04-08-2008, 07:31 AM   #11
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Unfortunately it is not as simple as just passing back an IOrder object since there is internal logic in the Set() methods. Any external intervention could cause unexpected behaviour.

I do see the convenience for sure...
NinjaTrader_Ray is offline  
Reply With Quote
Old 04-08-2008, 07:36 AM   #12
wayneFH
Senior Member
 
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
Default

Understood and that makes sense. So it seems fine as it is now. Returning IOrder would only have been icing on the cake.

Sincerely,
Wayne
wayneFH is offline  
Reply With Quote
Old 07-08-2008, 08:57 PM   #13
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

Hey Guys.

Is it possible to start with a trailing stop, then submit a stop loss at a certain point which may be overridden by an actual exit call within an NT strategy?

I want to have SetTrailStop() within my initialize() method. Once a certain point is reached I can leave the trailing stop in, but I want to submit a SetStopLoss() value to always stay as a break even. If that stop is not hit, I will use an ExitLong / ExitShort call to exit the position.

Is this logical? Can I submit a trailing stop initially then use a stop loss that will supersede my trail?

Thanks
Anthony
mrlogik is offline  
Reply With Quote
Old 07-08-2008, 09:06 PM   #14
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Unfortunately not. To what you want you are best monitoring OnMarketData() and writing your own code for submission, trail etc... based off on incoming market data.
NinjaTrader_Ray is offline  
Reply With Quote
Old 07-08-2008, 09:07 PM   #15
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

Ray.

Will the OnMarketData() method get called for every tick even if my strategy is end-of-bar?
mrlogik 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
stop loss, trailing stop modification in NT6 alfie Strategy Development 1 03-26-2008 03:46 PM
Order Entry Property: Use stop market for stop loss orders higler SuperDOM and other Order Entry Windows 7 07-02-2007 09:06 AM
Re: Setting a trailing stop & ATM Strategy mikesbarrett SuperDOM and other Order Entry Windows 1 06-29-2007 08:57 AM
Need help automating trailing stop strategy oriondesign Automated Trading 1 05-28-2007 01:50 PM
Stop Loss Orders activated by number of trades past stop level DoveforUsAll Suggestions And Feedback 1 02-08-2006 01:19 AM


All times are GMT -6. The time now is 11:36 PM.