![]() |
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
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Mar 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Mar 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Mar 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#7 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#9 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#11 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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...
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#12 |
|
Senior Member
Join Date: Mar 2008
Posts: 105
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#13 |
|
Certified NinjaScript Consultant
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
|
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 |
|
|
|
|
|
#14 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Certified NinjaScript Consultant
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
|
Ray.
Will the OnMarketData() method get called for every tick even if my strategy is end-of-bar? |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |