Try this:
Code:
program:
marked = from_search('id', 'marked:true');
count(marked, ',')
Edit: As rule, calibre templates only work per book (horizontally). If you want to get data that depends on more that one book, Action Chains functions (from_search & from_selection) should help by pulling data from multiple books (vertically).