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
#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]

Comment