Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Break Even

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

    Break Even

    Hi,

    I'm working with a strategy that enters two trades with the same stop loss and two different targets.

    What I'm trying to do is that if the firstTPShort is filled, then set the Short2 trade is set to break even.

    Here's the code I have:

    Code:
    protected override void OnBarUpdate()
            {
    
                if (BarsInProgress != 0)
                    return;​
    
                if (condition == true)
                {
                    SetStopLoss("Short1", CalculationMode.Price, stopLossShort, false);
                    SetProfitTarget("Short1", CalculationMode.Price, firstTPShort);
    
                    SetStopLoss("Short2", CalculationMode.Price, stopLossShort, false);
                    SetProfitTarget("Short2", CalculationMode.Price, secondTPShort);
                    
                    shortOrder1 = EnterShortStopMarket(0, true, 1, adjustedSessionLow, "Short1");
                    shortOrder2 = EnterShortStopMarket(0, true, 1, adjustedSessionLow, "Short2");​
                }
    }
    
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
                    if (shortOrder2 != null)
                    {
                        Print("Estado de Short2: " + shortOrder2.OrderState);
                        if (shortOrder2.OrderState == OrderState.Filled || shortOrder2.OrderState == OrderState.Accepted)
                        {
                            double breakEvenPriceShort = Position.AveragePrice; // Usar el precio promedio de la posición actual como el nuevo StopLoss
                            Print("Moviendo StopLoss de Short2 a BreakEven en: " + breakEvenPriceShort);
                            //ChangeOrder(shortOrder2, shortOrder2.Quantity, breakEvenPriceShort, shortOrder2.StopPrice);
                            ExitShortStopMarket(1,19513.75,"","");
                        }
                        else
                        {
                            Print("Short2 no está en estado Filled o Accepted, no se mueve el StopLoss.");
                        }
                    }
                    else
                    {
                        Print("shortOrder2 es null, no se puede ajustar el StopLoss.");
                    }​
    }​
    ​

    I tried many different things, can anyone help me?

    #2
    Hello juliourenaForum,

    The order from SetStopLoss() will always be "Stop loss".

    Code:
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (execution.Order.Name == "Stop loss" && execution.Order.FromEntrySignal == "Short1")
    {
    SetStopLoss("Short2", CalculationMode.Price, Math.Max(GetCurrentAsk() + TickSize, Position.AveragePrice), false);
    }
    }
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    128 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    65 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    117 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X