Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

I want to open two orders buy stop and sell stop

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

    I want to open two orders buy stop and sell stop

    I want to open two orders at the same time.
    buy stop and sell stop.


    but it only opens one for me. whoever is first. in this case buystop
    Code:
    6/02/2025 4:13:12 p. m. Strategy 'MctNotiBot/350074865': Entered internal SubmitOrderManaged() method at 6/02/2025 4:13:12 p. m.: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=10 LimitPrice=0 StopPrice=21832,50 SignalName='BuyStopOCO' FromEntrySignal=''
    6/02/2025 4:13:12 p. m. Strategy 'MctNotiBot/350074865': Entered internal SubmitOrderManaged() method at 6/02/2025 4:13:12 p. m.: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=10 LimitPrice=0 StopPrice=21807,50 SignalName='SellStopOCO' FromEntrySignal=''
    6/02/2025 4:13:12 p. m. Strategy 'MctNotiBot/350074865': Ignored SubmitOrderManaged() method at 6/02/2025 4:13:12 p. m.: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=10 LimitPrice=0 StopPrice=21807,50 SignalName='SellStopOCO' FromEntrySignal='' Reason='An Enter() method to submit an entry order has been ignored. Please search on the term 'Internal Order Handling Rules that Reduce Unwanted Positions' in the Help Guide for detailed explanation.'
    Strategy 'MctNotiBot/350074865': An Enter() method to submit an entry order at '02/06/2025 16:13:30' has been ignored. Please search on the term 'Internal Order Handling Rules that Reduce Unwanted Positions' in the Help Guide for detailed explanation.

    Code:
    {
        public class MctNotiBot : Strategy
        {
            private bool ordersPlaced = false; // Bandera para controlar si las órdenes ya se colocaron
            private Order buyStopOrder; // Variable para almacenar la orden Buy Stop
            private Order sellStopOrder; // Variable para almacenar la orden Sell Stop
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Estrategia con órdenes OCO: Buy Stop y Sell Stop.";
                    Name                                        = "MctNotiBot";
                    Calculate                                    = Calculate.OnEachTick;
                    EntriesPerDirection                            = 2; // Permitir 2 entradas simultáneas (Buy y Sell)
                    EntryHandling                                = EntryHandling.AllEntries; // Permitir entradas en ambas direcciones
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat; // Esperar a estar plano antes de operar
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = true; // Habilitar para depuración
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    IsInstantiatedOnEachOptimizationIteration    = true;
                }
                else if (State == State.Configure)
                {
                    // Configurar el manejo de órdenes OCO
                }
                else if (State == State.DataLoaded)
                {
                    // Inicialización adicional si es necesario
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (State != State.Realtime || ordersPlaced || Position.MarketPosition != MarketPosition.Flat)
                    return;
    
                // Colocar las órdenes solo una vez cuando la estrategia esté activa y no haya posición abierta
                PlaceOCOOrders();
                ordersPlaced = true; // Marcar que las órdenes ya se colocaron
            }
    
            private void PlaceOCOOrders()
            {
                double currentPrice = Close[0];
                double buyStopPrice = currentPrice + 50 * TickSize; // 50 puntos arriba
                double sellStopPrice = currentPrice - 50 * TickSize; // 50 puntos abajo
    
                // Colocar la orden Buy Stop con 10 micros
                buyStopOrder = EnterLongStopMarket(10, buyStopPrice, "BuyStopOCO");
    
                // Colocar la orden Sell Stop con 10 micros
                sellStopOrder = EnterShortStopMarket(10, sellStopPrice, "SellStopOCO");
    
                // Vincular las órdenes como OCO (One Cancels the Other)
                buyStopOrder.Oco = "OCOGroup";
                sellStopOrder.Oco = "OCOGroup";
            }
    
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
                // Si una de las órdenes es ejecutada, cancelar la otra
                if (execution.Order == buyStopOrder || execution.Order == sellStopOrder)
                {
                    if (buyStopOrder != null && buyStopOrder != execution.Order)
                    {
                        CancelOrder(buyStopOrder);
                        buyStopOrder = null;
                    }
    
                    if (sellStopOrder != null && sellStopOrder != execution.Order)
                    {
                        CancelOrder(sellStopOrder);
                        sellStopOrder = null;
                    }
                }
            }
        }
    }​

    #2
    Hello leojimenezp,

    You would need to use the unmanaged approach for that. There is a post about doing that type of entry here:

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    553 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    100 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    543 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    546 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X