Add files via upload
This commit is contained in:
124
Core/CoreTraidMath.py
Normal file
124
Core/CoreTraidMath.py
Normal file
@@ -0,0 +1,124 @@
|
||||
|
||||
import pandas as pd
|
||||
import datetime
|
||||
import numpy as np
|
||||
import plotly as pl
|
||||
import plotly.graph_objs as go
|
||||
import matplotlib.pyplot as plt
|
||||
import math
|
||||
import scipy
|
||||
import random
|
||||
import statistics
|
||||
|
||||
|
||||
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()
|
||||
|
||||
class CoreMath:
|
||||
|
||||
def __init__(self, base_df, params={
|
||||
'dataType':'ohcl',
|
||||
'colName':{
|
||||
'open':'open',
|
||||
'close':'close',
|
||||
'high':'high',
|
||||
'low':'low',
|
||||
'date':'date'
|
||||
},
|
||||
'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['colName'][self.params['actionOptions']['valueType']]]
|
||||
elif self.params['dataType']=='series':
|
||||
self.col=self.base_df
|
||||
self.ans=self.getAns()
|
||||
|
||||
|
||||
def getAns(self):
|
||||
ans=None
|
||||
|
||||
if self.params['action']=='findExt':
|
||||
ans = self.getExtremumValue()
|
||||
if self.params['action']=='findMean':
|
||||
ans = self.getMeanValue()
|
||||
|
||||
|
||||
return ans
|
||||
|
||||
|
||||
def getExtremumValue(self):
|
||||
ans=None
|
||||
'''
|
||||
actionOptions:
|
||||
'extremumtype':
|
||||
'min'
|
||||
'max'
|
||||
'valueType':
|
||||
'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()
|
||||
if self.params['actionOptions']['MeanType']=='EMA':
|
||||
ans=self.col.ewm(span=elf.params['actionOptions']['span'], adjust=False).mean()
|
||||
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)
|
||||
|
||||
|
||||
|
||||
return(ans)
|
||||
|
||||
Reference in New Issue
Block a user