Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple ExitShortStop Strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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(NumContracts, SMA(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 " + orderID, CalculationMode.Ticks, Stop, false);
    }
     
    //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, 12:44 AM.

    #2
    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.

    Comment


      #3
      if --&gt; 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 " + orderID, CalculationMode.Ticks, Stop, false);
      }
      else if (Position.AvgPrice - TicksFromMA * TickSize < SMA(ExitMAperiod)[1] 
      && Position.MarketPosition == MarketPosition.Long)
      {
      ExitLongStop(0, true, Position.Quantity, SMA(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

      Comment


        #4
        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

        The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()


        To backtest your strategy with more realistic intrabar fills, please see this sample here - http://www.ninjatrader-support2.com/...ead.php?t=6652

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        630 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        364 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        566 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X