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 09-25-2007, 03:18 PM   #1
dgregor5
Senior Member
 
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
Default Dynamic Trailing Stop

Hi....
I am coding a dynamic trailing stop as per the following sample:

// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetTrailStop(CalculationMode.Ticks, TrailingStop10);
Trail =
166;
highestHigh = High[
0];
}

// If a long position is open, allow for stop loss modification.
elseif (Position.MarketPosition == MarketPosition.Long)
{
// if high of current bar greater than prior high, modify stop loss.
if (High[0] > highestHigh)
{
Trail = Trail - (Factor/
10 * (High[0] - highestHigh));

SetTrailStop(CalculationMode.Ticks, Trail);
highestHigh = High[
0];

}

The issue is that if the trailing stop value (Trail) < 0 AND the the high of the next bar is lower than that of the previous bar, then the trade exits at the current price of Trail - which is higher than any tick of the current bar. i.e. trade exits in mid-air !

Is this clear ??? ---it is difficult to explain without pictures. I am struggling to attached a screenshot.

thx
David



dgregor5 is offline  
Reply With Quote
Old 09-25-2007, 04:38 PM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

Yes, this is expected since your trail stop price in a real time market would have executed and the bar would look different.
NinjaTrader_Ray is offline  
Reply With Quote
Old 09-25-2007, 11:50 PM   #3
dgregor5
Senior Member
 
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
Default

thx for the response. To confirm then.....

In real time the trailing stop would have executed on the previous bar at the specific trailing stop point? (which is identified by the mid-air exit on the current bar) or at the openining price of the current bar. the reason this is important is because clearly it will effect the profit / loss of that particular trade.

thx
David
dgregor5 is offline  
Reply With Quote
Old 09-26-2007, 12:40 AM   #4
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

In real-time the trailing stop would have executed whenever the conditions were met. This generally means it will execute on the current bar, but please be aware that the current bar in real-time is loosely equivalent to the previous bar in a backtest. This is the case because in a backtest you are running everything as if you had CalculateOnBarClose = True. This means you only know if your conditions are met at the end of the bar and can only act on the start of the next bar with orders. The caveat you should be aware of is that the trailing stop stuff will execute even on the current bar regardless of when the order was filled because thats how it would normally behave. What I mean by that is it will exit if the conditions were met instead of waiting till the next bar to act.
Last edited by NinjaTrader_Josh; 09-26-2007 at 10:43 AM.
NinjaTrader_Josh is offline  
Reply With Quote
Old 09-26-2007, 05:35 AM   #5
dgregor5
Senior Member
 
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
Default

Josh -
I understand everything you have said (but I am still not clear), & I presume you mean CalculateOnBarClose = True rather than False.

By the way my strategy is coded with CalculateOnBarClose = True which means my backtest data should be very similar to my realtime data - correct?
Therefore, I still do not understand the answers to my questions below:

1/ My SetTrailStop is in the OnBarUpdate section which I assume means that it will only be updated at the close of the bar (when set to true) --- & this includes the SetTrailStop as well as the indicators.

2/ In real time, would the trailing stop have executed on the previous bar at the specific trailing stop point? ---- which I believe it should do (which is identified by the mid-air exit on the current bar). I am struggling to understand why I can have a mid-air exit on the current bar (higher than current bar price) --- which I assume is the trailstop exit point --- should that have been executed at a real-price on the current bar (i.e. lower that the mid-air position) or at a price on the previous bar.

The most fundamental question probably is the following....with CalculateOnBarClose=True, is the SetTrailStop function still triggered throughout the duration of that bar or only at the end of the bar IF SetTrailStop function is coded in the OnBarUpdate section of the code.

The reason this is important is because clearly it will effect the profit / loss of that particular trade.

thx in advance
David
dgregor5 is offline  
Reply With Quote
Old 09-26-2007, 07:02 AM   #6
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

1) If these methods are called before a position is opened then the orders generated by these methods are submitted in real-time as the entry order is filled or even part filled. If these methods are called with a different price, existing orders will be modified. This is true for when the method is called. So if called on the close of the bar, order prices are modified at the close of a bar.

2) In real-time, the orders are live at the exchange or broker so they are triggered when the market reaches the price which is independant of the bar. The executions of these orders will display at the bar time where the execution occured.
NinjaTrader_Ray is offline  
Reply With Quote
Old 09-26-2007, 09:12 AM   #7
dgregor5
Senior Member
 
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
Default

thx - one more question. is it possible to dynamically change the stop loss via the 'initialise' sequence - e.g. using similar code as below:

// Resets the stop loss to the original value when all positions are closed
if
(Position.MarketPosition == MarketPosition.Flat)
{
SetTrailStop(CalculationMode.Ticks, TrailingStop10);
Trail =
166
;
highestHigh = High[
0
];
}

// If a long position is open, allow for stop loss modification.
elseif
(Position.MarketPosition == MarketPosition.Long)
{
// if high of current bar greater than prior high, modify stop loss.
if (High[0
] > highestHigh)
{
Trail = Trail - (Factor/
10 * (High[0
] - highestHigh));

SetTrailStop(CalculationMode.Ticks, Trail);
highestHigh = High[
0
];
dgregor5 is offline  
Reply With Quote
Old 09-26-2007, 09:20 AM   #8
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

Please check out our educational resource here: http://www.ninjatrader-support.com/v...ead.php?t=3222
NinjaTrader_Dierk is offline  
Reply With Quote
Old 09-26-2007, 10:43 AM   #9
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

The Initialize sequence will only be executed once at the very being when you load the indicator. You can "start" the SetStopLoss() in the Initialize() section and then you can make dynamic updates to it through the OnBarUpdate() section.

The reference sample Dierk linked you to is an example of just that.

P.S. Thanks for the catch on the typo.
NinjaTrader_Josh is offline  
Reply With Quote
Old 09-26-2007, 01:58 PM   #10
dgregor5
Senior Member
 
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
Default

Have checked this out tonight in real time vs. backtest mode:

1/ In Realtime mode with CalculateOnBarClose=True. If SetTrailStop value of <= 0 achieved at close of prior bar (i.e. had created a higher high), trade exited at current bar open price minus one. This is as I would expect.

2/ In Backtest mode with CalculateOnBarClose=True. If SetTrailStop value of <=0 achieved at close of prior bar (i.e. had created a higher high), trade exited in mid-air if high[0] is lower than high[1].

This clearly creates a discrepency in profit/loss with realtime vs. backtest mode.

Am I being stupid ?
dgregor5 is offline  
Reply With Quote
Old 09-26-2007, 02:01 PM   #11
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

In real-time, do you mean that the stop loss order triggered on bars built in real-time (incoming market data) or do you mean on historical data?
NinjaTrader_Ray is offline  
Reply With Quote
Old 09-26-2007, 02:12 PM   #12
dgregor5
Senior Member
 
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
Default

Ray - incoming market data
dgregor5 is offline  
Reply With Quote
Old 09-26-2007, 03:14 PM   #13
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

Thanks.

You can not compare a live order (real-time) with results from a backtest. A live order is triggered by the market/exchange or market/simulation engine. Backtest is its own separate enginer that aproximates fills based on bar data.
NinjaTrader_Ray is offline  
Reply With Quote
Old 09-26-2007, 03:21 PM   #14
dgregor5
Senior Member
 
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
Default

understood, but the fill for the backtest is irrational !?!?! - i.e. why does it not exit within the limits of the high-low of the current bar when there has been a 'highest high' on the prior par. thx. DG
dgregor5 is offline  
Reply With Quote
Old 09-26-2007, 03:28 PM   #15
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

Its not irrational. If you have a sell stop at price X and the next bar gaps down, we fill you at price X and not the high of the gap down bar. In rea-time, this is what would happen. Visually, you will see a fill in mid air on the bar where the fill occured.
NinjaTrader_Ray 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
Switching from stop to trailing MrBaffalo Strategy Development 1 02-02-2007 03:33 AM
Dynamic Trailing Stops whitmark Suggestions And Feedback 4 05-09-2006 11:57 PM
Can't get trailing stop Savage1701 Automated Trading 1 04-07-2006 08:03 AM
Trailing Stop Question underground Miscellaneous Support 3 06-06-2005 07:11 AM
Dynamic Stop and Adjust OIF scjohn Automated Trading 19 03-24-2005 04:37 AM


All times are GMT -6. The time now is 09:00 PM.