I’m having issues with a trailing stop and target on a indicator. I know there are several posts about this but I can’t find anything to fix the issues I’m having.
If I set the target on a indicator and hard stop no problem it works great. if I set the trailing stop with a hard target it works great the problem I can’t get both of them to work together. I can get one to work but not the other.
Here is the code for the Target
protectedoverridevoid OnExecution(IExecution iexec){
double entryPrice = iexec.Price;
double tickSize = Instrument.MasterInstrument.TickSize;
if (iexec.MarketPosition == MarketPosition.Long){
double stopPrice = entryPrice - stop*tickSize;
ExitLongLimit(0, true, iexec.Order.Quantity, ExitPrice[0], "Sweet Exit",iexec.Order.FromEntrySignal);
ExitLongStop(0, true, iexec.Order.Quantity, stopPrice, "Sad exit", iexec.Order.FromEntrySignal);
}
else if (iexec.MarketPosition == MarketPosition.Short) {
double stopPrice = entryPrice + stop*tickSize;
ExitShortStop(0, true, iexec.Order.Quantity, stopPrice, "Sad exit", iexec.Order.FromEntrySignal);
ExitShortStop(0, true, iexec.Order.Quantity, TrailExit [0], "Sad exit", iexec.Order.FromEntrySignal);
ExitShortLimit(0, true, iexec.Order.Quantity, ExitPrice[0], "Sweet Exit", iexec.Order.FromEntrySignal);
}
Here is the code for the Trailing stop
protectedoverridevoid OnBarUpdate()
{
#region Breakeven and Trailing Stops
// Resets the stop loss to the original value when all positions are closed
switch (Position.MarketPosition)
{
case MarketPosition.Flat:
SetStopLoss(CalculationMode.Ticks, stopLossTicks);
previousPrice = 0;
break;
case MarketPosition.Long:
// Once the price is greater than entry price+ breakEvenTicks ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + breakEvenTicks * TickSize
&& previousPrice == 0)
{
initialBreakEven = Position.AvgPrice + plusBreakEven * TickSize;
SetStopLoss(CalculationMode.Price, initialBreakEven);
previousPrice = Position.AvgPrice;
PrintWithTimeStamp("previousPrice = "+previousPrice);
}
// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
elseif (previousPrice != 0////StopLoss is at breakeven
&& GetCurrentAsk() > previousPrice + trailProfitTrigger * TickSize
)
{
newPrice = previousPrice + trailStepTicks * TickSize;
SetStopLoss(CalculationMode.Price, newPrice);
previousPrice = newPrice;
PrintWithTimeStamp("previousPrice = "+previousPrice);
}
break;
case MarketPosition.Short:
// Once the price is Less than entry price - breakEvenTicks ticks, set stop loss to breakeven
if (Close[0] < Position.AvgPrice - breakEvenTicks * TickSize
&& previousPrice == 0)
{
initialBreakEven = Position.AvgPrice - plusBreakEven * TickSize;
SetStopLoss(CalculationMode.Price, initialBreakEven);
previousPrice = Position.AvgPrice;
PrintWithTimeStamp("previousPrice = "+previousPrice);
}
// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
elseif (previousPrice != 0////StopLoss is at breakeven
&& GetCurrentAsk() < previousPrice - trailProfitTrigger * TickSize
)
{
newPrice = previousPrice - trailStepTicks * TickSize;
SetStopLoss(CalculationMode.Price, newPrice);
previousPrice = newPrice;
PrintWithTimeStamp("previousPrice = "+previousPrice);
}
break;
default:
//autoStopA900Traded = false;
break;
}
What do I need to do to fix this issue?
Comment