Create a Corresponding Entity Reference preset programmatically
[EDIT]

Almost a year later, I found a better way (the entity_import way actually did not work).

CERs are entities. The best way to create one is using $cer = entity_create('cer', array('bundle' =&gt; 'cer'));   adding appropriate values to the fields of the created entity, and saving it by using entity_save('cer', $cer); 

=======================

Finally figured it out. You can use the entity_import function. 


Create a variable containing a JSON-like string of the entity (which you can get by exporting a CER entity), something like:

$j = '{
  "identifier" : "node:time_slot:field_ref_student*commerce_line_item:registration:field_ref_slots",
  "cer_bidirectional" : { "und" : [ { "value" : "1" } ] },
  "cer_enabled" : { "und" : [ { "value" : "1" } ] },
  "cer_weight" : { "und" : [ { "value" : "0" } ] },
  "cer_left" : { "und" : [ { "path" : "node:time_slot:field_ref_student" } ] },
  "cer_right" : { "und" : [ { "path" : "commerce_line_item:registration:field_ref_slots" } ] },
  "rdf_mapping" : []
}';
Then import and save:

$e = entity_import('cer', $j);
entity_save('cer', $e);