“Bias in the Effective Bid-Ask Spread“, published in Journal of Financial Economics, shows that the effective spread measured relative to the spread midpoint is positively biased. To fix the bias, all you need is to replace the midpoint with the weighted midpoint:
# Midpoint X = (P_ask + P_bid) / 2;
# Weighted midpoint
X = (P_ask * Q_bid + P_bid * Q_ask) / (Q_bid + Q_ask);
In the paper, I show that the weighted midpoint effective spread and the micro-price effective spread yield similar results. For more about the weighted midpoint and the micro-price, see Stoikov (2018).
For R code on the weighted midpoint and micro-price implementation, as well as the tables and figures of my paper, download the replication code here. I can’t publish the data, but the code includes a random data generator that allows the scripts to run.