Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limiting SetStopLoss() by Time

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

    Limiting SetStopLoss() by Time

    Hi -

    I'm coding a strategy where I only want the stops and profit targets to work during certain times, even though I'll have positions on at other points in my strategy.

    For example, the strategy enters a position on the close and holds it overnight. The following day, I don't want the stops to work prior to 841 AM and not after 3:00 PM.

    How would I code this time limitation for the SetStopLoss() function (where time >= 841 & <=1500)?

    Thanks in advance.

    #2
    Submit your Set() methods inside the OnBarUpdate() method then instead of the Initialize() method. Place a time filter over them. Please take a look at this reference sample: http://www.ninjatrader-support.com/v...ead.php?t=3226
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      What am I doing wrong

      What is wrong with this sample code - the setstoploss funtion is executing stops beyond the limit of the time filter

      // Overnight Long Conditions
      if (ToTime(Time[0]) == ToTime(15, 13, 0)
      && Close[
      0] < Open [0])
      {
      EnterLong(DefaultQuantity,
      "Overnight Long");
      }

      // Overnight Long Stop Entry Condition
      if (ToTime(Time[0]) >= ToTime(8, 41, 0)
      && ToTime(Time[
      0]) <= ToTime(15, 10, 0))
      {
      SetStopLoss(
      "Overnight Long", CalculationMode.Price, OvernightStopLoss, false);
      }

      Comment


        #4
        The issue is that once you have already submitted a stop loss it will remain active. You will need to use IOrders instead and manually manage stop orders. Please see this reference sample: http://www.ninjatrader-support.com/v...ead.php?t=3917
        Coupled with the time filter I think you should be able to achieve what you want.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Light Years Ahead of Where I was BUT

          Thanks for the sample code, that really helped. I'm still having problems with this code and using a time filter for stops. When this code is exeuted, the stop does not work or obey the time filter. Here is how am doing it:

          protectedoverridevoid OnBarUpdate()
          {
          //Store prior day closing price as an integer
          if (ToTime(Time[0]) > ToTime(8, 30, 0))
          {
          priorDayClose = (PriorDayOHLC().PriorClose[0]);
          }

          //Store morning low as an integer
          if (ToTime(Time[0]) == ToTime(8, 41, 0))
          {
          morningLow = (MIN(Low, 10)[0]);
          }

          // Stops null before 841 AM and after 1510 PM
          if (ToTime(Time[0]) >= ToTime(15, 10, 0) && (ToTime(Time[0]) <= ToTime(8, 41, 0)))
          {
          ReverseLongStop = null;
          }

          // Entry
          if (ToTime(Time[0]) == ToTime(8, 33, 0)
          && Position.MarketPosition == MarketPosition.Short)
          {
          ReverseLong = EnterLong(DefaultQuantity, "Reverse Long");
          }

          }

          protectedoverridevoid OnOrderUpdate(IOrder order)
          {
          if (ReverseLong!= null && ReverseLong.Token == order.Token)
          {

          if (order.OrderState == OrderState.Filled || (order.OrderState == OrderState.Cancelled && order.Filled > 0))
          {
          // Stop-Loss order
          ReverseLongStop = ExitLongStop(0, true, order.Filled, moningLow, "Reverse Long Stop", "Reverse Long");

          // Target order
          ReverseLongProfitTarget = ExitLongLimit(0, true, order.Filled, priorDayClose + -2 * TickSize, "Reverse Long Profit Target", "Reverse Long");


          ReverseLong = null;

          }
          elseif (order.OrderState == OrderState.Cancelled && order.Filled == 0)
          {
          ReverseLong = null;
          }

          }

          //

          Thanks in advance for any assistance.


          Comment


            #6
            Hi johnroe,

            Setting to null does not cancel the current order. What you want to do is CancelOrder() and then pass in the IOrder object of the order you want to cancel. So when the time is outside the trading range you want, cancel your stop order.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Thanks again

              The cancel order works, but it cancels the stop forever. How do I resubmit this stop?

              Comment


                #8
                If you call

                CancelOrder(IOrder orderToCancel)

                multiple times it will likely attempt to cancel multiple times. You will have to ensure in your code you only call it once for each unique order.

                To resubmit the stop, just call ExitLongStop() again.
                RayNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                582 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                338 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                103 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                554 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                552 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X