- The relatedness r of X to Y
- The cost C of the altruistic behavior (reduction in fitness of X)
- The benefit B of the altruistic behavior (increase in fitness of Y)
Sure, it's better to do an altruistic act for a co-ethnic than a foreigner. But it's better in the sense that you lose less if you help a co-ethnic than a foreigner. Your genes don't gain anything in either case. Altruism is adaptive for your close kin. Altruism for your co-ethnics or foreigners is (usually) a strictly lose or lose-more proposition.