You have to introduce some lag, i.e. make it calculate EMA for some periods before. If you want to calculate 5 min ema I suggest introducing past 3 days 5min candle data and then calculating it for the current day. You can concat the resulting df later on.
Once you will call Historical data of 5 minutes timeframe, you can calculate EMA on it using talib library.
Refer this documentation