import ipywidgets as wd
import cufflinks as cf
import pandas as pd
import yfinance as yf
from plotly.offline import iplot,init_notebook_mode
from ipywidgets import interact,interact_manual
init_notebook_mode()
stocks=['QQQ','MSFT','GOOGL','FB','TSLA','AAPL']
indicators=['Bollinger Bands','MACD','RSI']
def ta_dashboard(asset,indicator,start_date,end_date,bb_k,bb_n,macd_fast,macd_slow,macd_signal,rsi_periods,rsi_upper,rsi_lower):
df=yf.download(asset,start=start_date,end=end_date,progress=False,auto_adjust=True)
qf=cf.QuantFig(df,title=f'TA Dashboard={asset}',legend='right',name=f'{asset}')
if 'Bollinger Bands' in indicator:
qf.add_bollinger_bands(periods=bb_n,boll_std=bb_k)
if 'MACD' in indicator:
qf.add_macd(fast_period=macd_fast,slow_period=macd_slow,signal_period=macd_signal)
if 'RSI' in indicator:
qf.add_rsi(period=rsi_periods,rsi_upper=rsi_upper,rsi_lower=rsi_lower,showbands=True)
return qf.iplot()
controls_dict={'asset':stocks_selector,
'indicator':indicator_selector,
'start_date':start_date_selector,
'end_date':end_date_selector,
'bb_k':k_param,
'bb_n':n_param,
'macd_fast':macd_fast,
'macd_slow':macd_slow,
'macd_signal':macd_signal,
'rsi_periods':rsi_period,
'rsi_upper':rsi_upper,
'rsi_lower':rsi_lower}
ui=wd.HBox([main_selector_box,secondary_selector_box])
out=wd.interactive_output(ta_dashboard,controls_dict)
I got the error message:
PlotlyRequestError: Authentication credentials were not provided which point to return qf.plot() part
I saw some post on stackoverflow says change iplot() to fig.show()
I want to change it to qf.show() but got another error qf don't have show()
what can I do now?
I notice this in recent github issue in cufflink respository.
Add cf.go_offline() help to solve the problem because now it have offline mode and online mode
I change it to
def ta_dashboard(asset,indicator,start_date,end_date,bb_k,bb_n,macd_fast,macd_slow,macd_signal,rsi_periods,rsi_upper,rsi_lower):
cf.go_offline()
df=yf.download(asset,start=start_date,end=end_date,progress=False,auto_adjust=True)
qf=cf.QuantFig(df,title=f'TA Dashboard={asset}',legend='right',name=f'{asset}')
if 'Bollinger Bands' in indicator:
qf.add_bollinger_bands(periods=bb_n,boll_std=bb_k)
if 'MACD' in indicator:
qf.add_macd(fast_period=macd_fast,slow_period=macd_slow,signal_period=macd_signal)
if 'RSI' in indicator:
qf.add_rsi(period=rsi_periods,rsi_upper=rsi_upper,rsi_lower=rsi_lower,showbands=True)
return qf.iplot()
and it works
From Cufflinks github issue: https://github.com/santosjorge/cufflinks/issues/267
Related
I am not able to see my plot in my kaggle notebook.
The following is my code.
import ipywidgets as widgets
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
import seaborn as sns
df = pd.read_csv('../input/sdd1247/COMED_hourly.csv', parse_dates=["Datetime"], index_col="Datetime")
def Visualization_plots(x):
y = df.loc['2017']
y['COMED_MW'].resample('H').mean().plot()
button = widgets.Button(
description='Submit',
disabled=False,
button_style='success', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Click me',
icon='alicorn' # (FontAwesome names without the `fa-` prefix)
)
button.on_click(Visualization_plots)
button
The button shows up.. but when I click on it, I expected the graph to show below it. But nothing shows up.
I do however see the graph in the console. How can I show the graph in my notebook instead?
This is how it shows up in the console window:
I would add plt.show() on the line after the plot instruction.
If it doesn't work please provide a few lines of your dataset.
I have this code from this website:
https://geopandas.org/en/stable/docs/user_guide/interactive_mapping.html
import geopandas as gpd
import matplotlib.pyplot as plt
world_filepath = gpd.datasets.get_path('naturalearth_lowres')
world = gpd.read_file(world_filepath)
print(world.head())
world.explore(column='pop_est',cmap='Set2')
plt.show()
I try to run the code, the geodataframe data is printed but no plot is shown.
What am i missing?
Thnx in advanced.
world.explore(column='pop_est',cmap='Set2') should return and display a folium map object so you shouldn't need that plt.show() as the bottom.
Also, since you're using an IDE (not jupyter) we need to write the map to disk then open it up in the broswer.
Try
import geopandas as gpd
import webbrowser
import os
world_filepath = gpd.datasets.get_path('naturalearth_lowres')
world = gpd.read_file(world_filepath)
print(world.head())
# world.explore() returns a folium map
m = world.explore(column='pop_est',cmap='Set2')
# and then we write the map to disk
m.save('my_map.html')
# then open it
webbrowser.open('file://' + os.path.realpath('my_map.html'))
export() creates a folium object, not a matplotlib figure
in jupyter just have m as last item in code cell
in other environments you can save as HTML and launch into a browser
import geopandas as gpd
import webbrowser
from pathlib import Path
world_filepath = gpd.datasets.get_path('naturalearth_lowres')
world = gpd.read_file(world_filepath)
print(world.head())
m = world.explore(column='pop_est',cmap='Set2')
f = Path.cwd().joinpath("map.html")
m.save(str(f))
webbrowser.open("file://" + str(f))
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")
mum = sns.load_dataset("EE770_Mumbai")
sns.relplot(x="Hour of the year", y="RH(%)", data=mum)
seaborn.load_dataset is documented here: https://seaborn.pydata.org/generated/seaborn.load_dataset.html
In summary, it loads a sample dataset from here: https://github.com/mwaskom/seaborn-data.
The dataset "EE770_Mumbai" doesn't exist at that Github address.
You can run seaborn.get_dataset_names() to show the datasets currently available
I am trying to connect to FXCM throuhg an api and am constantly getting an error:
|ERROR|2020-01-11 20:42:41,825|Socket returns an error: ('Connection aborted.', OSError("(10054, 'WSAECONNRESET')")).
The code is :
import fxcmpy
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
from datetime import date
import numpy as np
TOKEN = "hidden"
con = fxcmpy.fxcmpy(access_token=TOKEN, log_level='error')
I have been using this for a while now but error suddenly showed up today. How can i fix this?
I got the same problem. I got an email from api#fxcm.com below.
"We did a release on demo on 1/12 to improve the Rest API.
With that said, Our REST API wrapper fxcmpy has been updated, you need to install the latest fxcmpy version at 1.2.6.
Here is the link where you can find the library: https://pypi.org/project/fxcmpy/#files
Please have in mind that just with pip install fxcmpy it might not work as it won’t update the library, please use below command."
pip install –U fxcmpy
I am trying to plot a candle stick chart in python. Here is my code
from pandas_datareader import data as pdr
import plotly.plotly as py
import plotly.graph_objs as go
import fix_yahoo_finance as yf
yf.pdr_override()
mcd = pdr.get_data_yahoo("MCD", start="2004-01-01", end="2005-07-31")
mcd_candle = go.Candlestick(x=mcd.index,open=mcd.Open,high=mcd.High,low=mcd.Low,close=mcd.Close)
data = [mcd_candle]
py.iplot(data, filename='Candle Stick')
This is the error I am getting
PlotlyError: Because you didn't supply a 'file_id' in the call, we're assuming you're trying to snag a figure from a url. You supplied the url, '', we expected it to start with 'https://plot.ly'.
Any idea how I can draw a Candle Stick Chart?
The problem must be because you didn't provide the username and api key which you will get from the https://plot.ly/settings/api link. If you want to use plotly online to create this graph. First create an account, then get the username and api key and insert it in the below code.
from pandas_datareader import data as pdr
import plotly.plotly as py
import plotly.graph_objs as go
import fix_yahoo_finance as yf
py.sign_in('<<username here>>', '<<api key here>>')
yf.pdr_override()
mcd = pdr.get_data_yahoo("MCD", start="2004-01-01", end="2005-07-31")
mcd_candle = go.Candlestick(x=mcd.index,open=mcd.Open,high=mcd.High,low=mcd.Low,close=mcd.Close)
data = [mcd_candle]
py.iplot(data, filename='Candle Stick')
There is another option of using plotly offline which does not need all this procedure, please find below the implementation code.
from pandas_datareader import data as pdr
import plotly.offline as py_offline
import plotly.graph_objs as go
import fix_yahoo_finance as yf
py_offline.init_notebook_mode()
yf.pdr_override()
mcd = pdr.get_data_yahoo("MCD", start="2004-01-01", end="2005-07-31")
mcd_candle = go.Candlestick(x=mcd.index,open=mcd.Open,high=mcd.High,low=mcd.Low,close=mcd.Close)
data = [mcd_candle]
py_offline.iplot(data, filename='Candle Stick')
#for Spyder plotting use the below line instead
#py_offline.plot(data, filename='Candle Stick')
Please make sure to install the libraries pandas_datareader and fix_yahoo_finance using pip, if these libraries are not already present!