Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,564 changes: 1,564 additions & 0 deletions examples/data/SP500_NOV2019_IDayRVol.csv

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions examples/original_flavor/finance_demo_newapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import os.path

import mplfinance as mpf

date1 = "2004-2-1"
date2 = "2004-4-12"

infile = os.path.join('data','yahoofinance-INTC-19950101-20040412.csv')
quotes = pd.read_csv(infile, index_col=0, parse_dates=True, infer_datetime_format=True)

# select desired range of dates
quotes = quotes[(quotes.index >= date1) & (quotes.index <= date2)]

mpf.plot(quotes,type='candle',style='checkers')
521 changes: 521 additions & 0 deletions examples/scratch_pad/Axes.scatter.ipynb

Large diffs are not rendered by default.

Binary file added examples/scratch_pad/animation/gc.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/scratch_pad/animation/gcmacd.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/scratch_pad/animation/gcmacd2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions examples/scratch_pad/animation/genvol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import random

def gen_vol( numpoints, totalvol ):
rlist = []
for jj in range(0,num):
r = 0
for kk in range(0,3):
r += random.random()
rlist.append(r/3.0)
rlist
s = sum(rlist)
vol = []
for r in rlist:
vol.append((r/s)*tot)



11/5/2019,3080.8,3083.95,3072.15,3074.62,585634570
5 11/6/2019,3075.1,3078.34,3065.89,3076.78,544288522
6 11/7/2019,3087.02,3097.77,3080.23,3085.18,566117910
7 11/8/2019,3081.25,3093.09,3073.58,3093.08,460757054
Binary file added examples/scratch_pad/animation/idaygcmacd.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions examples/scratch_pad/animation/mpf_anim_iday_gif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'''
This file contains a animation demo using mplfinance demonstating resampling
of the data and re-displaying the most recent, partially resampled, candle.

The idea for this example came from Issue #256
(https://github.com/matplotlib/mplfinance/issues/256)

The typical use-case is where the user has real-time data from an API,
perhaps to the second, or minute, but wants to aggregate that data to
15 minutes per canlde, or maybe 30 minutes per candle. At the same time,
during those 15 or 30 minutes, the user wants to see the most recent
candle changing and developing as real-time data continues to come in.

In the example presented in this file, the data is once per minute,
with an aggregation of 15 minutes per candle. But, for this *simulation*
we set the animation rate to 250ms, which means we are getting 1 minute's
worth of data from the API every 1/4 second. Thus, this simulation is
running 240 times faster than real-time.

In a real-life case, if we have data once per second, and want to aggregate
15 minutes per candle, we would set the animation interval to something
like 5000ms (once every 5 seconds) because a more frequent visualization
might be impractical to watch or to use for decision making.

PLEASE NOTE: In this example, we resample the *entire* data set with each
animation cycle. This is inefficient, but works fine for less than 5000
data points or so. For larger data sets it may be practical to cache
the resampled data up to the last "full" candle, and only resample the
data that contributes to the final candle (and append it to the cached
resampled data). If I have time, I will work up and example doing that.

NOTE: Presently mplfinance does not support "blitting" (blitting makes animation
more efficient). Nonetheless, the animation is efficient enough to update at least
once per second, and typically more frequently depending on the size of the plot.
'''
import pandas as pd
import mplfinance as mpf
import matplotlib.animation as animation

## Class to simulate getting more data from API:

class RealTimeAPI():
def __init__(self):
self.data_pointer = 0
self.data_frame = pd.read_csv('data/SP500_NOV2019_IDay.csv',index_col=0,parse_dates=True)
self.data_frame = self.data_frame.iloc[0:480,:]
self.df_len = len(self.data_frame)

def fetch_next(self):
r1 = self.data_pointer
self.data_pointer += 1
if self.data_pointer >= self.df_len:
return None
return self.data_frame.iloc[r1:self.data_pointer,:]

def initial_fetch(self):
if self.data_pointer > 0:
return
r1 = self.data_pointer
self.data_pointer += int(0.3*self.df_len)
return self.data_frame.iloc[r1:self.data_pointer,:]

rtapi = RealTimeAPI()

resample_map ={'Open' :'first',
'High' :'max' ,
'Low' :'min' ,
'Close':'last' }
resample_period = '15T'

df = rtapi.initial_fetch()
rs = df.resample(resample_period).agg(resample_map).dropna()

fig, axes = mpf.plot(rs,returnfig=True,figsize=(11,8),type='candle',title='\n\nGrowing Candle')
ax = axes[0]

def animate(ival):
global df
global rs
nxt = rtapi.fetch_next()
if nxt is None:
#print('no more data to plot')
#ani.event_source.interval *= 3
#if ani.event_source.interval > 12000:
exit()
#return
df = df.append(nxt)
rs = df.resample(resample_period).agg(resample_map).dropna()
ax.clear()
mpf.plot(rs,ax=ax,type='candle')

ani = animation.FuncAnimation(fig, animate, interval=250)

print('about to save')
ani.save('gc.gif', writer='imagemagick',fps=4)
print('DONE saving')

mpf.show()
127 changes: 127 additions & 0 deletions examples/scratch_pad/animation/mpf_anim_idaymacd_gif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
'''
This file contains a animation demo using mplfinance "external axes mode",
in which animate both the display of candlesticks as well as the display
of MACD (Moving Average Convergence Divergence) visual analysis.

In this example, instead of creating the Figure and Axes external to mplfiance,
we allow mplfinance to create the Figure and Axes using its "panel method", and
set kwarg `returnfig=True` so that mplfinance will return the Figure and Axes.

We then take those Axes and pass them back into mplfinance ("external axes mode")
as part of the animation.

Note that presently mplfinance does not support "blitting" (blitting makes animation
more efficient). Nonetheless, the animation is efficient enough to update at least
once per second, and typically more frequently depending on the size of the plot.
'''
import pandas as pd
import mplfinance as mpf
import matplotlib.animation as animation

mpf.__version__

## Class to simulate getting more data from API:

class RealTimeAPI():
def __init__(self):
self.data_pointer = 0
self.data_frame = pd.read_csv('../../data/SP500_NOV2019_IDayRVol.csv',index_col=0,parse_dates=True)
#self.data_frame = self.data_frame.iloc[0:120,:]
self.df_len = len(self.data_frame)

def fetch_next(self):
r1 = self.data_pointer
self.data_pointer += 1
if self.data_pointer >= self.df_len:
return None
return self.data_frame.iloc[r1:self.data_pointer,:]

def initial_fetch(self,num):
if self.data_pointer > 0:
return
r1 = self.data_pointer
self.data_pointer += num
return self.data_frame.iloc[r1:self.data_pointer,:]

rtapi = RealTimeAPI()

# =======
# MACD:

df = rtapi.initial_fetch(450)

resample_map ={'Open' :'first',
'High' :'max' ,
'Low' :'min' ,
'Close' :'last' ,
'Volume':'sum' }

resample_period = '5T'

rs = df.resample(resample_period).agg(resample_map).dropna()

exp12 = rs['Close'].ewm(span=12, adjust=False).mean()
exp26 = rs['Close'].ewm(span=26, adjust=False).mean()
macd = exp12 - exp26
signal = macd.ewm(span=9, adjust=False).mean()
histogram = macd - signal

apds = [mpf.make_addplot(exp12,color='lime'),
mpf.make_addplot(exp26,color='c'),
mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,
color='dimgray',alpha=1,secondary_y=False),
mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=True),
mpf.make_addplot(signal,panel=1,color='b',secondary_y=True),
]

s = mpf.make_mpf_style(base_mpf_style='classic',rc={'figure.facecolor':'lightgray'})

fig, axes = mpf.plot(rs,type='candle',addplot=apds,figscale=1.5,figratio=(7,5),title='\n\nMACD',
style=s,volume=True,volume_panel=2,panel_ratios=(6,3,2),returnfig=True)

ax_main = axes[0]
ax_emav = ax_main
ax_hisg = axes[2]
ax_macd = axes[3]
ax_sign = ax_macd
ax_volu = axes[4]


def animate(ival):
global df, rs
nxt = rtapi.fetch_next()
if nxt is None:
print('no more data to plot')
exit()
df = df.append(nxt)
rs = df.resample(resample_period).agg(resample_map).dropna()
if len(rs) > 90:
rs = rs[-90:]
data = rs
exp12 = data['Close'].ewm(span=12, adjust=False).mean()
exp26 = data['Close'].ewm(span=26, adjust=False).mean()
macd = exp12 - exp26
signal = macd.ewm(span=9, adjust=False).mean()
histogram = macd - signal
apds = [mpf.make_addplot(exp12,color='lime',ax=ax_emav),
mpf.make_addplot(exp26,color='c',ax=ax_emav),
mpf.make_addplot(histogram,type='bar',width=0.7,
color='dimgray',alpha=1,ax=ax_hisg),
mpf.make_addplot(macd,color='fuchsia',ax=ax_macd),
mpf.make_addplot(signal,color='b',ax=ax_sign),
]

for ax in axes:
ax.clear()
mpf.plot(data,type='candle',addplot=apds,ax=ax_main,volume=ax_volu)

ani = animation.FuncAnimation(fig,animate,interval=50)

## print('about to save')
## ani.save('idaygcmacd.gif', writer='imagemagick',fps=5)
## print('DONE saving')

import matplotlib.pyplot as plt
print('Backend: ',plt.get_backend())

mpf.show()
36 changes: 36 additions & 0 deletions examples/scratch_pad/animation/mpf_animation_demo1mod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'''
This file contains a simple animation demo using mplfinance "external axes mode".

Note that presently mplfinance does not support "blitting" (blitting makes animation
more efficient). Nonetheless, the animation is efficient enough to update at least
once per second, and typically more frequently depending on the size of the plot.
'''
import pandas as pd
import mplfinance as mpf
import matplotlib.animation as animation

idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
idf.shape
idf.head(3)
idf.tail(3)
df = idf.loc['2011-07-01':'2011-12-30',:]

fig = mpf.figure(style='charles',figsize=(7,8))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(3,1,3)

def animate(ival):
if (22+ival) > len(df):
print('no more data to plot')
ani.event_source.interval *= 3
if ani.event_source.interval > 12000:
exit()
return
data = df.iloc[0+ival:(22+ival)]
ax1.clear()
ax2.clear()
mpf.plot(data,ax=ax1,volume=ax2,type='ohlc')

ani = animation.FuncAnimation(fig, animate, interval=250)

mpf.show()
45 changes: 45 additions & 0 deletions examples/scratch_pad/animation/mpf_animation_demo2mod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'''
This file contains a simple animation demo using mplfinance "external axes mode".

In this example, instead of creating the Figure and Axes external to mplfiance,
we allow mplfinance to create the Figure and Axes using its "panel method", and
set kwarg `returnfig=True` so that mplfinance will return the Figure and Axes.

We then take those Axes and pass them back into mplfinance ("external axes mode")
as part of the animation.

Note that presently mplfinance does not support "blitting" (blitting makes animation
more efficient). Nonetheless, the animation is efficient enough to update at least
once per second, and typically more frequently depending on the size of the plot.
'''
import pandas as pd
import mplfinance as mpf
import matplotlib.animation as animation

idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)

df = idf.loc['2011-07-01':'2011-12-30',:]

pkwargs=dict(type='candle',mav=(7,12))

fig, axes = mpf.plot(df.iloc[0:22],returnfig=True,volume=True,
figsize=(11,8),panel_ratios=(2,1),
title='\n\nS&P 500 ETF',**pkwargs)
ax1 = axes[0]
ax2 = axes[2]

def animate(ival):
if (20+ival) > len(df):
print('no more data to plot')
ani.event_source.interval *= 3
if ani.event_source.interval > 12000:
exit()
return
data = df.iloc[0+ival:(22+ival)]
ax1.clear()
ax2.clear()
mpf.plot(data,ax=ax1,volume=ax2,**pkwargs)

ani = animation.FuncAnimation(fig, animate, interval=200)

mpf.show()
Loading