-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeframechange.py
More file actions
88 lines (69 loc) · 2.88 KB
/
timeframechange.py
File metadata and controls
88 lines (69 loc) · 2.88 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import pandas as pd
import numpy as np
import time
import math
new_df = pd.read_csv("data.csv")
def minute_conversion(timeframe, data):
i = 0
value1, value1_index, value2, value2_index = 0, 0, 0, 0
count = 0
new_list = []
df = data
day_change_index = []
for i, x in enumerate(data.date):
if x[11:] == "00:05:00.000Z":
day_change_index.append(i)
for j in day_change_index:
d = math.ceil((j - i) / timeframe)
count = 0
for k in range(i, j + 1, timeframe):
count += 1
if count != d:
m = df.loc[k, "date"]
n = df.loc[k + (timeframe) - 1, "date"]
value1_index = df.index[df['date'] == m].tolist()[0]
value2_index = df.index[df['date'] == n].tolist()[0]
new_list.append([n, df.loc[value1_index, "open"], max(df.loc[value1_index:value2_index, "high"]),
min(df.loc[value1_index:value2_index, "low"]), df.loc[value2_index, "close"],
sum(df.loc[value1_index:value2_index, "volume"])])
else:
m = df.loc[k, "date"]
n = df.loc[j - 1, "date"]
value1_index = df.index[df['date'] == m].tolist()[0]
value2_index = df.index[df['date'] == n].tolist()[0]
new_list.append([n, df.loc[value1_index, "open"], max(df.loc[value1_index:value2_index, "high"]),
min(df.loc[value1_index:value2_index, "low"]), df.loc[value2_index, "close"],
sum(df.loc[value1_index:value2_index, "volume"])])
i = j
new_data = pd.DataFrame(new_list, columns=["date", "open", "high", "low", "close", "volume"])
return new_data
# Example usage:
a = minute_conversion(72, new_df)
a = a.drop_duplicates()
a = a.reset_index(drop=True)
# b = HA(new_df)
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
# Assume 'a' is the DataFrame that has been returned from the minute_conversion function
# Convert 'date' column to datetime format if it's not already
a['date'] = pd.to_datetime(a['date'])
# Plotting
fig, ax = plt.subplots(figsize=(10, 6))
# Plot the open, high, low, and close as candlestick
ax.plot(a['date'], a['open'], label="Open", color='blue', lw=1)
ax.plot(a['date'], a['high'], label="High", color='green', lw=1)
ax.plot(a['date'], a['low'], label="Low", color='red', lw=1)
ax.plot(a['date'], a['close'], label="Close", color='black', lw=2)
# Formatting the x-axis (date)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
ax.xaxis.set_major_locator(mdates.HourLocator(interval=1))
plt.xticks(rotation=45)
plt.xlabel('Date')
plt.ylabel('Price')
# Adding title and legend
plt.title('Financial Data: Open, High, Low, Close')
plt.legend(loc='best')
# Show the plot
plt.tight_layout()
plt.show()