Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Breakeven/Trailing for appropriate Signal Name

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

    Breakeven/Trailing for appropriate Signal Name

    Good morning Team. I want to manage each signal/position in different way. This is my approach for ABC setup which doesn't work

    OnBarUpdate piece of CODE:
    Code:
     else if (Position.MarketPosition != MarketPosition.Flat)
      {
          switch (Position.MarketPosition)
          {
              case MarketPosition.Long:
      
                  break;
              case MarketPosition.Short:
      
                  if(entryOrder != null)
                      Print($"{entryOrder.Name}");
      
                  if (entryOrder != null && entryOrder.Name == "ABC_Short")
                  {
                      Print($"{entryOrder.Name}");
                      try
                      {
                          double breakevenPrice = breakevenSwingShort.GetFibLevel(0.7);
      
                          if (Close[0] <= breakevenPrice)
                          {
                              if (stopOrder != null && stopOrder.StopPrice > Position.AveragePrice)
                              {
                                  stopOrder = ExitShortStopMarket(0, true, stopOrder.Quantity, Position.AveragePrice, "Abc_StopShort", "ABC_Short");
                              }
                          }
                      } catch (Exception)
                      {
                          Print("Cos jest nie tak...");
                      }
                      
                  }
     }
    So what exactly I know that is the fact entryOrder always stays null.
    Normally I use your code from custom example of handling orders.

    Code:
    ​protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) {
       
          List<String> orders = new List<string> { "LongEntry", "ShortEntry", "BOS_UP_ENTRY", "ABC_Long", "ABC_Short", "BoS1_Long" };
       
          if (orders.Contains(order.Name))
          {
              entryOrder = order;
              if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
              {
                  entryOrder = null;
                  sumFilled = 0;
              }
          }
      }
       
      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
          if (entryOrder != null && entryOrder == execution.Order)
          {
       
              if (execution.Order.Name == "ABC_Long" || execution.Order.Name == "ABC_Short" || execution.Order.Name == "BoS1_Long")
              {
                  entryOrder = execution.Order;
                  Print($"✅ entryOrder ustawione: {entryOrder.Name}, cena: {entryOrder.AverageFillPrice}, ilość: {entryOrder.Quantity}");
              }
              if (execution.Order.OrderState == OrderState.Filled
                  || execution.Order.OrderState == OrderState.PartFilled
                  || (execution.Order.OrderState == OrderState.Cancelled
                  && execution.Order.Filled > 0))
              {
                  sumFilled += execution.Quantity;
       
                  if (execution.Order.OrderState == OrderState.PartFilled)
                  {
                      if ((execution.Order.IsLong))
                      {
       
                          // ABC Settings part 1
                          stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, StopLossPrice, "ABC_StopLong", "ABC_Long");
                          targetOrder = ExitLongLimit(0, true, execution.Order.Filled, TakeProfitPrice, "ABC_ProfitLong", "ABC_Long");
       
                          // BoS1_Long
                          stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, s_bos1_loss, "BoS1_StopLong", "BoS1_Long");
                          targetOrder = ExitLongLimit(0, true, execution.Order.Filled, s_bos1_profit, "BoS1_ProfitLong", "BoS1_Long");
       
                      }
                      else if ((execution.Order.IsShort))
                      {
       
                          stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, StopLossPrice, "Abc_StopShort", "ABC_Short");
                          targetOrder = ExitShortLimit(0, true, execution.Order.Filled, TakeProfitPrice, "Abc_ProfitShort", "ABC_Short");
       
       
                      }
       
                  }
                  else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
                  {
                      if ((execution.Order.IsLong))
                      {
       
       
                          // ABC Settings part 1
                          stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, StopLossPrice, "ABC_StopLong", "ABC_Long");
                          targetOrder = ExitLongLimit(0, true, execution.Order.Filled, TakeProfitPrice, "ABC_ProfitLong", "ABC_Long");
       
                          // BOS 1 Settings
                          // BoS1_Long
                          stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, s_bos1_loss, "BoS1_StopLong", "BoS1_Long");
                          targetOrder = ExitLongLimit(0, true, execution.Order.Filled, s_bos1_profit, "BoS1_ProfitLong", "BoS1_Long");
       
                      }
                      else if ((execution.Order.IsShort))
                      {
       
                          stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, StopLossPrice, "Abc_StopShort", "ABC_Short");
                          targetOrder = ExitShortLimit(0, true, execution.Order.Filled, TakeProfitPrice, "Abc_ProfitShort", "ABC_Short");
                      }
                  }
                  if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
                  {
                      entryOrder = null;
                      sumFilled = 0;
                  }
              }
          }
          if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
          {
              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
              {
                  stopOrder = null;
                  targetOrder = null;
       
                  lastTradeTime = time;
                  Print($"Trade zakończony (Stop/Tp) o {time}. Wprowadzono cooldown 30 minut.");
              }
          }
     }

    Take Profits and stop losses orders work well ! The thing I want to achieve is breakeven on my conditions for a specific signal i onBarUpdate. The issue is the Order entryOrder stays null and I can't handle a name.

    #2
    Is entryOrder getting assigned properly in OnOrderUpdate? Before if (orders.Contains(order.Name)), add Print("order.Name: " + order.Name); and see if the order.Name is putting the name you want. I have had issues where I assigned it the wrong name (mismatch of Upper/ lower case in the name).
    If entryOrder stays null, you have to just follow the trail and ensure it changes from null in the first place, (if not, find out why and fix that) then make sure that doesn't change until you no longer need it (once your order has changed to your breakeven requirements and is Filled).

    Comment


      #3
      Hello darthTraderrr,

      In order to track a order while its active you need to search for its name in the OnOrderUpdate override events and then assign it to a variable. You can see an example of doing that here:

      Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


      In your current code you only assign a entryOrder variable from OnOrderUpdate. You also need to assign the other variables from that method too. The code stopOrder = ExitLongStopMarket is not guaranteed to produce an order object so you can just call ExitLongStopMarket without doing an assignment. You should always look for an actual order in OnOrderUpdate to assign a variable, that means the order was registered and processed and an Order object was generated.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      32 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      124 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      64 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      41 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X