Ultraluminous X-ray sources out to z~0.3 in the COSMOS field