-
Notifications
You must be signed in to change notification settings - Fork 19
/
count_trade_trend.py
88 lines (57 loc) · 1.53 KB
/
count_trade_trend.py
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
from sklearn import preprocessing
import matplotlib.pyplot as plt
data = pd.read_csv('2330.TW_deal_sim.csv')
price = data['Close']
x_p = np.array(price)#:243
x_price = x_p[0:243]
i = []
for x in range(1,267):
i.append(x)
x_range = np.array(i)
x_count = np.array([1,267])
x_train_time = np.array([220.5,235.5])
x_for_slope = np.polyfit(x_count, x_train_time, 1)
slope = np.poly1d(x_for_slope)
# slope_plot = slope(x_count)
slope_price = slope(x_range)
slope_price
loss = x_p-slope_price
np.mean(loss)
np.mean(loss)
#===================計算漲跌==============================
h = []
n = 0
for t in range(len(x_p)):
if x_p[n+1]> x_p[n]:
h.append(1) #漲
elif x_p[n+1]== x_p[n]:
h.append(0) #不漲也不跌
else:
h.append(2) #跌
n+=1
len(h)
data_trend = pd.DataFrame(h)
data_trend.to_csv('2330trend.csv')
#===================計算交易點 ==============================
p= []
for t in range(len(x_p)):
if t ==0 or t==265:
p.append(0)
elif x_p[t]<x_p[t+1] and sum(p[0:t])==0:
p.append(1)
elif sum(p[0:t])==1:
p.append(-1)
else:
p.append(0)
for n,g in enumerate(p):
if g==-1:
p[n]=2
##0持平 1買 2賣
data_trade = pd.DataFrame(p)
data_trade.to_csv('2330trade.csv')
plt.plot(x_range,x_p)
plt.plot(x_count,slope(x_count), color='g')
# plt.scatter(x_range, q ,color = 'r')
plt.show()