quant 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.0.4 (2012-06-19)
2
+
3
+ * True Range and ATR added
4
+
1
5
  0.0.3 (2012-06-19)
2
6
 
3
7
  * Tests organized
data/lib/array.rb ADDED
@@ -0,0 +1,11 @@
1
+ class Array
2
+
3
+ def highs
4
+ inject([]) { |a, e| a << e[0] }
5
+ end
6
+
7
+ def lows
8
+ inject([]) { |a, e| a << e[1] }
9
+ end
10
+
11
+ end
data/lib/quant/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Quant
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/quant.rb CHANGED
@@ -30,6 +30,29 @@ module Quant
30
30
  dc
31
31
  end
32
32
 
33
+ def self.tr(ohlc)
34
+ tr = []
35
+ ohlc.each_with_index do |e, i|
36
+ tr << if i == 0
37
+ not_a_number
38
+ else
39
+ high = e[1]
40
+ low = e[2]
41
+ last_close = ohlc[i - 1][3]
42
+ [ high - low, (high - last_close).abs, (last_close - low).abs ].max
43
+ end
44
+ end
45
+ tr
46
+ end
47
+
48
+ def self.atr(ohlc, n)
49
+ tr = tr(ohlc)
50
+ tr.delete_if{ |e| e.nan? }
51
+ a = sma(tr, n)
52
+ 0.upto(ohlc.length - tr.length - 1) { a.unshift(not_a_number) } if ohlc.length > tr.length
53
+ a
54
+ end
55
+
33
56
  private
34
57
 
35
58
  def self.not_a_number
data/lib/util.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Util
2
2
 
3
3
  def self.compare_2_arrays_of_floats(a, b)
4
- delete_nans_and_truncate_floats_to_strings(a) == delete_nans_and_truncate_floats_to_strings(b)
4
+ a.length == b.length && delete_nans_and_truncate_floats_to_strings(a) == delete_nans_and_truncate_floats_to_strings(b)
5
5
  end
6
6
 
7
7
  def self.truncate_float_to_string(f)
@@ -18,6 +18,6 @@ module Util
18
18
  o << [opens[i], highs[i], lows[i], closes[i]]
19
19
  end
20
20
  o
21
- end
21
+ end
22
22
 
23
23
  end
@@ -1,6 +1,7 @@
1
1
  require 'minitest/autorun'
2
2
  require 'quant'
3
3
  require 'enumerable'
4
+ require 'array'
4
5
  require 'util'
5
6
  require 'samples/ohlc'
6
7
  require 'samples/ttr'
data/test/samples/ttr.rb CHANGED
@@ -32,6 +32,26 @@ module Samples
32
32
  [not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, 76.53, 76.53, 76.53, 76.53, 76.53, 76.19, 76.14, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.0, 76.02, 76.12, 76.47, 76.49, 76.69, 77.01, 77.34, 77.34, 77.34, 78.16, 78.34, 78.77, 79.34, 79.53, 79.62, 79.41, 79.41, 79.41, 79.41, 79.41, 79.41, 79.11, 78.99, 78.99, 78.99, 78.99, 78.99, 78.99, 78.99, 78.99, 78.84, 78.19, 77.64, 77.64]
33
33
  end
34
34
 
35
+ # True Range
36
+
37
+ def self.eurusd_50_ttr_tr
38
+ [not_a_number, 0.009600000000000053, 0.01729999999999987, 0.025900000000000034, 0.011300000000000088, 0.010499999999999954, 0.014700000000000157, 0.018399999999999972, 0.010500000000000176, 0.020399999999999974, 0.018000000000000016, 0.009600000000000053, 0.016799999999999926, 0.006299999999999972, 0.016199999999999992, 0.010199999999999987, 0.012399999999999967, 0.013900000000000023, 0.013100000000000112, 0.013699999999999823, 0.013500000000000068, 0.034499999999999975, 0.012799999999999923, 0.005300000000000082, 0.018100000000000005, 0.010999999999999899, 0.008899999999999908, 0.011200000000000099, 0.009500000000000064, 0.021400000000000086, 0.009300000000000086, 0.024399999999999977, 0.011600000000000055, 0.01200000000000001, 0.018799999999999928, 0.018000000000000016, 0.019099999999999895, 0.017000000000000126, 0.012899999999999912, 0.009200000000000097, 0.007400000000000073, 0.012799999999999923, 0.011099999999999888, 0.01639999999999997, 0.011400000000000077, 0.010700000000000154, 0.011599999999999833, 0.01749999999999985, 0.009000000000000119, 0.01550000000000007]
39
+ end
40
+
41
+ def self.usdjpy_150_ttr_tr
42
+ [not_a_number, 0.3400000000000034, 0.36999999999999034, 0.6299999999999955, 0.4299999999999926, 0.5699999999999932, 0.29000000000000625, 0.1799999999999926, 0.46000000000000796, 0.23000000000000398, 0.4899999999999949, 0.6599999999999966, 0.3200000000000074, 0.6700000000000017, 0.769999999999996, 0.6600000000000108, 0.8599999999999994, 0.3200000000000074, 0.35999999999999943, 0.4099999999999966, 0.20999999999999375, 0.1600000000000108, 0.6599999999999966, 0.29000000000000625, 0.46999999999999886, 0.36999999999999034, 0.22999999999998977, 0.0, 0.46999999999999886, 0.4000000000000057, 0.3400000000000034, 0.4099999999999966, 0.20000000000000284, 0.21999999999999886, 0.1799999999999926, 0.23000000000000398, 0.46999999999999886, 0.36000000000001364, 0.8400000000000034, 0.14000000000000057, 0.3400000000000034, 0.21999999999999886, 0.5799999999999983, 0.35999999999999943, 0.23000000000000398, 0.12999999999999545, 0.20000000000000284, 0.30999999999998806, 0.3400000000000034, 0.25, 0.3199999999999932, 0.21999999999999886, 0.6099999999999994, 0.39000000000000057, 0.21999999999999886, 0.8900000000000006, 0.7199999999999989, 0.5100000000000051, 0.8299999999999983, 0.5600000000000023, 0.25, 0.3299999999999983, 0.20000000000000284, 0.5899999999999892, 0.3100000000000023, 0.45000000000000284, 0.46999999999999886, 0.6899999999999977, 0.28000000000000114, 0.39000000000000057, 1.1700000000000017, 0.480000000000004, 0.5999999999999943, 0.710000000000008, 0.519999999999996, 0.28999999999999204, 0.6700000000000017, 0.5, 1.0700000000000074, 1.4899999999999949, 0.7600000000000051, 1.0499999999999972, 0.5400000000000063, 0.7800000000000011, 0.710000000000008, 0.9900000000000091, 0.6400000000000006, 0.6400000000000006, 1.1700000000000017, 0.3999999999999915, 1.1099999999999994, 0.9299999999999926, 0.980000000000004, 0.0, 0.5999999999999943, 0.5100000000000051, 0.7999999999999972, 0.009999999999990905, 1.4300000000000068, 0.6600000000000108, 0.7399999999999949, 0.5799999999999983, 1.039999999999992, 1.039999999999992, 1.4200000000000017, 1.4399999999999977, 0.8400000000000034, 0.6400000000000006, 1.230000000000004, 0.45999999999999375, 1.230000000000004, 0.539999999999992, 0.4000000000000057, 0.3500000000000085, 0.09999999999999432, 0.5999999999999943, 0.6800000000000068, 0.5599999999999881, 0.28000000000000114, 0.6899999999999977, 0.5400000000000063, 0.6099999999999994, 0.7600000000000051, 1.0900000000000034, 0.6500000000000057, 0.6599999999999966, 0.5600000000000023, 0.45000000000000284, 0.5600000000000023, 0.3499999999999943, 0.35999999999999943, 0.5100000000000051, 0.4099999999999966, 0.2600000000000051, 0.5, 0.519999999999996, 0.35999999999999943, 1.2600000000000051, 0.45000000000000284, 0.37000000000000455, 0.8700000000000045, 0.8599999999999994, 0.29000000000000625, 0.3199999999999932, 0.3400000000000034, 0.2600000000000051, 0.7000000000000028, 0.9200000000000017, 1.0100000000000051, 0.4300000000000068]
43
+ end
44
+
45
+ # ATR
46
+
47
+ def self.eurusd_50_ttr_atr_20
48
+ [not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, 0.014115000000000011, 0.015360000000000007, 0.01513500000000001, 0.014105000000000012, 0.014445000000000008, 0.014470000000000005, 0.014179999999999993, 0.013819999999999999, 0.013769999999999994, 0.013819999999999999, 0.013385000000000003, 0.014124999999999999, 0.013865000000000006, 0.014150000000000006, 0.014280000000000004, 0.014670000000000006, 0.015005000000000001, 0.015160000000000007, 0.015149999999999997, 0.014925000000000011, 0.014620000000000011, 0.013535000000000009, 0.013450000000000007, 0.014005, 0.013670000000000005, 0.013655000000000018, 0.013790000000000014, 0.014105000000000001, 0.014080000000000004, 0.013785000000000002]
49
+ end
50
+
51
+ def self.usdjpy_150_ttr_atr_32
52
+ [not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, not_a_number, 0.4134374999999997, 0.40968749999999954, 0.4037499999999996, 0.3912499999999999, 0.39250000000000007, 0.3859375000000007, 0.4031250000000006, 0.40187500000000087, 0.39812500000000073, 0.39781250000000057, 0.4006250000000007, 0.39125000000000076, 0.38843750000000066, 0.37156250000000046, 0.3537500000000007, 0.34281249999999996, 0.3265625000000001, 0.32437499999999986, 0.32312499999999966, 0.31718749999999973, 0.3296874999999999, 0.3368749999999996, 0.32312499999999966, 0.3418749999999995, 0.3496874999999995, 0.35406249999999995, 0.3728125000000002, 0.3903125000000003, 0.3834375000000003, 0.3812500000000001, 0.37687500000000007, 0.38249999999999984, 0.3859374999999998, 0.39312499999999995, 0.40218750000000014, 0.41656249999999995, 0.410625, 0.4115624999999996, 0.42187499999999956, 0.43249999999999966, 0.4406249999999994, 0.45593749999999966, 0.4540624999999996, 0.45187499999999936, 0.4656249999999993, 0.47718749999999943, 0.5043749999999996, 0.5412499999999998, 0.5543749999999998, 0.5793749999999998, 0.5862500000000002, 0.6037500000000002, 0.6068750000000005, 0.6256250000000008, 0.6387500000000008, 0.6309375000000008, 0.6450000000000009, 0.6415625000000005, 0.6503125000000005, 0.6618750000000002, 0.6846875000000003, 0.6743750000000004, 0.6868750000000001, 0.6843750000000006, 0.6996875000000005, 0.6859375000000001, 0.7159375000000003, 0.7150000000000007, 0.7293750000000006, 0.7353125000000005, 0.7312500000000002, 0.7487499999999998, 0.774375, 0.7971874999999997, 0.8071875, 0.8181250000000002, 0.8356250000000003, 0.8343750000000001, 0.839375, 0.8096874999999999, 0.7984374999999999, 0.7765625000000003, 0.7628124999999999, 0.7571874999999997, 0.7562499999999996, 0.742812499999999, 0.731562499999999, 0.7331249999999989, 0.7134374999999991, 0.7199999999999993, 0.7090624999999995, 0.7140624999999998, 0.7037499999999999, 0.7243749999999998, 0.723125, 0.72125, 0.7137500000000001, 0.7243750000000002, 0.6909375, 0.6862499999999998, 0.6759374999999999, 0.6659375000000001, 0.6490625000000003, 0.6328125000000004, 0.5996875000000004, 0.5940625000000006, 0.5818750000000006, 0.5734375000000007, 0.5621875000000007, 0.5746875000000009, 0.545312500000001, 0.538437500000001, 0.5365625000000009, 0.5337500000000008, 0.5525000000000011, 0.5625000000000013, 0.5728125000000013, 0.5687500000000019]
53
+ end
54
+
35
55
  private
36
56
 
37
57
  def self.not_a_number
data/test/test_atr.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'common_requires'
2
+
3
+ class TestSma < MiniTest::Unit::TestCase
4
+
5
+ def test_eurusd_50_tr
6
+ c = Util.ohlc(Samples::Ohlc.eurusd_50_opens, Samples::Ohlc.eurusd_50_highs, Samples::Ohlc.eurusd_50_lows, Samples::Ohlc.eurusd_50_closes)
7
+ assert Util.compare_2_arrays_of_floats Quant.tr(c), Samples::Ttr.eurusd_50_ttr_tr
8
+ end
9
+
10
+ def test_usdjpy_150_tr
11
+ c = Util.ohlc(Samples::Ohlc.usdjpy_150_opens, Samples::Ohlc.usdjpy_150_highs, Samples::Ohlc.usdjpy_150_lows, Samples::Ohlc.usdjpy_150_closes)
12
+ assert Util.compare_2_arrays_of_floats Quant.tr(c), Samples::Ttr.usdjpy_150_ttr_tr
13
+ end
14
+
15
+ def test_eurusd_50_atr20
16
+ c = Util.ohlc(Samples::Ohlc.eurusd_50_opens, Samples::Ohlc.eurusd_50_highs, Samples::Ohlc.eurusd_50_lows, Samples::Ohlc.eurusd_50_closes)
17
+ assert Util.compare_2_arrays_of_floats Quant.atr(c, 20), Samples::Ttr.eurusd_50_ttr_atr_20
18
+ end
19
+
20
+ def test_usdjpy_150_atr32
21
+ c = Util.ohlc(Samples::Ohlc.usdjpy_150_opens, Samples::Ohlc.usdjpy_150_highs, Samples::Ohlc.usdjpy_150_lows, Samples::Ohlc.usdjpy_150_closes)
22
+ assert Util.compare_2_arrays_of_floats Quant.atr(c, 32), Samples::Ttr.usdjpy_150_ttr_atr_32
23
+ end
24
+
25
+ end
@@ -4,26 +4,22 @@ class TestDonchianChannel < MiniTest::Unit::TestCase
4
4
 
5
5
  def test_eurusd_50_donchian_channel_20_highs
6
6
  c = Util.ohlc(Samples::Ohlc.eurusd_50_opens, Samples::Ohlc.eurusd_50_highs, Samples::Ohlc.eurusd_50_lows, Samples::Ohlc.eurusd_50_closes)
7
- dc20_highs = Quant.donchian_channel(c, 20).inject([]) { |a, e| a << e[0] }
8
- assert Util.compare_2_arrays_of_floats dc20_highs , Samples::Ttr.eurusd_50_ttr_donchian_channel_20_highs
7
+ assert Util.compare_2_arrays_of_floats Quant.donchian_channel(c, 20).highs , Samples::Ttr.eurusd_50_ttr_donchian_channel_20_highs
9
8
  end
10
9
 
11
10
  def test_eurusd_50_donchian_channel_20_lows
12
11
  c = Util.ohlc(Samples::Ohlc.eurusd_50_opens, Samples::Ohlc.eurusd_50_highs, Samples::Ohlc.eurusd_50_lows, Samples::Ohlc.eurusd_50_closes)
13
- dc20_lows = Quant.donchian_channel(c, 20).inject([]) { |a, e| a << e[1] }
14
- assert Util.compare_2_arrays_of_floats dc20_lows , Samples::Ttr.eurusd_50_ttr_donchian_channel_20_lows
12
+ assert Util.compare_2_arrays_of_floats Quant.donchian_channel(c, 20).lows , Samples::Ttr.eurusd_50_ttr_donchian_channel_20_lows
15
13
  end
16
14
 
17
15
  def test_usdjpy_150_donchian_channel_55_highs
18
16
  c = Util.ohlc(Samples::Ohlc.usdjpy_150_opens, Samples::Ohlc.usdjpy_150_highs, Samples::Ohlc.usdjpy_150_lows, Samples::Ohlc.usdjpy_150_closes)
19
- dc55_highs = Quant.donchian_channel(c, 55).inject([]) { |a, e| a << e[0] }
20
- assert Util.compare_2_arrays_of_floats dc55_highs , Samples::Ttr.usdjpy_150_ttr_donchian_channel_55_highs
17
+ assert Util.compare_2_arrays_of_floats Quant.donchian_channel(c, 55).highs , Samples::Ttr.usdjpy_150_ttr_donchian_channel_55_highs
21
18
  end
22
19
 
23
20
  def test_usdjpy_150_donchian_channel_55_lows
24
21
  c = Util.ohlc(Samples::Ohlc.usdjpy_150_opens, Samples::Ohlc.usdjpy_150_highs, Samples::Ohlc.usdjpy_150_lows, Samples::Ohlc.usdjpy_150_closes)
25
- dc55_lows = Quant.donchian_channel(c, 55).inject([]) { |a, e| a << e[1] }
26
- assert Util.compare_2_arrays_of_floats dc55_lows , Samples::Ttr.usdjpy_150_ttr_donchian_channel_55_lows
22
+ assert Util.compare_2_arrays_of_floats Quant.donchian_channel(c, 55).lows , Samples::Ttr.usdjpy_150_ttr_donchian_channel_55_lows
27
23
  end
28
24
 
29
25
  end
data/test/test_sma.rb CHANGED
@@ -10,4 +10,4 @@ class TestSma < MiniTest::Unit::TestCase
10
10
  assert Util.compare_2_arrays_of_floats Quant.sma(Samples::Ohlc.usdjpy_150_closes, 25), Samples::Ttr.usdjpy_150_ttr_sma25
11
11
  end
12
12
 
13
- end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -24,6 +24,7 @@ files:
24
24
  - LICENSE
25
25
  - README.md
26
26
  - Rakefile
27
+ - lib/array.rb
27
28
  - lib/enumerable.rb
28
29
  - lib/quant.rb
29
30
  - lib/quant/version.rb
@@ -32,6 +33,7 @@ files:
32
33
  - test/common_requires.rb
33
34
  - test/samples/ohlc.rb
34
35
  - test/samples/ttr.rb
36
+ - test/test_atr.rb
35
37
  - test/test_donchian_channel.rb
36
38
  - test/test_sma.rb
37
39
  homepage: ''
@@ -62,5 +64,6 @@ test_files:
62
64
  - test/common_requires.rb
63
65
  - test/samples/ohlc.rb
64
66
  - test/samples/ttr.rb
67
+ - test/test_atr.rb
65
68
  - test/test_donchian_channel.rb
66
69
  - test/test_sma.rb