Importing numpy
, pandas
and matplotlib
In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
Importing ofcandle
module from pyorderedfuzzy.ofcandles
In [2]:
from pyorderedfuzzy.ofcandles import ofcandle
Data loading
In [3]:
df = pd.read_csv('KGHM.prn')
print('rows:',len(df))
df.head()
Out[3]:
Transformation to the daily period
In [4]:
dic = {'Date': [], 'Time': [], 'Close': [], 'Volume': []}
df_days = df.groupby('Date')
print('days:',len(df_days))
for day, df_day in df_days:
dic['Date'].append(day)
dic['Time'].append(np.array(df_day['Time']))
dic['Close'].append(np.array(df_day['Close']))
dic['Volume'].append(np.array(df_day['Volume']))
df_days = pd.DataFrame(dic)
print(len(df_days))
df_days.head()
Out[4]:
Preparing data for ofcandle
In [5]:
day = 20001120
idx = pd.Index(df_days['Date']).get_loc(day)
data = df_days.iloc[idx]
price = data['Close']
volume = data['Volume']
spread = np.zeros(len(price))
ctype = 'linear' # candle type: 'linear', 'gauss', 'empirical'
wtype = 'volume' # weight type: 'clear', 'volume', 'lt', 'et', or combination e.g. ['volume', 'et']
param_s = 'mix_average' # params S1, S2 type: 'average', 'lw_average', 'ew_average', 'mix_average'
param_c = 'none' # params C1, C2 type: 'none', 'std', 'volatility'
dim = 11 # int
can_data = {'price': price, 'volume': volume, 'spread': spread}
can_params = {'wtype': wtype, 'param_s': param_s, 'param_c': param_c, 'dim': dim}
print('candle data:', can_data)
print('candle type:', ctype)
print('candle params:', can_params)
Trapezoidal OFCandle
In [6]:
can_trap = ofcandle.OFCandle(ctype, can_data, can_params)
print('param_s:', can_trap.param_s)
print('param_c:', can_trap.param_c)
print('param_ab:', can_trap.param_ab)
# plot ofcandle with data
can_trap.plot_candle()
Gaussian OFCandle
In [7]:
ctype = 'gauss'
can_gauss = ofcandle.OFCandle(ctype, can_data, can_params)
# plot ofcandle with data
can_gauss.plot_candle()
Empirical OFCandle
In [8]:
ctype = 'empirical'
can_emp = ofcandle.OFCandle(ctype, can_data, can_params)
# plot ofcandle with data
can_emp.plot_candle()
OFCandle to OFNumber
In [9]:
ofn_gauss = can_gauss.to_ofn()
print('can_gauss:', type(can_gauss))
print('ofn_gauss:', type(ofn_gauss))
# plot
options_f = {'c':'r', 'ls': '-'}
options_g = {'c':'b', 'ls': '-.'}
fig = plt.figure('OFNumber', figsize=(8,4))
ax1 = plt.subplot2grid((1, 2), (0, 0))
ax2 = plt.subplot2grid((1, 2), (0, 1))
can_gauss.plot_ofn(ax1, kwargs_f=options_f, kwargs_g=options_g)
ax1.set_title('can_gauss.plot_ofn()')
ofn_gauss.plot_ofn(ax2, kwargs_f=options_f, kwargs_g=options_g)
ax2.set_title('ofn_gauss.plot_ofn()')
plt.show()
In [ ]: