My code below creates a bracket trade. I have then utilized OnExecution to create a stop loss ( see 2nd part of code ). I then do a few things in the OnBarUpdate() section...basically I keep the Stoploss adjusted tick by tick.
The problem however is that when the Stop Loss is activated the system does not always go back to " OnMarketData(MarketDataEventArgs e" to create a new Bracket trade.
sometimes it does... most of the time it doesn't
Please help
protected override void Initialize()
{
CalculateOnBarClose = false;
Unmanaged = true;
}
protected override void OnMarketData(MarketDataEventArgs e)
{
if
( longOrder == null || shortOrder == null)
{
// Create the Bracket on Initialization
// Automatically recreates a Bracket when the previous one has run its course!!!
longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, GetCurrentBid() +23 * TickSize, GetCurrentBid() +10 * TickSize, "Oil", "long limit entry");
shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, GetCurrentBid() -23 * TickSize, GetCurrentBid() -10 * TickSize, "Oil", "Short limit entry");
}
}
/* 2nd Part of code */
protected override void OnExecution(IExecution execution)
{
pTickPrice = Close[0]; // Keep a record of the Execution TickPrice
if (longOrder != null && longOrder == execution.Order) // we need a short position Stop loss
{
stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, 1, Position.AvgPrice +1 * TickSize, Position.AvgPrice +1 * TickSize, "protectLong", "Stop loss long");
}
else if (shortOrder != null && shortOrder == execution.Order)
//(Position.MarketPosition == MarketPosition.Short && stopOrder == null)
{
// (2)
stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.StopLimit, 1, pTickPrice -1 * TickSize, pTickPrice -1 * TickSize, "Oil", "Stop loss short");
}
// (4) if stopOrder is executed then the long / short position must have been closed
if (stopOrder != null && stopOrder == execution.Order)
{
//shortOrder = longOrder = null;
}

Comment