Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

move the stop of a strategy freely manually

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

    move the stop of a strategy freely manually

    Click image for larger version  Name:	Captura de pantalla 2024-08-28 131204.png Views:	0 Size:	74.5 KB ID:	1316083

    I have this strategy, where I create buttons​
    sl button, moves the stop in the direction of the entry point​ (+4tick)
    be button puts be the position​
    tp button, moves the tp ​ (+4tick)


    BUY, generates a purchase order with 80 of sl​
    SELL,generates a SELL order with 80 of sl​


    What I want is that once I make the example entry with the SELL button I want it to let the STOP or TP move without it returning it to me​

    example. Once I make the sell entry I press the BE button, it puts the order in BE but after 5 seconds the strategy automatically returns the sl where it was




    Code:
            #region CREAORDENCOMPRA
                private void EnterLongWithSLTP(int slTicks, int tpTicks)
                {
                    
                    double ultimoAlto = MAX(High, 5)[0];
                    double ultimoBajo = MIN(Low, 5)[0];
                    double ajusteTicks = 5 * TickSize;
                    double ultimoAltoAjustado = ultimoAlto + ajusteTicks;
                    double ultimoBajoAjustado = ultimoBajo - ajusteTicks;
                    
                        Draw.HorizontalLine(this, "UltimoAltoAjustado", ultimoAltoAjustado, Brushes.Red);
                        Draw.HorizontalLine(this, "UltimoBajoAjustado", ultimoBajoAjustado, Brushes.Green);
                    
                    if (Position.MarketPosition != MarketPosition.Flat)
                    {
                        Print("Ya hay una posición abierta en este instrumento.");
                        return;
                    }
    
                    // Configura el Stop Loss y Take Profit
                    SetStopLoss(CalculationMode.Ticks, slTicks);
                    SetProfitTarget(CalculationMode.Ticks, tpTicks);
    
                    // Entra en una posición de compra
                    EnterLong(1, "MyBuyOrder");
                }
            #endregion
            
            #region CREAORDENVENTA
                private void EnterShortWithSLTP(int slTicks, int tpTicks)
                {
                    double riesgoEnUSD = 1000;
                    int cantidadContratosRedondeada=0;
                    double valorPorTick = Instrument.MasterInstrument.PointValue; // Valor en USD por tick
    
                    // Calcular la cantidad de contratos
                    double cantidadContratos = (riesgoEnUSD / (slTicks * valorPorTick));
                     cantidadContratosRedondeada = (int)Math.Round(cantidadContratos)*4;
                    
                    
                    // Obtén el último alto y bajo de las últimas 5 barras
                    double ultimoAlto = MAX(High, 5)[0];
                    double ultimoBajo = MIN(Low, 5)[0];
                    double ajusteTicks = 5 * TickSize;
                    
                    // Ajusta el último alto y bajo
                    double ultimoAltoAjustado = ultimoAlto + ajusteTicks;
                    double ultimoBajoAjustado = ultimoBajo - ajusteTicks;
                    
                    // Dibuja las líneas horizontales en el gráfico
                    Draw.HorizontalLine(this, "UltimoAltoAjustado", ultimoAltoAjustado, Brushes.Red);
                    Draw.HorizontalLine(this, "UltimoBajoAjustado", ultimoBajoAjustado, Brushes.Green);
                    
                    // Asegúrate de que no haya posiciones abiertas
                    if (Position.MarketPosition != MarketPosition.Flat)
                    {
                        Print("Ya hay una posición abierta en este instrumento.");
                        return;
                    }
                    
                    EnterShort( Convert.ToInt32(cantidadContratosRedondeada),"MyShort" );    
                    SetStopLoss("MyShort", CalculationMode.Ticks, slTicks, false);
                    SetProfitTarget("MyShort", CalculationMode.Ticks, tpTicks);
    
                  
                }
                #endregion
    
            #region MOVESL METHODS
            
                private void MoveSL (int Ticks)
                {
                    xAlselector = Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
                    string currentAccount = xAlselector.SelectedAccount.ToString();
                    
                    Account Acct = Account.All.FirstOrDefault(x => currentAccount.Contains(x.Name));
                    
                    xInSelector = Window.GetWindow(ChartControl.OwnerChart).FindFirst("ChartWindowInstrumentSelector") as NinjaTrader.Gui.Tools.InstrumentSelector;
                    string currentInstrument =    xInSelector.Instrument.ToString();
                    
                    Position thisPosition=Acct.Positions.FirstOrDefault(x => currentInstrument.Contains(x.Instrument.FullName));
                    foreach (Order order in Acct.Orders)
                    {
                        if (thisPosition.Account == order.Account && thisPosition.Instrument == order.Instrument)
                        {
                            if(order.OrderType==OrderType.StopMarket || order.OrderType==OrderType.StopLimit)
                            {                
                                if(order.OrderState != OrderState.Cancelled & order.OrderState != OrderState.Filled)
                                {        
                                    Order stopOrder=order;
                                    if (thisPosition.MarketPosition.ToString() == "Short")
                                    {
                                        stopOrder.StopPriceChanged = order.StopPrice - Ticks * order.Instrument.MasterInstrument.TickSize;
                                        Acct.Change(new[] { stopOrder });
                                    }
                                    if (thisPosition.MarketPosition.ToString() == "Long")
                                    {
                                        stopOrder.StopPriceChanged = order.StopPrice + Ticks * order.Instrument.MasterInstrument.TickSize;
                                        Acct.Change(new[] { stopOrder });
                                    }
                                    #region CANCEL ORDER
                                        /*
                                        if (thisPosition.MarketPosition.ToString() == "Flat")
                                        {
                                              Acct.Cancel(new[] { stopOrder });
                                        }
                                        */
                                    #endregion
                                }
                            }        
                        }
                    }        
                }
                
            #endregion
            
            #region MOVEBE METHODS
                
                private void MoveToBreakEven()
                {
                    xAlselector = Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
                    string currentAccount = xAlselector.SelectedAccount.ToString();
                    
                    Account Acct = Account.All.FirstOrDefault(x => currentAccount.Contains(x.Name));
                    
                    xInSelector = Window.GetWindow(ChartControl.OwnerChart).FindFirst("ChartWindowInstrumentSelector") as NinjaTrader.Gui.Tools.InstrumentSelector;
                    string currentInstrument = xInSelector.Instrument.ToString();
                    
                    Position thisPosition = Acct.Positions.FirstOrDefault(x => currentInstrument.Contains(x.Instrument.FullName));
                    foreach (Order order in Acct.Orders)
                    {
                        if (thisPosition.Account == order.Account && thisPosition.Instrument == order.Instrument)
                        {
                            if(order.OrderType == OrderType.StopMarket || order.OrderType == OrderType.StopLimit)
                            {                
                                if(order.OrderState != OrderState.Cancelled && order.OrderState != OrderState.Filled)
                                {        
                                    Order stopOrder = order;
                                    
                                    if (thisPosition.MarketPosition == MarketPosition.Short)
                                    {
                                        stopOrder.StopPriceChanged = thisPosition.AveragePrice;  // Mueve SL al precio de entrada para posición Short
                                        Acct.Change(new[] { stopOrder });
                                    }
                                    else if (thisPosition.MarketPosition == MarketPosition.Long)
                                    {
                                        stopOrder.StopPriceChanged = thisPosition.AveragePrice;  // Mueve SL al precio de entrada para posición Long
                                        Acct.Change(new[] { stopOrder });
                                    }
                                }
                            }        
                        }
                    }        
                }
                
            #endregion
                
            #region MOVETP METHODS
                
                private void MoveTP(int Ticks)
                {
                    xAlselector = Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
                    string currentAccount = xAlselector.SelectedAccount.ToString();
    
                    Account Acct = Account.All.FirstOrDefault(x => currentAccount.Contains(x.Name));
    
                    xInSelector = Window.GetWindow(ChartControl.OwnerChart).FindFirst("ChartWindowInstrumentSelector") as NinjaTrader.Gui.Tools.InstrumentSelector;
                    string currentInstrument = xInSelector.Instrument.ToString();
    
                    Position thisPosition = Acct.Positions.FirstOrDefault(x => currentInstrument.Contains(x.Instrument.FullName));
                    foreach (Order order in Acct.Orders)
                    {
                        if (thisPosition.Account == order.Account && thisPosition.Instrument == order.Instrument)
                        {
                            if (order.OrderType == OrderType.Limit)
                            {
                                if (order.OrderState != OrderState.Cancelled && order.OrderState != OrderState.Filled)
                                {
                                    Order tpOrder = order;
    
                                    if (thisPosition.MarketPosition == MarketPosition.Short)
                                    {
                                        tpOrder.LimitPriceChanged = order.LimitPrice - Ticks * order.Instrument.MasterInstrument.TickSize;
                                        Acct.Change(new[] { tpOrder });
                                    }
                                    else if (thisPosition.MarketPosition == MarketPosition.Long)
                                    {
                                        tpOrder.LimitPriceChanged = order.LimitPrice + Ticks * order.Instrument.MasterInstrument.TickSize;
                                        Acct.Change(new[] { tpOrder });
                                    }
                                }
                            }
                        }
                    }
                }
    
            
            #endregion​
    
    [B][/B]
    Attached Files
    Last edited by leojimenezp; 08-28-2024, 12:21 PM.

    #2
    Hello cbadr,

    Set methods are managed by NinjaTrader and are not intended to be managed by the user. If you modify an order submitted by a set method, the action will be undone the next time the Set method updates.

    Use Exit order methods with isLiveUntilCancelled as true instead if you intend to manually manage these.​

    Below is a link to an example.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 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
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X