If the last trade is a sell:

// Calculate the amount for trade 3 $trade_3_amount = $trade_2_amount; // The amount for trade 3 is the same as the amount used in trade 2

// Calculate the value of trade 3 based on its amount and the given exchange rate $trade_3_val = $trade_3_amount * $arbitrage['trade_3_rate'];

// Since this is the last trade in the sequence, calculate the percentage profit from the start to finish $percent = (($trade_3_val / $start_val) * 100) - 100;

In simpler terms:

The code first sets the amount for the third trade to be the same as the amount used in the second trade.

Then it calculates the value of the third trade by multiplying its amount with a specific exchange rate.

Finally, it computes the percentage profit from the start of the trading sequence to the end. It divides the value of the third trade by the starting value, multiplies by 100 to get a percentage, and then subtracts 100 to find the profit percentage.

If the last trade is a buy:

// Calculate the amount for trade 3 $trade_3_amount = $trade_2_amount; // The amount for trade 3 is the same as the amount used in trade 2

// Calculate the value of trade 3 based on its amount and the given exchange rate $trade_3_val = $trade_3_amount / $arbitrage['trade_3_rate']; // Instead of multiplication, it's division

// Since this is the last trade in the sequence and it's a buy, calculate the percentage profit from start to finish $percent = ((($start_val / $trade_3_val) * 100) - 100) * -1; // Here, we reverse the division to find the profit percentage

In simpler terms:

The code first sets the amount for the third trade to be the same as the amount used in the second trade.

Then it calculates the value of the third trade by dividing its amount by a specific exchange rate.

Since this is the last trade and it's a buy, to calculate the profit percentage, we divide the starting value by the value of the third trade, multiply by 100 to get a percentage, and then subtract 100. Finally, we multiply the result by -1 to make it positive (since it's a buy).