'Interval Parameter not working on FuncAnimation
I want to provide 1min interval before my candlestick plot is refreshed/updated with new data using import matplotlib.animation (FuncAnimation). I am using mplfinance to create the candlestick plot and yahoofinance for get the data every 1min. But the code is getting executed every second not every minute. I have provide the interval as 60000.
Code for matplotlib.animation (FuncAnimation).
ani = animation.FuncAnimation(fig, Get_Data(LiveData), interval = 60000)
Below is my complete code for complete visibility or if anyone wants to use any of it for their projects.
import yfinance as yf
# Calling Pandas to create dataframe
import pandas as pd
#Importing Datetime package
from datetime import datetime, timedelta
from dateutil.parser import parse
import pytz
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.animation as animation
import mplfinance as mpf
import warnings
warnings.filterwarnings("ignore")
LiveData = yf.download(tickers="LCID",interval="1m", period="1d")
LiveData.reset_index(inplace = True)
LiveData['Datetime'] = LiveData['Datetime'].dt.tz_convert('US/Eastern').dt.tz_localize(None)
LiveData['Datetime'] = pd.to_datetime(LiveData['Datetime'])
LiveData=LiveData.set_index('Datetime')
fig = mpf.figure(style='charles',figsize=(12,7))
ax1 = fig.add_subplot()
ax1.xaxis.set_major_locator(plt.MultipleLocator(1))
ax1.xaxis.set_major_locator(plt.MaxNLocator(20))
mpf.plot(LiveData,ax=ax1, type='candle',show_nontrading=True,volume=False)
mpf.plot(LiveData,ax=ax1, type='line',show_nontrading=True,volume=False)
def Get_Data(LiveData):
Data = yf.download(tickers="LCID",interval="1m", period="1d")
if len(Data)<=len(LiveData):
return print('No Update')
Data.reset_index(inplace = True)
Data['Datetime'] = Data['Datetime'].dt.tz_convert('US/Eastern').dt.tz_localize(None)
Data['Datetime'] = pd.to_datetime(Data['Datetime'])
Data=Data.set_index('Datetime')
#fig = mpf.figure(style='charles',figsize=(12,7))
#ax1 = fig.add_subplot()
ax1.clear()
ax1.xaxis.set_major_locator(plt.MultipleLocator(1))
ax1.xaxis.set_major_locator(plt.MaxNLocator(20))
mpf.plot(Data,ax=ax1, type='candle',show_nontrading=True,volume=False)
mpf.plot(Data,ax=ax1, type='line',show_nontrading=True,volume=False)
return
def get_time():
now = datetime.now()
nyc_time = datetime.now(tz=pytz.timezone('US/Eastern'))
nyc_time = nyc_time.strftime("%H:%M:%S")
alarm = now.replace(hour=16, minute=0, second=0, microsecond=0)
opentime = now.replace(hour=9, minute=30, second=0, microsecond=0)
alarm = alarm.strftime("%H:%M:%S")
opentime = opentime.strftime("%H:%M:%S")
nyc_time = parse(nyc_time)
alarm = parse(alarm)
opentime = parse(opentime)
return nyc_time, alarm, opentime
while True:
nyc_time, alarm, opentime = get_time()
if nyc_time > alarm or nyc_time < opentime:
if nyc_time > alarm:
print("NASDAQ Closed for the day")
break
if nyc_time < alarm:
print("NASDAQ has not opened for the day")
break
ani = animation.FuncAnimation(fig, Get_Data(LiveData), interval = 60000)
Please guide me where am I going wrong.
Regards Sudhir
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
