Please help. I have code that sets long position stop loss or short position stop loss orders as is appropriate. Although the long stops do sometimes execute, the short do not. These orders are entered in the OnExecution() method as follows:
As you can see, the chart text is placed by the code. So the order is at least entered, but it seems it always blows through.
OnBarUpdate()
if (Position.MarketPosition == MarketPosition.Long){
if (fallingBar1 && fallingBar0 && shortThreshold && shortXADXVMA) // short entry signal
{
exitOrder = ExitLong(Position.Quantity, "exitLongOnShortX", "LongOnADX");
entryOrder = EnterShortLimit(simLotsTraded, GetCurrentAsk(), "ShortOnADX");
//Log("Short order placed because close plus threshold (" + longTrigger1.ToString() + ") is less than the ADX (" + ADXVMA(ADX_Per)[0].ToString() + ").", LogLevel.Warning);
} else if (!ADXVMARising && shortCross && exitOnFlatCross) {
exitOrder = ExitLong(Position.Quantity, "exitLongOnShortXAndFlat", "LongOnADX");
}
} else if (Position.MarketPosition == MarketPosition.Short) {
if (risingBar1 && risingBar0 && longThreshold && longXADXVMA ) // long entry signal
{
exitOrder = ExitShort(Position.Quantity, "exitShortOnLongX", "ShortOnADX");
entryOrder = EnterLongLimit(simLotsTraded, GetCurrentBid(), "LongOnADX");
//Log("Long order placed because close minus threshold (" + longTrigger1.ToString() + ") is greater than the ADX (" + ADXVMA(ADX_Per)[0].ToString() + ").", LogLevel.Warning);
} else if (!ADXVMAFalling && longCross && exitOnFlatCross ){
exitOrder = ExitShort(Position.Quantity, "exitShortOnLongXAndFlat", "ShortOnADX");
}
} else { // no position
if (risingBar1 && risingBar0 && longThreshold && longXADXVMA )
{
entryOrder = EnterLongLimit(simLotsTraded, GetCurrentBid(), "LongOnADX");
//Log("Long order placed because close minus threshold (" + longTrigger1.ToString() + ") is greater than the ADX (" + ADXVMA(ADX_Per)[0].ToString() + ").", LogLevel.Warning);
}
// Condition to short
if (fallingBar1 && fallingBar0 && shortThreshold && shortXADXVMA)
{
entryOrder = EnterShortLimit(simLotsTraded, GetCurrentAsk(), "ShortOnADX");
//Log("Short order placed because close plus threshold (" + longTrigger1.ToString() + ") is less than the ADX (" + ADXVMA(ADX_Per)[0].ToString() + ").", LogLevel.Warning);
}
}
OnExecution()
if (entryOrder != null && entryOrder.Token == execution.Order.Token){
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
if (entryOrder.Name.Equals("LongOnADX")) {
// Stop-Loss order entryrange ticks below our entry price
//stop1Price = (10000*execution.Order.AvgFillPrice - entryThreshold)/10000;
stop1Price = (10000*ADXVMA(aDX_Per)[0] - 5)/10000;
stopOrder = ExitLongStop(0, true, execution.Order.Filled, stop1Price, "longStop", "LongOnADX");
dotCounter += 1;
dotName = "dot" + dotCounter.ToString();
someString = "Setting Long Stop at " + stop1Price.ToString();
DrawText(dotName, someString, 0, (Close[0]+.0045), Color.Black);
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder = null;
}
} else if (entryOrder.Name.Equals("ShortOnADX")){
// Stop-Loss order entryrange ticks above our entry price
//stop1Price = (10000*execution.Order.AvgFillPrice + entryThreshold)/10000;
stop1Price = (10000*ADXVMA(aDX_Per)[0] + 5)/10000;
stopOrder = ExitLongStop(0, true, execution.Order.Filled, stop1Price, "shortStop", "ShortOnADX");
dotCounter += 1;
dotName = "dot" + dotCounter.ToString();
someString = "Setting Short Stop at " + stop1Price.ToString();
DrawText(dotName, someString, 0, (Close[0]-.004), Color.Black);
// enter a stop order at the market open (10 pip loss)
//stopOrder = ExitShortStop(0, true, execution.Order.Filled, stop1Price, "shortStop1", "enterShortOnADXCross");
// Target order profitTarget below our entry price /// use heuristics to exit order
//targetOrder = ExitShortLimit(0, true, execution.Order.Filled/2, shortTarget1 , "shortTarget1", "enterShortOnADXCross");
// Resets the entryOrder object to null after the order has been filled or partially filled to completion
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder = null;
}
} // enterLongOnADXCross or enterShortOnADXCross processing
} // was the entry order filled at all?
} // is this an entryOrder?

Comment