I have a problem trying to cancel orders submitted, but not filled in a Strategy designed for backtesting. I will explain the code I've pasted below:
1.I add 1min bars to gain "granularity" to send the orders and try to approach the market more realistic because I use triggers in the 30min chart.
2.In the primary bars (30min), if MarketPosition is flat and we are in the correct time (between hInicio and hFin), the strategy evaluates the triggers for long and short positions. If they are true, it puts an Stop Order entry and calculates targets and stops.
3.In the secondary bars, the strategy manages orders. If the entry orders is filled, it puts the target and stop orders and if the time is past the hCierre hour, it closes positions.
4. The problem comes in the last part of the code that is "commented" with //. If I leave it commented (without use) the strategy works fine with only one problem: It opens positions past hCierre and cancel them immediately. So I need to cancel the orders submitted but not filled. I tried the code commented, but the strategy doesn't work, it mades 0 trades.
How can I cancel those orders? Is a better way to do that?
Thanks in advance,
This is the code
protected override void Initialize()
{
Add(PeriodType.Minute, 1);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (BarsInProgress == 0)
{
if(Position.MarketPosition == MarketPosition.Flat)
{
if (ToTime(Time[0]) > ToTime(hInicio, 0, 0) && ToTime(Time[0]) < ToTime(hFin, 0, 0))
{
// Condiciones para posible apertura de largos
if (triggerLong)
{
PIVOTEL = High[0] + filtro * 10 * TickSize;
OBJETIVOL = Instrument.MasterInstrument.Round2TickSize(PIVOTEL + TARGETL);
STOPL = Instrument.MasterInstrument.Round2TickSize(PIVOTEL - STOPL);
entryOrderLong=EnterLongStop(1,true,100000, PIVOTEL, "EntradaLargos");
}
// Condiciones para la posibel apertura de cortos
if (triggerShort)
{
PIVOTEC = Low[0] - filtro * 10 * TickSize;
OBJETIVOC = Instrument.MasterInstrument.Round2TickSize(PIVOTEC - TARGETS);
STOPC = Instrument.MasterInstrument.Round2TickSize(PIVOTEC + STOPS);
entryOrderShort=EnterShortStop(1,true,100000, PIVOTEC, "EntradaCortos");
}
}
}
}
// When the OnBarUpdate() is called from the secondary bar series, manejamos STOPS y OBJETIVOS
else
{
// C.Manejando posiciones
if (Position.MarketPosition == MarketPosition.Long)
{
if (ToTime(Time[0]) >= ToTime(hCierre, 0, 0))
{
ExitLong(0,100000,"CierreLargos","EntradaLargos");
}
else
{
ExitLongLimit(100000,OBJETIVOL, "ObjetivoLargos", "EntradaLargos");
ExitLongStop(100000,STOPL, "StopLargos", "EntradaLargos");
}
}
else if (Position.MarketPosition == MarketPosition.Short)
{
if (ToTime(Time[0]) >= ToTime(hCierre, 0, 0))
{
ExitShort(0,100000,"CierreCortos","EntradaCortos");
}
else
{
ExitShortLimit(100000,OBJETIVOC, "ObjetivoCortos", "EntradaCortos");
ExitShortStop(100000,STOPC, "StopCortos", "EntradaCortos");
}
}
else
{
// if (ToTime(Time[0]) >= ToTime(hCierre, 0, 0))
// {
// CancelOrder(entryOrderShort);
// CancelOrder(entryOrderLong);
//
// }
return;
}
}
}

Comment