Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Button BE error

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

    Button BE error

    Click image for larger version

Name:	Captura de pantalla 2024-08-26 160547.png
Views:	212
Size:	61.5 KB
ID:	1315785


    I am trying to have a button that takes an open position that was done manually (charttrader).
    but the following error does not appear​
    McGEnergyValidation.cs 'NinjaTrader.Cbi.Position' does not contain a definition for 'AvgPrice' and no extension method 'AvgPrice' accepting a first argument of type 'NinjaTrader.Cbi.Position' could be found (are you missing a using directive or an assembly reference?) CS1061 341 56



    Code:
    private void MoveToBreakEven()
    {
        // Obtener el instrumento y la posición actual
        string currentInstrument = ChartControl.Instrument.FullName;
        Position position = Position; // Obtiene la posición actual del instrumento en la estrategia
        
        Print("Instrumento actual: " + currentInstrument);
        Print("Posición: " + position);
    
        if (position != null && position.Quantity > 0)
        {
            foreach (var order in Orders)
            {
                if (order.Instrument.FullName == currentInstrument)
                {
                    if (order.OrderType == OrderType.StopMarket || order.OrderType == OrderType.StopLimit)
                    {
                        if (order.OrderState != OrderState.Cancelled && order.OrderState != OrderState.Filled)
                        {
                            // Cancelar la orden existente
                            CancelOrder(order);
    
                            // Obtener el precio promedio de la posición
                            double averagePrice = position.AvgPrice; // Usa AvgPrice
    
                            // Crear una nueva orden de Stop con el precio promedio
                            if (position.MarketPosition == MarketPosition.Short)
                            {
                                SubmitOrder(0, OrderAction.BuyToCover, OrderType.StopMarket, position.Quantity, averagePrice, 0, "BreakEven", "BreakEvenSignal");
                            }
                            else if (position.MarketPosition == MarketPosition.Long)
                            {
                                SubmitOrder(0, OrderAction.Sell, OrderType.StopMarket, position.Quantity, averagePrice, 0, "BreakEven", "BreakEvenSignal");
                            }
                        }
                    }
                }
            }
        }
        else
        {
            Print("No se encontró posición para el instrumento actual o la posición está vacía.");
        }
    }
    
      
    private void SubmitOrder(int barsAgo, OrderAction action, OrderType orderType, double quantity, double limitPrice, double stopPrice, string signalName, string orderTag)
    {
        // Enviar la orden
        SubmitOrder(barsAgo, action, orderType, quantity, limitPrice, stopPrice, signalName, orderTag);
    }
    
    ​

    #2
    Hello leojimenezp,

    It is AveragePrice not AvgPrice.

    Comment


      #3
      Tk, but It doesn't read the open position I have from the charttrader

      Click image for larger version

Name:	Captura de pantalla 2024-08-26 172828.png
Views:	186
Size:	90.7 KB
ID:	1315790

      I have some Print but in the output I get that it does not take the position


      Code:
      private void OnBreakEvenButtonClick(object sender, RoutedEventArgs e)
              {
                  MoveToBreakEven(); // Llama a tu función para mover a Break Even
              }
                      
                      
                  private void MoveToBreakEven()
              {
                  
                  // Obtener el instrumento y la posición actual
                  string currentInstrument = ChartControl.Instrument.FullName;
                  Position position = Position; // Obtiene la posición actual del instrumento en la estrategia
      
                  Print("Instrumento actual: " + currentInstrument);
                  Print("Posición: " + position);
                  
                  Print("Instrumento actual: " + currentInstrument);
                  Print("Posición: " + position);
                  if (position != null)
                  {
                      Print("Estado de la posición: " + position.MarketPosition);
                      Print("Cantidad de la posición: " + position.Quantity);
                      Print("Precio promedio de la posición: " + position.AveragePrice);
                  }
      
                  if (position != null && position.Quantity > 0)
                  {
                      foreach (var order in Orders)
                      {
                          if (order.Instrument.FullName == currentInstrument)
                          {
                              if (order.OrderType == OrderType.StopMarket || order.OrderType == OrderType.StopLimit)
                              {
                                  if (order.OrderState != OrderState.Cancelled && order.OrderState != OrderState.Filled)
                                  {
                                      // Cancelar la orden existente
                                      CancelOrder(order);
      
                                      // Obtener el precio promedio de la posición
                                      double averagePrice = position.AveragePrice; // Usa AveragePrice
      
                                      // Crear una nueva orden de Stop con el precio promedio
                                      if (position.MarketPosition == MarketPosition.Short)
                                      {
                                          SubmitOrder(0, OrderAction.BuyToCover, OrderType.StopMarket, position.Quantity, averagePrice, 0, "BreakEven", "BreakEvenSignal");
                                      }
                                      else if (position.MarketPosition == MarketPosition.Long)
                                      {
                                          SubmitOrder(0, OrderAction.Sell, OrderType.StopMarket, position.Quantity, averagePrice, 0, "BreakEven", "BreakEvenSignal");
                                      }
                                  }
                              }
                          }
                      }
                  }
                  else
                  {
                      Print("No se encontró posición para el instrumento actual o la posición está vacía.");
                  }
              }
      
              private void SubmitOrder(int barsAgo, OrderAction action, OrderType orderType, double quantity, double limitPrice, double stopPrice, string signalName, string orderTag)
              {
                  // Aquí llamamos al método SubmitOrder de NinjaTrader con la firma correcta
                  Print("Enviando orden: " + action + ", " + orderType + ", " + quantity + ", " + limitPrice + ", " + stopPrice + ", " + signalName + ", " + orderTag);
                  // El método real de envío de órdenes puede variar según tu configuración. Asegúrate de que este método esté correcto.
                  SubmitOrder(barsAgo, action, orderType, quantity, limitPrice, stopPrice, signalName, orderTag);
              }​

      Comment


        #4
        Hello leojimenezp,

        In the code you provided you are referencing the strategy Position property which would be the strategies virtual position and not the one in the chart trader. It looks like you are mixing up the account values and strategy values. Looping over the Orders like you are is not supported when using strategies, you should instead be using order instance like the sample here:



        If you place trades manually or from other strategies this strategy will not be able to see those orders or positions. A strategy needs to enter the position itself to see it.

        With this code: Position position = Position; < this is the strategy virtual position so the strategy needs to have entered a position for this to be populated.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        607 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        353 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
        560 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        561 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X