![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM 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: 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,163
Thanks: 6
Thanked 45 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,163
Thanks: 6
Thanked 45 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 |
|
|
|
![]() |
| 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 |