Take `dates` as a parameter, and move the function xySeries outside this block.