Late Trade Buys Sells and Close Positions

 
Im very new to using MQL5 and Expert Advisors. I have spent a while getting the basic code for a Triple Super trend advisor. At the point im at it buys and sells and closes the position. However, it is extremely slow in doing each action resulting in missing the trend. I really cant understand if it is the way i have codded it or something else. Some of this code was me trying new ideas. I have also attached a file showing the extent of delay. After further testing it seems as though it will trigger the close or opening of position but takes a while for it go through as shown by the closing of the postion in image 2 eventhough the super trend all show as 3 red. This also goes for historical debug. It seems to be effected by the timescale of the chart. 5m takes longer then 1m.
#property version "0.1"

int stHandle1;  
int stHandle2;
int stHandle3;
#include <Trade/Trade.mqh>
input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;
input int Periods1 = 10;
input double Multiplier1 = 1.0;
input int Periods2 = 11;
input double Multiplier2 = 2.0;
input int Periods3 = 12;
input double Multiplier3 = 3.0;

double st1[];
double st2[];
double st3[];

input double Lots = 0.1;

int totalbars;

CTrade trade;
ulong aticket;
bool isTradeInProgress = false;  


input int TimerInterval = 500;

int timerId;

void CloseTrade() {
    if (aticket > 0) {
        trade.PositionClose(aticket);
        Print(__FUNCTION__, " > Trade closed. Ticket: ", aticket);
        aticket = 0; 
    }
    isTradeInProgress = false;  
}

void HandlePriceChange()
{
    double close1 = iClose(_Symbol, Timeframe, 1);
    double close2 = iClose(_Symbol, Timeframe, 2);
    double close3 = iClose(_Symbol, Timeframe, 3);

    CopyBuffer(stHandle1, 0, 0, 3, st1);
    CopyBuffer(stHandle2, 0, 0, 3, st2);
    CopyBuffer(stHandle3, 0, 0, 3, st3);

   
    bool allBelow = (st1[0] < close1 && st2[0] < close2 && st3[0] < close3);

   
    bool allAbove = (st1[0] > close1 && st2[0] > close2 && st3[0] > close3);

    
    if (!isTradeInProgress) {
        // Buy Condition
        if (allBelow) {
            // Open a new buy trade
            trade.Buy(Lots, _Symbol);
            if (trade.ResultRetcode() == TRADE_RETCODE_DONE) {
                aticket = trade.ResultOrder();
                isTradeInProgress = true;  
                Print(__FUNCTION__, " > Buy Signal ...");
            }
        }

       
        if (allAbove) {
            
            trade.Sell(Lots, _Symbol);
            if (trade.ResultRetcode() == TRADE_RETCODE_DONE) {
                aticket = trade.ResultOrder();
                isTradeInProgress = true; 
                Print(__FUNCTION__, " > Sell Signal ...");
            }
        }
    } else {
        
        if ((!allBelow && (st1[0] < close1 || st2[0] < close2 || st3[0] < close3)) ||
            (!allAbove && (st1[0] > close1 || st2[0] > close2 || st3[0] > close3))) {
            CloseTrade();  
        }
    }
}


void OnTimer()
{
    
    HandlePriceChange();
}

int OnInit()
{
   
    totalbars = iBars(_Symbol, Timeframe);
    stHandle1 = iCustom(_Symbol, Timeframe, "SuperTrend.ex5", Periods1, Multiplier1);
    stHandle2 = iCustom(_Symbol, Timeframe, "SuperTrend2.ex5", Periods2, Multiplier2);
    stHandle3 = iCustom(_Symbol, Timeframe, "SuperTrend3.ex5", Periods3, Multiplier3);

    // Start the timer to check for price changes
    timerId = EventSetMillisecondTimer(TimerInterval);
    return INIT_SUCCEEDED;
}

void OnDeinit(const int reason){
    
    EventKillTimer();
}