Link Displays Between Two iMacs (Late-2015 Retina running Sierra and 2017 running Mojave) Using Thunderbolt-to-Thunderbolt
I'm afraid neither of those iMacs is capable of Target Display Mode
Generally it was iMacs between 2009 &amp; 2014 that supported it, no retina iMacs were included.
See Lifewire - How to Use Your Old iMac as a Monitor
Unfortunately, Apple's own KB article on this seems to have recently vanished.