In this strategy, there are two different stop losses with two different names. Both are often used in the same trade.
- TrailingStopLong
- LongAdjustedStop
The code for LongAdjustedStop gets called when certain criteria are met, and then when the trend starts going in my direction, then LongAdjustedStop is to no longer be used; instead, TrailingStopLong takes over. Currently, in my code, when TrailingStopLong takes over, I have it set to cancel the order for LongAdjustedStop.
Here is the code for that:
if (LongLimit != null
&& LongLimit.OrderState == OrderState.Filled)
{
if (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc)
&& LongAdjustedStop != null)
{
TrailingStopLong = ExitLongStop(0,true,positionSize, tStopPriceLong,"TrailingStopLong","LongEntryB");
Print(Time[0] + " Trailing Stop = " + tStopPriceLong);
Print("LongAdjustedStop = " + adjustedStopLong);
CancelOrder(LongAdjustedStop);
Print("Hooray!!!! LongTrailingStop is initially triggered @ " + tStopPriceLong + Time[0]);
}
}
If you look at that picture then you will see two trades. The first trade closed a position using TrailingStopLong at the price of 102.41 (which is correct!). The second trade also closed part of the position using TrailingStopLong at a price of 101.60 which is not correct. In this case, it should have exited using LongAdjustedStop.
As a result of this error, I decided to not use the "CancelOrder" command in order to leave LongAdjustedStop within the code. Please look at the other attached file called OrderNotCancelled.
In this picture you see the same trades. But this time, you will find that the first trade exits using LongAdjustedStop at a price of 102.40 which is not correct and the second trade exits with LongAdjustedStop at 101.75'5 which is correct.
Another element to this that I do not understand is that in the first trade (using the OrderNotCancelled attachment) the proper stop was 102.41 but it was stopped out at 102.40 using the wrong order. From my print statements, the TrailingStopLong is properly being called and trailing and is at the right price at the time that it should be stopped out. You can see that in the attachment called "output window."
Can somebody please help me fix this part of the code?
Thank you very very much!

Comment