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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    56 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    133 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    73 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X