stock_price_imitation 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/bin/price-imi +3 -4
- data/lib/stock_price_imitation/generator.rb +38 -29
- data/lib/stock_price_imitation/version.rb +1 -1
- data/stock_price_imitation.gemspec +1 -0
- data/test/test_generator.rb +9 -25
- metadata +16 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcbefe942a877e0597a831fd5f1862c9a4bc84dd
|
4
|
+
data.tar.gz: 28261e06db49d73c4f84a55431c2d29b47d0beae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 683a913af8ceb7613f7b24e8709d8946d2c1356314fca686003b8d9c105322e9fa137d52a68519c190a40737bed718ce25c3e7a5510aba485e2b5a262b75b333
|
7
|
+
data.tar.gz: 9189b5b5c4502e516f4519ca3ce15a2730921eb0cc231b473b2bda539d89ecd6c6b23c6c4426141cb4f5858c87489b79725bd0b92e2a59e9abe8cab4dce492b7
|
data/README.md
CHANGED
data/bin/price-imi
CHANGED
@@ -4,14 +4,13 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
|
4
4
|
require 'stock_price_imitation'
|
5
5
|
|
6
6
|
step = ARGV.pop || 100
|
7
|
-
|
8
|
-
volatility = 0.2
|
7
|
+
first_seed_price = 1000
|
9
8
|
|
10
|
-
prices = StockPriceImitation::Generator.new(step,
|
9
|
+
prices = StockPriceImitation::Generator.new(step, first_seed_price).execute
|
11
10
|
|
12
11
|
header = prices.first.keys.join("\t")
|
13
12
|
puts header
|
14
13
|
|
15
14
|
prices.each do |price|
|
16
|
-
puts price.values.join("\t")
|
15
|
+
puts price.values.map{|p|p.round(2)}.join("\t")
|
17
16
|
end
|
@@ -1,49 +1,49 @@
|
|
1
|
+
require 'random_bell'
|
2
|
+
|
1
3
|
class StockPriceImitation::Generator
|
2
|
-
def initialize(step_count,
|
4
|
+
def initialize(step_count, first_seed_price=1000)
|
3
5
|
@step_count = step_count
|
4
|
-
@
|
5
|
-
@
|
6
|
+
@first_seed_price = first_seed_price
|
7
|
+
@bell = RandomBell.new(mu: 1.0, range: 0.0..2.0)
|
6
8
|
|
7
9
|
@steps = []
|
8
10
|
end
|
9
11
|
|
10
12
|
def execute
|
11
|
-
|
12
|
-
@steps.map{|s| to_hash(s) }
|
13
|
+
prices_by(seed_prices)
|
13
14
|
end
|
14
15
|
|
15
16
|
private
|
16
17
|
|
17
|
-
def
|
18
|
-
@
|
19
|
-
seed_price = rand seed_price_range(i)
|
20
|
-
@steps << Step.new(seed_price, @volatility)
|
21
|
-
end
|
22
|
-
end
|
18
|
+
def seed_prices
|
19
|
+
@seeds = []
|
23
20
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
21
|
+
@step_count.times.each do |i|
|
22
|
+
if i == 0
|
23
|
+
@seeds << @first_seed_price
|
24
|
+
else
|
25
|
+
previous = @seeds[i-1]
|
26
|
+
@seeds << previous * @bell.rand
|
27
|
+
end
|
31
28
|
end
|
29
|
+
|
30
|
+
@seeds
|
32
31
|
end
|
33
32
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
low_price: step.low_price,
|
39
|
-
high_price: step.high_price,
|
40
|
-
}
|
33
|
+
def prices_by(seed_prices)
|
34
|
+
seed_prices.map do |seed|
|
35
|
+
Step.new(seed, @volatility).to_hash
|
36
|
+
end
|
41
37
|
end
|
42
38
|
|
43
39
|
class Step
|
44
40
|
def initialize(seed_price, volatility)
|
45
|
-
|
46
|
-
|
41
|
+
bell = RandomBell.new
|
42
|
+
|
43
|
+
@seed_price = seed_price
|
44
|
+
|
45
|
+
prices = 2.times.map{ @seed_price * bell.rand }
|
46
|
+
@low_price, @high_price = prices.sort
|
47
47
|
end
|
48
48
|
|
49
49
|
def low_price
|
@@ -55,11 +55,20 @@ class StockPriceImitation::Generator
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def opening_price
|
58
|
-
rand low_price..high_price
|
58
|
+
(rand low_price..high_price)
|
59
59
|
end
|
60
60
|
|
61
61
|
def closing_price
|
62
|
-
rand low_price..high_price
|
62
|
+
(rand low_price..high_price)
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_hash
|
66
|
+
{
|
67
|
+
opening_price: self.opening_price,
|
68
|
+
closing_price: self.closing_price,
|
69
|
+
low_price: self.low_price,
|
70
|
+
high_price: self.high_price,
|
71
|
+
}
|
63
72
|
end
|
64
73
|
end
|
65
74
|
end
|
data/test/test_generator.rb
CHANGED
@@ -13,10 +13,9 @@ describe StockPriceImitation::Generator do
|
|
13
13
|
describe '#execute' do
|
14
14
|
before do
|
15
15
|
@step_count = 100
|
16
|
-
@
|
17
|
-
@volatility = 0.1
|
16
|
+
@first_price = 1000
|
18
17
|
|
19
|
-
@generator = StockPriceImitation::Generator.new(@step_count, @
|
18
|
+
@generator = StockPriceImitation::Generator.new(@step_count, @first_price)
|
20
19
|
@price_movement = @generator.execute
|
21
20
|
end
|
22
21
|
|
@@ -34,36 +33,21 @@ describe StockPriceImitation::Generator do
|
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
37
|
-
it 'high
|
36
|
+
it 'high is bigger than low' do
|
38
37
|
@price_movement.each do |step|
|
39
38
|
assert step[:high_price] >= step[:low_price]
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
43
|
-
it '
|
44
|
-
@price_movement.each do |
|
45
|
-
|
46
|
-
assert expect_price_range.include? step[:opening_price]
|
42
|
+
it 'open between high and low' do
|
43
|
+
@price_movement.each do |price|
|
44
|
+
assert (price[:low_price]..price[:high_price]).include? price[:opening_price]
|
47
45
|
end
|
48
46
|
end
|
49
47
|
|
50
|
-
it '
|
51
|
-
|
52
|
-
|
53
|
-
assert @first_price_range.include? first_step[:high_price]
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'step between (previous * volatility)' do
|
57
|
-
@price_movement.each.with_index do |step, i|
|
58
|
-
next if i == 0
|
59
|
-
|
60
|
-
previous_low_price = @price_movement[i - 1][:low_price]
|
61
|
-
current_low_price = step[:low_price]
|
62
|
-
|
63
|
-
min = previous_low_price - (previous_low_price * @volatility)
|
64
|
-
max = previous_low_price + (previous_low_price * @volatility)
|
65
|
-
|
66
|
-
assert (min..max).include? current_low_price
|
48
|
+
it 'close between high and low' do
|
49
|
+
@price_movement.each do |price|
|
50
|
+
assert (price[:low_price]..price[:high_price]).include? price[:closing_price]
|
67
51
|
end
|
68
52
|
end
|
69
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stock_price_imitation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shinya131
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: random_bell
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Stock price movement imitation generator. It is generate by random number.
|
56
70
|
email:
|
57
71
|
- nagai3mt5b@gmail.com
|
@@ -102,4 +116,3 @@ test_files:
|
|
102
116
|
- test/minitest_helper.rb
|
103
117
|
- test/test_generator.rb
|
104
118
|
- test/test_stock_price_imitation.rb
|
105
|
-
has_rdoc:
|