Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating a SL, and 2 target profits.

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

    Creating a SL, and 2 target profits.

    Hi all,

    I've perused the forum but couldn't find an answer to my problem so sorry if it exists already and the double question.

    using the NT8 script, I am able to enter the market, but placing and SL and TP1/TP2 arent being placed.

    any help would be appreciated.

    if (IsValidBullishPinBar(ema8, ema20, ema50) && CheckSlopeConditions(ema50, ema20, ema8, EmaSlopeThreshold))
    {
    Draw.ArrowUp(this, "Bull_PinBar_" + CurrentBar, true, 0, Low[0], Brushes.Yellow);

    // Define SL and TP levels
    double stopLossPrice = Low[0] - 1 * atrValue; // SL 1x ATR below the low of the bar
    double tp1Price = Close[0] + TakeProfit1Ratio * atrValue; // TP1 level
    double tp2Price = Close[0] + TakeProfit2Ratio * atrValue; // TP2 level

    // Split the position for TP1 and TP2
    int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // Cast to Int32
    int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Remaining for TP2

    // Enter the long position with the full size
    EnterLong(Convert.ToInt32(PositionSize), "Bull_Entry_" + CurrentBar); // Cast to Int32 with unique entry name

    // Print order details for debugging
    Print("Entering Long for Bullish Pin Bar at price: " + Close[0]);
    Print("SL: " + stopLossPrice + " TP1: " + tp1Price + " TP2: " + tp2Price);
    }

    // For Bearish Pin Bars - Enter SHORT position and manage SL, TP1, and TP2
    if (IsValidBearishPinBar(ema8, ema20, ema50) && CheckSlopeConditions(ema50, ema20, ema8, EmaSlopeThreshold))
    {
    Draw.ArrowDown(this, "Bear_PinBar_" + CurrentBar, true, 0, High[0], Brushes.Red);

    // Define SL and TP levels
    double stopLossPrice = High[0] + 1 * atrValue; // SL 1x ATR above the high of the bar
    double tp1Price = Close[0] - TakeProfit1Ratio * atrValue; // TP1 level
    double tp2Price = Close[0] - TakeProfit2Ratio * atrValue; // TP2 level

    // Split the position for TP1 and TP2
    int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // Cast to Int32
    int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Remaining for TP2

    // Enter the short position with the full size
    EnterShort(Convert.ToInt32(PositionSize), "Bear_Entry_" + CurrentBar); // Cast to Int32 with unique entry name

    // Print order details for debugging
    Print("Entering Short for Bearish Pin Bar at price: " + Close[0]);
    Print("SL: " + stopLossPrice + " TP1: " + tp1Price + " TP2: " + tp2Price);
    }
    }

    // OnPositionUpdate: Handles SL and TP1/TP2 once the entry is confirmed
    protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, MarketPosition marketPosition)
    {
    // If a Long position is confirmed
    if (marketPosition == MarketPosition.Long && position.Quantity == PositionSize)
    {
    double stopLossPrice = Low[0] - 1 * atrValue; // SL recalculated
    double tp1Price = averagePrice + TakeProfit1Ratio * atrValue; // Recalculate TP1
    double tp2Price = averagePrice + TakeProfit2Ratio * atrValue; // Recalculate TP2

    int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // Half position for TP1
    int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Other half for TP2

    // Place stop-loss and profit target orders after position is confirmed
    SetStopLoss("Bull_Entry_SL_" + CurrentBar, CalculationMode.Price, stopLossPrice, false);
    SetProfitTarget("Bull_Entry_TP1_" + CurrentBar, CalculationMode.Price, tp1Price); // TP1 for first half
    SetProfitTarget("Bull_Entry_TP2_" + CurrentBar, CalculationMode.Price, tp2Price); // TP2 for second half

    // Print debugging information
    Print("SL placed at: " + stopLossPrice + " TP1 at: " + tp1Price + " TP2 at: " + tp2Price + " for Long position");
    }

    // If a Short position is confirmed
    else if (marketPosition == MarketPosition.Short && position.Quantity == PositionSize)
    {
    double stopLossPrice = High[0] + 1 * atrValue; // SL recalculated
    double tp1Price = averagePrice - TakeProfit1Ratio * atrValue; // Recalculate TP1
    double tp2Price = averagePrice - TakeProfit2Ratio * atrValue; // Recalculate TP2

    int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // Half position for TP1
    int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Other half for TP2

    // Place stop-loss and profit target orders after position is confirmed
    SetStopLoss("Bear_Entry_SL_" + CurrentBar, CalculationMode.Price, stopLossPrice, false);
    SetProfitTarget("Bear_Entry_TP1_" + CurrentBar, CalculationMode.Price, tp1Price); // TP1 for first half
    SetProfitTarget("Bear_Entry_TP2_" + CurrentBar, CalculationMode.Price, tp2Price); // TP2 for second half

    // Print debugging information
    Print("SL placed at: " + stopLossPrice + " TP1 at: " + tp1Price + " TP2 at: " + tp2Price + " for Short position");
    }
    }​

    #2
    Hello Joseph Davidow,

    Thank you for your post.

    Set methods cannot be unset. This means it is important to call the Set method with CalculationMode.Ticks to reset this before calling an entry method (not after).​

    If you want to dynamically change/modify the price of the stop loss and profit target, the method should be called from within OnBarUpdate() and the price should always be reset when the strategy becomes flat again. This information is noted in the "Tips" section on the following help guide pages:
    The reference sample script SamplePriceModification demonstrates how to change the price of stop loss and profit target orders:

    https://ninjatrader.com/support/helpGuides/nt8/modifying\_the\_price\_of\_stop\_lo.htm

    Please let us know if you have any further questions.

    Comment


      #3
      Thanks for your response.
      I tried making the adaptions (hope I understood them correctly) but now I get an error that the order isnt even being placed.

      my new code.
      / Ensure strategy is enabled and in real-time
      if (State == State.Realtime)
      {
      // Check if the strategy is flat, reset StopLoss and ProfitTarget
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      // Reset the stop loss and profit targets before any new entry
      SetStopLoss(CalculationMode.Ticks, 0); // Reset to 0 ticks when flat
      SetProfitTarget(CalculationMode.Ticks, 0); // Reset to 0 ticks when flat
      }


      // For Bullish Pin Bars - Enter LONG position and manage SL, TP1, and TP2
      if (IsValidBullishPinBar(ema8, ema20, ema50) && CheckSlopeConditions(ema50, ema20, ema8, EmaSlopeThreshold))
      {
      Draw.ArrowUp(this, "Bull_PinBar_" + CurrentBar, true, 0, Low[0], Brushes.Yellow);

      // Define SL and TP levels
      double stopLossPrice = Low[0] - 1 * atrValue; // SL 1x ATR below the low of the bar
      double tp1Price = Close[0] + TakeProfit1Ratio * atrValue; // TP1 level
      double tp2Price = Close[0] + TakeProfit2Ratio * atrValue; // TP2 level

      // Split the position for TP1 and TP2
      int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // Cast to Int32
      int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Remaining for TP2

      // Enter the long position with the full size
      EnterLong(Convert.ToInt32(PositionSize), "Bull_Entry_" + CurrentBar); // Cast to Int32 with unique entry name

      // Print order details for debugging
      Print("Entering Long for Bullish Pin Bar at price: " + Close[0]);
      Print("SL: " + stopLossPrice + " TP1: " + tp1Price + " TP2: " + tp2Price);
      }

      // For Bearish Pin Bars - Enter SHORT position and manage SL, TP1, and TP2
      if (IsValidBearishPinBar(ema8, ema20, ema50) && CheckSlopeConditions(ema50, ema20, ema8, EmaSlopeThreshold))
      {
      Draw.ArrowDown(this, "Bear_PinBar_" + CurrentBar, true, 0, High[0], Brushes.Red);

      // Define SL and TP levels
      double stopLossPrice = High[0] + 1 * atrValue; // SL 1x ATR above the high of the bar
      double tp1Price = Close[0] - TakeProfit1Ratio * atrValue; // TP1 level
      double tp2Price = Close[0] - TakeProfit2Ratio * atrValue; // TP2 level

      // Split the position for TP1 and TP2
      int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // Cast to Int32
      int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Remaining for TP2

      // Enter the short position with the full size
      EnterShort(Convert.ToInt32(PositionSize), "Bear_Entry_" + CurrentBar); // Cast to Int32 with unique entry name

      // Print order details for debugging
      Print("Entering Short for Bearish Pin Bar at price: " + Close[0]);
      Print("SL: " + stopLossPrice + " TP1: " + tp1Price + " TP2: " + tp2Price);
      }
      }
      }
      // OnPositionUpdate: Handles SL and TP1/TP2 once the entry is confirmed
      protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, MarketPosition marketPosition)
      {
      // If a Long position is confirmed
      if (marketPosition == MarketPosition.Long && position.Quantity == PositionSize)
      {
      double stopLossPrice = Low[0] - 1 * atrValue; // SL recalculated
      double tp1Price = averagePrice + TakeProfit1Ratio * atrValue; // Recalculate TP1
      double tp2Price = averagePrice + TakeProfit2Ratio * atrValue; // Recalculate TP2

      int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // Half position for TP1
      int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Other half for TP2

      // Place stop-loss and profit target orders after position is confirmed
      SetStopLoss("Bull_Entry_SL_" + CurrentBar, CalculationMode.Price, stopLossPrice, false);
      SetProfitTarget("Bull_Entry_TP1_" + CurrentBar, CalculationMode.Price, tp1Price); // TP1 for first half
      SetProfitTarget("Bull_Entry_TP2_" + CurrentBar, CalculationMode.Price, tp2Price); // TP2 for second half

      // Print debugging information
      Print("SL placed at: " + stopLossPrice + " TP1 at: " + tp1Price + " TP2 at: " + tp2Price + " for Long position");
      }

      // If a Short position is confirmed
      else if (marketPosition == MarketPosition.Short && position.Quantity == PositionSize)
      {
      double stopLossPrice = High[0] + 1 * atrValue; // SL recalculated
      double tp1Price = averagePrice - TakeProfit1Ratio * atrValue; // Recalculate TP1
      double tp2Price = averagePrice - TakeProfit2Ratio * atrValue; // Recalculate TP2

      int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // Half position for TP1
      int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Other half for TP2

      // Place stop-loss and profit target orders after position is confirmed
      SetStopLoss("Bear_Entry_SL_" + CurrentBar, CalculationMode.Price, stopLossPrice, false);
      SetProfitTarget("Bear_Entry_TP1_" + CurrentBar, CalculationMode.Price, tp1Price); // TP1 for first half
      SetProfitTarget("Bear_Entry_TP2_" + CurrentBar, CalculationMode.Price, tp2Price); // TP2 for second half

      // Print debugging information
      Print("SL placed at: " + stopLossPrice + " TP1 at: " + tp1Price + " TP2 at: " + tp2Price + " for Short position");
      }
      }​

      Comment


        #4
        Hello,

        I see that your code is still calling SetStopLoss and SetProfitTarget dynamically from OnPositionUpdate, please note that if you're going to call these dynamically they should be called from OnBarUpdate().

        If you want to submit your profit target or stop loss from outside OnBarUpdate, consider using Exit methods instead. We have an example script that demonstrates.

        Comment


          #5
          Hi Gaby,

          I made the changes to the code as you suggested and the strategy is now opening the order and placing the full position size for the SL but also for the Target.
          I would like it to split the position size in 2 and have two targets, each with half the position sizing.

          protected override void OnBarUpdate()
          {
          if (CurrentBar < 20) return;

          // Calculate EMA values on each bar update
          double ema8 = EMA(8)[0];
          double ema20 = EMA(20)[0];
          double ema50 = EMA(50)[0];
          atrValue = atr[0]; // Calculate the ATR value

          // Plot EMAs
          Values[0][0] = ema8; // EMA8
          Values[1][0] = ema20; // EMA20
          Values[2][0] = ema50; // EMA50

          // Ensure strategy is enabled and in real-time
          if (State == State.Realtime)
          {
          // Check if the strategy is flat, reset StopLoss and ProfitTarget
          if (Position.MarketPosition == MarketPosition.Flat)
          {
          // Reset the stop loss and profit targets before any new entry
          SetStopLoss(CalculationMode.Ticks, 0); // Reset to 0 ticks when flat
          SetProfitTarget(CalculationMode.Ticks, 0); // Reset to 0 ticks when flat
          }

          // Entry logic for bullish pin bar with SL, TP1, and TP2 management
          // Entry logic for bullish pin bar with SL, TP1, and TP2 management
          if (IsValidBullishPinBar(ema8, ema20, ema50) && CheckSlopeConditions(ema50, ema20, ema8, EmaSlopeThreshold))
          {
          Draw.ArrowUp(this, "Bull_PinBar_" + CurrentBar, true, 0, Low[0], Brushes.Yellow);

          double stopLossPrice = Low[0] - 1 * atrValue; // SL is 1x ATR below the low of the bar
          double tp1Price = Close[0] + TakeProfit1Ratio * atrValue; // TP1
          double tp2Price = Close[0] + TakeProfit2Ratio * atrValue; // TP2

          // Split the position for TP1 and TP2
          int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // First half
          int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Remaining

          Print("Entering Long for Bullish Pin Bar at price: " + Close[0]);
          Print("SL: " + stopLossPrice + " TP1: " + tp1Price + " TP2: " + tp2Price);

          // Set StopLoss and ProfitTargets before EnterLong call
          SetStopLoss(CalculationMode.Price, stopLossPrice); // Set stop loss
          SetProfitTarget(CalculationMode.Price, tp1Price); // TP1

          // Enter the position with the first half
          EnterLong(firstPositionSize, "Bull_Entry_TP1_" + CurrentBar);

          // Manage the second half for TP2
          SetProfitTarget(CalculationMode.Price, tp2Price);
          EnterLong(secondPositionSize, "Bull_Entry_TP2_" + CurrentBar);
          }

          // Similar logic for bearish pin bars
          else if (IsValidBearishPinBar(ema8, ema20, ema50) && CheckSlopeConditions(ema50, ema20, ema8, EmaSlopeThreshold))
          {
          Draw.ArrowDown(this, "Bear_PinBar_" + CurrentBar, true, 0, High[0], Brushes.Red);

          double stopLossPrice = High[0] + 1 * atrValue; // SL 1x ATR above the high of the bar
          double tp1Price = Close[0] - TakeProfit1Ratio * atrValue; // TP1
          double tp2Price = Close[0] - TakeProfit2Ratio * atrValue; // TP2

          // Split the position for TP1 and TP2
          int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // First half
          int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Remaining

          // Set StopLoss and ProfitTargets before EnterShort call
          SetStopLoss(CalculationMode.Price, stopLossPrice); // SL
          SetProfitTarget(CalculationMode.Price, tp1Price); // TP1

          // Enter short for first half
          EnterShort(firstPositionSize, "Bear_Entry_TP1_" + CurrentBar);

          // Manage second half for TP2
          SetProfitTarget(CalculationMode.Price, tp2Price);
          EnterShort(secondPositionSize, "Bear_Entry_TP2_" + CurrentBar);
          }

          }


          }​

          Comment


            #6
            Hello,

            While using the Managed Approach, you may only use one SetProfitTarget() per each Entry method.

            If you wanted to set multiple profit targets, you would need to create separate entries.

            Comment


              #7
              I changed the code and it is still only placing 1 limit order (Take profit target) with the full position size.

              My new code:
              double stopLossPrice = Low[0] - 1 * atrValue; // SL is 1x ATR below the low of the bar
              double tp1Price = Close[0] + TakeProfit1Ratio * atrValue; // TP1
              double tp2Price = Close[0] + TakeProfit2Ratio * atrValue; // TP2


              // Split the position for TP1 and TP2
              int firstPositionSize = Convert.ToInt32(Math.Floor(PositionSize / 2.0)); // First half
              int secondPositionSize = Convert.ToInt32(PositionSize - firstPositionSize); // Remaining


              Print("Entering Long for Bullish Pin Bar at price: " + Close[0]);
              Print("SL: " + stopLossPrice + " TP1: " + tp1Price + " TP2: " + tp2Price);

              SetStopLoss(CalculationMode.Price, stopLossPrice);
              SetProfitTarget("BullishEntry1", CalculationMode.Price, tp1Price);
              SetProfitTarget("BullishEntry2", CalculationMode.Price, tp2Price);

              //EnterLong(int quantity, string signalName)
              EnterLong(firstPositionSize, "BullishEntry1"); // Entry with signal name "BullishEntry1"

              EnterLong(secondPositionSize, "BullishEntry2"); // Entry with signal name "BullishEntry2"​
              Last edited by Joseph Davidow; 10-07-2024, 08:00 AM.

              Comment


                #8
                Hello Joseph,

                It's not possible to have it take half the position size with SetProfitTarget(). You will notice that this method does not have the ability to specify quantity. It will always match the quantity that the entry order has.

                You may consider using exit methods instead. This would allow you to specify a quantity.

                Comment


                  #9
                  So what do you suggest I do?
                  I want to be able to place two limit orders for either short or long.
                  Enter full position, place the SL with full position and then 2 exit limit orders with half the position size each.

                  ExitLongStopLimit() - ?
                  ExitLongLimit() -?

                  "BullishEntry1", CalculationMode.Price, tp1Price
                  "BullishEntry2", CalculationMode.Price, tp2Price​

                  Comment


                    #10
                    You can use exit methods and give each method your desired quantity, as we've previously discussed.

                    This sample script demonstrates using exit methods to place protective orders.

                    Comment


                      #11
                      so bottom line, I have to use both:
                      OnOrderUpdate()

                      OnExecutionUpdate()

                      Comment


                        #12
                        Hello,

                        Yes, to use exit methods for your protective orders you should use both as per the example script.

                        Comment


                          #13
                          Hi,

                          thank you for your help so far. I am still, however having issues.
                          Trying to place the second entry, after all the debugging, I am still seeing only 1 order.
                          Really not sure what I am doing wrong.
                          If I change the "if(execution.order....) to not include the "&& execution.Order == entryOrder1" part, it works fine and places two entries, but with that part, it only enters one entry


                          protected override void OnBarUpdate()
                          {
                          if (CurrentBar < 20) return;


                          // Check if we can place a new entry order
                          if (entryOrder1 == null && Position.MarketPosition == MarketPosition.Flat && CurrentBar > BarsRequiredToTrade)
                          {
                          entryOrder1 = EnterLong(firstPositionSize, "BullishEntry1");

                          }

                          }


                          protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                          {

                          if (execution.Order != null && execution.Order.OrderState == OrderState.Filled && execution.Order == entryOrder1)
                          {


                          if (execution.Order.Name == "BullishEntry1")
                          {

                          entryOrder2 = EnterLong(secondPositionSize, "BullishEntry2");
                          }

                          }
                          }​

                          Edit****
                          I changed the code to use the execution.Order.Name name instead of execution.Order == entryOrder1 and it works now. However, what I see happening is that after 1 bar, it is closing the SL, Target 1 and target 2 before either are reached and is just leaving the position open without any protective orders.

                          protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                          {


                          // Check if entry order 1 is filled
                          if (execution.Order != null && execution.Order.OrderState == OrderState.Filled )//&& execution.Order == entryOrder1)
                          {


                          // Place the second entry order
                          if (execution.Order.Name == "BullishEntry1")
                          {
                          Print("Placing second order - BullishEntry2 with size: " + secondPositionSize);
                          entryOrder2 = EnterLong(secondPositionSize, "BullishEntry2");

                          // Now place stop and target orders for entryOrder1

                          ExitLongStopMarket(firstPositionSize, stopLossPrice, "Stop1", "BullishEntry1");
                          ExitLongLimit(firstPositionSize, tp1Price, "Target1", "BullishEntry1");

                          }

                          else if (execution.Order.Name == "BullishEntry2")
                          {
                          Print("Placing stop and target orders for entryOrder2 with quantity 2000");

                          // Set stop-loss and take-profit for entryOrder2
                          ExitLongStopMarket(secondPositionSize, stopLossPrice, "Stop2", "BullishEntry2");
                          ExitLongLimit(secondPositionSize, tp2Price, "Target2", "BullishEntry2");
                          }

                          }

                          }​
                          Last edited by Joseph Davidow; 10-08-2024, 02:23 PM.

                          Comment


                            #14
                            Hello,

                            . However, what I see happening is that after 1 bar, it is closing the SL, Target 1 and target 2 before either are reached and is just leaving the position open without any protective orders.
                            In order to keep orders alive, they must be called with each new bar update, or you can use the IsLiveUntilCancelled overloads.

                            Keeping Orders Alive example - https://ninjatrader.com/support/help...ders_alive.htm

                            Order methods - https://ninjatrader.com/support/help...er_methods.htm

                            Comment


                              #15

                              Hi Gaby,

                              I am slowly making some headway but have reached another obstacle.
                              using the example, "SampleOnOrderUpdate"; for moving SL to BE after reaching a certain profit level (in my case, after target1 is hit), I have adjusted my code to match the example, however, what is happening is that the SL is either being split in 2 and half is staying at the original price and the other half is moving to breakeven, or sometimes half the SL is being cancelled with the 1st target, and staying at the original price.

                              Both are not good.

                              Below is my OnExecutionUpdate () code. I hope you can help.

                              protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                              {
                              // Handle BullishEntry1 execution
                              if (execution.Order != null && execution.Order.Name == "BullishEntry1")
                              {
                              // We sum the quantities of each execution making up the entry order
                              sumFilled += execution.Quantity;

                              // Submit exit orders for partial fills (BullishEntry1)
                              if (execution.Order.OrderState == OrderState.PartFilled)
                              {
                              stopLossOrder1 = ExitLongStopMarket(0, true, execution.Order.Filled, stopLossPrice, "Stop1", "BullishEntry1");
                              targetOrder1 = ExitLongLimit(0, true, execution.Order.Filled, tp1Price, "Target1", "BullishEntry1");
                              Print("Partial fill for BullishEntry1. StopLoss and Target orders placed for partial position.");
                              }
                              // Submit exit orders for full fill (BullishEntry1)
                              else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
                              {
                              stopLossOrder1 = ExitLongStopMarket(0, true, firstPositionSize, stopLossPrice, "Stop1", "BullishEntry1");
                              targetOrder1 = ExitLongLimit(0, true, firstPositionSize, tp1Price, "Target1", "BullishEntry1");

                              // Enter second position
                              entryPrice2 = price;
                              EnterLong(secondPositionSize, "BullishEntry2");
                              Print("BullishEntry1 fully filled. StopLoss and Target1 placed. Entering BullishEntry2.");
                              }

                              // Reset after order fully filled
                              if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
                              {
                              sumFilled = 0;
                              Print("BullishEntry1 order reset after full execution.");
                              }
                              }

                              // Handle BullishEntry2 execution
                              if (execution.Order != null && execution.Order.Name == "BullishEntry2")
                              {
                              if (execution.Order.OrderState == OrderState.PartFilled)
                              {
                              stopLossOrder2 = ExitLongStopMarket(0, true, execution.Order.Filled, stopLossPrice, "Stop2", "BullishEntry2");
                              targetOrder2 = ExitLongLimit(0, true, execution.Order.Filled, tp2Price, "Target2", "BullishEntry2");
                              Print("Partial fill for BullishEntry2. StopLoss and Target orders placed for partial position.");
                              }
                              else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
                              {
                              stopLossOrder2 = ExitLongStopMarket(0, true, secondPositionSize, stopLossPrice, "Stop2", "BullishEntry2");
                              targetOrder2 = ExitLongLimit(0, true, secondPositionSize, tp2Price, "Target2", "BullishEntry2");
                              Print("BullishEntry2 fully filled. StopLoss and Target2 placed.");
                              }
                              }

                              // Handle TP1 hit and adjust StopLoss for BullishEntry2
                              if (execution.Order != null && execution.Order.Name == "Target1")
                              {
                              Print("TP1 hit. Adjusting stop-loss for BullishEntry2.");

                              // Cancel existing stop-loss order for BullishEntry2 if still active
                              if (stopLossOrder2 != null && stopLossOrder2.OrderState == OrderState.Working)
                              {
                              CancelOrder(stopLossOrder2);
                              stopLossOrder2 = null;
                              Print("Canceled stop-loss for BullishEntry2.");
                              }

                              // Ensure stop-loss for full second position is moved to BE + pips
                              double newStopPrice = entryPrice1 + (BreakEvenPipAdjustment * pipSize);
                              stopLossOrder2 = ExitLongStopMarket(0, true, secondPositionSize, newStopPrice, "AdjustedStop", "BullishEntry2");
                              Print("Moved stop-loss for BullishEntry2 to BE + " + BreakEvenPipAdjustment + " pips at price: " + newStopPrice);
                              }

                              // Reset orders after execution for BullishEntry1
                              if ((stopLossOrder1 != null && stopLossOrder1 == execution.Order) || (targetOrder1 != null && targetOrder1 == execution.Order))
                              {
                              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                              {
                              stopLossOrder1 = null;
                              targetOrder1 = null;
                              Print("BullishEntry1 orders reset after execution.");
                              }
                              }

                              // Reset orders after execution for BullishEntry2
                              if ((stopLossOrder2 != null && stopLossOrder2 == execution.Order) || (targetOrder2 != null && targetOrder2 == execution.Order))
                              {
                              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                              {
                              stopLossOrder2 = null;
                              targetOrder2 = null;
                              Print("BullishEntry2 orders reset after execution.");
                              }
                              }
                              }


                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              578 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              334 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 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
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X