NinjaTrader Support Forum  
X

Attention!

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


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 07-15-2009, 12:42 AM   #1
stevescott05
Member
 
Join Date: Apr 2008
Location: Seattle, WA
Posts: 47
Thanks: 0
Thanked 1 time in 1 post
Question Simple ExitShortStop Strategy

I'm having difficulty getting my exits to work the way I intend them to. Basically, I want a short/long position closed out the instant price action crosses an SMA. Using EnterShortStop, this works for the entry, but not the exit thus far.

PHP Code:
string orderID Instrument.FullName " " BarsPeriod.Value
 
// Condition set 1
if (SMA(TrendFastMAperiod)[1] < SMA(TrendSlowMAperiod)[1]
&& 
SMA(ExitMAperiod)[1] > SMA(EntryMAperiod)[1]
&& 
Position.Quantity TotalContracts)
{
EnterShortStop(NumContractsSMA(EntryMAperiod)[1] - TicksFromMA TickSize"S1_short " orderID);
//ExitShortStop(0, true, Position.Quantity, SMA(ExitMAperiod)[1], "S1_short " + orderID + " Stop", "S1_short " + orderID);
SetStopLoss("S1_Short " orderIDCalculationMode.TicksStopfalse);
}
 
//Condition set 2
if (CrossAbove(DefaultInput[0], SMA(ExitMAperiod), 1)
&& 
Position.MarketPosition == MarketPosition.Short)
{
ExitShort("S1_Short " orderID);

You can see I commented out the last ExitShortStop attempt I made.

I can use ExitShort currently, shown in Condition 2, however it always takes me out in strange places (subsequent bars, open of current bar, strange), always later than I want.

Anyone able to show me some code using ExitShortStop to accomplish what I'm shooting for here?

Thanks in advance

Steve
Last edited by stevescott05; 07-15-2009 at 12:44 AM.
stevescott05 is offline  
Reply With Quote
Old 07-15-2009, 05:53 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
Default

Steve, when you run this with TraceOrders = true in the Initialize() you'll most likely find orders ignored as you run into the 'Internal Order Handling Rules' with - http://www.ninjatrader-support.com/H...verview36.html

This is because your SetStopLoss is used together with the ExitShortStop you attempt to place (botton section of the link above).

For NinjaTrader 7, we add the 'Unmanaged Order Submission' feature -

New Unmanaged Order Submission
In 6.5 some users were burdened with our "Internal Order Handling" rules. We have introduced unmanaged order submission which bypasses the convenience of our order handling layer. This lower level of programming allows you to do what you want relative to order submission/management without any limitations other than any imposed by your broker. There are only three methods, SubmitOrder(), ChangeOrder() and CancelOrder(). You then get the flexibility of managing your orders how you see fit and optionally handling rejections.

http://www.ninjatrader.com/webnew/NT7/NinjaTrader7.html
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 07-15-2009, 04:27 PM   #3
stevescott05
Member
 
Join Date: Apr 2008
Location: Seattle, WA
Posts: 47
Thanks: 0
Thanked 1 time in 1 post
Question if --> else if

I attempted to resolve the OrderHandling Issue by using something like this, but to no avail:

PHP Code:
// Suppose I'm looking to go long, I used something very similar to this
 
if (Position.AvgPrice TicksFromMA TickSize SMA(ExitMAperiod)[1]
&& 
Position.MarketPosition == MarketPosition.Long)
{
SetStopLoss("S1_Long " orderIDCalculationMode.TicksStopfalse);
}
else if (
Position.AvgPrice TicksFromMA TickSize SMA(ExitMAperiod)[1
&& 
Position.MarketPosition == MarketPosition.Long)
{
ExitLongStop(0truePosition.QuantitySMA(ExitMAperiod)[1], "S1_Long " orderID " Stop""S1_Long " orderID);

Why doesn't that solve the Order Problem? Shouldn't only one of the Exit orders be active at a time since they are in an if-else state? Only one set of conditions is satisfied at any given time, so only one should be active, no ignoring should occur (so I thought anyhow)....

The results I've been seeing are always pretty much the same: One entry at the beginning of my historical data with either (1) a long, flat, dotted line connecting the entry to an exit point 50 days later after price returned to same level. Or, (2) no dotted line, and no additional trade activity, because the order never closed by exit order (and I have it set to only trade one contract at a time).

What to do? Anyone have an idea how to code this? I just want the order closed immediately upon crossing an SMA. I don't want to see an exit on the open after a bar which closed after CrossAbove() or CrossBelow() the SMA. That's already too late for me. I want to see markers in the middle of the bar where it crossed the SMA.

Again, thanks in advance for any and all ideas!

Steve
stevescott05 is offline  
Reply With Quote
Old 07-16-2009, 05:35 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
Default

stevescott05, since you cannot cancel SetStopLoss you would need to work with IOrder objects on this to CancelOrder() your offending stop before you place the 'SMACrossExit' - http://www.ninjatrader-support.com/H...ncelOrder.html

http://www.ninjatrader-support2.com/...ead.php?t=7499

To backtest your strategy with more realistic intrabar fills, please see this sample here - http://www.ninjatrader-support2.com/...ead.php?t=6652
NinjaTrader_Bertrand 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
ExitShortStop fired again xewoox Automated Trading 9 02-17-2009 11:18 AM
ExitShortLimit , ExitShortStop xewoox Strategy Development 1 02-16-2009 09:15 AM
Simple question about a simple strategy. shakira Strategy Development 1 02-14-2009 10:56 AM
ExitShortStop not getting executed: why? stefy Strategy Development 7 01-14-2009 09:36 AM
ExitShortStop doesn't works julen Strategy Development 1 09-16-2008 12:04 PM


All times are GMT -6. The time now is 08:19 AM.