Home > front end >  How to correctly send a matplotlib.figure.Figure to a private telegram channel on Python3 via Telegr
How to correctly send a matplotlib.figure.Figure to a private telegram channel on Python3 via Telegr

Time:08-15

Say I have the following df called df_trading_pair_date_time_index which contains the following data:

                         Open      High       Low     Close                End Date
Start Date                                                                         
2022-08-12 00:25:00  23834.13  23909.27  23830.00  23877.62 2022-08-12 00:29:59.999
2022-08-12 00:30:00  23877.62  23968.52  23877.62  23936.89 2022-08-12 00:34:59.999
2022-08-12 00:35:00  23936.89  23989.95  23915.92  23962.50 2022-08-12 00:39:59.999
2022-08-12 00:40:00  23960.64  23985.03  23935.60  23966.71 2022-08-12 00:44:59.999
2022-08-12 00:45:00  23966.71  23996.94  23958.00  23983.68 2022-08-12 00:49:59.999
2022-08-12 00:50:00  23982.53  24009.67  23958.89  23996.59 2022-08-12 00:54:59.999
2022-08-12 00:55:00  23995.49  24005.30  23963.92  23964.37 2022-08-12 00:59:59.999
2022-08-12 01:00:00  23965.31  24000.00  23940.61  23975.64 2022-08-12 01:04:59.999
2022-08-12 01:05:00  23977.04  23996.85  23928.95  23943.09 2022-08-12 01:09:59.999
2022-08-12 01:10:00  23944.05  23972.86  23885.00  23905.23 2022-08-12 01:14:59.999
2022-08-12 01:15:00  23905.23  23944.66  23901.74  23925.72 2022-08-12 01:19:59.999
2022-08-12 01:20:00  23925.72  23951.21  23917.84  23945.03 2022-08-12 01:24:59.999
2022-08-12 01:25:00  23945.03  23961.78  23935.12  23945.60 2022-08-12 01:29:59.999
2022-08-12 01:30:00  23945.60  23949.86  23919.90  23934.50 2022-08-12 01:34:59.999
2022-08-12 01:35:00  23934.49  23934.50  23853.65  23895.44 2022-08-12 01:39:59.999
2022-08-12 01:40:00  23895.44  23932.11  23894.67  23906.00 2022-08-12 01:44:59.999
2022-08-12 01:45:00  23905.42  23927.26  23878.57  23902.75 2022-08-12 01:49:59.999
2022-08-12 01:50:00  23902.76  23915.00  23888.08  23889.19 2022-08-12 01:54:59.999

When running df_trading_pair_date_time_index.dtypes the following output is presented:

Open               float64
High               float64
Low                float64
Close              float64
End Date    datetime64[ns]
dtype: object

And when running df_trading_pair_date_time_index.index the following output is returned:

DatetimeIndex(['2022-08-12 00:25:00', '2022-08-12 00:30:00', '2022-08-12 00:35:00', '2022-08-12 00:40:00', '2022-08-12 00:45:00', '2022-08-12 00:50:00', '2022-08-12 00:55:00', '2022-08-12 01:00:00',
               '2022-08-12 01:05:00', '2022-08-12 01:10:00', '2022-08-12 01:15:00', '2022-08-12 01:20:00', '2022-08-12 01:25:00', '2022-08-12 01:30:00', '2022-08-12 01:35:00', '2022-08-12 01:40:00',
               '2022-08-12 01:45:00', '2022-08-12 01:50:00'],
              dtype='datetime64[ns]', name='Start Date', freq=None)

In order to plot the data above, I used the following code:

import requests
import mplfinance as mpf
import matplotlib.pyplot as plt
import pandas as pd

# Plotting
# Create my own `marketcolors` style:
mc = mpf.make_marketcolors(up='#2fc71e',down='#ed2f1a',inherit=True)
# Create my own `MatPlotFinance` style:
s  = mpf.make_mpf_style(base_mpl_style=['bmh', 'dark_background'],marketcolors=mc, y_on_right=True)    

# Plot it
btc_plot, axlist = mpf.plot(df_trading_pair_date_time_index,
                            figratio=(10, 6),
                            type="candle",
                            style=s,
                            tight_layout=True,
                            datetime_format = '%H:%M',
                            ylabel = "Precio ($)",
                            returnfig=True)
# Add Title
axlist[0].set_title("BTC/USDT - 5m", fontsize=25, style='italic', fontfamily='fantasy' )

After running btc_plot in the console, the following chart is returned:

chart

And finally when running type(btc_plot) the following output is returned:

matplotlib.figure.Figure

The problem:

I am having a hard time dealing with a "simple" thing, I want to send such plotted trading chart to a private telegram channel through the Telegram API with Python3, so according to wtf

May I get here some alternative solution or an improvement to my current one?

CodePudding user response:

Found an alternative, which I think it actually always was the right way to do it.

import telegram

btc_plot.savefig('signal.png',dpi=300, bbox_inches = "tight")
telegram.Bot(token= token_str).send_photo(chat_id= chat_id_str,
                                          photo=open("signal.png", 'rb'),
                                          caption="Fue detectada una señal bajista en el par BTC/USDT!")

Output:

enter image description here

  • Related