ta-indicator 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +10 -0
- data/LICENSE-ta-lib +32 -0
- data/README.rdoc +196 -0
- data/Rakefile +13 -0
- data/examples/mapping.rb +43 -0
- data/lib/indicator.rb +56 -0
- data/lib/indicator/auto_gen.rb +163 -0
- data/lib/indicator/auto_gen/acos.rb +44 -0
- data/lib/indicator/auto_gen/ad.rb +44 -0
- data/lib/indicator/auto_gen/ad_osc.rb +58 -0
- data/lib/indicator/auto_gen/add.rb +45 -0
- data/lib/indicator/auto_gen/adx.rb +53 -0
- data/lib/indicator/auto_gen/adxr.rb +53 -0
- data/lib/indicator/auto_gen/apo.rb +63 -0
- data/lib/indicator/auto_gen/aroon.rb +56 -0
- data/lib/indicator/auto_gen/aroon_osc.rb +53 -0
- data/lib/indicator/auto_gen/asin.rb +44 -0
- data/lib/indicator/auto_gen/atan.rb +44 -0
- data/lib/indicator/auto_gen/atr.rb +53 -0
- data/lib/indicator/auto_gen/avg_price.rb +44 -0
- data/lib/indicator/auto_gen/bbands.rb +74 -0
- data/lib/indicator/auto_gen/beta.rb +54 -0
- data/lib/indicator/auto_gen/bop.rb +44 -0
- data/lib/indicator/auto_gen/cci.rb +53 -0
- data/lib/indicator/auto_gen/cdl2_crows.rb +44 -0
- data/lib/indicator/auto_gen/cdl3_black_crows.rb +44 -0
- data/lib/indicator/auto_gen/cdl3_inside.rb +44 -0
- data/lib/indicator/auto_gen/cdl3_line_strike.rb +44 -0
- data/lib/indicator/auto_gen/cdl3_outside.rb +44 -0
- data/lib/indicator/auto_gen/cdl3_stars_in_south.rb +44 -0
- data/lib/indicator/auto_gen/cdl3_white_soldiers.rb +44 -0
- data/lib/indicator/auto_gen/cdl_abandoned_baby.rb +53 -0
- data/lib/indicator/auto_gen/cdl_advance_block.rb +44 -0
- data/lib/indicator/auto_gen/cdl_belt_hold.rb +44 -0
- data/lib/indicator/auto_gen/cdl_breakaway.rb +44 -0
- data/lib/indicator/auto_gen/cdl_closing_marubozu.rb +44 -0
- data/lib/indicator/auto_gen/cdl_conceal_babys_wall.rb +44 -0
- data/lib/indicator/auto_gen/cdl_counter_attack.rb +44 -0
- data/lib/indicator/auto_gen/cdl_dark_cloud_cover.rb +53 -0
- data/lib/indicator/auto_gen/cdl_doji.rb +44 -0
- data/lib/indicator/auto_gen/cdl_doji_star.rb +44 -0
- data/lib/indicator/auto_gen/cdl_dragonfly_doji.rb +44 -0
- data/lib/indicator/auto_gen/cdl_engulfing.rb +44 -0
- data/lib/indicator/auto_gen/cdl_evening_doji_star.rb +53 -0
- data/lib/indicator/auto_gen/cdl_evening_star.rb +53 -0
- data/lib/indicator/auto_gen/cdl_gap_side_side_white.rb +44 -0
- data/lib/indicator/auto_gen/cdl_gravestone_doji.rb +44 -0
- data/lib/indicator/auto_gen/cdl_hammer.rb +44 -0
- data/lib/indicator/auto_gen/cdl_hanging_man.rb +44 -0
- data/lib/indicator/auto_gen/cdl_harami.rb +44 -0
- data/lib/indicator/auto_gen/cdl_harami_cross.rb +44 -0
- data/lib/indicator/auto_gen/cdl_hign_wave.rb +44 -0
- data/lib/indicator/auto_gen/cdl_hikkake.rb +44 -0
- data/lib/indicator/auto_gen/cdl_hikkake_mod.rb +44 -0
- data/lib/indicator/auto_gen/cdl_homing_pigeon.rb +44 -0
- data/lib/indicator/auto_gen/cdl_identical3_crows.rb +44 -0
- data/lib/indicator/auto_gen/cdl_in_neck.rb +44 -0
- data/lib/indicator/auto_gen/cdl_inverted_hammer.rb +44 -0
- data/lib/indicator/auto_gen/cdl_kicking.rb +44 -0
- data/lib/indicator/auto_gen/cdl_kicking_by_length.rb +44 -0
- data/lib/indicator/auto_gen/cdl_ladder_bottom.rb +44 -0
- data/lib/indicator/auto_gen/cdl_long_legged_doji.rb +44 -0
- data/lib/indicator/auto_gen/cdl_long_line.rb +44 -0
- data/lib/indicator/auto_gen/cdl_marubozu.rb +44 -0
- data/lib/indicator/auto_gen/cdl_mat_hold.rb +53 -0
- data/lib/indicator/auto_gen/cdl_matching_low.rb +44 -0
- data/lib/indicator/auto_gen/cdl_morning_doji_star.rb +53 -0
- data/lib/indicator/auto_gen/cdl_morning_star.rb +53 -0
- data/lib/indicator/auto_gen/cdl_on_neck.rb +44 -0
- data/lib/indicator/auto_gen/cdl_piercing.rb +44 -0
- data/lib/indicator/auto_gen/cdl_rickshaw_man.rb +44 -0
- data/lib/indicator/auto_gen/cdl_rise_fall3_methods.rb +44 -0
- data/lib/indicator/auto_gen/cdl_seperating_lines.rb +44 -0
- data/lib/indicator/auto_gen/cdl_shooting_star.rb +44 -0
- data/lib/indicator/auto_gen/cdl_short_line.rb +44 -0
- data/lib/indicator/auto_gen/cdl_spinning_top.rb +44 -0
- data/lib/indicator/auto_gen/cdl_stalled_pattern.rb +44 -0
- data/lib/indicator/auto_gen/cdl_stick_sandwhich.rb +44 -0
- data/lib/indicator/auto_gen/cdl_takuri.rb +44 -0
- data/lib/indicator/auto_gen/cdl_tasuki_gap.rb +44 -0
- data/lib/indicator/auto_gen/cdl_thrusting.rb +44 -0
- data/lib/indicator/auto_gen/cdl_tristar.rb +44 -0
- data/lib/indicator/auto_gen/cdl_unique3_river.rb +44 -0
- data/lib/indicator/auto_gen/cdl_upside_gap2_crows.rb +44 -0
- data/lib/indicator/auto_gen/cdl_x_side_gap3_methods.rb +44 -0
- data/lib/indicator/auto_gen/ceil.rb +44 -0
- data/lib/indicator/auto_gen/cmo.rb +53 -0
- data/lib/indicator/auto_gen/correl.rb +54 -0
- data/lib/indicator/auto_gen/cos.rb +44 -0
- data/lib/indicator/auto_gen/cosh.rb +44 -0
- data/lib/indicator/auto_gen/dema.rb +53 -0
- data/lib/indicator/auto_gen/div.rb +45 -0
- data/lib/indicator/auto_gen/dx.rb +53 -0
- data/lib/indicator/auto_gen/ema.rb +53 -0
- data/lib/indicator/auto_gen/exp.rb +44 -0
- data/lib/indicator/auto_gen/floor.rb +44 -0
- data/lib/indicator/auto_gen/ht_dc_period.rb +44 -0
- data/lib/indicator/auto_gen/ht_dc_phase.rb +44 -0
- data/lib/indicator/auto_gen/ht_phasor.rb +47 -0
- data/lib/indicator/auto_gen/ht_sine.rb +47 -0
- data/lib/indicator/auto_gen/ht_trend_mode.rb +44 -0
- data/lib/indicator/auto_gen/ht_trendline.rb +44 -0
- data/lib/indicator/auto_gen/kama.rb +53 -0
- data/lib/indicator/auto_gen/linear_reg.rb +53 -0
- data/lib/indicator/auto_gen/linear_reg_angle.rb +53 -0
- data/lib/indicator/auto_gen/linear_reg_intercept.rb +53 -0
- data/lib/indicator/auto_gen/linear_reg_slope.rb +53 -0
- data/lib/indicator/auto_gen/ln.rb +44 -0
- data/lib/indicator/auto_gen/log10.rb +44 -0
- data/lib/indicator/auto_gen/macd.rb +69 -0
- data/lib/indicator/auto_gen/macd_ext.rb +84 -0
- data/lib/indicator/auto_gen/macd_fix.rb +59 -0
- data/lib/indicator/auto_gen/mama.rb +61 -0
- data/lib/indicator/auto_gen/max.rb +53 -0
- data/lib/indicator/auto_gen/max_index.rb +53 -0
- data/lib/indicator/auto_gen/med_price.rb +44 -0
- data/lib/indicator/auto_gen/mfi.rb +53 -0
- data/lib/indicator/auto_gen/mid_point.rb +53 -0
- data/lib/indicator/auto_gen/mid_price.rb +53 -0
- data/lib/indicator/auto_gen/min.rb +53 -0
- data/lib/indicator/auto_gen/min_index.rb +53 -0
- data/lib/indicator/auto_gen/min_max.rb +56 -0
- data/lib/indicator/auto_gen/min_max_index.rb +56 -0
- data/lib/indicator/auto_gen/minus_di.rb +53 -0
- data/lib/indicator/auto_gen/minus_dm.rb +53 -0
- data/lib/indicator/auto_gen/mom.rb +53 -0
- data/lib/indicator/auto_gen/moving_average.rb +58 -0
- data/lib/indicator/auto_gen/moving_average_variable_period.rb +64 -0
- data/lib/indicator/auto_gen/mult.rb +45 -0
- data/lib/indicator/auto_gen/natr.rb +53 -0
- data/lib/indicator/auto_gen/obv.rb +44 -0
- data/lib/indicator/auto_gen/plus_di.rb +53 -0
- data/lib/indicator/auto_gen/plus_dm.rb +53 -0
- data/lib/indicator/auto_gen/ppo.rb +63 -0
- data/lib/indicator/auto_gen/roc.rb +53 -0
- data/lib/indicator/auto_gen/roc_p.rb +53 -0
- data/lib/indicator/auto_gen/roc_r.rb +53 -0
- data/lib/indicator/auto_gen/roc_r100.rb +53 -0
- data/lib/indicator/auto_gen/rsi.rb +53 -0
- data/lib/indicator/auto_gen/sar.rb +58 -0
- data/lib/indicator/auto_gen/sar_ext.rb +88 -0
- data/lib/indicator/auto_gen/sin.rb +44 -0
- data/lib/indicator/auto_gen/sinh.rb +44 -0
- data/lib/indicator/auto_gen/sma.rb +53 -0
- data/lib/indicator/auto_gen/sqrt.rb +44 -0
- data/lib/indicator/auto_gen/std_dev.rb +58 -0
- data/lib/indicator/auto_gen/stoch.rb +76 -0
- data/lib/indicator/auto_gen/stoch_f.rb +66 -0
- data/lib/indicator/auto_gen/stoch_rsi.rb +71 -0
- data/lib/indicator/auto_gen/sub.rb +45 -0
- data/lib/indicator/auto_gen/sum.rb +53 -0
- data/lib/indicator/auto_gen/t3.rb +58 -0
- data/lib/indicator/auto_gen/tan.rb +44 -0
- data/lib/indicator/auto_gen/tanh.rb +44 -0
- data/lib/indicator/auto_gen/tema.rb +53 -0
- data/lib/indicator/auto_gen/trima.rb +53 -0
- data/lib/indicator/auto_gen/trix.rb +53 -0
- data/lib/indicator/auto_gen/true_range.rb +44 -0
- data/lib/indicator/auto_gen/tsf.rb +53 -0
- data/lib/indicator/auto_gen/typ_price.rb +44 -0
- data/lib/indicator/auto_gen/ult_osc.rb +63 -0
- data/lib/indicator/auto_gen/variance.rb +58 -0
- data/lib/indicator/auto_gen/wcl_price.rb +44 -0
- data/lib/indicator/auto_gen/will_r.rb +53 -0
- data/lib/indicator/auto_gen/wma.rb +53 -0
- data/lib/indicator/base.rb +19 -0
- data/lib/indicator/data_mapper.rb +93 -0
- data/lib/indicator/mixin.rb +15 -0
- data/lib/indicator/version.rb +3 -0
- data/test/data_mapper_test.rb +76 -0
- metadata +226 -0
data/LICENSE
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright 2012 Michael Lamb
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
10
|
+
|
data/LICENSE-ta-lib
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
TA-Lib Copyright (c) 1999-2008, Mario Fortier
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or
|
5
|
+
without modification, are permitted provided that the following
|
6
|
+
conditions are met:
|
7
|
+
|
8
|
+
- Redistributions of source code must retain the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
- Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer in
|
13
|
+
the documentation and/or other materials provided with the
|
14
|
+
distribution.
|
15
|
+
|
16
|
+
- Neither name of author nor the names of its contributors
|
17
|
+
may be used to endorse or promote products derived from this
|
18
|
+
software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
23
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
24
|
+
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
25
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
26
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
27
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
28
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
29
|
+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
30
|
+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
31
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
= Technical Analysis Indicator Library
|
2
|
+
|
3
|
+
== Overview
|
4
|
+
|
5
|
+
Provides a wrapper around the talib-ruby[https://github.com/rivella50/talib-ruby] library which is a ruby wrapper for the ta-lib[http://ta-lib.org/] library.
|
6
|
+
|
7
|
+
This library has been designed to make interfacing with the ta-lib functions easy by wrapping each function in a ruby class.
|
8
|
+
|
9
|
+
The wrappers are autogenerated from the xml function description provided with the ta-lib project.
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
Requires the talib-ruby gem. The examples require the yahoofinance gem.
|
14
|
+
|
15
|
+
=== Simple Example
|
16
|
+
The easiest way to use the indicator library is to include the indicator mixin module. This module extends the Array class with the Indicator::Mixin module allowing quick calculations to be performed:
|
17
|
+
|
18
|
+
require 'indicator/mixin'
|
19
|
+
|
20
|
+
data = [1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3]
|
21
|
+
results = data.indicator(:sma, :time_period => 5)
|
22
|
+
results = data.indicator(:sma, 5)
|
23
|
+
results = data.indicator(:sma_5)
|
24
|
+
|
25
|
+
All perform a simple moving average with time period of 5 and produce the same results:
|
26
|
+
|
27
|
+
=> [nil, nil, nil, nil, 2.2, 2.4, 2.6, 2.4, 2.6, 2.4, 2.6]
|
28
|
+
|
29
|
+
More complex indicators can also be used
|
30
|
+
|
31
|
+
result = data.indicator(:macd_12_26_9)
|
32
|
+
|
33
|
+
This performs a macd calculation using a slow period of 12, fast period of 26 and signal period of 9.
|
34
|
+
|
35
|
+
The list of available indicators can be retrieved via
|
36
|
+
|
37
|
+
Indicator.list
|
38
|
+
|
39
|
+
Information about each indicator including a list of arguments can be retrieved via:
|
40
|
+
|
41
|
+
Indicator.info :sma
|
42
|
+
Inficator.info :macd
|
43
|
+
...
|
44
|
+
|
45
|
+
== Details
|
46
|
+
The base Indicator functions are located in the Indicator module and the auto generated classed are located in the Indicator::AutoGen module. All of the following examples assume these modules have been included:
|
47
|
+
|
48
|
+
require 'indicator'
|
49
|
+
include Indicator
|
50
|
+
include Indicator::AutoGen
|
51
|
+
|
52
|
+
=== Class Construction
|
53
|
+
|
54
|
+
All ta-lib functions are wrapped in a class named using the camel class name of the function: SMA is Sma, AVGPRICE is AvgPrice, etc. The constructor takes the optional arguments either as a hash or argument list. For example the Macd class takes three arguments, fast_period, slow_period and signal_period. They can be initialised in any of the following ways:
|
55
|
+
|
56
|
+
sma = Sma.new 5
|
57
|
+
macd = Macd.new 12,26,9
|
58
|
+
or
|
59
|
+
sma = Sma.new :time_period => 5
|
60
|
+
macd = Macd.new :fast_period => 12, :slow_period => 26, :signal_period => 9
|
61
|
+
|
62
|
+
If a parameter is not passed in then it will be initialised to the default value as specified by the ta-lib interface.
|
63
|
+
|
64
|
+
The Indicator module also supports factory style functions to simplify class creation.
|
65
|
+
|
66
|
+
sma = Indicator.create :sma
|
67
|
+
sma = Indicator.create :sma, 5
|
68
|
+
sma = Indicator.create :sma, time_period => 5
|
69
|
+
|
70
|
+
macd = Indicator.create :macd
|
71
|
+
macd = Indicator.create :macd, 12,26,9
|
72
|
+
macd = Indicator.create :macd, :fast_period => 12, :slow_period => 26, :signal_period => 9
|
73
|
+
|
74
|
+
If a indicator is to be constructed using only integer parameters then an indicator can be constructed using the create_named factory function. The create_named function expects the indicator name optionally followed by it's arguments joined by an underscore.
|
75
|
+
|
76
|
+
sma = Indicator.create_named :sma_5
|
77
|
+
macd = Indicator.create_named :sma_12_26_9
|
78
|
+
|
79
|
+
=== Argument attributes
|
80
|
+
|
81
|
+
All arguments can be updated through attributes:
|
82
|
+
macd.fast_period
|
83
|
+
=> 12
|
84
|
+
macd.slow_period = 30
|
85
|
+
=> 30
|
86
|
+
|
87
|
+
A list of all the arguments that a particular class takes can be be retrieved using the class method 'arguments'.
|
88
|
+
Macd.arguments
|
89
|
+
=> [:fast_period, :slow_period, :signal_period]
|
90
|
+
Sma.arguments
|
91
|
+
=> [:time_period]
|
92
|
+
|
93
|
+
=== Running The Indicators
|
94
|
+
Each indicator defines a run method that takes in each input array to be processed as a List. An example of an indicator that takes one input:
|
95
|
+
sma = Indicator.create_named :sma_5
|
96
|
+
sma.run [1,2,3,4,3,2,3,2,3,2,3,2]
|
97
|
+
=> [nil, nil, nil, nil, 2.6, 2.8, 3.0, 2.8, 2.6, 2.4, 2.6, 2.4]
|
98
|
+
|
99
|
+
An example of an indicator that takes two inputs:
|
100
|
+
add = Indicator.create_named :add
|
101
|
+
add.run [1,2,3,4,5], [5,4,3,2,1]
|
102
|
+
=> [6.0, 6.0, 6.0, 6.0, 6.0]
|
103
|
+
|
104
|
+
Some indicators require OHLC (and optionally V) price inputs. An example of an indicator that takes price inputs:
|
105
|
+
avg = AvgPrice.new
|
106
|
+
# AvgPrice run function takes open, high, low, close
|
107
|
+
avg.run [1,2,1,2], [2,1,2,1], [3,1,3,1], [1,3,1,3]
|
108
|
+
=> [1.75, 1.75, 1.75, 1.75]
|
109
|
+
|
110
|
+
A list of inputs that a indicator run function requires can be retrieved using the class method 'inputs'.
|
111
|
+
Sma.inputs
|
112
|
+
=> [:in_real]
|
113
|
+
Add.inputs
|
114
|
+
=> [:in_real0, in_real1]
|
115
|
+
AvgPrice.inputs
|
116
|
+
=> [:open, :high, :low, :close]
|
117
|
+
AdOsc.inputs
|
118
|
+
=> [:open, :high, :low, :close, :volume]
|
119
|
+
|
120
|
+
=== Outputs
|
121
|
+
The run function returns an array containing the results if there is only one List to return otherwise it returns a hash that maps each output List to it's name. For eample:
|
122
|
+
sma = Sma.new(5)
|
123
|
+
sma.run [1,2,3,4,3,2,3,2,3,2,3,2]
|
124
|
+
=> [nil, nil, nil, nil, 2.6, 2.8, 3.0, 2.8, 2.6, 2.4, 2.6, 2.4]
|
125
|
+
macd = Macd.new 3,4,1
|
126
|
+
result = macd.run [1,2,3,4,4,3]
|
127
|
+
puts results[:out_macd]
|
128
|
+
=> [nil, nil, -3.785768566076978e-270, 0.5, nil, nil]
|
129
|
+
puts result[:out_macd_signal]
|
130
|
+
=> [nil, nil, 0.399999999999999, 0.189999999999999, nil, nil],
|
131
|
+
puts result[:out_macd_hist]
|
132
|
+
=> [nil, nil, -0.3999999999999999, 0.31000000000000005, nil, nil]
|
133
|
+
|
134
|
+
A list of outputs that an indicator run function returns can be retrieved using the class method 'outputs'.
|
135
|
+
Sma.outputs
|
136
|
+
=> [:out_real]
|
137
|
+
Macd.outputs
|
138
|
+
=> [:out_macd, :out_macd_signal, :out_macd_hist]
|
139
|
+
|
140
|
+
=== Data Mapping
|
141
|
+
The run function also accepts a DataMapping::Map structure which can be used to extract data from an enumerable collection.
|
142
|
+
|
143
|
+
# Assume historical_data is a list of hashes with the following keys
|
144
|
+
# :open, :high, :low, :close, :volume
|
145
|
+
sma = Sma.new 5
|
146
|
+
sma_results = sma.run(DataMapper::Map.new(historical_data, :open))
|
147
|
+
|
148
|
+
When the above example is run the DataMapper will attempt to extract data from the historical_data collection by either calling a function called open or retrieving a hash value using the key :open. You can also directly pass in a lambda function if a more complex mapping function is required.
|
149
|
+
To reduce the amount of code required a helper function called new_map is available:
|
150
|
+
|
151
|
+
sma = Sma.new 5
|
152
|
+
sma_results = sma.run new_map(historical_data, :open)
|
153
|
+
sma_results = sma.run new_map(historical_data, lambda => |b| { b[:open] / 2 })
|
154
|
+
|
155
|
+
By default the run function will attempt to use a default getter of :open, so the following is also valid:
|
156
|
+
|
157
|
+
sma = Sma.new 5
|
158
|
+
sma_results = sma.run historical_data
|
159
|
+
# Calculates the sma by accessing [:open] on each element of historical_data
|
160
|
+
|
161
|
+
The default getter can be changed:
|
162
|
+
sma = Sma.new 5
|
163
|
+
sma.default_getter :high
|
164
|
+
sma_results = sma.run historical_data
|
165
|
+
# Calculates the sma by accessing [:high] on each element of historical_data
|
166
|
+
|
167
|
+
The same mapping system works for functions requiring price data as an input. The AvgPrice function can be called using either of the following syntax:
|
168
|
+
|
169
|
+
ap = AvgPrice.new
|
170
|
+
ap.run(
|
171
|
+
new_map(historical_data, :open),
|
172
|
+
new_map(historical_data, :high),
|
173
|
+
new_map(historical_data, :low),
|
174
|
+
new_map(historical_data, :close))
|
175
|
+
|
176
|
+
or
|
177
|
+
|
178
|
+
ap = AvgPrice.new
|
179
|
+
ap.run historical_data
|
180
|
+
|
181
|
+
The run function will again attempt to use sensible default mappings of :open, :high, :low, :close and :volume (if required).
|
182
|
+
|
183
|
+
== Regenerating the wrappers
|
184
|
+
|
185
|
+
The wrappers can be regenerated using the gen_wrapper.rb file in the tools directory:
|
186
|
+
|
187
|
+
ruby gen_wrapper.rb
|
188
|
+
|
189
|
+
This script must be run from the tools directory as it uses realtive paths.
|
190
|
+
The gen_wrapper.rb script uses the erb library to generate code from the defined templates.
|
191
|
+
|
192
|
+
== Copyright
|
193
|
+
|
194
|
+
Copyright (c) 2012 Michael Lamb. See LICENSE for details.
|
195
|
+
|
196
|
+
The files ta_func_api.xml and ta_func_api.xsd are from the talib library. The license details are contained within LICENSE-ta-lib.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#
|
2
|
+
# To change this template, choose Tools | Templates
|
3
|
+
# and open the template in the editor.
|
4
|
+
|
5
|
+
require 'rake'
|
6
|
+
require 'rake/clean'
|
7
|
+
require 'rake/testtask'
|
8
|
+
|
9
|
+
Rake::TestTask.new do |t|
|
10
|
+
t.libs << 'lib' << 'test' << 'examples'
|
11
|
+
t.test_files = FileList['test/**/*.rb']
|
12
|
+
t.verbose = false
|
13
|
+
end
|
data/examples/mapping.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# This sample requires the yahoo finance gem
|
2
|
+
require 'yahoofinance'
|
3
|
+
require 'indicator'
|
4
|
+
|
5
|
+
def bar_to_hash r
|
6
|
+
h = {}
|
7
|
+
h[:date] = Date.parse(r[0])
|
8
|
+
h[:open] = r[1].to_f
|
9
|
+
h[:high] = r[2].to_f
|
10
|
+
h[:low] = r[3].to_f
|
11
|
+
h[:close] = r[4].to_f
|
12
|
+
h[:volume] = r[5].to_f
|
13
|
+
h
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_data_100 ticker
|
17
|
+
YahooFinance::get_historical_quotes_days(ticker, 100)
|
18
|
+
end
|
19
|
+
|
20
|
+
puts 'Retrieving historical data'
|
21
|
+
cba = get_data_100("CBA.AX").inject([]) { |lst, q| lst << bar_to_hash(q) }
|
22
|
+
nab = get_data_100("NAB.AX").inject([]) { |lst, q| lst << bar_to_hash(q) }
|
23
|
+
|
24
|
+
sma = Indicator.create_named :sma_13
|
25
|
+
sma_results = sma.run(Indicator::DataMapper::Map.new(cba, :open))
|
26
|
+
|
27
|
+
ema = Indicator.create_named :ema_25
|
28
|
+
ema.default_getter = :high
|
29
|
+
ema_results = sma.run cba
|
30
|
+
|
31
|
+
sub = Indicator.create :sub
|
32
|
+
sub.default_getter = :low
|
33
|
+
sub_results = sub.run cba, nab
|
34
|
+
|
35
|
+
adx = Indicator.create :adx
|
36
|
+
adx_results = adx.run(
|
37
|
+
new_map(nab, :open),
|
38
|
+
new_map(nab, :high),
|
39
|
+
new_map(nab, :low),
|
40
|
+
new_map(nab, :close))
|
41
|
+
|
42
|
+
# Or the default mapping
|
43
|
+
adx_results = adx.run nab
|
data/lib/indicator.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'talib_ruby'
|
2
|
+
require 'indicator/data_mapper'
|
3
|
+
require 'indicator/base'
|
4
|
+
require 'indicator/auto_gen'
|
5
|
+
|
6
|
+
module Indicator
|
7
|
+
|
8
|
+
class UnknownIndicator < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.contains? name
|
12
|
+
not Indicator.get(name).nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.get name
|
16
|
+
name = name.to_s
|
17
|
+
Base.indicators.find do |klass|
|
18
|
+
klass_name = klass.name.split('::')[-1]
|
19
|
+
klass_name.casecmp(name) == 0
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.create type, *args
|
24
|
+
klass = Indicator.get type
|
25
|
+
raise UnknownIndicator,type if klass.nil?
|
26
|
+
|
27
|
+
klass.send(:new, *args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.create_named name, *args
|
31
|
+
sp = name.to_s.split "_"
|
32
|
+
|
33
|
+
type = sp.first.to_sym
|
34
|
+
|
35
|
+
args = sp.drop(1).collect { |a| a.to_i } if args.empty?
|
36
|
+
|
37
|
+
obj = Indicator.create type, *args
|
38
|
+
obj.name = name
|
39
|
+
obj
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.list
|
43
|
+
Indicator::Base.indicators
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.info type
|
47
|
+
klass = Indicator.get type
|
48
|
+
raise UnknownIndicator,type if klass.nil?
|
49
|
+
"""Indicator: #{klass.name}
|
50
|
+
Arguments: #{klass.arguments}
|
51
|
+
Input Count: #{klass.inputs.count}
|
52
|
+
Inputs: #{klass.inputs}
|
53
|
+
Output Count: #{klass.outputs.count}
|
54
|
+
Outputs: #{klass.outputs}"""
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
module Indicator
|
2
|
+
module AutoGen
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'indicator/auto_gen/acos'
|
7
|
+
require 'indicator/auto_gen/ad'
|
8
|
+
require 'indicator/auto_gen/add'
|
9
|
+
require 'indicator/auto_gen/ad_osc'
|
10
|
+
require 'indicator/auto_gen/adx'
|
11
|
+
require 'indicator/auto_gen/adxr'
|
12
|
+
require 'indicator/auto_gen/apo'
|
13
|
+
require 'indicator/auto_gen/aroon'
|
14
|
+
require 'indicator/auto_gen/aroon_osc'
|
15
|
+
require 'indicator/auto_gen/asin'
|
16
|
+
require 'indicator/auto_gen/atan'
|
17
|
+
require 'indicator/auto_gen/atr'
|
18
|
+
require 'indicator/auto_gen/avg_price'
|
19
|
+
require 'indicator/auto_gen/bbands'
|
20
|
+
require 'indicator/auto_gen/beta'
|
21
|
+
require 'indicator/auto_gen/bop'
|
22
|
+
require 'indicator/auto_gen/cci'
|
23
|
+
require 'indicator/auto_gen/cdl2_crows'
|
24
|
+
require 'indicator/auto_gen/cdl3_black_crows'
|
25
|
+
require 'indicator/auto_gen/cdl3_inside'
|
26
|
+
require 'indicator/auto_gen/cdl3_line_strike'
|
27
|
+
require 'indicator/auto_gen/cdl3_outside'
|
28
|
+
require 'indicator/auto_gen/cdl3_stars_in_south'
|
29
|
+
require 'indicator/auto_gen/cdl3_white_soldiers'
|
30
|
+
require 'indicator/auto_gen/cdl_abandoned_baby'
|
31
|
+
require 'indicator/auto_gen/cdl_advance_block'
|
32
|
+
require 'indicator/auto_gen/cdl_belt_hold'
|
33
|
+
require 'indicator/auto_gen/cdl_breakaway'
|
34
|
+
require 'indicator/auto_gen/cdl_closing_marubozu'
|
35
|
+
require 'indicator/auto_gen/cdl_conceal_babys_wall'
|
36
|
+
require 'indicator/auto_gen/cdl_counter_attack'
|
37
|
+
require 'indicator/auto_gen/cdl_dark_cloud_cover'
|
38
|
+
require 'indicator/auto_gen/cdl_doji'
|
39
|
+
require 'indicator/auto_gen/cdl_doji_star'
|
40
|
+
require 'indicator/auto_gen/cdl_dragonfly_doji'
|
41
|
+
require 'indicator/auto_gen/cdl_engulfing'
|
42
|
+
require 'indicator/auto_gen/cdl_evening_doji_star'
|
43
|
+
require 'indicator/auto_gen/cdl_evening_star'
|
44
|
+
require 'indicator/auto_gen/cdl_gap_side_side_white'
|
45
|
+
require 'indicator/auto_gen/cdl_gravestone_doji'
|
46
|
+
require 'indicator/auto_gen/cdl_hammer'
|
47
|
+
require 'indicator/auto_gen/cdl_hanging_man'
|
48
|
+
require 'indicator/auto_gen/cdl_harami'
|
49
|
+
require 'indicator/auto_gen/cdl_harami_cross'
|
50
|
+
require 'indicator/auto_gen/cdl_hign_wave'
|
51
|
+
require 'indicator/auto_gen/cdl_hikkake'
|
52
|
+
require 'indicator/auto_gen/cdl_hikkake_mod'
|
53
|
+
require 'indicator/auto_gen/cdl_homing_pigeon'
|
54
|
+
require 'indicator/auto_gen/cdl_identical3_crows'
|
55
|
+
require 'indicator/auto_gen/cdl_in_neck'
|
56
|
+
require 'indicator/auto_gen/cdl_inverted_hammer'
|
57
|
+
require 'indicator/auto_gen/cdl_kicking'
|
58
|
+
require 'indicator/auto_gen/cdl_kicking_by_length'
|
59
|
+
require 'indicator/auto_gen/cdl_ladder_bottom'
|
60
|
+
require 'indicator/auto_gen/cdl_long_legged_doji'
|
61
|
+
require 'indicator/auto_gen/cdl_long_line'
|
62
|
+
require 'indicator/auto_gen/cdl_marubozu'
|
63
|
+
require 'indicator/auto_gen/cdl_matching_low'
|
64
|
+
require 'indicator/auto_gen/cdl_mat_hold'
|
65
|
+
require 'indicator/auto_gen/cdl_morning_doji_star'
|
66
|
+
require 'indicator/auto_gen/cdl_morning_star'
|
67
|
+
require 'indicator/auto_gen/cdl_on_neck'
|
68
|
+
require 'indicator/auto_gen/cdl_piercing'
|
69
|
+
require 'indicator/auto_gen/cdl_rickshaw_man'
|
70
|
+
require 'indicator/auto_gen/cdl_rise_fall3_methods'
|
71
|
+
require 'indicator/auto_gen/cdl_seperating_lines'
|
72
|
+
require 'indicator/auto_gen/cdl_shooting_star'
|
73
|
+
require 'indicator/auto_gen/cdl_short_line'
|
74
|
+
require 'indicator/auto_gen/cdl_spinning_top'
|
75
|
+
require 'indicator/auto_gen/cdl_stalled_pattern'
|
76
|
+
require 'indicator/auto_gen/cdl_stick_sandwhich'
|
77
|
+
require 'indicator/auto_gen/cdl_takuri'
|
78
|
+
require 'indicator/auto_gen/cdl_tasuki_gap'
|
79
|
+
require 'indicator/auto_gen/cdl_thrusting'
|
80
|
+
require 'indicator/auto_gen/cdl_tristar'
|
81
|
+
require 'indicator/auto_gen/cdl_unique3_river'
|
82
|
+
require 'indicator/auto_gen/cdl_upside_gap2_crows'
|
83
|
+
require 'indicator/auto_gen/cdl_x_side_gap3_methods'
|
84
|
+
require 'indicator/auto_gen/ceil'
|
85
|
+
require 'indicator/auto_gen/cmo'
|
86
|
+
require 'indicator/auto_gen/correl'
|
87
|
+
require 'indicator/auto_gen/cos'
|
88
|
+
require 'indicator/auto_gen/cosh'
|
89
|
+
require 'indicator/auto_gen/dema'
|
90
|
+
require 'indicator/auto_gen/div'
|
91
|
+
require 'indicator/auto_gen/dx'
|
92
|
+
require 'indicator/auto_gen/ema'
|
93
|
+
require 'indicator/auto_gen/exp'
|
94
|
+
require 'indicator/auto_gen/floor'
|
95
|
+
require 'indicator/auto_gen/ht_dc_period'
|
96
|
+
require 'indicator/auto_gen/ht_dc_phase'
|
97
|
+
require 'indicator/auto_gen/ht_phasor'
|
98
|
+
require 'indicator/auto_gen/ht_sine'
|
99
|
+
require 'indicator/auto_gen/ht_trendline'
|
100
|
+
require 'indicator/auto_gen/ht_trend_mode'
|
101
|
+
require 'indicator/auto_gen/kama'
|
102
|
+
require 'indicator/auto_gen/linear_reg'
|
103
|
+
require 'indicator/auto_gen/linear_reg_angle'
|
104
|
+
require 'indicator/auto_gen/linear_reg_intercept'
|
105
|
+
require 'indicator/auto_gen/linear_reg_slope'
|
106
|
+
require 'indicator/auto_gen/ln'
|
107
|
+
require 'indicator/auto_gen/log10'
|
108
|
+
require 'indicator/auto_gen/moving_average'
|
109
|
+
require 'indicator/auto_gen/macd'
|
110
|
+
require 'indicator/auto_gen/macd_ext'
|
111
|
+
require 'indicator/auto_gen/macd_fix'
|
112
|
+
require 'indicator/auto_gen/mama'
|
113
|
+
require 'indicator/auto_gen/moving_average_variable_period'
|
114
|
+
require 'indicator/auto_gen/max'
|
115
|
+
require 'indicator/auto_gen/max_index'
|
116
|
+
require 'indicator/auto_gen/med_price'
|
117
|
+
require 'indicator/auto_gen/mfi'
|
118
|
+
require 'indicator/auto_gen/mid_point'
|
119
|
+
require 'indicator/auto_gen/mid_price'
|
120
|
+
require 'indicator/auto_gen/min'
|
121
|
+
require 'indicator/auto_gen/min_index'
|
122
|
+
require 'indicator/auto_gen/min_max'
|
123
|
+
require 'indicator/auto_gen/min_max_index'
|
124
|
+
require 'indicator/auto_gen/minus_di'
|
125
|
+
require 'indicator/auto_gen/minus_dm'
|
126
|
+
require 'indicator/auto_gen/mom'
|
127
|
+
require 'indicator/auto_gen/mult'
|
128
|
+
require 'indicator/auto_gen/natr'
|
129
|
+
require 'indicator/auto_gen/obv'
|
130
|
+
require 'indicator/auto_gen/plus_di'
|
131
|
+
require 'indicator/auto_gen/plus_dm'
|
132
|
+
require 'indicator/auto_gen/ppo'
|
133
|
+
require 'indicator/auto_gen/roc'
|
134
|
+
require 'indicator/auto_gen/roc_p'
|
135
|
+
require 'indicator/auto_gen/roc_r'
|
136
|
+
require 'indicator/auto_gen/roc_r100'
|
137
|
+
require 'indicator/auto_gen/rsi'
|
138
|
+
require 'indicator/auto_gen/sar'
|
139
|
+
require 'indicator/auto_gen/sar_ext'
|
140
|
+
require 'indicator/auto_gen/sin'
|
141
|
+
require 'indicator/auto_gen/sinh'
|
142
|
+
require 'indicator/auto_gen/sma'
|
143
|
+
require 'indicator/auto_gen/sqrt'
|
144
|
+
require 'indicator/auto_gen/std_dev'
|
145
|
+
require 'indicator/auto_gen/stoch'
|
146
|
+
require 'indicator/auto_gen/stoch_f'
|
147
|
+
require 'indicator/auto_gen/stoch_rsi'
|
148
|
+
require 'indicator/auto_gen/sub'
|
149
|
+
require 'indicator/auto_gen/sum'
|
150
|
+
require 'indicator/auto_gen/t3'
|
151
|
+
require 'indicator/auto_gen/tan'
|
152
|
+
require 'indicator/auto_gen/tanh'
|
153
|
+
require 'indicator/auto_gen/tema'
|
154
|
+
require 'indicator/auto_gen/true_range'
|
155
|
+
require 'indicator/auto_gen/trima'
|
156
|
+
require 'indicator/auto_gen/trix'
|
157
|
+
require 'indicator/auto_gen/tsf'
|
158
|
+
require 'indicator/auto_gen/typ_price'
|
159
|
+
require 'indicator/auto_gen/ult_osc'
|
160
|
+
require 'indicator/auto_gen/variance'
|
161
|
+
require 'indicator/auto_gen/wcl_price'
|
162
|
+
require 'indicator/auto_gen/will_r'
|
163
|
+
require 'indicator/auto_gen/wma'
|