In the stock market, some option traders (typically larger ones) use techniques for risk management known as delta neutral and delta hedging. These operations create a relationship between stock prices and open positions on options. Maximum Pain is the value that maximizes option buyer's losses and the theory predicts that stock prices on expiration day will trend towards this value.
Calculating Buyers' Gain
Open Interest is the amount of option contracts that have been purchased and sold for specific stock prices (the strike price) and expiration dates. Call contracts give the buyer the right to buy at the strike price. Call buyers make money based on the difference betweeen stock price and strike price when the stock price is above the strike, otherwise they don't make any money. Put contracts give a right to sell and contract buyers make the difference between strike and option price when the price is below the strike price, zero otherwise. For each strike price we can calculate the Call and Put buyers' gains and aggregate them.