# Finding all intersection point between two time series

I've the following time series in smoothedData:

tmp = WolframAlpha[
"EUR vs USD in 2020", {{"Result", 1}, "ComputableData"}];
data = TimeSeries@tmp[];
smoothedData = MovingAverage[data, #] & /@ {10, 50};
DateListPlot[smoothedData, PlotTheme -> "Marketing"] How can I find the intersection points between those two time series?

I don't know if it's better to represent them as interpolating functions or as time series and in this case the points should be the samples that are next to the intersection (i.e. where the difference of time series change sign between a timestamp and the next one).

What's the best solution?

• Just look for points where they are equal or when the difference changes sign.
– A.G.
Apr 24, 2021 at 20:04

There's a long winded way with TimeSeriesThread or interpolation and subtracting the curves to find zero crossings, however there's also a shortcut where you can extract the lines from the plot and immediately get the intersections:

tmp = WolframAlpha["EUR vs USD in 2020", {{"Result", 1}, "ComputableData"}];
data = TimeSeries@tmp[];
smoothedData = MovingAverage[data, #] & /@ {10, 50};
plot = DateListPlot[smoothedData];
points = RegionIntersection @@ Cases[plot, Line[_], Infinity];

plot = DateListPlot[smoothedData, PlotTheme -> "Marketing",
Epilog -> {White, PointSize[Large], points}] ... and if you're okay with using undocumented functions you could alternatively write:

points = Point[GraphicsMeshFindIntersections[plot]]

• Thanks a lot this is what I needed! Apr 25, 2021 at 8:09

Find where the differences between two time series change sign by subtracting the second series from the first. The transitions show the dates where the two series intersect.

transitions = Sign[smoothedData[] - smoothedData[]];
DateListPlot[transitions] None of the intersecting values are equal (none of the differences are zero).

Position[transitions["Values"], 0]
(* {} *)


Find the dates before and after each transition value. Get the values for each date from the smoothed values. Plot the dates with the smoothed data plot.

pos = Sort@Flatten[{#, # + 1}] &@
Flatten[Position[Abs[Differences[transitions]["Values"]], 2]];
dates = transitions["Dates"][[pos]];
dateValues = Transpose[{dates, smoothedData[][dates]}];

Show[DateListPlot[smoothedData, PlotTheme -> "Marketing"],
DateListPlot[Style[dateValues, Yellow], Joined -> False]] Here are the dates before and after each intersection between the time series.

TableForm[Partition[{DateString[#1, "Date"], #2} & @@@ dateValues, 2]] • Thanks, this is a really nice solution. Apr 25, 2021 at 17:09