I don't have a code snippet I'm afraid, but I guess the logic (for fixed odds bets rather than Asians) must be as follows:
- Step 1 - For each market and selection, calculate the amount you'd lose if the selection won (i.e. sum over lay bets of stake*(odds-1) - sum over back bets of stake*(odds-1) and the amount you'd lose if the selection lost (i.e. sum of back bet stakes - sum of lay bet stakes). (I'm assuming we'll treat losses as positive and so come up with a positive exposure value, but you can flip all the signs and treat exposures as negative if that's more convenient).
- Step 2 - Look at each market as a whole. Find the largest of all the individual loss values calculated for that market in step 1, and that's your exposure to the market as a whole i.e. the worst-case scenario. (This is capped at zero i.e. the exposure for an all-green book is zero.)
- Step 3 - Add the exposures for all the individual markets to get a total exposure.