It happens that I have a called df 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 executing 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 executing 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 graph the data from my df, I created 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 executing it and writing btc_plot
to the console for its execution, the following graph is returned:
And finally when executing type(btc_plot)
the following output is returned:
matplotlib.figure.Figure
The problem:
I'm packed trying to do a "simple" thing, I want to send the graph stored in btc_plot
to a private Telegram channel using the Telegram API with Python3, and according to this section of the Telegram API documentation, the following line should work:
requests.post(f'https://api.telegram.org/bot{bot_str}/sendPhoto',
data = {'chat_id':f'-100{channel_id_str}', 'photo': btc_plot, 'caption':'Fue detectada una señal bajista en el par BTC/USDT!'})
However, after executing that line, all I got was an <Response [400]>
as output with no further explanation or exception from the console.
I think it may be happening because I'm trying to send an object matplotlib.figure.Figure
, but I mistakenly thought the variable btc_plot
had stored an object png
because an image was returned when executing btc_plot
to the console, so I'm lost.
Ironically, the following line of code works as expected with a <Response [200]>
as output:
requests.post(f'https://api.telegram.org/bot{bot_str}/sendMessage',
data = {'chat_id':f'-100{channel_id_str}', 'text': 'Fue detectada una señal bajista en el par BTC/USDT!'})
I honestly don't know how to fix it, can I get some feedback to improve my solution or maybe an alternative?
I found an alternative, which I actually think was always the right way to do what you wanted:
Output: