![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#2 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
|
Yes, this is expected since your trail stop price in a real time market would have executed and the bar would look different.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#4 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service
Last edited by NinjaTrader_Josh; 09-26-2007 at 10:43 AM.
|
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#6 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
|
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]; |
|
|
|
|
|
#8 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
Please check out our educational resource here: http://www.ninjatrader-support.com/v...ead.php?t=3222
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
|
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 ? |
|
|
|
|
|
#11 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
|
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?
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#12 |
|
Senior Member
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
|
Ray - incoming market data
|
|
|
|
|
|
#13 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#14 |
|
Senior Member
Join Date: Jul 2007
Posts: 225
Thanks: 0
Thanked 0 times in 0 posts
|
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
|
|
|
|
|
|
#15 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |