-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion_ohlc_hikenashi.py
More file actions
74 lines (63 loc) · 2.26 KB
/
Copy pathconversion_ohlc_hikenashi.py
File metadata and controls
74 lines (63 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pandas as pd
import numpy as np
import time
import math
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from mplfinance.original_flavor import candlestick_ohlc
new_df = pd.read_csv("data.csv")
new_df = new_df[['date','open','high','low','close','volume']]
new_df.head()
# date open high low close volume
# 0 2018-04-12 09:15:00+05:30 295.00 295.75 293.25 293.80 55378
# 1 2018-04-12 09:20:00+05:30 293.75 293.75 292.55 292.95 32219
# 2 2018-04-12 09:25:00+05:30 292.95 293.40 292.65 292.80 23643
# 3 2018-04-12 09:30:00+05:30 292.80 293.00 292.75 292.80 12313
# 4 2018-04-12 09:35:00+05:30 292.75 292.85 291.50 291.55 32198
def HA(df, ohlc=['open', 'high', 'low', 'close']):
ha_open = 'HA_' + ohlc[0]
ha_high = 'HA_' + ohlc[1]
ha_low = 'HA_' + ohlc[2]
ha_close = 'HA_' + ohlc[3]
df[ha_open] = 0.0000
df[ha_high] = 0.0000
df[ha_low] = 0.0000
df[ha_close] = 0.0000
df[ha_close] = (df[ohlc[0]] + df[ohlc[1]] + df[ohlc[2]] + df[ohlc[3]]) / 4
df[ha_open] = 0.00
for i in range(0, len(df)):
if i == 0:
df[ha_open].iat[i] = (df[ohlc[0]].iat[i] + df[ohlc[3]].iat[i]) / 2
else:
df[ha_open].iat[i] = (df[ha_open].iat[i - 1] + df[ha_close].iat[i - 1]) / 2
df[ha_high]=df[[ha_open, ha_close, ohlc[1]]].max(axis=1)
df[ha_low]=df[[ha_open, ha_close, ohlc[2]]].min(axis=1)
del df['open']
del df['high']
del df['low']
del df['close']
df['open'] = df['HA_open']
df['high'] = df['HA_high']
df['low'] = df['HA_low']
df['close'] = df['HA_close']
df = df[['date','open','high','low','close','volume']]
df = df.round(1)
return df
z=HA(new_df)
z.head()
# Convert date to datetime and then to float for plotting
new_df['date'] = pd.to_datetime(new_df['date'])
new_df['date'] = new_df['date'].apply(mdates.date2num)
# Create a new DataFrame for plotting
plot_df = new_df[['date', 'open', 'high', 'low', 'close']]
# Plotting
fig, ax = plt.subplots()
candlestick_ohlc(ax, plot_df.values, width=0.01, colorup='g', colordown='r')
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
plt.xticks(rotation=45)
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('OHLC Chart')
plt.grid(True)
plt.show()