Issue
Using the following functions round_up
and round_down
I can return columns containing rounded output as follows:
Open High Low Close round_up round_down
date
2020-03-19 86.55 86.88 85.83 85.83 87.0 85.0
2020-03-20 85.94 86.30 85.20 85.23 87.0 85.0
2020-03-21 85.06 85.18 84.13 84.23 86.0 84.0
2020-03-22 84.37 84.72 83.62 84.72 85.0 83.0
2020-03-23 84.83 85.70 84.41 85.70 86.0 84.0
2020-03-24 85.87 86.79 85.58 86.73 87.0 85.0
2020-03-25 86.90 86.90 86.15 86.87 87.0 86.0
2020-03-26 87.18 88.92 87.15 88.65 89.0 87.0
2020-03-27 88.69 88.95 87.90 88.40 89.0 87.0
2020-03-28 88.31 88.50 87.69 87.92 89.0 87.0
2020-03-29 87.84 87.84 86.04 86.31 88.0 86.0
2020-03-30 86.27 86.38 85.57 86.30 87.0 85.0
2020-03-31 86.72 87.23 86.24 86.90 88.0 86.0
2020-04-01 86.83 87.48 86.65 86.65 88.0 86.0
2020-04-02 86.53 86.75 85.32 86.13 87.0 85.0
How can I modify these functions to return this desired output where round up
and round down
are to the nearest 5
:
Open High Low Close round_up round_down
date
2020-03-19 86.55 86.88 85.83 85.83 90.0 85.0
2020-03-20 85.94 86.30 85.20 85.23 90.0 85.0
2020-03-21 85.06 85.18 84.13 84.23 90.0 80.0
2020-03-22 84.37 84.72 83.62 84.72 85.0 80.0
Code to reproduce the data above
def genMockDataFrame(days,startPrice,colName,startDate,seed=None,round_to=4,scale=0.0018):
periods = days*24
np.random.seed(seed)
steps = np.random.normal(loc=0, scale=scale, size=periods)
steps[0]=0
P = startPrice+np.cumsum(steps)
P = [round(i,round_to) for i in P]
fxDF = pd.DataFrame({
'ticker':np.repeat( [colName], periods ),
'date':np.tile( pd.date_range(startDate, periods=periods, freq='H'), 1 ),
'price':(P)})
fxDF.index = pd.to_datetime(fxDF.date)
fxDF = fxDF.price.resample('D').ohlc()
fxDF.columns = [i.title() for i in fxDF.columns]
return fxDF
ticker = 'XXX_USD'
df = genMockDataFrame(15,86.55,ticker,'19/3/2020',seed=157,round_to=2,scale=0.18)
def round_up(x, decimals=0):
n = x.max()
multiplier = 10 ** decimals
return math.ceil(n * multiplier) / multiplier
def round_down(x, decimals=0):
n = x.min()
multiplier = 10 ** decimals
return math.floor(n * multiplier) / multiplier
df["round_up"] = df.apply(round_up,decimals=0,axis=1)
df["round_down"] = df.apply(round_down,decimals=0,axis=1)
Solution
Try:
def round_up(df, base=5):
n = df.max(axis=1)
return n + (base - n) % base
def round_down(df, base=5):
n = df.min(axis=1)
return n - (n % base)
cols = ['Open', 'High', 'Low', 'Close']
df['round_up'] = round_up(df[cols])
df['round_down'] = round_down(df[cols])
Output:
>>> df
Open High Low Close round_up round_down
date
2020-03-19 86.55 86.88 85.83 85.83 90.0 85.0
2020-03-20 85.94 86.30 85.20 85.23 90.0 85.0
2020-03-21 85.06 85.18 84.13 84.23 90.0 80.0
2020-03-22 84.37 84.72 83.62 84.72 85.0 80.0
2020-03-23 84.83 85.70 84.41 85.70 90.0 80.0
2020-03-24 85.87 86.79 85.58 86.73 90.0 85.0
2020-03-25 86.90 86.90 86.15 86.87 90.0 85.0
2020-03-26 87.18 88.92 87.15 88.65 90.0 85.0
2020-03-27 88.69 88.95 87.90 88.40 90.0 85.0
2020-03-28 88.31 88.50 87.69 87.92 90.0 85.0
2020-03-29 87.84 87.84 86.04 86.31 90.0 85.0
2020-03-30 86.27 86.38 85.57 86.30 90.0 85.0
2020-03-31 86.72 87.23 86.24 86.90 90.0 85.0
2020-04-01 86.83 87.48 86.65 86.65 90.0 85.0
2020-04-02 86.53 86.75 85.32 86.13 90.0 85.0
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.