Need Help Optimizing Code

I have a csv file of trade orders that I want to analyze on tick data (yes, sometimes the orders won't match up with price which is fine) on multiple futures contracts of the same symbol (in this case 3 contracts). So, I have created an Exploration for this purpose, but I am running into quite a slow-down which I somewhat expected (Load Factor is at 390%). I am sure that my code is not efficient, but not sure what to try next. I am using the following code and would appreciate any pointers on how to make this more efficient:

file = "blah.csv"; 
n=Param("look ahead for MAE MFE",100,10,20000,2);
MA_length = Param("length of averaging function",5,2,30,1);

dt = DateTime();
dt_1=Ref(dt,1);
// Initialize variables
Buy = Sell = Short = Cover = possize = counter = priceindex= 0;
ms=MilliSec();
ms_1=Ref(ms,1);
fh = fopen( file, "r" );
SetOption( "NoDefaultColumns", True );
nam=Name();

if( fh ) {
    while( !feof( fh ) ) {
        line = fgets( fh );
        sym = StrExtract( line, 0 );
        
        if( Name() == sym ) {
            trade = StrExtract( line, 1 );
            datetimeStr = StrExtract( line, 2 ) ; // "MM/DD/YYYY HH:MM:SS.mmm"
            // Extract milliseconds from your data
            millisPart = Strright(datetimeStr,3); // Assuming last 3 chars are milliseconds
             fileMilliseconds = StrToNum(millisPart);
        
            // Extract the date and time without milliseconds for comparison
            datetimePart = StrLeft(datetimeStr, 19); // Up to seconds
           trade_datetime = StrToDateTime(datetimePart);
			
            price = StrToNum( StrExtract( line, 3 ) );
            
            //Extract index
            index=StrToNum(StrExtract(line,5));
            
            //shares = StrToNum( StrExtract( line, 4 ) );

            // For the current bar, compare both datetime and milliseconds
      

	if ( trade == "Buy" )
			{
				dt_timeDiff = ( DateTimeDiff( trade_datetime, dt ) + ( filemilliseconds - ms ) / 1000 ); //saving some repetitve formulas
				Samebar = dt_timeDiff == 0 ;
				diffbar = ( ( dt_timeDiff > 0 )  AND( DateTimeDiff( trade_datetime, dt_1 ) + ( filemilliseconds - ms_1 ) / 1000 ) < 0 ) ;
				newbuy =   exrem( samebar, not samebar ) or diffbar  ;
				Buy = Buy OR newbuy;
				BuyPrice = IIf( newbuy , price, BuyPrice );
				priceindex = iif( newbuy, index, priceindex );

			//possize = IIf( newbuy, shares, possize );
			}

	if ( trade == "Sell" )
            {
				dt_timeDiff=(DateTimeDiff( trade_datetime, dt ) + ( filemilliseconds - ms )/1000); //saving some repetitve formulas
				Samebar= dt_timeDiff == 0 ;
				diffbar=(( dt_timeDiff > 0 )  AND ( DateTimeDiff( trade_datetime, dt_1 ) + ( filemilliseconds - ms_1)/1000) < 0 ) ;
                newsell = exrem(samebar,not samebar) or diffbar;
                Sell = Sell OR newsell; // combine previous sell signals with new
                SellPrice = IIf( newsell, price, SellPrice );
				priceindex = iif(newsell,index,priceindex); 
                
               // if(newsell) SellTime = dt; // store the time of the sell signal
            }            // Similar logic for Sell, Short, Cover with adjusted conditions
            
   if ( trade == "Cover" )
            {
				dt_timeDiff=(DateTimeDiff( trade_datetime, dt ) + ( filemilliseconds - ms )/1000); //saving some repetitve formulas
				Samebar= dt_timeDiff == 0 ;
				diffbar=(( dt_timeDiff > 0 )  AND ( DateTimeDiff( trade_datetime, dt_1 ) + ( filemilliseconds - ms_1)/1000) < 0 ) ;
                newcover = exrem(samebar,not samebar) or diffbar;
                cover = cover OR newcover; // combine previous cover signals with new
                coverPrice = IIf( newcover, price, coverPrice );
				priceindex = iif(newcover,index,priceindex); 
                
               // if(newsell) SellTime = dt; // store the time of the sell signal
            }            // Similar logic for Sell, Short, Cover with adjusted conditions
     if ( trade == "Short" )
            {
				dt_timeDiff=(DateTimeDiff( trade_datetime, dt ) + ( filemilliseconds - ms )/1000); //saving some repetitve formulas
				Samebar= dt_timeDiff == 0 ;
				diffbar=(( dt_timeDiff > 0 )  AND ( DateTimeDiff( trade_datetime, dt_1 ) + ( filemilliseconds - ms_1)/1000) < 0 ) ;
                newshort = exrem(samebar,not samebar) or diffbar;
                short = short OR newshort; // combine previous short signals with new
                shortPrice = IIf( newshort, price, shortPrice );
				priceindex = iif(newshort,index,priceindex); 
                
               // if(newsell) SellTime = dt; // store the time of the sell signal
            }            // Similar logic for Sell, Short, Cover with adjusted conditions

		}
    }
    fclose( fh );
} else {
    Error( "ERROR: file cannot be opened" );
}



function profitfactor( num )
{
    // Identify positive and negative future bars
    Prevclose = Ref( Close, -1 );
    positiveBars = Close > Prevclose;
    negativeBars = Close < Prevclose;

	// Sum of positive and negative differences
    log_close_Change = ln( Close / Prevclose );
    sumPositive = Sum( positiveBars * log_close_Change , num );
    sumNegative = Sum( negativeBars * log_close_Change, num );
    // Calculate the ratio, handling division by zero
    return Ref( IIf( sumNegative != 0, ln( sumPositive / -sumNegative ), 0 ), num );
}


Date_=Datenum();

averageNahead = ( MA( C, MA_length ) + Ref( MA( C, MA_length ), MA_length ) ) / 2;

flag=Sell OR Buy OR Cover OR short;
timecond=BarsSince(flag);

Filter=flag;

Textlist="NO SIGNAL\nBuy\nSell\nBuy & Sell\nShort\nBuy & Short\n"+
			"Sell & Short\nBuy & Sell & Short\nCover\nBuy & Cover\n"+
			"Sell & Cover\nBuy & Sell & Cover\nShort & Cover\n"+
			"Buy & Short & Cover\nSell & Short & Cover\nBuy & Sell & Short & Cover";
TextSelector = 1 * Buy + 2 * Sell + 4 * Short + 8 * Cover;
fontcolor = colorWhite;
cellcolor = colorDarkGrey;

AddColumn( dt, "Date Time", formatDateTime );
AddTextColumn( Name(), "Symbol", 1.0, fontcolor, cellcolor, 70 );
AddColumn(High,"High Price");
AddColumn(Low, "Low Price");
AddColumn( IIf(Sell,SellPrice, IIf(Buy,BuyPrice, IIf(Cover, CoverPrice,IIf(Short,ShortPrice,0)))), "Entry Price" );
AddColumn( SellPrice, "Sell Price" );
AddColumn( BuyPrice, "Buy Price" );
AddColumn( shortPrice, "Short Price" );
AddColumn( CoverPrice, "Cover Price" );
AddColumn(ms,"Millisecond");
AddColumn(priceindex,"Index");
AddColumn(Ref(timecond,-1),"bars between flags");

//Show Profit Factor
AddColumn( profitfactor(n), "PF "+n );
AddColumn( profitfactor(2*n), "PF " +2*n);
AddColumn( profitfactor(4*n), "PF " +4*n);
AddColumn( profitfactor(8*n), "PF " +8*n);
AddColumn( profitfactor(16*n), "PF " +16*n);
AddColumn( profitfactor(32*n), "PF " +32*n);
AddColumn( profitfactor(64*n), "PF " +64*n);
AddColumn( profitfactor(128*n), "PF " +128*n);
AddColumn( profitfactor(256*n), "PF " +256*n);

AddColumn( Ref( HHV( H, n ), n ), "Highest High" );
AddColumn( Ref( llV( L, n ), n ), "Lowest Low" );
AddColumn( Close, "Close Price" );

AddColumn( Buy, "Buy Signal" );
AddColumn( Sell, "Sell Signal" );
AddColumn( short, "Short Signal" );
AddColumn( Cover, "Cover Signal" );

AddMultiTextColumn(TextSelector, Textlist,"Trade Type");

AddColumn( Ref( averageNahead, n ), "Average tick" );
AddColumn( Ref( averageNahead, n / 2 ), "Average tick/2" );
AddColumn( Ref( averageNahead, n / 4 ), "Average tick/4" );
AddColumn( Ref( averageNahead, n / 8 ), "Average tick/8" );

Thanks!