Hi @ Levitikon and others,
Would anyone know why it seems to be triggering both the profit target and the stop loss at the same time in the following code:
In the code: I entering 1 order at a specific time I would like (8am).
Any idea why both are getting filled?
Thanks for the code!
namespace QuantConnect
{
public class indicator_res_test : QCAlgorithm
{
private OrderTicket EntryOrder { get; set; }
private Func<QCAlgorithm, string, decimal, OneCancelsOtherTicketSet> OnOrderFilledEvent { get; set; }
private OneCancelsOtherTicketSet ProfitLossOrders { get; set; }
public override void Initialize()
{
SetStartDate(2011, 6, 9);
SetEndDate(2011, 7, 1);
SetCash(10000);
AddSecurity(SecurityType.Forex, "EURUSD", Resolution.Minute, true, 50.0m, false);
}
public void OnData(TradeBars data)
{
if (!Portfolio.Invested && Time.TimeOfDay.Hours == 08)
{
this.OnOrderFilledEvent = (algo, symbol, filledPrice) =>
{
return new OneCancelsOtherTicketSet(
algo.LimitOrder(symbol, -45000, filledPrice + 0.00212m, "Profit Target"),
algo.StopMarketOrder(symbol, -45000, filledPrice - 0.00212m, "Stop Loss"));
};
this.EntryOrder = MarketOrder("EURUSD", 45000, false, "Entry");
}
}
public override void OnOrderEvent(OrderEvent orderEvent)
{
if (EntryOrder != null)
{
this.EntryOrder = null;
}
if (orderEvent.Status == OrderStatus.Filled || orderEvent.Status == OrderStatus.PartiallyFilled)
{
if (this.OnOrderFilledEvent != null)
{
this.ProfitLossOrders = OnOrderFilledEvent(this, orderEvent.Symbol, orderEvent.FillPrice);
OnOrderFilledEvent = null;
}
else if (this.ProfitLossOrders != null)
{
this.ProfitLossOrders.Filled();
Log("Profit/Loss Filled" + "Portfolio Invested Check: =" + Portfolio.Invested);
this.ProfitLossOrders = null;
}
}
}
}
}