Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reverse on StopLoss example

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

    Reverse on StopLoss example

    Hi, i've been searching the forum and in the docs for a simple Reverse on SlopLoss and I have not found.

    I would appreciate if anyone could help provide a simple example.
    Here is what I'm trying as a simple example:

    protected override void OnBarUpdate()
    if (Close[0] > Close[1])
    {
    EnterLong(DefaultQuantity, "L1");
    Variable0 = (Low[0]);
    SetStopLoss("L1", CalculationMode.Price, Variable0 , false);
    }

    if (BarsSinceEntry() >= 1)
    ExitLong("L1");


    if (stopLoss is triggered)
    {
    EnterShort(DefaultQuantity, "S1");
    Variable1 = (High[0]);
    SetStopLoss("S1", CalculationMode.Price, Variable1 , false);
    }

    if (BarsSinceEntry() >= 1)
    ExitShort("S1");

    #2
    Hello John,

    Thank you for your note.

    Your best bet would be to detect the the StopLoss being filled from the OnOrderUpdate() method so that you can then submit the entry order for the opposite position.

    http://www.ninjatrader.com/support/h...rderupdate.htm

    You want to check of the order name is for Stop loss

    if(order.Name == "Stop loss" && order.OrderState == OrderState.Filled)
    {
    // Reverse your position here
    }
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal,

      I have tried following your example and also the documentation, but I have not had any luck.
      Here is the code I got which seems to be triggering a reverse order but it is triggering an order on every exit. I would appreciate some hints:

      protected override void OnBarUpdate()
      {
      if (entryOrder1 == null && (CrossAbove(Stochastics(3, 8, 3).K, Stochastics(3, 8, 3).D, 1)));
      entryOrder1 = EnterLong(1, "MyEntryLong");
      Variable0 = Low[0] - 10 * TickSize;
      SetStopLoss("MyEntryLong", CalculationMode.Price, Variable0, false);
      SetProfitTarget("MyEntryLong", CalculationMode.Ticks, 100);
      }

      protected override void OnOrderUpdate(IOrder order)
      {
      if(order.Name == "Stop loss" && order.OrderState == OrderState.Filled)
      {
      entryOrder2 = EnterShort(1, "MyEntryShort");
      Variable1 = High[0] + 10 * TickSize;
      SetStopLoss("MyEntryShort", CalculationMode.Price, Variable1, false);
      SetProfitTarget("MyEntryShort", CalculationMode.Ticks, 100);
      }
      Attached Files

      Comment


        #4
        Hello John,

        That is expected since we are using the Set() method and every time the Stop loss gets filled the system places an EnterShort() order for it.

        You would need to have the logic set the order to only place when you need it too for the short order.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Cal, if you look closely in the picture, the Entry Long Orders are in an infinite loop. Once 1 reach Profit target, it enters another one on the very next bar. Do you have an idea why? It should only enter long when Stoc K CrossesAbove Stoc D.

          Comment


            #6
            John,

            This is because you have a semi-colon at the end of your if statement
            Code:
            if (entryOrder1 == null && (CrossAbove(Stochastics(3, 8, 3).K, Stochastics(3, 8, 3).D, 1)));
            You will want to remove the ; at the end and try running the strategy then.
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Cal, the implementation we worked previously did not work very well. I have been able to get a better one here so I'm posting so others can benefit from it too.

              I welcome any feedback too.

              ===============================
              public class ReserveOnStop : Strategy
              {
              #region Variables
              private IOrder entryOrder1 = null;
              private IOrder entryOrder2 = null;
              private IOrder stopOrder1 = null;
              private IOrder stopOrder2 = null;
              private IOrder targetOrder1 = null;
              private IOrder targetOrder2 = null;
              #endregion

              protected override void Initialize()
              {
              CalculateOnBarClose = true;
              Add(Stochastics(7, 14, 3));
              }

              protected override void OnBarUpdate()

              {
              // Submit an entry limit order if Stoc K crossAbove D & we don't have an entry order open
              if (entryOrder1 == null && (CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1)))
              {
              entryOrder1 = EnterLong(1, "MyLong");
              }
              }


              protected override void OnExecution(IExecution execution)
              {

              if (entryOrder1 != null && entryOrder1 == execution.Order)
              {
              if (execution.Order.OrderState == OrderState.Filled && execution.Order.Filled > 0)
              {
              // Stop-Loss order 75 ticks below our entry price
              stopOrder1 = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 75 * TickSize, "MyStop1", "MyLong");

              // Target order 75 ticks above our entry price
              targetOrder1 = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 75 * TickSize, "MyTarget1", "MyLong");

              // Resets the entryOrder object to null after the order has been filled
              if (execution.Order.OrderState != OrderState.PartFilled)
              {
              entryOrder1 = null;
              }
              }
              }
              // Reverses the position if stopOrder1 is triggered
              if ((stopOrder1 != null && stopOrder1 == execution.Order))
              {
              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
              {
              entryOrder2 = EnterShort(1, "MyShort");
              }
              if (entryOrder2 != null && entryOrder2 == execution.Order)
              {
              if (execution.Order.OrderState == OrderState.Filled && execution.Order.Filled > 0)
              {
              // Stop-Loss order 75 ticks above our entry price
              stopOrder2 = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 75 * TickSize, "MyStop2", "MyShort");

              // Target order 75 ticks below our entry price
              targetOrder2 = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 75 * TickSize, "MyTarget2", "MyShort");
              }

              }
              }

              // Reset our stop order and target orders' IOrder objects after our position is closed.
              if ((stopOrder2 != null && stopOrder2 == execution.Order) || (targetOrder2 != null && targetOrder2 == execution.Order))
              {
              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
              {
              stopOrder2 = null;
              targetOrder2 = null;
              }
              }
              }

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by gravdigaz6, Today, 01:43 AM
              0 responses
              3 views
              0 likes
              Last Post gravdigaz6  
              Started by DayneSwift, Yesterday, 11:06 PM
              0 responses
              2 views
              0 likes
              Last Post DayneSwift  
              Started by AidanLawrence, Yesterday, 10:03 PM
              0 responses
              2 views
              0 likes
              Last Post AidanLawrence  
              Started by butt_toast, Yesterday, 09:47 PM
              0 responses
              3 views
              0 likes
              Last Post butt_toast  
              Started by GaryOz, Yesterday, 06:54 PM
              0 responses
              5 views
              0 likes
              Last Post GaryOz
              by GaryOz
               
              Working...
              X