![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Mar 2009
Posts: 66
Thanks: 0
Thanked 0 times in 0 posts
|
The indicator under consideration is a variation of the Chandelier stop and reverse that works off of high/low rather than close (thus allowing use of stop limit orders instead of cross-over triggered orders that are subject to large amounts of slippage.)
Let me repeat, using the "CrossAbove" or "CrossBelow" commands are a non-starter. I will not consider implementing them in the strategy due to slippage, so don't even go there. The problem: The trade is active long (due to the same problem but for the purpose of visual illustration let us concentrate on the exit). The indicator has calculated the value of 1103.13 for the exit long stop limit value. The market dropped to 1103.25 and triggered the stop and exited the trade despite never touching 1103.13, thus kicking me out of a trade that would have made a decent amount of money. Yes I know that NQ (the financial instrument) trades only in quarter points, but since it never touched 1103.13, let alone 1103.00 the order should not have occured. ![]() How can I adjust the strategy to not execute such trade until it physically touches the order level? Is this a question of setting an offset of .0125 (assuming a Ninjatrader half tick rounding)? I never really understood how "offset" was supposed to work. ![]() This problem is not unique to this indicator or strategy. The same issue arises from the Parabolic SAR. Any thoughts or suggestions on how to address this in the strategy? |
|
|
|
|
|
#2 |
|
Junior Member
Join Date: Nov 2009
Posts: 24
Thanks: 0
Thanked 0 times in 0 posts
|
I suspect your stop was rounded to the closest tick to make a valid stop:
i.e. an NQ stop could only be .00 or .25 .13 is closer to .25 than .00, and so likely became .25 You'll probably need to have some code to handle your calculated stop and turn it into the valid stop you want (i.e. .24 and lower should be set to .00) This line might be a starting test at least: double myActualStop = Bars.Instrument.MasterInstrument.Round2TickSize (calculated_stop) |
|
|
|
|
|
#3 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,559
Thanks: 261
Thanked 1,015 times in 996 posts
|
Thanks SIFTrader, I would suggest debugging with TraceOrders and Prints to see at exactly which level the stop was placed - if the rounding is the issue, which seems logical - Round2TickSize would be the way to go to make the order valid -
http://www.ninjatrader-support.com/H...2TickSize.html
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Feb 2010
Posts: 2
Thanks: 0
Thanked 0 times in 0 posts
|
Question, would that rounding be taking place at the indicator or strategy level? If it is taking place at the indicator level, wouldn't it inadvertently cause a miscalculation of the indicator?
|
|
|
|
|
|
#5 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Timelord,
Rounding is done wherever that line of code is placed. On the chart visually, the numbers may be rounded, but if you actually print out the number it would be unrounded unless explicitly rounded via code.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Member
Join Date: Mar 2009
Posts: 66
Thanks: 0
Thanked 0 times in 0 posts
|
If rounding is done wherever it was placed, then couldn't that induce an error in the indicator?
Say it is a short strategy on the NQ. The indicator calculated value is 1793.43. Rounding would place the indicator (and strategy) at 1793.5. The market data dips exactly to 1793.50 triggering an entry. ![]() But wait, the indicator should not reverse because the raw 1793.43 threshold has not been passed. The market price moves away and the indicator again moves to follow it reaching. 1793.88. The market data then moves past that level and should trigger the trade. ![]() It may only be half a point but that adds up fast. I don't see how a pure rounding at the indicator will help give correct trades. Is there a way to ensure something is rounded DOWN to the nearest tick value? |
|
|
|
|
|
#7 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Rounding is done in the code. If you want to round you can round. If you don't want to round then don't round it. Whether that causes you issues is something you have to decide for yourself in how you want to use it.
We provide you with function to round to the nearest tick size. If you want to round down to nearest, then you need to program it yourself. Run your own logic and check it against tick size. Then bring the value down if need be. Just do simple math. Something like absolute value of (0.30 - 0.25) = 0.05 is significantly less than the absolute value of (0.30 - 0.50) so you should round to 0.25.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Member
Join Date: Mar 2009
Posts: 66
Thanks: 0
Thanked 0 times in 0 posts
|
It does compile which is a miracle in and of itself for me....
I have placed in bold the code that I have added. For Condition Set 1 (Entry) the rounding must be down. For Condition Set 2 (Exit) the rounding must be up Code:
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class ChandyShort830EXP : Strategy
{
#region Variables
// Wizard generated variables
private double chandyRounded = 1; // Default setting for ChandyRounded
private double chandyRemainder = 1; // Default setting for ChandyRemainder
private double roundUp = 1; // Default setting for RoundUp
private double roundDown = 1; // Default setting for RoundDown
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
chandyRounded = Math.Round( CustomIndicator(5, 3.5).CustomInd[0], 0 );
chandyRemainder = CustomIndicator(5, 3.5).CustonIndChandelier[0] - chandyRounded;
// Condition set 1
if (DefaultInput[0] > CustomIndicator(5, 3.5).CustomInd[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
roundDown = chandyRemainder >= 0.75 ? chandyRounded + 0.75 :
chandyRemainder >= 0.5 ? chandyRounded + 0.5 :
chandyRemainder >= 0.25 ? chandyRounded + 0.25 :
chandyRemainder;
EnterShortStopLimit(DefaultQuantity, roundDown, roundDown, "");
Alert("MyAlert0", Priority.High, "", "", 1, Color.White, Color.Black);
}
// Condition set 2
if (DefaultInput[0] < CustomIndicator(5, 3.5).CustomInd[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
roundUp = chandyRemainder <= 0.25 ? chandyRounded + 0.25 :
chandyRemainder <= 0.5 ? chandyRounded + 0.5 :
chandyRemainder <= 0.75 ? chandyRounded + 0.75 :
chandyRemainder;
ExitShortStopLimit(roundUp, roundUp, "", "");
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\Alert3.wav");
}
// Condition set 3
if (ToTime(Time[0]) > ToTime(8, 30, 0)
&& DefaultInput[0] < CustomIndicator(5, 3.5).CustomInd[0]
&& ToTime(Time[0]) < ToTime(8, 31, 0))
{
EnterShort(DefaultQuantity, "");
}
// Condition set 4
if (ToTime(Time[0]) >= ToTime(15, 0, 0))
{
ExitShort("", "");
}
}
Last edited by kocmodpom; 02-23-2010 at 02:01 PM.
|
|
|
|
|
|
#9 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,559
Thanks: 261
Thanked 1,015 times in 996 posts
|
kocmodpom, what exactly is 'not working'?
Do you run into errors? Or does it take the trades you think it should? For this please consider working with TraceOrders to debug your order placement behavior - http://www.ninjatrader-support.com/H...aceOrders.html For the rounding, please print out the calculated values to check if they round to what you expect them to, you might need to include the Math Round overload with the MidPoint mode parameter...
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Member
Join Date: Mar 2009
Posts: 66
Thanks: 0
Thanked 0 times in 0 posts
|
No error messages, and no orders are being placed or filled. Will I have to do that printing and testing using recorded data or something else?
Is something amazing supposed to happen when I add: Code:
protected override void Initialize()
{
TraceOrders = true;
}
Nothing magical happened when I compiled. No pop-up or anything that I can tell when rerunning the strat on NQ. What should I be looking for?
Last edited by kocmodpom; 02-18-2010 at 01:21 PM.
|
|
|
|
|
|
#11 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,559
Thanks: 261
Thanked 1,015 times in 996 posts
|
Using the TraceOrders feature enables valuabe debugging info from each Order method to be listed in your output window when running the strategy either in Market Replay, live trading or backtesting. Thus you can then check what happens 'under the hood' to see which issues needed to be addressed.
I would also add a visual check (an arrow, dot...) to your conditions so it's easier to see if your conditions trigger as expected.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#12 |
|
Member
Join Date: Mar 2009
Posts: 66
Thanks: 0
Thanked 0 times in 0 posts
|
That is the thing, no orders are being executed now. Absolutely nothing.
|
|
|
|
|
|
#13 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,559
Thanks: 261
Thanked 1,015 times in 996 posts
|
Ok, did you add the visual check to your conditions to see if they trigger at all? If not you would need to rework those...
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#14 |
|
Member
Join Date: Mar 2009
Posts: 66
Thanks: 0
Thanked 0 times in 0 posts
|
I did figure out the problem. It was a programming issue. Apparently one cannot use the code:
Code:
Math.Round(Variable, 0); Math.Truncate |
|
|
|
|
|
#15 |
|
Member
Join Date: Mar 2009
Posts: 66
Thanks: 0
Thanked 0 times in 0 posts
|
SIFTrader, you are the man! I figured it all out after your very generous starting point to the code.
I am stoked! Just got the license and start trading tomorrow! |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how does this credit thing work ? | satbirm | SuperDOM and other Order Entry Windows | 3 | 03-16-2010 09:57 AM |
| Autotrade will not work | Josey | Miscellaneous Support | 1 | 10-16-2009 02:44 AM |
| Autotrade question | T4M | Automated Trading | 1 | 05-16-2008 04:59 AM |
| pivots are wrong, maybe a time thing? | chartlearner | Charting | 1 | 04-30-2008 08:19 AM |
| Reverse on autotrade | biswar | Automated Trading | 1 | 06-15-2005 06:04 AM |