unageanu-jiji 1.1.4 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. data/ChangeLog +24 -1
  2. data/README +0 -0
  3. data/base/shared_lib/moving_average.rb +34 -33
  4. data/base/shared_lib/system/cross.rb +76 -0
  5. data/base/shared_lib/system/position_manager.rb +160 -0
  6. data/base/shared_lib/system/signal.rb +347 -0
  7. data/html/css/datatable.css +4 -4
  8. data/html/css/default.css +57 -5
  9. data/html/css/treeview.css +205 -0
  10. data/html/img/bin_closed.png +0 -0
  11. data/html/img/bin_empty.png +0 -0
  12. data/html/img/button_add_small.gif +0 -0
  13. data/html/img/button_add_small_gray.gif +0 -0
  14. data/html/img/button_add_small_over.gif +0 -0
  15. data/html/img/button_mkcol.gif +0 -0
  16. data/html/img/button_mkcol_gray.gif +0 -0
  17. data/html/img/button_mkcol_over.gif +0 -0
  18. data/html/img/button_remove_small.gif +0 -0
  19. data/html/img/button_remove_small_gray.gif +0 -0
  20. data/html/img/button_remove_small_over.gif +0 -0
  21. data/html/img/button_rename.gif +0 -0
  22. data/html/img/button_rename_gray.gif +0 -0
  23. data/html/img/button_rename_over.gif +0 -0
  24. data/html/img/control_play.png +0 -0
  25. data/html/img/control_play_blue.png +0 -0
  26. data/html/img/folder.png +0 -0
  27. data/html/img/folder_brick.png +0 -0
  28. data/html/img/folder_user.png +0 -0
  29. data/html/{js/codepress/images → img}/line-numbers.png +0 -0
  30. data/html/img/page_white_ruby.png +0 -0
  31. data/html/img/sidebar_agent_edit.png +0 -0
  32. data/html/img/sidebar_agent_edit_over.png +0 -0
  33. data/html/img/sidebar_agent_edit_s.png +0 -0
  34. data/html/img/yui/treeview-sprite.gif +0 -0
  35. data/html/index.html +34 -18
  36. data/html/js/agent-editor/agent-editor-page.js +324 -0
  37. data/html/js/agent-editor/agent-editor.js +363 -0
  38. data/html/js/agent-editor/agent-list-tree.js +251 -0
  39. data/html/js/agent-selector.js +23 -28
  40. data/html/js/app.js +63 -91
  41. data/html/js/bt-create-page.js +25 -19
  42. data/html/js/container-min.js +1 -1
  43. data/html/js/container.js +944 -0
  44. data/html/js/edit_area/autocompletion.js +11 -13
  45. data/html/js/edit_area/edit_area.css +79 -40
  46. data/html/js/edit_area/edit_area.js +255 -226
  47. data/html/js/edit_area/edit_area_compressor.php +4 -4
  48. data/html/js/edit_area/edit_area_full.gz +0 -0
  49. data/html/js/edit_area/edit_area_full.js +31 -31
  50. data/html/js/edit_area/edit_area_full_with_plugins.gz +0 -0
  51. data/html/js/edit_area/edit_area_full_with_plugins.js +31 -31
  52. data/html/js/edit_area/edit_area_functions.js +448 -341
  53. data/html/js/edit_area/edit_area_loader.js +409 -397
  54. data/html/js/edit_area/elements_functions.js +120 -123
  55. data/html/js/edit_area/highlight.js +305 -197
  56. data/html/js/edit_area/images/goto.png +0 -0
  57. data/html/js/edit_area/images/help.png +0 -0
  58. data/html/js/edit_area/images/redo.png +0 -0
  59. data/html/js/edit_area/images/save.png +0 -0
  60. data/html/js/edit_area/images/search.png +0 -0
  61. data/html/js/edit_area/images/undo.png +0 -0
  62. data/html/js/edit_area/images/word_wrap.gif +0 -0
  63. data/html/js/edit_area/keyboard.js +5 -5
  64. data/html/js/edit_area/langs/bg.js +73 -0
  65. data/html/js/edit_area/langs/cs.js +2 -0
  66. data/html/js/edit_area/langs/de.js +2 -0
  67. data/html/js/edit_area/langs/dk.js +2 -0
  68. data/html/js/edit_area/langs/en.js +2 -0
  69. data/html/js/edit_area/langs/eo.js +2 -0
  70. data/html/js/edit_area/langs/es.js +2 -0
  71. data/html/js/edit_area/langs/fi.js +67 -0
  72. data/html/js/edit_area/langs/fr.js +2 -0
  73. data/html/js/edit_area/langs/hr.js +2 -0
  74. data/html/js/edit_area/langs/it.js +2 -0
  75. data/html/js/edit_area/langs/ja.js +2 -0
  76. data/html/js/edit_area/langs/mk.js +2 -0
  77. data/html/js/edit_area/langs/nl.js +2 -0
  78. data/html/js/edit_area/langs/pl.js +2 -0
  79. data/html/js/edit_area/langs/pt.js +2 -0
  80. data/html/js/edit_area/langs/ru.js +2 -0
  81. data/html/js/edit_area/langs/sk.js +2 -0
  82. data/html/js/edit_area/langs/zh.js +67 -0
  83. data/html/js/edit_area/manage_area.js +362 -205
  84. data/html/js/edit_area/plugins/charmap/langs/bg.js +12 -0
  85. data/html/js/edit_area/plugins/charmap/langs/zh.js +6 -0
  86. data/html/js/edit_area/plugins/test/langs/bg.js +10 -0
  87. data/html/js/edit_area/plugins/test/langs/zh.js +4 -0
  88. data/html/js/edit_area/reg_syntax/java.js +56 -0
  89. data/html/js/edit_area/reg_syntax/ruby.js +9 -9
  90. data/html/js/edit_area/reg_syntax.js +15 -13
  91. data/html/js/edit_area/regexp.js +36 -32
  92. data/html/js/edit_area/resize_area.js +43 -47
  93. data/html/js/edit_area/search_replace.js +29 -29
  94. data/html/js/edit_area/template.html +6 -4
  95. data/html/js/json-broker-client.js +23 -17
  96. data/html/js/result-page.js +107 -57
  97. data/html/js/rt-setting-page.js +38 -15
  98. data/html/js/sidebar.js +41 -27
  99. data/html/js/templates.js +167 -32
  100. data/html/js/utils.js +143 -7
  101. data/html/js/yui/treeview.js +3671 -0
  102. data/html/swf/chart.swf +0 -0
  103. data/html/test/agent_editor_spec.js +815 -0
  104. data/html/test/index.html +40 -0
  105. data/html/test/jsspec/JSSpec.css +224 -0
  106. data/html/test/jsspec/JSSpec.js +1549 -0
  107. data/html/test/jsspec/diff_match_patch.js +1 -0
  108. data/html/test/utils_spec.js +111 -0
  109. data/lib/jiji/agent/agent.rb +69 -12
  110. data/lib/jiji/agent/agent_manager.rb +18 -12
  111. data/lib/jiji/agent/agent_registry.rb +35 -121
  112. data/lib/jiji/collector.rb +16 -6
  113. data/lib/jiji/command.rb +46 -5
  114. data/lib/jiji/dao/file_system_dao.rb +158 -0
  115. data/lib/jiji/dao/timed_data_dao.rb +2 -0
  116. data/lib/jiji/dao/trade_result_dao.rb +1 -1
  117. data/lib/jiji/error.rb +24 -8
  118. data/lib/jiji/migration/migrator1_2_0.rb +67 -0
  119. data/lib/jiji/models.rb +82 -24
  120. data/lib/jiji/operator.rb +55 -51
  121. data/lib/jiji/output.rb +85 -29
  122. data/lib/jiji/output_manager.rb +84 -0
  123. data/lib/jiji/plugin/embedded/single_click_client.rb +2 -2
  124. data/lib/jiji/plugin/securities_plugin.rb +0 -1
  125. data/lib/jiji/process.rb +229 -208
  126. data/lib/jiji/process_manager.rb +190 -96
  127. data/lib/jiji/registry.rb +87 -19
  128. data/lib/jiji/server.rb +1 -0
  129. data/lib/jiji/service/agent_service.rb +147 -48
  130. data/lib/jiji/service/output_service.rb +37 -17
  131. data/lib/jiji/service/process_service.rb +3 -5
  132. data/lib/jiji/service/trade_result_service.rb +4 -5
  133. data/lib/jiji/util/file_lock.rb +4 -4
  134. data/lib/jiji/util/include_proxy.rb +17 -0
  135. data/lib/jiji/util/json_broker.rb +6 -4
  136. data/lib/jiji/util/util.rb +1 -1
  137. data/swf/chart/fx/chart/Chart.as +7 -0
  138. data/swf/chart/fx/chart/ui/graph/GraphManager.as +15 -2
  139. data/test/ProcessTest/agents/foo.rb +10 -0
  140. data/test/ProcessTest/conf/configuration.yaml +3 -0
  141. data/test/agent/agent_tests.rb +10 -0
  142. data/test/agent/test_AgentManager.rb +28 -12
  143. data/test/agent/test_AgentRegistry.rb +194 -99
  144. data/test/agent/test_PeriodicallyAgent.rb +1 -2
  145. data/test/agent/test_Permitter.rb +1 -2
  146. data/test/all_tests.rb +7 -19
  147. data/test/dao/dao_tests.rb +9 -0
  148. data/test/dao/test_FileSystemDao.rb +431 -0
  149. data/test/dao/test_RateDao.rb +5 -7
  150. data/test/dao/test_TradeResultDao.rb +1 -2
  151. data/test/migration/migration_tests.rb +10 -0
  152. data/test/migration/migrator1_2_0test_data/basic/out/M2NlOTA2ODEtZDdlNi00NWU1LWIwNDQtMjBmODY2ZGNkNzBj/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  153. data/test/migration/migrator1_2_0test_data/basic/out/MDVhYzcxMjYtMGFlMS00Mzk0LWEyNmUtYjVjZjgwNDA0ZmE2/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  154. data/test/migration/migrator1_2_0test_data/basic/out/MzA1YTk0NDgtNzhjNi00NDk3LTk2NTktYzE1ZjBhNzdiYjNj/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  155. data/test/migration/migrator1_2_0test_data/basic/out/YjRkOTI1MzEtZTM4MS00YjQwLTg1ZTQtMWFmZDRlNDUwMzBm/56e75YuV5bmz5Z2H57ea/meta.yaml +9 -0
  156. data/test/migration/migrator1_2_0test_data/basic/props.yaml +85 -0
  157. data/test/migration/migrator1_2_0test_data/illegal_props/out/M2NlOTA2ODEtZDdlNi00NWU1LWIwNDQtMjBmODY2ZGNkNzBj/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  158. data/test/migration/migrator1_2_0test_data/illegal_props/out/MDVhYzcxMjYtMGFlMS00Mzk0LWEyNmUtYjVjZjgwNDA0ZmE2/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  159. data/test/migration/migrator1_2_0test_data/illegal_props/out/MzA1YTk0NDgtNzhjNi00NDk3LTk2NTktYzE1ZjBhNzdiYjNj/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  160. data/test/migration/migrator1_2_0test_data/illegal_props/out/YjRkOTI1MzEtZTM4MS00YjQwLTg1ZTQtMWFmZDRlNDUwMzBm/56e75YuV5bmz5Z2H57ea/meta.yaml +9 -0
  161. data/test/migration/migrator1_2_0test_data/illegal_props/props.yaml +1 -0
  162. data/test/migration/migrator1_2_0test_data/no_outs/props.yaml +85 -0
  163. data/test/migration/migrator1_2_0test_data/no_props/out/M2NlOTA2ODEtZDdlNi00NWU1LWIwNDQtMjBmODY2ZGNkNzBj/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  164. data/test/migration/migrator1_2_0test_data/no_props/out/MDVhYzcxMjYtMGFlMS00Mzk0LWEyNmUtYjVjZjgwNDA0ZmE2/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  165. data/test/migration/migrator1_2_0test_data/no_props/out/MzA1YTk0NDgtNzhjNi00NDk3LTk2NTktYzE1ZjBhNzdiYjNj/56e75YuV5bmz5Z2H57ea/meta.yaml +8 -0
  166. data/test/migration/migrator1_2_0test_data/no_props/out/YjRkOTI1MzEtZTM4MS00YjQwLTg1ZTQtMWFmZDRlNDUwMzBm/56e75YuV5bmz5Z2H57ea/meta.yaml +9 -0
  167. data/test/migration/test_Migrator.rb +1 -1
  168. data/test/migration/test_Migrator1_0_3.rb +1 -1
  169. data/test/migration/test_Migrator1_1_0.rb +1 -1
  170. data/test/migration/test_Migrator1_2_0.rb +94 -0
  171. data/test/plugin/embedded/test_SingleClickClient.rb +1 -2
  172. data/test/plugin/plugin_tests.rb +8 -0
  173. data/test/plugin/test_Loader.rb +1 -1
  174. data/test/shared/rate.csv +144 -0
  175. data/test/shared/shared_tests.rb +9 -0
  176. data/test/shared/test_Cross.rb +144 -0
  177. data/test/shared/test_PositionManager.rb +285 -0
  178. data/test/shared/test_Signal.rb +65 -0
  179. data/test/test_Output.rb +28 -21
  180. data/test/test_OutputManager.rb +162 -0
  181. data/test/test_Output_registry.rb +6 -17
  182. data/test/test_Process.rb +434 -222
  183. data/test/test_ProcessManager.rb +458 -101
  184. data/test/test_utils.rb +71 -8
  185. data/test/util/test_BlockToSession.rb +1 -2
  186. data/test/util/test_CSV.rb +1 -2
  187. data/test/util/test_SynchronizeInterceptor.rb +1 -2
  188. data/test/util/util_tests.rb +9 -0
  189. metadata +127 -47
  190. data/html/js/agent-editor-page.js +0 -440
  191. data/html/js/codepress/codepress.css +0 -21
  192. data/html/js/codepress/codepress.html +0 -35
  193. data/html/js/codepress/codepress.js +0 -138
  194. data/html/js/codepress/engines/gecko.js +0 -293
  195. data/html/js/codepress/engines/khtml.js +0 -0
  196. data/html/js/codepress/engines/msie.js +0 -304
  197. data/html/js/codepress/engines/older.js +0 -0
  198. data/html/js/codepress/engines/opera.js +0 -260
  199. data/html/js/codepress/images/line-numbers.gif +0 -0
  200. data/html/js/codepress/index.html +0 -443
  201. data/html/js/codepress/languages/asp.css +0 -71
  202. data/html/js/codepress/languages/asp.js +0 -117
  203. data/html/js/codepress/languages/autoit.css +0 -13
  204. data/html/js/codepress/languages/autoit.js +0 -32
  205. data/html/js/codepress/languages/csharp.css +0 -9
  206. data/html/js/codepress/languages/csharp.js +0 -25
  207. data/html/js/codepress/languages/css.css +0 -10
  208. data/html/js/codepress/languages/css.js +0 -23
  209. data/html/js/codepress/languages/generic.css +0 -9
  210. data/html/js/codepress/languages/generic.js +0 -25
  211. data/html/js/codepress/languages/html.css +0 -13
  212. data/html/js/codepress/languages/html.js +0 -59
  213. data/html/js/codepress/languages/java.css +0 -7
  214. data/html/js/codepress/languages/java.js +0 -24
  215. data/html/js/codepress/languages/javascript.css +0 -8
  216. data/html/js/codepress/languages/javascript.js +0 -30
  217. data/html/js/codepress/languages/perl.css +0 -11
  218. data/html/js/codepress/languages/perl.js +0 -27
  219. data/html/js/codepress/languages/php.css +0 -12
  220. data/html/js/codepress/languages/php.js +0 -61
  221. data/html/js/codepress/languages/ruby.css +0 -10
  222. data/html/js/codepress/languages/ruby.js +0 -26
  223. data/html/js/codepress/languages/sql.css +0 -10
  224. data/html/js/codepress/languages/sql.js +0 -30
  225. data/html/js/codepress/languages/text.css +0 -5
  226. data/html/js/codepress/languages/text.js +0 -9
  227. data/html/js/codepress/languages/vbscript.css +0 -71
  228. data/html/js/codepress/languages/vbscript.js +0 -117
  229. data/html/js/codepress/languages/xsl.css +0 -15
  230. data/html/js/codepress/languages/xsl.js +0 -103
  231. data/html/js/codepress/license.txt +0 -458
  232. data/html/js/edit_area/images/Thumbs.db +0 -0
  233. data/test/plugin/test_gem/a/lib/jiji_plugin_test.rb +0 -6
@@ -0,0 +1,285 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $: << "../lib"
4
+ $: << "../base/shared_lib"
5
+
6
+ require 'rubygems'
7
+ require 'runit/testcase'
8
+ require 'runit/cui/testrunner'
9
+ require 'system/position_manager'
10
+ require 'set'
11
+ require 'jiji/operator'
12
+
13
+ class PositionManagerTest < RUNIT::TestCase
14
+
15
+ # 前準備
16
+ def setup
17
+ @op = MockOperator.new
18
+ @pm = PositionManager.new(@op)
19
+ end
20
+
21
+ # 後始末
22
+ def teardown
23
+ end
24
+
25
+
26
+ # 基本動作のテスト
27
+ def test_commit
28
+
29
+ assert_equals( @pm.total_profit_or_loss, 0)
30
+
31
+ #each
32
+ assert_equals( @pm.map.size, 0)
33
+
34
+ #commit
35
+ @pm.commit_by {|p| true }
36
+ assert_equals(@op.commited.size, 0)
37
+ @op.commited.clear
38
+ @pm.commit_all
39
+ assert_equals(@op.commited.size, 0)
40
+
41
+ #ポジションを追加
42
+ ps = [
43
+ Position.new( 1, 100, JIJI::Position::STATE_START ),
44
+ Position.new( 2, 200, JIJI::Position::STATE_START ),
45
+ Position.new( 3, 300, JIJI::Position::STATE_START ),
46
+ Position.new( 4, 400, JIJI::Position::STATE_START )
47
+ ]
48
+ ps.each{|p| @op << p }
49
+
50
+ assert_equals( @pm.total_profit_or_loss, 1000)
51
+
52
+ # each
53
+ assert_equals( @pm.map.size, 4)
54
+
55
+ @pm.commit_by {|p| p.position_id == 1 }
56
+ assert_equals(@op.commited.size, 1)
57
+ @pm.commit_all
58
+ assert_equals(@op.commited.size, 4)
59
+ end
60
+
61
+ # strategyの追加、削除のテスト
62
+ def test_strategy
63
+ #ポジションを追加
64
+ p = Position.new( 1, 0, JIJI::Position::STATE_START )
65
+ @op << p
66
+
67
+ assert( !@pm.registered?(p.position_id) )
68
+ assert_equals( @pm.get_registered_strategy(p.position_id).size, 0 )
69
+
70
+ #損切り対象としてマーク
71
+ @pm.register_loss_cut( p.position_id, -1000 )
72
+ @pm.register_trailing_stop( p.position_id, -1000 )
73
+ @pm.register( p.position_id, Object.new )
74
+
75
+ registerd = @pm.get_registered_strategy(p.position_id)
76
+ assert( @pm.registered?(p.position_id) )
77
+ assert_equals( registerd.size, 3 )
78
+
79
+ #削除
80
+ @pm.unregister( p.position_id, registerd[1] )
81
+ assert( @pm.registered?(p.position_id) )
82
+ assert_equals( @pm.get_registered_strategy(p.position_id).size, 2 )
83
+
84
+ @pm.unregister_all( p.position_id )
85
+ assert( !@pm.registered?(p.position_id) )
86
+ assert_equals( @pm.get_registered_strategy(p.position_id).size, 0 )
87
+
88
+ end
89
+
90
+ # losscutのテスト
91
+ def test_losscut
92
+
93
+ #ポジションを追加
94
+ ps = [
95
+ Position.new( 1, 0, JIJI::Position::STATE_START ),
96
+ Position.new( 2, 0, JIJI::Position::STATE_START ),
97
+ Position.new( 3, 0, JIJI::Position::STATE_START ),
98
+ Position.new( 4, 0, JIJI::Position::STATE_START )
99
+ ]
100
+ ps.each{|p| @op << p }
101
+
102
+ #損切り対象としてマーク
103
+ @pm.register_loss_cut( ps[0].position_id, -1000 )
104
+ @pm.register_loss_cut( ps[1].position_id, -1000 )
105
+ @pm.register_loss_cut( ps[2].position_id, -1500 )
106
+ @pm.register_loss_cut( ps[3].position_id, -2000 )
107
+ ps.each {|p|
108
+ assert( @pm.registered?(p.position_id) )
109
+ assert_equals( @pm.get_registered_strategy(p.position_id).size, 1 )
110
+ }
111
+
112
+ # 初回のチェック。何もコミットされない
113
+ assert_equals( @pm.check, [] )
114
+
115
+ # ポジションの損失を-1100に設定
116
+ ps.each {|p| p.profit_or_loss = -1100 }
117
+ assert_equals( @pm.check, ps[0..1] ) # 1~2が決済される
118
+ assert( @op.commited.include?( 1 ) )
119
+ assert( @op.commited.include?( 2 ) )
120
+
121
+ assert( !@pm.registered?(ps[0].position_id) )
122
+ assert_equals( @pm.get_registered_strategy(ps[0].position_id).size, 0 )
123
+ assert( !@pm.registered?(ps[1].position_id) )
124
+ assert_equals( @pm.get_registered_strategy(ps[1].position_id).size, 0 )
125
+
126
+ # ポジションの損失を-1500に設定
127
+ ps.each {|p| p.profit_or_loss = -1500 }
128
+ assert_equals( @pm.check, [ps[2]] ) # 3が決済される
129
+ assert( @op.commited.include?( 1 ) )
130
+ assert( @op.commited.include?( 2 ) )
131
+ assert( @op.commited.include?( 3 ) )
132
+
133
+ assert( !@pm.registered?(ps[2].position_id) )
134
+ assert_equals( @pm.get_registered_strategy(ps[2].position_id).size, 0 )
135
+
136
+ end
137
+
138
+ # trailing_stopのテスト
139
+ def test_trailing_stop
140
+
141
+ #ポジションを追加
142
+ ps = [
143
+ Position.new( 1, 0, JIJI::Position::STATE_START ),
144
+ Position.new( 2, 0, JIJI::Position::STATE_START ),
145
+ Position.new( 3, 0, JIJI::Position::STATE_START ),
146
+ Position.new( 4, 0, JIJI::Position::STATE_START )
147
+ ]
148
+ ps.each{|p| @op << p }
149
+
150
+ #トレーリングストップの対象としてマーク
151
+ @pm.register_trailing_stop( ps[0].position_id, -1000 )
152
+ @pm.register_trailing_stop( ps[1].position_id, -1000 )
153
+ @pm.register_trailing_stop( ps[2].position_id, -1500 )
154
+ @pm.register_trailing_stop( ps[3].position_id, -2000 )
155
+ ps.each {|p|
156
+ assert( @pm.registered?(p.position_id) )
157
+ assert_equals( @pm.get_registered_strategy(p.position_id).size, 1 )
158
+ }
159
+
160
+ # 初回のチェック。何もコミットされない
161
+ assert_equals( @pm.check, [] )
162
+
163
+ # ポジションの損失を-1100に設定
164
+ ps.each {|p| p.profit_or_loss = -1100 }
165
+ assert_equals( @pm.check, ps[0..1] ) # 1~2が決済される
166
+ assert( @op.commited.include?( 1 ) )
167
+ assert( @op.commited.include?( 2 ) )
168
+
169
+ assert( !@pm.registered?(ps[0].position_id) )
170
+ assert_equals( @pm.get_registered_strategy(ps[0].position_id).size, 0 )
171
+
172
+ assert( !@pm.registered?(ps[1].position_id) )
173
+ assert_equals( @pm.get_registered_strategy(ps[1].position_id).size, 0 )
174
+
175
+ # ポジションの利益を1500に設定
176
+ ps.each {|p| p.profit_or_loss = 1500 }
177
+ assert_equals( @pm.check, [] ) # 決済はされない
178
+
179
+ # ポジションの利益を2000に設定
180
+ ps.each {|p| p.profit_or_loss = 2000 }
181
+ assert_equals( @pm.check, [] ) # 決済はされない
182
+
183
+ # ポジションの利益を500に設定
184
+ ps.each {|p| p.profit_or_loss = 500 }
185
+ assert_equals( @pm.check, [ps[2]] ) # 最大値から1500下がったので、3が決済される
186
+ assert( @op.commited.include?( 1 ) )
187
+ assert( @op.commited.include?( 2 ) )
188
+ assert( @op.commited.include?( 3 ) )
189
+
190
+ assert( !@pm.registered?(ps[2].position_id) )
191
+ assert_equals( @pm.get_registered_strategy(ps[2].position_id).size, 0 )
192
+
193
+ # ポジションの利益を2500に設定
194
+ ps.each {|p| p.profit_or_loss = 2500 }
195
+ assert_equals( @pm.check, [] ) # 決済はされない
196
+
197
+ # ポジションの利益を500に設定
198
+ ps.each {|p| p.profit_or_loss = 500 }
199
+ assert_equals( @pm.check, [ps[3]] ) # 最大値から1500下がったので、4が決済される
200
+ assert( @op.commited.include?( 1 ) )
201
+ assert( @op.commited.include?( 2 ) )
202
+ assert( @op.commited.include?( 3 ) )
203
+ assert( @op.commited.include?( 4 ) )
204
+
205
+ assert( !@pm.registered?(ps[3].position_id) )
206
+ assert_equals( @pm.get_registered_strategy(ps[3].position_id).size, 0 )
207
+
208
+ end
209
+
210
+ # 損切りロジックが複数登録されている場合のテスト
211
+ def test_multi_stop
212
+ #ポジションを追加
213
+ ps = [
214
+ Position.new( 1, 0, JIJI::Position::STATE_START ),
215
+ Position.new( 2, 0, JIJI::Position::STATE_START )
216
+ ]
217
+ ps.each{|p| @op << p }
218
+
219
+ # ロスカット
220
+ @pm.register_loss_cut( ps[0].position_id, -500 )
221
+ @pm.register_loss_cut( ps[1].position_id, -2000 )
222
+
223
+ @pm.register_trailing_stop( ps[0].position_id, -1000 )
224
+ @pm.register_trailing_stop( ps[1].position_id, -1000 )
225
+ ps.each {|p|
226
+ assert( @pm.registered?(p.position_id) )
227
+ assert_equals( @pm.get_registered_strategy(p.position_id).size, 2 )
228
+ }
229
+
230
+ # 初回のチェック。何もコミットされない
231
+ assert_equals( @pm.check, [] )
232
+
233
+ # ポジションの損失を-200に設定
234
+ ps.each {|p| p.profit_or_loss = -200 }
235
+ assert_equals( @pm.check, [] )
236
+
237
+ # ポジションの利益を200に設定
238
+ ps.each {|p| p.profit_or_loss = 200 }
239
+ assert_equals( @pm.check, [] )
240
+
241
+ # ポジションの損失を-500に設定
242
+ ps.each {|p| p.profit_or_loss = -500 }
243
+ assert_equals( @pm.check, [ps[0]] ) # 1が決済される
244
+ assert( @op.commited.include?( 1 ) )
245
+
246
+ assert( !@pm.registered?(ps[0].position_id) )
247
+ assert_equals( @pm.get_registered_strategy(ps[0].position_id).size, 0 )
248
+
249
+ # ポジションの利益を1500に設定
250
+ ps.each {|p| p.profit_or_loss = 1500 }
251
+ assert_equals( @pm.check, [] ) # 決済はされない
252
+
253
+ # ポジションの利益を2000に設定
254
+ ps.each {|p| p.profit_or_loss = 2000 }
255
+ assert_equals( @pm.check, [] ) # 決済はされない
256
+
257
+ # ポジションの利益を500に設定
258
+ ps.each {|p| p.profit_or_loss = 1000 }
259
+ assert_equals( @pm.check, [ps[1]] ) # 最大値から1000下がったので、2が決済される
260
+ assert( @op.commited.include?( 1 ) )
261
+
262
+ assert( !@pm.registered?(ps[1].position_id) )
263
+ assert_equals( @pm.get_registered_strategy(ps[1].position_id).size, 0 )
264
+ end
265
+
266
+ Position = Struct.new( :position_id, :profit_or_loss, :state )
267
+
268
+ # テスト用のダミーoperator
269
+ class MockOperator
270
+ def initialize
271
+ @positions = {}
272
+ @commited = []
273
+ end
274
+ def <<(p)
275
+ @positions[p.position_id] = p
276
+ end
277
+ def commit( position )
278
+ @commited ||= Set.new
279
+ @commited << position.position_id
280
+ @positions.delete position.position_id
281
+ end
282
+ attr :commited, true
283
+ attr :positions, true
284
+ end
285
+ end
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $: << "../lib"
4
+ $: << "../base/shared_lib"
5
+
6
+ require 'rubygems'
7
+ require 'runit/testcase'
8
+ require 'runit/cui/testrunner'
9
+ require 'system/signal'
10
+ require 'csv'
11
+
12
+ class SignalTest < RUNIT::TestCase
13
+
14
+ # 前準備
15
+ def setup
16
+ end
17
+
18
+ # 後始末
19
+ def teardown
20
+ end
21
+
22
+ def test_signal
23
+ signals = [
24
+ Signal::MovingAverage.new,
25
+ Signal::WeightedMovingAverage.new,
26
+ Signal::ExponentialMovingAverage.new,
27
+ Signal::BollingerBands.new,
28
+ Signal::BollingerBands.new {|datas| Signal.ema(datas) },
29
+ Signal::Vector.new,
30
+ Signal::Momentum.new,
31
+ Signal::MACD.new,
32
+ Signal::RSI.new,
33
+ Signal::ROC.new
34
+ ]
35
+ signals.each {|s|
36
+ puts "\n---" + s.class.to_s
37
+ each {|rate|
38
+ p s.next_data( rate )
39
+ }
40
+ }
41
+
42
+ signals = [
43
+ Signal::DMI.new
44
+ ]
45
+ signals.each {|s|
46
+ puts "\n---" + s.class.to_s
47
+ each_rates {|rate|
48
+ p s.next_data( rate )
49
+ }
50
+ }
51
+ end
52
+
53
+ def each
54
+ each_rates {|r|
55
+ yield r.start
56
+ }
57
+ end
58
+ def each_rates( )
59
+ CSV.foreach( File.dirname(__FILE__) + "/rate.csv" ) {|row|
60
+ yield Rate.new( row[0].to_f, row[1].to_f, row[2].to_f, row[3].to_f )
61
+ }
62
+ end
63
+
64
+ Rate = Struct.new( :start, :end, :max, :min )
65
+ end
data/test/test_Output.rb CHANGED
@@ -8,13 +8,14 @@ require 'jiji/output'
8
8
  require 'fileutils'
9
9
 
10
10
  class OutputTest < RUNIT::TestCase
11
-
11
+
12
12
  def setup
13
13
  @dir = File.dirname(__FILE__) + "/OutputTest.tmp"
14
14
  FileUtils.mkdir_p @dir
15
-
16
- @o = JIJI::Output.new( "テストエージェント", @dir )
15
+
16
+ @o = JIJI::Output.new( "agent_id", @dir )
17
17
  @o.scales = ["30s","1m","1h"]
18
+ @o.agent_name = "エージェント名"
18
19
  end
19
20
 
20
21
  def teardown
@@ -22,14 +23,15 @@ class OutputTest < RUNIT::TestCase
22
23
  end
23
24
 
24
25
  def test_basic
25
-
26
+
26
27
  # 書き込み
27
28
  out = @o.get( "テスト", :event, { :a=>"aaa", :b=>"bbb" } )
28
29
  out2 = @o.get( "テスト2", :graph )
29
-
30
+
31
+ assert_equals @o.agent_name, "エージェント名"
30
32
  assert_equals out.options, { :a=>"aaa", :b=>"bbb", :name=>"テスト", :type=>"event" }
31
33
  assert_equals out2.options, { :name=>"テスト2", :type=>"graph" }
32
-
34
+
33
35
  @o.time = Time.local(2008, 8, 1, 10, 0, 0)
34
36
  out.put( "type", "データ\nデータ" )
35
37
  out2.put 100, 110
@@ -41,7 +43,7 @@ class OutputTest < RUNIT::TestCase
41
43
  @o.time = Time.local(2008, 8, 3, 10, 0, 0)
42
44
  out.put( "type4", "データ4" )
43
45
  out2.put 10, 20
44
-
46
+
45
47
  # データの取得
46
48
  list = read( out, Time.local(2008), Time.local(2008,8,10) )
47
49
  assert_equals list, [
@@ -74,7 +76,7 @@ class OutputTest < RUNIT::TestCase
74
76
  ]
75
77
  list = read( out, Time.local(2008, 9, 2, 12, 10, 0), Time.local(2008, 9, 2, 13, 10, 0) )
76
78
  assert_equals list, []
77
-
79
+
78
80
 
79
81
  list = read( out2, Time.local(2008), Time.local(2008,8,10) )
80
82
  assert_equals list, [
@@ -102,9 +104,9 @@ class OutputTest < RUNIT::TestCase
102
104
  ["200", "220", Time.local(2008, 8, 1, 12, 10, 20).to_i.to_s]
103
105
  ]
104
106
  list = read( out2, Time.local(2008, 8, 1, 13, 10, 0), Time.local(2008, 8, 1, 13, 10, 0) )
105
- assert_equals list, []
106
-
107
-
107
+ assert_equals list, []
108
+
109
+
108
110
  # 出力先一覧
109
111
  @o.each {|k, v|
110
112
  case k
@@ -116,23 +118,27 @@ class OutputTest < RUNIT::TestCase
116
118
  fail
117
119
  end
118
120
  }
119
-
121
+
120
122
  # 出力先を再取得 # 同じインスタンスが返される
121
123
  assert_equals out.object_id, @o.get( "テスト", :event ).object_id
122
124
  assert_equals out2.object_id, @o.get( "テスト2", :event ).object_id
123
-
125
+
124
126
  #プロパティの更新
125
127
  assert_equals out.options, { :a=>"aaa", :b=>"bbb", :name=>"テスト", :type=>"event" }
126
- assert_equals out2.options, { :name=>"テスト2", :type=>"graph" }
127
-
128
+ assert_equals out2.options, { :name=>"テスト2", :type=>"graph" }
129
+
128
130
  out.set_properties( {:x=>"xxx", "a"=>"abc"} ) # 文字列をキーにしてもto_symされたものがキーにされる。
129
131
  out2.set_properties( {:y=>"yyy", :a=>"ab"} )
130
-
132
+
131
133
  assert_equals out.options, {:x=>"xxx", :a=>"abc", :b=>"bbb", :name=>"テスト", :type=>"event" }
132
- assert_equals out2.options, { :y=>"yyy", :a=>"ab", :name=>"テスト2", :type=>"graph" }
133
-
134
+ assert_equals out2.options, { :y=>"yyy", :a=>"ab", :name=>"テスト2", :type=>"graph" }
135
+
136
+ #名前の更新
137
+ @o.agent_name = "エージェント名2"
138
+ assert_equals @o.agent_name, "エージェント名2"
139
+
134
140
  # 再作成 # 既存のデータがロードされる
135
- @o = JIJI::Output.new( "テストエージェント", @dir )
141
+ @o = JIJI::Output.new( "agent_id", @dir )
136
142
  list = read( @o.get("テスト"), nil, nil )
137
143
  assert_equals list, [
138
144
  ["type", "データ\nデータ", Time.local(2008, 8, 1, 10, 0, 0).to_i.to_s],
@@ -146,6 +152,7 @@ class OutputTest < RUNIT::TestCase
146
152
  ["200", "220", Time.local(2008, 8, 1, 12, 10, 20).to_i.to_s],
147
153
  ["10" , "20", Time.local(2008, 8, 3, 10, 0, 0).to_i.to_s]
148
154
  ]
155
+ assert_equals @o.agent_name, "エージェント名2"
149
156
  assert_equals @o.get("テスト").options, {:x=>"xxx", :a=>"abc", :b=>"bbb", :name=>"テスト", :type=>"event" }
150
157
  assert_equals @o.get("テスト2").options, { :y=>"yyy", :a=>"ab", :name=>"テスト2", :type=>"graph" }
151
158
  @o.each {|k, v|
@@ -157,9 +164,9 @@ class OutputTest < RUNIT::TestCase
157
164
  else
158
165
  fail
159
166
  end
160
- }
167
+ }
161
168
  end
162
-
169
+
163
170
  def read( out, start_date, end_date, scale=:raw )
164
171
  list = []
165
172
  out.each( scale, start_date, end_date ) {|row| list << row }
@@ -0,0 +1,162 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $: << "../lib"
4
+
5
+ require 'runit/testcase'
6
+ require 'runit/cui/testrunner'
7
+ require 'jiji/registry'
8
+ require 'fileutils'
9
+
10
+ class OutputManagerTest < RUNIT::TestCase
11
+
12
+ def setup
13
+ @dir = File.dirname(__FILE__) + "/OutputregistryTest"
14
+ @registry = JIJI::Registry.new(@dir , nil)
15
+ @registry.plugin_loader.load
16
+ @mng = @registry.output_manager
17
+ end
18
+
19
+ def teardown
20
+ Test.destry( @registry, @dir )
21
+ FileUtils.rm_rf "#{@dir}/agents"
22
+ FileUtils.rm_rf "#{@dir}/shared_lib"
23
+ end
24
+
25
+ def test_basic
26
+
27
+ #最初はデータがない
28
+ assert_equals read( "pid1" ), []
29
+
30
+ #outputを作成
31
+ out1_1 = @mng.create( "pid1", "agent1" )
32
+ out1_2 = @mng.create( "pid1", "agent2" )
33
+ out2_1 = @mng.create( "pid2", "agent1" )
34
+
35
+ out1_1.agent_name = "agent1_name"
36
+ out1_2.agent_name = "agent2_name"
37
+ out2_1.agent_name = "agent1_name"
38
+
39
+ assert_equals out1_1.agent_id, "agent1"
40
+ assert_equals out1_2.agent_id, "agent2"
41
+ assert_equals out2_1.agent_id, "agent1"
42
+
43
+ #すでに作成済みのものを作成 → 同じインスタンスが返される
44
+ out1_1_2 = @mng.create( "pid1", "agent1" )
45
+ assert_equals out1_1.object_id, out1_1_2.object_id
46
+
47
+ #作成したoutputを取得
48
+ out1_1 = @mng.get( "pid1", "agent1" )
49
+ out1_2 = @mng.get( "pid1", "agent2" )
50
+ out2_1 = @mng.get( "pid2", "agent1" )
51
+
52
+ assert_equals out1_1.agent_id, "agent1"
53
+ assert_equals out1_2.agent_id, "agent2"
54
+ assert_equals out2_1.agent_id, "agent1"
55
+
56
+ #未作成のものを取得→エラー
57
+ assert_error( JIJI::UserError, JIJI::ERROR_NOT_FOUND ) {
58
+ @mng.get( "not found", "agent2" )
59
+ }
60
+ assert_error( JIJI::UserError, JIJI::ERROR_NOT_FOUND ) {
61
+ @mng.get( "pid1", "not found" )
62
+ }
63
+
64
+ #列挙
65
+ assert_equals read( "pid1" ), [
66
+ out1_1, out1_2
67
+ ]
68
+ assert_equals read( "pid2" ), [
69
+ out2_1
70
+ ]
71
+ assert_equals @mng.get_process_map( "pid1" ), {
72
+ "agent1"=>out1_1,
73
+ "agent2"=>out1_2
74
+ }
75
+ assert_equals @mng.get_process_map( "pid2" ), {
76
+ "agent1"=>out2_1
77
+ }
78
+
79
+ #存在しないものを列挙 → 空と同じ扱いになる
80
+ assert_equals read( "not found" ), []
81
+ assert_equals @mng.get_process_map( "not found" ), {}
82
+
83
+
84
+ #削除
85
+ @mng.delete( "pid1", "agent2" )
86
+ assert_equals read( "pid1" ), [
87
+ out1_1
88
+ ]
89
+ assert_equals @mng.get_process_map( "pid1" ), {
90
+ "agent1"=>out1_1
91
+ }
92
+ assert_error( JIJI::UserError, JIJI::ERROR_NOT_FOUND ) {
93
+ @mng.get( "pid1", "agent2" )
94
+ }
95
+
96
+ #再作成
97
+ Test.destry( @registry )
98
+ @registry = JIJI::Registry.new(@dir , nil)
99
+ @registry.plugin_loader.load
100
+ @mng = @registry.output_manager
101
+
102
+ #取得、作成、削除を一通り試す
103
+ out1_1 = @mng.get( "pid1", "agent1" )
104
+ out2_1 = @mng.get( "pid2", "agent1" )
105
+
106
+ assert_equals out1_1.agent_id, "agent1"
107
+ assert_equals out2_1.agent_id, "agent1"
108
+
109
+ assert_equals read( "pid1" ), [
110
+ out1_1
111
+ ]
112
+ assert_equals read( "pid2" ), [
113
+ out2_1
114
+ ]
115
+ assert_equals @mng.get_process_map( "pid1" ), {
116
+ "agent1"=>out1_1
117
+ }
118
+ assert_equals @mng.get_process_map( "pid2" ), {
119
+ "agent1"=>out2_1
120
+ }
121
+
122
+ out1_3 = @mng.create( "pid1", "agent3" )
123
+ out1_3.agent_name = "agent3_name"
124
+ assert_equals out1_3.agent_id, "agent3"
125
+ assert_equals read( "pid1" ), [
126
+ out1_1, out1_3
127
+ ]
128
+ assert_equals @mng.get_process_map( "pid1" ), {
129
+ "agent1"=>out1_1, "agent3"=>out1_3
130
+ }
131
+
132
+ @mng.delete( "pid1", "agent1" )
133
+ assert_equals read( "pid1" ), [
134
+ out1_3
135
+ ]
136
+ assert_equals @mng.get_process_map( "pid1" ), {
137
+ "agent3"=>out1_3
138
+ }
139
+ assert_error( JIJI::UserError, JIJI::ERROR_NOT_FOUND ) {
140
+ @mng.get( "pid1", "agent1" )
141
+ }
142
+
143
+ end
144
+
145
+ #例外が発生することを評価する。
146
+ #codeも評価したいので独自で作成。
147
+ def assert_error( error, code )
148
+ begin
149
+ yield
150
+ fail
151
+ rescue error
152
+ assert_equals $!.code, code
153
+ end
154
+ end
155
+
156
+ #指定されたプロセスのoutput一覧を得る
157
+ def read( process_id )
158
+ list = []
159
+ @mng.each( process_id ) {|row| list << row }
160
+ return list.sort_by {|i| i.agent_id }
161
+ end
162
+ end
@@ -22,32 +22,21 @@ class OutputregistryTest < RUNIT::TestCase
22
22
  end
23
23
 
24
24
  def teardown
25
- begin
26
- @mng.stop
27
- begin
28
- @registry.permitter.close
29
- ensure
30
- @registry.server_logger.close
31
- end
32
- ensure
33
- FileUtils.rm_rf "#{@dir}/agents"
34
- FileUtils.rm_rf "#{@dir}/logs"
35
- FileUtils.rm_rf "#{@dir}/process_logs"
36
- FileUtils.rm_rf "#{@dir}/rate_datas"
37
- FileUtils.rm_rf "#{@dir}/shared_lib"
38
- end
25
+ Test.destry( @registry, @dir )
26
+ FileUtils.rm_rf "#{@dir}/agents"
27
+ FileUtils.rm_rf "#{@dir}/shared_lib"
39
28
  end
40
29
 
41
30
  # outputに2重にアスペクトが適用され、実行時エラーになっていた不具合の回収確認テスト。
42
- def testCreate
31
+ def testCreate
43
32
  out = @registry.output("rmt", "test_agent")
44
33
  out.time = Time.now
45
34
  out.get("foo").put( 1,2 )
46
35
  out.get("foo").put( 2,3 )
47
-
36
+
48
37
  out2 = @registry.output("rmt", "test_agent")
49
38
  out2.time = Time.now
50
39
  out2.get("foo").put( 2,3 )
51
40
  end
52
-
41
+
53
42
  end