I just realized that my trailing stop script that works fine in back testing is not working as I expect it too in live trading. What I expect the script to do is simply check different conditions and trail the current stop loss in order to lock in further profits on each primary bar update. The problem I am experiencing is that the current stop loss is cancelled on bar update but the new stop loss is calculated (as I see it in the Print field) but is not entered, therefore I end up with a position but with no stop loss. Are you able to spot anything erroneous in my script? I was thinking it could be the part highlighted in bold, but if I eliminate that I end up having 2 stops in the market, the old one and the new one..
if(BarsInProgress == 0) // primary time frame (e.i. 60 min)
{
#region Long - Stop Loss Management
{
if(Position.MarketPosition == MarketPosition.Long && stopOrderLong != null)
{
if(conditionA == true)
{
stopPriceA = Low[0];
}
if(conditionB == true)
{
stopPriceB = Low[1];
}
}
double newStop = Math.Max(stopPriceA,stopPriceB); // calculate new stop loss
if(newStop > stopOrderLong.StopPrice) // check if new stop loss is higher than current stop loss
{
if(newStop < Close[0])
{
[B]CancelOrder(stopOrderLong);[/B]
stopOrderLong = ExitLongStop(1,true,stopOrderLong.Quantity, newStop,"","");
}
else if(newStop >= Close[0])
{
CancelOrder(stopOrderLong);
ExitLong(1,stopOrderLong.Quantity,"","");
}
}

Comment