tafunc 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +20 -0
- data/README.md +113 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/examples/tafunc_adx.rb +30 -0
- data/examples/tafunc_hints.rb +13 -0
- data/examples/tafunc_ma.rb +25 -0
- data/examples/tafunc_macd.rb +40 -0
- data/lib/tafunc.rb +770 -0
- data/lib/tafunc_array.rb +50 -0
- data/tafunc.gemspec +64 -0
- data/test/helper.rb +18 -0
- data/test/test_tafunc.rb +505 -0
- data/test/test_tafunc_more.rb +53 -0
- metadata +145 -0
data/lib/tafunc_array.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#
|
2
|
+
# filename: tafunc_array.rb
|
3
|
+
#
|
4
|
+
#
|
5
|
+
|
6
|
+
require "tafunc"
|
7
|
+
|
8
|
+
|
9
|
+
#module TaLib; end
|
10
|
+
#class TaLib::TAFunc; end
|
11
|
+
|
12
|
+
# define Array#tafunc.
|
13
|
+
# ==== Args
|
14
|
+
#
|
15
|
+
# ==== Return
|
16
|
+
#
|
17
|
+
class Array
|
18
|
+
|
19
|
+
def tafunc( func )
|
20
|
+
|
21
|
+
#
|
22
|
+
#ret = {}
|
23
|
+
ret = nil
|
24
|
+
|
25
|
+
#
|
26
|
+
ret = TaLib::TAFunc.new( func ) do |taf|
|
27
|
+
|
28
|
+
raise "Conflicts: # of inputs!" if taf.param_attr( :in ).size != 1
|
29
|
+
|
30
|
+
# inputs.
|
31
|
+
taf.param_in_real = self
|
32
|
+
|
33
|
+
# outputs.
|
34
|
+
taf.param_out_setting
|
35
|
+
|
36
|
+
# options.
|
37
|
+
yield(taf) if block_given?
|
38
|
+
|
39
|
+
end.call
|
40
|
+
|
41
|
+
#
|
42
|
+
return ret
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
#### endof filename: tafunc_array.rb
|
data/tafunc.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: tafunc 0.0.1 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "tafunc"
|
9
|
+
s.version = "0.0.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.authors = ["YAMAMOTO, Masayuki"]
|
13
|
+
s.date = "2013-12-12"
|
14
|
+
s.description = "Useful methods are added to TaLib, TaLib::Function, and TaLib::TAFunc."
|
15
|
+
s.email = "martin.route66.blues+github@gmail.com"
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.md"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/tafunc.rb",
|
28
|
+
"test/helper.rb",
|
29
|
+
"test/test_tafunc.rb"
|
30
|
+
]
|
31
|
+
s.homepage = "http://github.com/mephistobooks/tafunc"
|
32
|
+
s.licenses = ["MIT"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = "2.1.9"
|
35
|
+
s.summary = "TAFunc: another talib_ruby wrapper and extensions"
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
s.specification_version = 4
|
39
|
+
|
40
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
41
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
42
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
43
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
44
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.7"])
|
45
|
+
s.add_development_dependency(%q<talib_ruby>, [">= 1.0.5"])
|
46
|
+
s.add_development_dependency(%q<activesupport>, [">= 4.0.0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
49
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
50
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
51
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
|
52
|
+
s.add_dependency(%q<talib_ruby>, [">= 1.0.5"])
|
53
|
+
s.add_dependency(%q<activesupport>, [">= 4.0.0"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
57
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
|
60
|
+
s.add_dependency(%q<talib_ruby>, [">= 1.0.5"])
|
61
|
+
s.add_dependency(%q<activesupport>, [">= 4.0.0"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'tafunc'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
data/test/test_tafunc.rb
ADDED
@@ -0,0 +1,505 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestTAFunc < Test::Unit::TestCase
|
4
|
+
|
5
|
+
#
|
6
|
+
#
|
7
|
+
#
|
8
|
+
def setup
|
9
|
+
|
10
|
+
# TA-Lib's MACD has a bug when signal_period = 1.
|
11
|
+
# so DO NOT USE.
|
12
|
+
#@test_func = :MACD
|
13
|
+
@test_func = :MACDEXT
|
14
|
+
@testee = TaLib::TAFunc.new( @test_func )
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
end
|
20
|
+
|
21
|
+
# test for initialize function(tafunc) object.
|
22
|
+
#
|
23
|
+
#
|
24
|
+
def test_new
|
25
|
+
|
26
|
+
# inputs.
|
27
|
+
attr_in = @testee.param_attr( :in )
|
28
|
+
assert_equal( [:param_in_real], attr_in )
|
29
|
+
assert_equal( nil, @testee.param_in_real ) # check initial value.
|
30
|
+
|
31
|
+
# options.
|
32
|
+
attr_opt = @testee.param_attr( :opt )
|
33
|
+
assert_equal( [:param_opt_in_fast_period,
|
34
|
+
:param_opt_in_fast_ma_type,
|
35
|
+
:param_opt_in_slow_period,
|
36
|
+
:param_opt_in_slow_ma_type,
|
37
|
+
:param_opt_in_signal_period,
|
38
|
+
:param_opt_in_signal_ma_type], attr_opt )
|
39
|
+
|
40
|
+
# outputs.
|
41
|
+
attr_out = @testee.param_attr( :out )
|
42
|
+
assert_equal( [:param_out_macd,
|
43
|
+
:param_out_macd_signal,
|
44
|
+
:param_out_macd_hist], attr_out )
|
45
|
+
assert_equal( nil, @testee.param_out_macd )
|
46
|
+
assert_equal( nil, @testee.param_out_macd_signal )
|
47
|
+
assert_equal( nil, @testee.param_out_macd_hist )
|
48
|
+
|
49
|
+
#
|
50
|
+
tmp = [0.0, 1.0, 2.0]
|
51
|
+
assert_raise(RuntimeError){ @testee.param_in_real( tmp ) }
|
52
|
+
|
53
|
+
#
|
54
|
+
#pp @testee.singleton_methods
|
55
|
+
@testee.param_in_real = tmp
|
56
|
+
assert_equal( tmp, @testee.param_in_real )
|
57
|
+
|
58
|
+
ret = @testee.param_out_setting
|
59
|
+
assert_equal( [nil,nil,nil], @testee.param_out_macd )
|
60
|
+
assert_equal( { :param_out_macd => [nil,nil,nil],
|
61
|
+
:param_out_macd_signal => [nil,nil,nil],
|
62
|
+
:param_out_macd_hist => [nil,nil,nil],
|
63
|
+
}, ret )
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
# ==== ATTENTION
|
69
|
+
# Don't use MACD. It has a bug when signal_period = 1.
|
70
|
+
#
|
71
|
+
def test_new_1
|
72
|
+
|
73
|
+
#
|
74
|
+
assert_nothing_raised { TaLib::TAFunc.new( "MACD" ) }
|
75
|
+
|
76
|
+
#
|
77
|
+
assert_nothing_raised { TaLib::TAFunc.new( :MACD ) }
|
78
|
+
|
79
|
+
#
|
80
|
+
e = assert_raise( RuntimeError ) { TaLib::TAFunc.new( [:MACD] ) }
|
81
|
+
assert_equal( 0, e.message =~ /Type error for the function name:/ )
|
82
|
+
|
83
|
+
#
|
84
|
+
e = assert_raise( RuntimeError ) { taf = TaLib::TAFunc.new( "" ) }
|
85
|
+
|
86
|
+
#
|
87
|
+
e = assert_raise( RuntimeError ) { taf = TaLib::TAFunc.new( "nonamef" ) }
|
88
|
+
assert_equal( false, e.message.match(/^no such function: nonamef/i).nil? )
|
89
|
+
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
#
|
94
|
+
#
|
95
|
+
#
|
96
|
+
def test_struct_ta_types
|
97
|
+
exp = [
|
98
|
+
:TA_RealRange,
|
99
|
+
:TA_IntegerRange,
|
100
|
+
:TA_RealDataPair,
|
101
|
+
:TA_IntegerDataPair,
|
102
|
+
:TA_RealList,
|
103
|
+
:TA_IntegerList,
|
104
|
+
:TA_InputParameterInfo,
|
105
|
+
:TA_OptInputParameterInfo,
|
106
|
+
:TA_OutputParameterInfo,
|
107
|
+
]
|
108
|
+
assert_equal( exp, Struct.ta_types )
|
109
|
+
#assert_equal( exp, Struct::TA_RealRange.to_s )
|
110
|
+
#assert_equal( false, Struct::TA_InputParameterInfo.new )
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
#
|
115
|
+
#
|
116
|
+
#
|
117
|
+
def test_talib_module
|
118
|
+
|
119
|
+
assert_equal( Module, TaLib.class )
|
120
|
+
|
121
|
+
#
|
122
|
+
exp = {
|
123
|
+
0=>:TA_Input_Price,
|
124
|
+
1=>:TA_Input_Real,
|
125
|
+
2=>:TA_Input_Integer,
|
126
|
+
}
|
127
|
+
assert_equal( exp, TaLib.input_types )
|
128
|
+
|
129
|
+
#
|
130
|
+
exp = {
|
131
|
+
0=>:TA_OptInput_RealRange,
|
132
|
+
1=>:TA_OptInput_RealList,
|
133
|
+
2=>:TA_OptInput_IntegerRange,
|
134
|
+
3=>:TA_OptInput_IntegerList,
|
135
|
+
}
|
136
|
+
assert_equal( exp, TaLib.optinput_types )
|
137
|
+
|
138
|
+
#
|
139
|
+
exp = {
|
140
|
+
0=>:TA_Output_Real,
|
141
|
+
1=>:TA_Output_Integer,
|
142
|
+
}
|
143
|
+
assert_equal( exp, TaLib.output_types )
|
144
|
+
|
145
|
+
#
|
146
|
+
exp = {
|
147
|
+
0=>:TA_MAType_SMA,
|
148
|
+
1=>:TA_MAType_EMA,
|
149
|
+
2=>:TA_MAType_WMA,
|
150
|
+
3=>:TA_MAType_DEMA,
|
151
|
+
4=>:TA_MAType_TEMA,
|
152
|
+
5=>:TA_MAType_TRIMA,
|
153
|
+
6=>:TA_MAType_KAMA,
|
154
|
+
7=>:TA_MAType_MAMA,
|
155
|
+
8=>:TA_MAType_T3,
|
156
|
+
}
|
157
|
+
assert_equal( exp, TaLib.ma_types )
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_function_class
|
162
|
+
|
163
|
+
#
|
164
|
+
ret = TaLib::TAFunc.groups
|
165
|
+
exp = [
|
166
|
+
"Math Operators",
|
167
|
+
"Math Transform",
|
168
|
+
"Overlap Studies",
|
169
|
+
"Volatility Indicators",
|
170
|
+
"Momentum Indicators",
|
171
|
+
"Cycle Indicators",
|
172
|
+
"Volume Indicators",
|
173
|
+
"Pattern Recognition",
|
174
|
+
"Statistic Functions",
|
175
|
+
"Price Transform",
|
176
|
+
]
|
177
|
+
assert_equal( 10, ret.size )
|
178
|
+
assert_equal( exp, ret )
|
179
|
+
assert_equal( TaLib::Function.groups, ret )
|
180
|
+
|
181
|
+
#
|
182
|
+
ret = TaLib::TAFunc.functions
|
183
|
+
exp = {
|
184
|
+
"Math Operators" => 11,
|
185
|
+
"Math Transform" => 15,
|
186
|
+
"Overlap Studies" => 17,
|
187
|
+
"Volatility Indicators" => 3,
|
188
|
+
"Momentum Indicators" => 30,
|
189
|
+
"Cycle Indicators" => 5,
|
190
|
+
"Volume Indicators" => 3,
|
191
|
+
"Pattern Recognition" => 61,
|
192
|
+
"Statistic Functions" => 9,
|
193
|
+
"Price Transform" => 4,
|
194
|
+
}
|
195
|
+
exp.keys.each do |grp|
|
196
|
+
|
197
|
+
assert_equal( exp[grp], ret[grp].size )
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
assert_equal( 158, ret.values.flatten.size )
|
202
|
+
#assert_equal( exp.values.inject(0){|r,i| r + i},
|
203
|
+
# ret.values.flatten.size )
|
204
|
+
assert_equal( TaLib::Function.functions.keys, TaLib::Function.groups )
|
205
|
+
end
|
206
|
+
|
207
|
+
# test for function_{find,exists?}, group_of_function.
|
208
|
+
def test_function_function
|
209
|
+
# function_find, exists?.
|
210
|
+
ret = TaLib::Function.function_find( :macdext )
|
211
|
+
assert_equal( "MACDEXT", ret )
|
212
|
+
|
213
|
+
ret = TaLib::Function.function_exists?( :MACDEXT )
|
214
|
+
assert_equal( true, ret )
|
215
|
+
|
216
|
+
ret = TaLib::Function.function_exists?( :macdext )
|
217
|
+
assert_equal( true, ret )
|
218
|
+
|
219
|
+
ret = TaLib::Function.function_exists?( "MACDEXT" )
|
220
|
+
assert_equal( true, ret )
|
221
|
+
|
222
|
+
ret = TaLib::Function.function_exists?( "macdext" )
|
223
|
+
assert_equal( true, ret )
|
224
|
+
|
225
|
+
ret = TaLib::Function.function_exists?( "MACDEXTfooobarrr" )
|
226
|
+
assert_equal( false, ret )
|
227
|
+
|
228
|
+
#
|
229
|
+
ret = TaLib::Function.group_of_function( :MACDEXT )
|
230
|
+
assert_equal( "Momentum Indicators", ret )
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
|
235
|
+
def test_function
|
236
|
+
|
237
|
+
#
|
238
|
+
assert_equal( @test_func.to_s, @testee.name )
|
239
|
+
|
240
|
+
#
|
241
|
+
ret = @testee.ifs_ins
|
242
|
+
assert_equal( 1, ret.size )
|
243
|
+
assert_equal( Struct::TA_InputParameterInfo, ret[0].class )
|
244
|
+
assert_equal( 1, ret[0].type )
|
245
|
+
assert_equal( "inReal", ret[0].param_name )
|
246
|
+
assert_equal( 0, ret[0].flags )
|
247
|
+
|
248
|
+
#
|
249
|
+
ret = @testee.ifs_opts
|
250
|
+
assert_equal( 6, ret.size )
|
251
|
+
ret.size.times do |i|
|
252
|
+
exp_param_name = [ "optInFastPeriod",
|
253
|
+
"optInFastMAType",
|
254
|
+
"optInSlowPeriod",
|
255
|
+
"optInSlowMAType",
|
256
|
+
"optInSignalPeriod",
|
257
|
+
"optInSignalMAType", ][i]
|
258
|
+
assert_equal( Struct::TA_OptInputParameterInfo, ret[i].class )
|
259
|
+
assert_equal( exp_param_name, ret[i].param_name )
|
260
|
+
end
|
261
|
+
|
262
|
+
ret = @testee.ifs_outs
|
263
|
+
assert_equal( 3, ret.size )
|
264
|
+
ret.size.times do |i|
|
265
|
+
exp_param_name = [ "outMACD",
|
266
|
+
"outMACDSignal",
|
267
|
+
"outMACDHist" ][i]
|
268
|
+
assert_equal( Struct::TA_OutputParameterInfo, ret[i].class )
|
269
|
+
assert_equal( exp_param_name, ret[i].param_name )
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
#
|
274
|
+
ret = @testee.ifs_all
|
275
|
+
assert_equal( 10, ret.size )
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_tafunc_new
|
280
|
+
|
281
|
+
assert_nothing_raised { macd = TaLib::TAFunc.new( :MA ) }
|
282
|
+
assert_equal( "MA", TaLib::TAFunc.new("ma").name )
|
283
|
+
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_tafunc
|
288
|
+
|
289
|
+
#
|
290
|
+
ret = TaLib::TAFunc.instance_methods.grep(/^param_(in|opt|out)$/)
|
291
|
+
assert_equal([:param_in, :param_opt, :param_out], ret)
|
292
|
+
|
293
|
+
# in case that the function is MACD.
|
294
|
+
exp = [
|
295
|
+
:param_in_real,
|
296
|
+
:param_in_real=,
|
297
|
+
:param_opt_in_fast_period,
|
298
|
+
:param_opt_in_fast_period=,
|
299
|
+
:param_opt_in_fast_ma_type,
|
300
|
+
:param_opt_in_fast_ma_type=,
|
301
|
+
:param_opt_in_slow_period,
|
302
|
+
:param_opt_in_slow_period=,
|
303
|
+
:param_opt_in_slow_ma_type,
|
304
|
+
:param_opt_in_slow_ma_type=,
|
305
|
+
:param_opt_in_signal_period,
|
306
|
+
:param_opt_in_signal_period=,
|
307
|
+
:param_opt_in_signal_ma_type,
|
308
|
+
:param_opt_in_signal_ma_type=,
|
309
|
+
:param_out_macd,
|
310
|
+
:param_out_macd=,
|
311
|
+
:param_out_macd_signal,
|
312
|
+
:param_out_macd_signal=,
|
313
|
+
:param_out_macd_hist,
|
314
|
+
:param_out_macd_hist=,
|
315
|
+
]
|
316
|
+
ret = @testee.singleton_methods.grep(/^param_/)
|
317
|
+
assert_equal(exp, ret)
|
318
|
+
ret = @testee.param_methods
|
319
|
+
assert_equal(exp, ret)
|
320
|
+
|
321
|
+
ret = @testee.param_methods( :in )
|
322
|
+
assert_equal( [:param_in_real, :param_in_real=], ret )
|
323
|
+
|
324
|
+
ret = @testee.param_attr( :in )
|
325
|
+
assert_equal( [:param_in_real], ret )
|
326
|
+
|
327
|
+
ret = @testee.param_attr( :opt )
|
328
|
+
assert_equal( [:param_opt_in_fast_period,
|
329
|
+
:param_opt_in_fast_ma_type,
|
330
|
+
:param_opt_in_slow_period,
|
331
|
+
:param_opt_in_slow_ma_type,
|
332
|
+
:param_opt_in_signal_period,
|
333
|
+
:param_opt_in_signal_ma_type,
|
334
|
+
], ret )
|
335
|
+
|
336
|
+
#ret = @testee.param_methods( :in, :type )
|
337
|
+
#assert_equal([:param_in_real], ret)
|
338
|
+
|
339
|
+
|
340
|
+
#
|
341
|
+
assert_equal( nil, @testee.param_in_real )
|
342
|
+
assert_equal( [1,2,3], @testee.param_in_real=[1,2,3] )
|
343
|
+
assert_equal( [1,2,3], @testee.param_in_real )
|
344
|
+
assert_equal( :TA_Input_Real, @testee.param_in_real(:type) )
|
345
|
+
|
346
|
+
h = {}
|
347
|
+
ret = @testee.param_out_setting( h )
|
348
|
+
assert_equal( { }, h )
|
349
|
+
|
350
|
+
ret = @testee.param_out_setting( h, force_mode: true )
|
351
|
+
assert_equal( { :param_out_macd =>[nil, nil, nil],
|
352
|
+
:param_out_macd_signal =>[nil, nil, nil],
|
353
|
+
:param_out_macd_hist =>[nil, nil, nil], }, h )
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_tafunc_call
|
358
|
+
|
359
|
+
#
|
360
|
+
e = assert_raise( RuntimeError ){ @testee.call }
|
361
|
+
assert_equal( 0, e.message =~ /No setting of param_in_/ )
|
362
|
+
|
363
|
+
#
|
364
|
+
tmp = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]
|
365
|
+
@testee.param_in_real = tmp
|
366
|
+
#@testee.param_opt_in_fast_period = 3
|
367
|
+
assert_equal( tmp, @testee.param_in_real )
|
368
|
+
e = assert_raise( RuntimeError ){ @testee.call }
|
369
|
+
assert_equal( 0, e.message =~ /unsuccess return code TA_CallFunc/ )
|
370
|
+
|
371
|
+
#
|
372
|
+
@testee.param_out_macd = Array.new(@testee.param_in_real.size)
|
373
|
+
@testee.param_out_macd_signal = Array.new(@testee.param_in_real.size)
|
374
|
+
@testee.param_out_macd_hist = Array.new(@testee.param_in_real.size)
|
375
|
+
assert_nothing_raised{ @testee.call }
|
376
|
+
assert_nothing_raised{ @testee.call(0,4) }
|
377
|
+
assert_nothing_raised{ @testee.call(1..3) }
|
378
|
+
assert_nothing_raised{ @testee.call(1...3) }
|
379
|
+
assert_raise( RuntimeError ){ @testee.call(1,8) }
|
380
|
+
assert_raise( RuntimeError ){ @testee.call(1..8) }
|
381
|
+
assert_raise( RuntimeError ){ @testee.call(1...8) }
|
382
|
+
assert_raise( RuntimeError ){ @testee.call(4,0) }
|
383
|
+
assert_nothing_raised{ @testee.call(tmp) }
|
384
|
+
|
385
|
+
end
|
386
|
+
|
387
|
+
# case without param_in_real.
|
388
|
+
def test_tafunc_call_1
|
389
|
+
|
390
|
+
#
|
391
|
+
tmp = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]
|
392
|
+
|
393
|
+
#
|
394
|
+
@testee.param_out_macd = Array.new(tmp)
|
395
|
+
@testee.param_out_macd_signal = Array.new(tmp)
|
396
|
+
@testee.param_out_macd_hist = Array.new(tmp)
|
397
|
+
assert_nothing_raised{ @testee.call(tmp) }
|
398
|
+
|
399
|
+
end
|
400
|
+
|
401
|
+
# MACD test with TA_MACDEXT.
|
402
|
+
#
|
403
|
+
def test_tafunc_macd
|
404
|
+
|
405
|
+
#
|
406
|
+
tmp = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]
|
407
|
+
@testee.param_in_real = tmp
|
408
|
+
@testee.param_opt_in_fast_period = 2 # MA of tmp by 2 periods.
|
409
|
+
@testee.param_opt_in_slow_period = 3 # MA of tmp by 3 periods.
|
410
|
+
@testee.param_opt_in_signal_period = 1 # MA of MACD by 1 period. (Signal=MACD)
|
411
|
+
[ :param_opt_in_fast_ma_type=,
|
412
|
+
:param_opt_in_slow_ma_type=,
|
413
|
+
:param_opt_in_signal_ma_type=, ].each {|param|
|
414
|
+
@testee.send( param, TaLib::TA_MAType_EMA )
|
415
|
+
}
|
416
|
+
|
417
|
+
# .
|
418
|
+
output_size = tmp.size + [ @testee.param_opt_in_fast_period,
|
419
|
+
@testee.param_opt_in_slow_period,
|
420
|
+
@testee.param_opt_in_signal_period ].max
|
421
|
+
#
|
422
|
+
@testee.param_out_macd = Array.new( output_size )
|
423
|
+
@testee.param_out_macd_signal = Array.new( output_size )
|
424
|
+
@testee.param_out_macd_hist = Array.new( output_size )
|
425
|
+
|
426
|
+
#
|
427
|
+
tmp = [nil, nil, nil, nil, nil, nil, nil, nil]
|
428
|
+
@testee.param_out_macd = tmp.dup
|
429
|
+
@testee.param_out_macd_signal = tmp.dup
|
430
|
+
@testee.param_out_macd_hist = tmp.dup
|
431
|
+
|
432
|
+
assert_equal( tmp, @testee.param_out_macd )
|
433
|
+
|
434
|
+
#
|
435
|
+
ret_call = nil
|
436
|
+
assert_nothing_raised{ ret_call = @testee.call(0, 4) }
|
437
|
+
|
438
|
+
# start_idx, num_elements.
|
439
|
+
assert_equal( [2, 3], [ret_call[:start_idx], ret_call[:num_elements]] )
|
440
|
+
|
441
|
+
#
|
442
|
+
assert_equal( [0.5, 0.5, 0.5],
|
443
|
+
@testee.param_out_macd[ 0..(ret_call[:num_elements]-1)] )
|
444
|
+
assert_equal( @testee.param_out_macd,
|
445
|
+
@testee.param_out_macd_signal )
|
446
|
+
assert_equal( [0.0, 0.0, 0.0],
|
447
|
+
@testee.param_out_macd_hist[0..(ret_call[:num_elements]-1)] )
|
448
|
+
end
|
449
|
+
|
450
|
+
def test_tafunc_macd_1
|
451
|
+
|
452
|
+
#
|
453
|
+
tmp = [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 0.0 ]
|
454
|
+
@testee.param_in_real = tmp
|
455
|
+
@testee.param_opt_in_fast_period = 2 # MA of tmp by 2 periods.
|
456
|
+
@testee.param_opt_in_slow_period = 3 # MA of tmp by 3 periods.
|
457
|
+
@testee.param_opt_in_signal_period = 1 # MA of MACD by 1 period. (Signal=MACD)
|
458
|
+
[ :param_opt_in_fast_ma_type=,
|
459
|
+
:param_opt_in_slow_ma_type=,
|
460
|
+
:param_opt_in_signal_ma_type=, ].each {|param|
|
461
|
+
@testee.send( param, TaLib::TA_MAType_EMA )
|
462
|
+
}
|
463
|
+
|
464
|
+
#
|
465
|
+
output_size = tmp.size
|
466
|
+
|
467
|
+
#
|
468
|
+
@testee.param_out_macd = Array.new( output_size )
|
469
|
+
@testee.param_out_macd_signal = Array.new( output_size )
|
470
|
+
@testee.param_out_macd_hist = Array.new( output_size )
|
471
|
+
|
472
|
+
#
|
473
|
+
tmp = [nil, nil, nil, nil, nil, nil, nil]
|
474
|
+
assert_equal( tmp, @testee.param_out_macd )
|
475
|
+
|
476
|
+
#
|
477
|
+
run_start_idx = 0
|
478
|
+
run_end_idx = 6
|
479
|
+
|
480
|
+
ret_call = nil
|
481
|
+
assert_nothing_raised{
|
482
|
+
ret_call = @testee.call(run_start_idx, run_end_idx) }
|
483
|
+
|
484
|
+
# start_idx, num_elements.
|
485
|
+
assert_equal( { :param_out_macd=>
|
486
|
+
[0.5, 0.5, 0.5, -0.5, -0.5, nil, nil],
|
487
|
+
:param_out_macd_signal=>
|
488
|
+
[0.5, 0.5, 0.5, -0.5, -0.5, nil, nil],
|
489
|
+
:param_out_macd_hist=>
|
490
|
+
[0.0, 0.0, 0.0, 0.0, 0.0, nil, nil],
|
491
|
+
:start_idx => 2,
|
492
|
+
:num_elements => 5, }, ret_call )
|
493
|
+
|
494
|
+
#
|
495
|
+
assert_equal( [0.5, 0.5, 0.5, -0.5, -0.5],
|
496
|
+
@testee.param_out_macd[ 0..(ret_call[:num_elements]-1)] )
|
497
|
+
assert_equal( [0.5, 0.5, 0.5, -0.5, -0.5],
|
498
|
+
@testee.param_out_macd_signal[ 0..(ret_call[:num_elements]-1)] )
|
499
|
+
assert_equal( [0.0, 0.0, 0.0, 0.0, 0.0 ],
|
500
|
+
@testee.param_out_macd_hist[ 0..(ret_call[:num_elements]-1)] )
|
501
|
+
|
502
|
+
end
|
503
|
+
|
504
|
+
|
505
|
+
end
|