Baseline correlation for template matching
This would be a cumbersome way to detect heart beats (or the QRS complex), if that is what you are trying to do ultimately.

A little bit about what you are trying to do currently:

Your observations are correct and to these I would like to add that no two heart beats are the same and therefore, strictly speaking, your template will be aligning just with similar heart beats. What if the subject started running in the mean time? That would be quite a different heart beat template to compare against.

If you absolutely must use a template matching technique and apply a hard threshold on the peaks of the cross correlation, here is a set of points that might help you:


Apply a high pass filter in the region of 0-5Hz. This will remove the slow varying components and what you observe as "drift".
Select a number of heart beats, at random, from the filtered signal and create an average "beat". Use this as your template.


A little bit about how is this task usually done:

Picking up the heart beats from an ECG signal usually involves rectification of the signal and sliding window integration with the possible addition of some filtering to reduce the effect of artefacts. The "ideal" result of this is a time series that looks like a square pulse with the positive transition of the pulse almost aligned with the start of the QRS complex and the negative transition of the pulse aligned with the end of the QRS complex. But in practice, you might find that this signal is more "floating" due to noise and artefacts which means that you might also have to use an adaptive threshold technique to improve the detection performance.

For more information please see this link. For much more information, including signals, please see this link.

Hope this helps.