candle_data 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ require_relative 'candle_data/candle'
2
+ require_relative 'candle_data/candle_builder'
@@ -0,0 +1,32 @@
1
+ module CandleData
2
+ class Candle
3
+ attr_reader :open_time, :open, :high, :low, :close, :volume
4
+
5
+ def initialize(open_time, open, high, low, close, volume)
6
+ @open_time = open_time
7
+ @open = open
8
+ @high = high
9
+ @low = low
10
+ @close = close
11
+ @volume = volume
12
+ end
13
+
14
+ def update(price, volume)
15
+ @close = price
16
+ @low = price if price < @low
17
+ @high = price if price > @high
18
+ @volume += volume
19
+ end
20
+
21
+ def to_h
22
+ {
23
+ open_time: open_time.iso8601,
24
+ open: open.to_s('F'),
25
+ high: high.to_s('F'),
26
+ low: low.to_s('F'),
27
+ close: close.to_s('F'),
28
+ volume: volume.to_s('F')
29
+ }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,58 @@
1
+ require_relative './candle'
2
+
3
+ module CandleData
4
+ class CandleBuilder
5
+ class CandleWidthNotSpecifiedError < StandardError; end
6
+
7
+ attr_reader :candles
8
+ def initialize(trades, options={})
9
+ @trades = trades
10
+ @candle_width = options.fetch(:candle_width) { raise CandleWidthNotSpecifiedError }
11
+ @candles = []
12
+ @last_time = nil
13
+ extract_candles
14
+ end
15
+
16
+ private
17
+ attr_reader :trades
18
+
19
+ def extract_candles
20
+ trades.each do |trade|
21
+ extract_candle(trade)
22
+ end
23
+ end
24
+
25
+ def extract_candle(trade)
26
+ if in_current_candle(trade)
27
+ add_to_current_candle(trade)
28
+ else
29
+ add_new_candle_for(trade)
30
+ end
31
+ end
32
+
33
+ def in_current_candle(trade)
34
+ return false unless last_candle
35
+ return trade[:time] < max_close_time
36
+ end
37
+
38
+ def max_close_time
39
+ last_candle.open_time + @candle_width
40
+ end
41
+
42
+ def add_new_candle_for(trade)
43
+ open_time = trade[:time]
44
+ open = close = high = low = trade[:price]
45
+ volume = trade[:amount]
46
+ candle = Candle.new(open_time, open, high, low, close, volume)
47
+ @candles << candle
48
+ end
49
+
50
+ def add_to_current_candle(trade)
51
+ last_candle.update(trade[:price], trade[:amount])
52
+ end
53
+
54
+ def last_candle
55
+ @candles.last
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module CandleData
2
+ VERSION = "0.0.3"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: candle_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-26 00:00:00.000000000 Z
12
+ date: 2013-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -33,7 +33,11 @@ email:
33
33
  executables: []
34
34
  extensions: []
35
35
  extra_rdoc_files: []
36
- files: []
36
+ files:
37
+ - lib/candle_data.rb
38
+ - lib/candle_data/candle.rb
39
+ - lib/candle_data/candle_builder.rb
40
+ - lib/candle_data/version.rb
37
41
  homepage: http://www.isotope11.com
38
42
  licenses: []
39
43
  post_install_message:
@@ -48,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
52
  version: '0'
49
53
  segments:
50
54
  - 0
51
- hash: -2765995280956628244
55
+ hash: 595672533271332823
52
56
  required_rubygems_version: !ruby/object:Gem::Requirement
53
57
  none: false
54
58
  requirements:
@@ -57,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
61
  version: '0'
58
62
  segments:
59
63
  - 0
60
- hash: -2765995280956628244
64
+ hash: 595672533271332823
61
65
  requirements: []
62
66
  rubyforge_project:
63
67
  rubygems_version: 1.8.24