Update CoreTraidMath.py
This commit is contained in:
parent
1d9729acbc
commit
6dfe72a3d9
@ -12,129 +12,110 @@ import statistics
|
|||||||
|
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import matplotlib.dates as mdates
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
import mplfinance as mpf
|
|
||||||
|
|
||||||
import plotly
|
|
||||||
#import plotly.plotly as py
|
|
||||||
import plotly.graph_objs as go
|
|
||||||
# these two lines allow your code to show up in a notebook
|
|
||||||
from plotly.offline import init_notebook_mode, iplot
|
|
||||||
from plotly.subplots import make_subplots
|
|
||||||
init_notebook_mode()
|
|
||||||
|
|
||||||
import CoreTraidMath
|
|
||||||
import plotly.express as px
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class agrigateFig():
|
|
||||||
|
|
||||||
def __init__(self,data=[],needDraw=False ,subplot_titles=None):
|
|
||||||
self.data=data
|
|
||||||
self.ans=self.getAgrPlt()
|
|
||||||
if needDraw:
|
|
||||||
self.subplot_titles=subplot_titles
|
|
||||||
self.fig=coreDraw(self.ans,True,self.subplot_titles)
|
|
||||||
|
|
||||||
|
|
||||||
def getAgrPlt(self):
|
|
||||||
count=0
|
|
||||||
ans=[]
|
|
||||||
for i in self.data:
|
|
||||||
count=count+1
|
|
||||||
if type(i)==list:
|
|
||||||
for g in i:
|
|
||||||
for j in g.figDict:
|
|
||||||
ans.append(j)
|
|
||||||
ans[-1]['row']=count
|
|
||||||
else:
|
|
||||||
for j in i.figDict:
|
|
||||||
ans.append(j)
|
|
||||||
ans[-1]['row']=count
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
class corePlt():
|
|
||||||
def __init__(self, params={
|
|
||||||
'vtype':'',
|
|
||||||
'df':pd.DataFrame(),
|
|
||||||
'row':1,
|
|
||||||
'col':1,
|
|
||||||
'name':''
|
|
||||||
}):
|
|
||||||
self.vtype=params['vtype']
|
|
||||||
self.df=params['df']
|
|
||||||
self.row=params['row']
|
|
||||||
self.col=params['col']
|
|
||||||
self.name=params['name']
|
|
||||||
if 'colorType' in params.keys():
|
|
||||||
self.colorType=params['colorType']
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class coreDraw():
|
class CoreMath:
|
||||||
def __init__(self, data=[],needShow=False,subplot_titles={}):
|
|
||||||
self.data=self.getPlts(data)
|
def __init__(self, base_df, params={
|
||||||
self.needShow=needShow
|
'dataType':'ohcl',
|
||||||
self.subplot_titles=subplot_titles
|
'action': None,
|
||||||
|
'actionOptions':{}
|
||||||
|
}
|
||||||
|
):
|
||||||
|
|
||||||
|
self.base_df=base_df.reset_index(drop=True)
|
||||||
|
self.params=params
|
||||||
|
if self.params['dataType']=='ohcl':
|
||||||
|
self.col=self.base_df[self.params['actionOptions']['valueType']]
|
||||||
|
elif self.params['dataType']=='series':
|
||||||
|
self.col=self.base_df
|
||||||
self.ans=self.getAns()
|
self.ans=self.getAns()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getBarColorList(self,l,colorType):
|
|
||||||
if colorType=='diffAbs':
|
|
||||||
ans=['green']
|
|
||||||
for i in range(1,len(l)):
|
|
||||||
if abs(l[i])>abs(l[i-1]):
|
|
||||||
ans.append('green')
|
|
||||||
else:
|
|
||||||
ans.append('red')
|
|
||||||
elif colorType=='diff':
|
|
||||||
ans=['green']
|
|
||||||
for i in range(1,len(l)):
|
|
||||||
if (l[i])>(l[i-1]):
|
|
||||||
ans.append('green')
|
|
||||||
else:
|
|
||||||
ans.append('red')
|
|
||||||
elif colorType=='normal':
|
|
||||||
ans=[]
|
|
||||||
for i in range(len(l)):
|
|
||||||
ans.append('gray')
|
|
||||||
return ans
|
|
||||||
|
|
||||||
def getPlts(self, data):
|
|
||||||
ans=None
|
|
||||||
|
|
||||||
if type(data)==list:
|
|
||||||
ans=[]
|
|
||||||
for i in data:
|
|
||||||
ans.append(corePlt(i))
|
|
||||||
else:
|
|
||||||
ans=[corePlt(data)]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return ans
|
|
||||||
|
|
||||||
def getAns(self):
|
def getAns(self):
|
||||||
|
ans=None
|
||||||
|
|
||||||
|
if self.params['action']=='findExt':
|
||||||
|
ans = self.getExtremumValue()
|
||||||
|
elif self.params['action']=='findMean':
|
||||||
|
ans = self.getMeanValue()
|
||||||
|
elif self.params['action']=='findSTD':
|
||||||
|
ans=self.getSTD()
|
||||||
|
|
||||||
|
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
def getExtremumValue(self):
|
||||||
|
ans=None
|
||||||
'''
|
'''
|
||||||
data list
|
actionOptions:
|
||||||
vtype
|
'extremumtype':
|
||||||
df
|
'min'
|
||||||
row=1
|
'max'
|
||||||
col=1
|
'valueType':
|
||||||
name
|
'open'
|
||||||
|
'close'
|
||||||
|
'high'
|
||||||
|
'low'
|
||||||
|
'''
|
||||||
|
if self.params['actionOptions']['extremumtype']=='max':
|
||||||
|
ans=max(self.col)
|
||||||
|
|
||||||
|
if self.params['actionOptions']['extremumtype']=='min':
|
||||||
|
ans=min(self.col)
|
||||||
|
|
||||||
|
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
def getMeanValue(self):
|
||||||
|
'''
|
||||||
|
actionOptions:
|
||||||
|
'MeanType':
|
||||||
|
'MA'
|
||||||
|
'SMA'
|
||||||
|
'EMA'
|
||||||
|
'WMA'
|
||||||
|
--'SMMA'
|
||||||
|
'valueType':
|
||||||
|
'open'
|
||||||
|
'close'
|
||||||
|
'high'
|
||||||
|
'low'
|
||||||
|
'window'
|
||||||
|
'span'
|
||||||
|
'weights'
|
||||||
|
'''
|
||||||
|
ans=None
|
||||||
|
if self.params['actionOptions']['MeanType']=='MA':
|
||||||
|
ans = self.col.mean()
|
||||||
|
if self.params['actionOptions']['MeanType']=='SMA':
|
||||||
|
ans=np.convolve(self.col, np.ones(self.params['actionOptions']['window']), 'valid') / self.params['actionOptions']['window']
|
||||||
|
#ans=self.col.rolling(window=self.params['actionOptions']['window']).mean().to_list()
|
||||||
|
|
||||||
|
if self.params['actionOptions']['MeanType']=='EMA':
|
||||||
|
ans=self.col.ewm(span=self.params['actionOptions']['span'], adjust=False).mean().to_list()
|
||||||
|
if self.params['actionOptions']['MeanType']=='WMA':
|
||||||
|
try:
|
||||||
|
weights=self.params['actionOptions']['weights']
|
||||||
|
except KeyError:
|
||||||
|
weights=np.arange(1,self.params['actionOptions']['window']+1)
|
||||||
|
ans=self.col.rolling(window=self.params['actionOptions']['window']).apply(lambda x: np.sum(weights*x) / weights.sum(), raw=False).to_list()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return(ans)
|
||||||
|
|
||||||
|
def getSTD(self):
|
||||||
|
'''
|
||||||
|
actionOptions:
|
||||||
|
window
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -143,58 +124,15 @@ class coreDraw():
|
|||||||
ans=None
|
ans=None
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
window=self.params['actionOptions']['window']
|
||||||
|
ans=np.asarray([])
|
||||||
|
for i in range(len(self.col)-window+1):
|
||||||
|
ans=np.append(ans,np.std(self.col[i:i+window], ddof=1))
|
||||||
|
|
||||||
|
except:
|
||||||
|
#window = len(self.col)
|
||||||
|
ans=np.std(self.col, ddof=1)
|
||||||
|
|
||||||
maxRow=1
|
|
||||||
maxCol=1
|
|
||||||
for i in self.data:
|
|
||||||
if i.row > maxRow:
|
|
||||||
maxRow =i.row
|
|
||||||
if i.col > maxCol:
|
|
||||||
maxCol =i.col
|
|
||||||
|
|
||||||
fig = make_subplots(
|
|
||||||
rows=maxRow,
|
|
||||||
cols=maxCol,
|
|
||||||
shared_xaxes=True,
|
|
||||||
vertical_spacing=0.1,
|
|
||||||
shared_yaxes=True,
|
|
||||||
#horizontal_spacing=0.02,
|
|
||||||
#column_widths=[]
|
|
||||||
subplot_titles=self.subplot_titles
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
fig.update_layout(xaxis_rangeslider_visible=False)
|
|
||||||
fig.update_layout(barmode='relative')
|
|
||||||
|
|
||||||
for i in self.data:
|
|
||||||
if i.vtype=='Scatter':
|
|
||||||
fig.add_trace(go.Scatter(x=i.df['date'],y=i.df['value'],name=i.name), row=i.row, col=i.col)
|
|
||||||
elif i.vtype=='OCHL':
|
|
||||||
fig.add_trace(go.Candlestick(
|
|
||||||
x=i.df['date'],
|
|
||||||
open=i.df['open'],
|
|
||||||
high=i.df['high'],
|
|
||||||
low=i.df['low'],
|
|
||||||
close=i.df['close'],
|
|
||||||
name=i.name),
|
|
||||||
row=i.row, col=i.col
|
|
||||||
)
|
|
||||||
elif i.vtype=='Bars':
|
|
||||||
for j in i.df.keys():
|
|
||||||
if j!='date':
|
|
||||||
try:
|
|
||||||
colorType=i.colorType
|
|
||||||
except:
|
|
||||||
colorType='normal'
|
|
||||||
colors=self.getBarColorList(i.df[j],colorType)
|
|
||||||
fig.add_trace(go.Bar(x=i.df['date'], y=i.df[j],name=j,marker_color=colors),row=i.row, col=i.col)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ans=fig
|
|
||||||
if self.needShow:
|
|
||||||
plotly.offline.iplot(fig)
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user