forked from china-quant/trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stan.rb
88 lines (80 loc) · 1.53 KB
/
stan.rb
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
def to_bool(str)
str == "true"
end
data = IO.read("dia.csv").split(',') # reads in the data, minus header line
index = 0
new_data = []
open = 0
data.each do |item|
case index
when 1
open = item.to_f
when 4
if item.to_f >= open
new_data.push(true)
else
new_data.push(false)
end
else
#do nothing
end
index +=1
index = 0 if index == 6
end
new_data = new_data.reverse
min_supp = 3
freq = []
occurence_count = []
potential = []
#detect all frequen 5-day patterns
i = 0
while (i+5) < new_data.length
pattern = [
new_data[i],
new_data[i+1],
new_data[i+2],
new_data[i+3],
new_data[i+4]
]
if freq.include? pattern
j = freq.rindex(pattern)
occurence_count[j] = occurence_count[j] + 1
elsif potential.include? pattern
freq.push pattern
occurence_count.push 1
else
potential.push pattern
end
i+=1
end
nf = []
oc = []
occurence_count.each.with_index(1) do |item, ind|
if item >= min_supp
nf.push freq[ind]
oc.push item
end
end
# show the frequent patterns
puts "Frequent weeks are:"
nf.each do |item|
puts "#{item[0]},#{item[1]},#{item[2]},#{item[3]},#{item[4]}: #{oc[nf.rindex(item)]}"
end
if ARGV.length > 0
possible = []
nf.each do |item|
matches = true
ARGV.each do |arg|
bool = (arg == "true")
if bool != item[ARGV.rindex(arg)]
matches = false
end
end
if matches
possible.push item
end
end
possible.each do |item|
puts "#{item[0]},#{item[1]},#{item[2]},#{item[3]},#{item[4]}"
end
end