Algorithm bugs on fast speeds while backtesting

 
void EditOperationLine()
   {
    string search = "-> #"; // trend text to find
    string str_iPonti;
    int iPonti = ObjectsTotal(0, 0, OBJ_TREND);
    for(int i = 0; i < iPonti; i++)
       {
        if(StringFind(ObjectName(0, i, 0, OBJ_TREND), search, 0) > 0)
           {
            // Check if the line was changed
            if(ObjectGetInteger(0, ObjectName(0, i, 0, OBJ_TREND), OBJPROP_WIDTH) != ipt_TrendWidth)
               {
                str_iPonti = ObjectName(0, i, 0, OBJ_TREND);
                ObjectSetInteger(0, str_iPonti, OBJPROP_WIDTH, ipt_TrendWidth);
                ObjectSetInteger(0, str_iPonti, OBJPROP_STYLE, STYLE_DASH);
                switch(LORT)
                   {
                    case was_zero :
                        ObjectSetInteger(0, str_iPonti, OBJPROP_COLOR, clrWheat);
                        LORT = LossOrTake::none;
                        break;
                    case was_take :
                        ObjectSetInteger(0, str_iPonti, OBJPROP_COLOR, clrGreen);
                        LORT = LossOrTake::none;
                        break;
                    case was_stop :
                        ObjectSetInteger(0, str_iPonti, OBJPROP_COLOR, clrRed);
                        LORT = LossOrTake::none;
                        break;
                    default:
                        break;
                   }
               }
           }
       }
   }

I have this code that runs onTick on a EA (normally it runs only on new candles, but to isolate the problem it's running onTick).

LORT is a enum which value is set OnTradeTransaction based on the deal reason and other logic.
Basically
'was_take' is DEAL_REASON_TP,
'was_stop' is DEAL_REASON_SL,
'was_zero' is if the profit value is zero to a degree of error.

The problem is that when the speed is fast the colors don't apply properly while in normal speed the colors are ok. My objective is to change the default trendline colors to match the result of the last closed operation: red for SL, green for TP and wheat for near zero profit.

Running each tick
Max speed colors are wrong, see file "SPEED BUG.jpg"

Fast but not so, colors are how I expect, see file "SPEED OK.jpg"

Is there something I can do to avoid this problem (other than not using max speed) or is it that the chart runs faster than my EA can process my function?

Thanks

Files:
SPEED_BUG.jpg  34 kb
SPEED_OK.jpg  36 kb
 
Your code logic is strange. LORT is a global variable you set in OnTradeTransaction() but in the posted code you taking the first trend line that match your condition without checking it's the one corresponding the the trade from OnTradeTransaction. You should not assume that your code (EA) and the chart display are synchronized, they are not and at full speed it matters.
 
Alain Verleyen #:
Your code logic is strange. LORT is a global variable you set in OnTradeTransaction() but in the posted code you taking the first trend line that match your condition without checking it's the one corresponding the the trade from OnTradeTransaction. You should not assume that your code (EA) and the chart display are synchronized, they are not and at full speed it matters.
I see, initially I tried to call the function inside OnTradeTransaction and every other places, but I wasn't thinking whether the chart was in sync or not, only the transactions and the results. Now I understood that I have to wait until the trendline is actually created. Thanks, I'll try to solve with this in mind.