-
Notifications
You must be signed in to change notification settings - Fork 0
/
toplist_classes.rb
executable file
·150 lines (119 loc) · 3.57 KB
/
toplist_classes.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
module Toplist_Classes
# Classes that implement the data structures for a daytrading alerts application which looks for trends and
# 'Velocity;' the rate that money is pouring into or out of a security. It's from 2000.
# The most interesting things to me, upon reexamination, are the definition of a to_s function for a class
# and the overriding of << to allow appending an object of one type to an object of another. This made
# the main line code more readable.
class Quote
attr_reader :symbol, :volume, :price, :time, :open_orders, :net_change, :percent_change, :rank
def initialize(symbol, volume, price, time, open_orders, net_change, percent_change, rank)
@symbol = symbol
@volume = volume
@price = price
@time = time
@open_orders = open_orders
@net_change = net_change
@percent_change = percent_change
@rank = rank
end
def to_s
"Volume: #{@volume}, Price: #{@price}, Time: #{@time}, Percent #{@percent_change}"
end # def to_s
end # class Quote
class Ticker
include Enumerable
attr_reader :velocity, :quotes, :symb, :up, :down, :rank
def initialize(symb)
@velocity = 0
@symb = symb
@quotes = []
@up = 0
@down = 0
@rank = 0
self
end
def << (theQuote)
if @quotes.size != 0 # if this is not the first entry
if @quotes.last.time != theQuote.time # if the timestamp is new
delta_minutes = (theQuote.time - self.quotes.first.time) / 60
delta_dollars = ((theQuote.price - self.quotes.first.price)*100).floor.to_f/100
@velocity = (delta_dollars / delta_minutes)
end # new timestamp
@up += 1 if @quotes.last.rank > theQuote.rank
@down += 1 if @quotes.last.rank < theQuote.rank
end # this is not the first entry
@quotes << theQuote
@rank = theQuote.rank
end # <<
def lister # Just for debugging
puts self
self.quotes.each { |aQuote| puts aQuote.to_s }
end
def to_s
"#{symb}" # just return the symbol
end # to_s
def size
@quotes.length # Our size is our number
end
end # Class Ticker
class TickerList
attr_reader :symbol_list
def initialize(file_name)
@file_name = file_name
@symbol_list = []
self
end
def addQuote(theQuote) # append quote to old or new symbol object
@symbol_list.each do |this|
if this.symb == theQuote.symbol
this << theQuote
return
end
end
puts "New: #{theQuote.symbol}"
@symbol_list << Ticker.new(theQuote.symbol)
@symbol_list.last << theQuote
end
def save
File.open(@file_name, "w+") { |f| Marshal.dump(@symbol_list,f) }
end
def restore
# begin
@symbol_list = []
File.open(@file_name) do |f|
@symbol_list = Marshal.load(f)
end
rescue SystemCallError
puts "No database found, initializing..."
end # TickerList.restore
def lister
@symbol_list.each { |theSymb| theSymb.lister }
end
def symbols
temp_array = []
@symbol_list.each { |theSymb| temp_array << theSymb }
temp_array # return the array
end
def each
@symbol_list.each {|x| yield x} # This should yield each object
end
def size
@symbol_list.length
end
end # class TickerList
# a Trend is an unbroken sequence of motion in a direction.
class Trend
attr_reader :symbol, :value, :direction, :delta_time, :delta_price
def initialize(symbol, delta_time, delta_price)
@symbol = symbol
@value = value
@direction = direction # +1 or -1, only
@delta_price = delta_price
@delta_time = delta_time
@quotes = []
end
def continue(theQuote)
#if theQuote.price - @quotes.last.price <= 0
end
end # class Trend
end # Module Toplist_Classes