Class: ClickClientScrap::FX::FxSession
- Inherits:
-
Object
- Object
- ClickClientScrap::FX::FxSession
- Defined in:
- lib/clickclient_scrap.rb
Overview
FX取引のためのセッションクラス
Client#fx_sessionのブロックの引数として渡されます。詳細はClient#fx_sessionを参照ください。
Class Method Summary collapse
-
.convert_rate(str) ⇒ Object
12.34-35 形式の文字列をbidレート、askレートに変換する。.
Instance Method Summary collapse
-
#cancel_order(order_no) ⇒ Object
注文をキャンセルします。.
-
#initialize(client, links, options = {}) ⇒ FxSession
constructor
A new instance of FxSession.
-
#list_open_interests(currency_pair_code = nil) ⇒ Object
建玉一覧を取得します。.
-
#list_orders(order_condition_code = ClickClientScrap::FX::ORDER_CONDITION_ALL, currency_pair_code = nil) ⇒ Object
注文一覧を取得します。.
-
#list_rates ⇒ Object
レート一覧を取得します。.
-
#list_swaps ⇒ Object
スワップの一覧を取得します。.
-
#logout ⇒ Object
ログアウトします。.
-
#order(currency_pair_code, sell_or_buy, unit, options = {}) ⇒ Object
注文を行います。.
-
#set_expiration(form, options, input_type, input_date) ⇒ Object
- 有効期限を設定する form
- フォーム options
- パラメータ input_type
- 有効期限の種別を入力するinput要素名 input_date
-
有効期限が日付指定の場合に、日付を入力するinput要素名.
-
#settle(open_interest_id, unit, options = {}) ⇒ Object
決済注文を行います。.
Constructor Details
#initialize(client, links, options = {}) ⇒ FxSession
Returns a new instance of FxSession.
187 188 189 190 191 |
# File 'lib/clickclient_scrap.rb', line 187 def initialize( client, links, ={} ) @client = client @links = links @options = end |
Class Method Details
.convert_rate(str) ⇒ Object
12.34-35 形式の文字列をbidレート、askレートに変換する。
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/clickclient_scrap.rb', line 217 def self.convert_rate( str ) #:nodoc: if str =~ /([\d]+)\.([\d]+)\-([\d]+)/ high = $1 low = $2 low2 = $3 bid = high.to_f+(low.to_f/(10**low.length)) ask_low = (low[0...low.length-low2.length] + low2).to_f if low.to_f > ask_low ask_low += 10**low2.length end ask = high.to_f+(ask_low/10**low.length) return [bid,ask] end end |
Instance Method Details
#cancel_order(order_no) ⇒ Object
注文をキャンセルします。
- order_no
-
注文番号
- 戻り値
-
なし
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/clickclient_scrap.rb', line 415 def cancel_order( order_no ) raise "order_no is nil." unless order_no # 注文一覧 result = link_click( "2" ) ClickClientScrap::Client.error( result ) if result.forms.empty? form = result.forms.first form["P002"] = ORDER_CONDITION_ON_ORDER result = @client.submit(form) # 対象となる注文をクリック link = result.links.find {|l| l.href =~ /[^"]*GKEY=([a-zA-Z0-9]*)[^"]*/ && $1 == order_no } raise "illegal order_no. order_no=#{order_no}" unless link result = @client.click(link) ClickClientScrap::Client.error( result ) if result.forms.empty? # キャンセル form = result.forms[1] result = @client.submit(form) ClickClientScrap::Client.error( result ) if result.forms.empty? form = result.forms.first result = @client.submit(form) ClickClientScrap::Client.error( result ) unless result.body.toutf8 =~ /注文取消受付完了/ end |
#list_open_interests(currency_pair_code = nil) ⇒ Object
建玉一覧を取得します。
- currency_pair_code
-
通貨ペアコード
- 戻り値
-
建玉IDをキーとするClickClientScrap::FX::OpenInterestのハッシュ。
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
# File 'lib/clickclient_scrap.rb', line 553 def list_open_interests( currency_pair_code=nil ) result = link_click( "3" ) ClickClientScrap::Client.error( result ) if result.forms.empty? form = result.forms.first form["P001"] = "" # TODO currency_pair_codeでの絞り込み result = @client.submit(form) list = result.body.toutf8.scan( /<a href="[^"]*">([A-Z]{3}\/[A-Z]{3}):([^<]*)<\/a><br>[^;]*;<font[^>]*>([^<]*)<\/font>([\d\.]*)[^\s@]*@([\d\.]*).*?<font[^>]*>([^<]*)<\/font>/m ) tmp = {} list.each {|i| open_interest_id = i[1] pair = to_pair( i[0] ) sell_or_buy = i[2] == "売" ? ClickClientScrap::FX::SELL : ClickClientScrap::FX::BUY count = i[3].to_i rate = i[4].to_f profit_or_loss = i[5].to_i tmp[open_interest_id] = OpenInterest.new(open_interest_id, pair, sell_or_buy, count, rate, profit_or_loss ) } return tmp end |
#list_orders(order_condition_code = ClickClientScrap::FX::ORDER_CONDITION_ALL, currency_pair_code = nil) ⇒ Object
注文一覧を取得します。
- order_condition_code
-
注文状況コード(必須)
- currency_pair_code
-
通貨ペアコード
- 戻り値
-
注文番号をキーとするClickClientScrap::FX::Orderのハッシュ。
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/clickclient_scrap.rb', line 517 def list_orders( order_condition_code=ClickClientScrap::FX::ORDER_CONDITION_ALL, currency_pair_code=nil ) result = link_click( "2" ) ClickClientScrap::Client.error( result ) if result.forms.empty? form = result.forms.first form["P001"] = "" # TODO currency_pair_codeでの絞り込み form["P002"] = order_condition_code result = @client.submit(form) list = result.body.toutf8.scan( /<a href="[^"]*GKEY=([a-zA-Z0-9]*)">([A-Z]{3}\/[A-Z]{3}) ([^<]*)<\/a><br>[^;]*;([^<]*)<font[^>]*>([^<]*)<\/font>([^@]*)@([\d\.]*)([^\s]*) ([^<]*)<br>/m ) tmp = {} list.each {|i| order_no = i[0] order_type = to_order_type_code(i[2]) trade_type = i[3] == "新" ? ClickClientScrap::FX::TRADE_TYPE_NEW : ClickClientScrap::FX::TRADE_TYPE_SETTLEMENT pair = to_pair( i[1] ) sell_or_buy = i[4] == "売" ? ClickClientScrap::FX::SELL : ClickClientScrap::FX::BUY count = pair == :ZARJPY ? i[5].to_i/10 : i[5].to_i rate = i[6].to_f execution_expression = if i[7] == "指" ClickClientScrap::FX::EXECUTION_EXPRESSION_LIMIT_ORDER elsif i[7] == "逆" ClickClientScrap::FX::EXECUTION_EXPRESSION_REVERSE_LIMIT_ORDER else ClickClientScrap::FX::EXECUTION_EXPRESSION_MARKET_ORDER end tmp[order_no] = Order.new( order_no, trade_type, order_type, execution_expression, sell_or_buy, pair, count, rate, i[8]) } return tmp end |
#list_rates ⇒ Object
レート一覧を取得します。
- 戻り値
-
通貨ペアをキーとするClickClientScrap::FX::Rateのハッシュ。
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/clickclient_scrap.rb', line 196 def list_rates result = link_click( "1" ) if !@last_update_time_of_swaps \ || Time.now.to_i - @last_update_time_of_swaps > (@options[:swap_update_interval] || 60*60) @swaps = list_swaps @last_update_time_of_swaps = Time.now.to_i end reg = />([A-Z]+\/[A-Z]+)<\/a>[^\-\.\d]*?([\d]+\.[\d]+\-[\d]+)/ tokens = result.body.toutf8.scan( reg ) ClickClientScrap::Client.error( result ) if !tokens || tokens.empty? return tokens.inject({}) {|r,l| pair = to_pair( l[0] ) swap = @swaps[pair] rate = FxSession.convert_rate l[1] if ( rate && swap ) r[pair] = Rate.new( pair, rate[0], rate[1], swap.sell_swap, swap.buy_swap ) end r } end |
#list_swaps ⇒ Object
スワップの一覧を取得します。
- 戻り値
-
通貨ペアをキーとするClickClientScrap::FX::Swapのハッシュ。
235 236 237 238 239 240 241 242 |
# File 'lib/clickclient_scrap.rb', line 235 def list_swaps result = link_click( "8" ) reg = /<dd>([A-Z]+\/[A-Z]+) <font[^>]*>売<\/font>[^\-\d]*?([\-\d]+)[^\-\d]*<font[^>]*>買<\/font>[^\-\d]*([\-\d]+)[^\-\d]*<\/dd>/ return result.body.toutf8.scan( reg ).inject({}) {|r,l| pair = to_pair( l[0] ) r[pair] = Swap.new( pair, l[1].to_i, l[2].to_i ); r } end |
#logout ⇒ Object
ログアウトします。
575 576 577 578 579 |
# File 'lib/clickclient_scrap.rb', line 575 def logout @client.click( @links.find {|i| i.text == "\303\233\302\270\303\236\302\261\302\263\303\204" }) end |
#order(currency_pair_code, sell_or_buy, unit, options = {}) ⇒ Object
注文を行います。
- currency_pair_code
-
通貨ペアコード(必須)
- sell_or_buy
-
売買区分。ClickClientScrap::FX::BUY,ClickClientScrap::FX::SELLのいずれかを指定します。(必須)
- unit
-
取引数量(必須)
- options
-
注文のオプション。注文方法に応じて以下の情報を設定できます。
- <b>成り行き注文</b> - <tt>:slippage</tt> .. スリッページ (オプション)。何pips以内かを整数で指定します。 - <b>通常注文</b> ※注文レートが設定されていれば通常取引となります。 - <tt>:rate</tt> .. 注文レート(必須) - <tt>:execution_expression</tt> .. 執行条件。ClickClientScrap::FX::EXECUTION_EXPRESSION_LIMIT_ORDER等を指定します(必須) - <tt>:expiration_type</tt> .. 有効期限。ClickClientScrap::FX::EXPIRATION_TYPE_TODAY等を指定します(必須) - <tt>:expiration_date</tt> .. 有効期限が「日付指定(ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED)」の場合の有効期限をDateで指定します。(有効期限が「日付指定」の場合、必須) - <b>OCO注文</b> ※逆指値レートが設定されていればOCO取引となります。 - <tt>:rate</tt> .. 注文レート(必須) - <tt>:stop_order_rate</tt> .. 逆指値レート(必須) - <tt>:expiration_type</tt> .. 有効期限。ClickClientScrap::FX::EXPIRATION_TYPE_TODAY等を指定します(必須) - <tt>:expiration_date</tt> .. 有効期限が「日付指定(ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED)」の場合の有効期限をDateで指定します。(有効期限が「日付指定」の場合、必須) - <b>IFD注文</b> ※決済取引の指定があればIFD取引となります。 - <tt>:rate</tt> .. 注文レート(必須) - <tt>:execution_expression</tt> .. 執行条件。ClickClientScrap::FX::EXECUTION_EXPRESSION_LIMIT_ORDER等を指定します(必須) - <tt>:expiration_type</tt> .. 有効期限。ClickClientScrap::FX::EXPIRATION_TYPE_TODAY等を指定します(必須) - <tt>:expiration_date</tt> .. 有効期限が「日付指定(ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED)」の場合の有効期限をDateで指定します。(有効期限が「日付指定」の場合、必須) - <tt>:settle</tt> .. 決済取引の指定。マップで指定します。 - <tt>:unit</tt> .. 決済取引の取引数量(必須) - <tt>:sell_or_buy</tt> .. 決済取引の売買区分。ClickClientScrap::FX::BUY,ClickClientScrap::FX::SELLのいずれかを指定します。(必須) - <tt>:rate</tt> .. 決済取引の注文レート(必須) - <tt>:execution_expression</tt> .. 決済取引の執行条件。ClickClientScrap::FX::EXECUTION_EXPRESSION_LIMIT_ORDER等を指定します(必須) - <tt>:expiration_type</tt> .. 決済取引の有効期限。ClickClientScrap::FX::EXPIRATION_TYPE_TODAY等を指定します(必須) - <tt>:expiration_date</tt> .. 決済取引の有効期限が「日付指定(ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED)」の場合の有効期限をDateで指定します。(有効期限が「日付指定」の場合、必須) - <b>IFD-OCO注文</b> ※決済取引の指定と逆指値レートの指定があればIFD-OCO取引となります。 - <tt>:rate</tt> .. 注文レート(必須) - <tt>:execution_expression</tt> .. 執行条件。ClickClientScrap::FX::EXECUTION_EXPRESSION_LIMIT_ORDER等を指定します(必須) - <tt>:expiration_type</tt> .. 有効期限。ClickClientScrap::FX::EXPIRATION_TYPE_TODAY等を指定します(必須) - <tt>:expiration_date</tt> .. 有効期限が「日付指定(ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED)」の場合の有効期限をDateで指定します。(有効期限が「日付指定」の場合、必須) - <tt>:settle</tt> .. 決済取引の指定。マップで指定します。 - <tt>:unit</tt> .. 決済取引の取引数量(必須) - <tt>:sell_or_buy</tt> .. 決済取引の売買区分。ClickClientScrap::FX::BUY,ClickClientScrap::FX::SELLのいずれかを指定します。(必須) - <tt>:rate</tt> .. 決済取引の注文レート(必須) - <tt>:stop_order_rate</tt> .. 決済取引の逆指値レート(必須) - <tt>:expiration_type</tt> .. 決済取引の有効期限。ClickClientScrap::FX::EXPIRATION_TYPE_TODAY等を指定します(必須) - <tt>:expiration_date</tt> .. 決済取引の有効期限が「日付指定(ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED)」の場合の有効期限をDateで指定します。(有効期限が「日付指定」の場合、必須) - 戻り値
-
ClickClientScrap::FX::OrderResult
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/clickclient_scrap.rb', line 289 def order ( currency_pair_code, sell_or_buy, unit, ={} ) # 取り引き種別の判別とパラメータチェック type = ORDER_TYPE_MARKET_ORDER if ( && [:settle] != nil ) if ( [:settle][:stop_order_rate] != nil) # 逆指値レートと決済取引の指定があればIFD-OCO取引 raise "options[:settle][:rate] is required." if [:settle][:rate] == nil type = ORDER_TYPE_IFD_OCO else # 決済取引の指定のみがあればIFD取引 raise "options[:settle][:rate] is required." if [:settle][:rate] == nil raise "options[:settle][:execution_expression] is required." if [:settle][:execution_expression] == nil type = ORDER_TYPE_IFD end raise "options[:rate] is required." if [:rate] == nil raise "options[:execution_expression] is required." if [:execution_expression] == nil raise "options[:expiration_type] is required." if [:expiration_type] == nil raise "options[:settle][:rate] is required." if [:settle][:rate] == nil raise "options[:settle][:sell_or_buy] is required." if [:settle][:sell_or_buy] == nil raise "options[:settle][:unit] is required." if [:settle][:unit] == nil raise "options[:settle][:expiration_type] is required." if [:expiration_type] == nil elsif ( && [:rate] != nil ) if ( [:stop_order_rate] != nil ) # 逆指値レートが指定されていればOCO取引 type = ORDER_TYPE_OCO else # そうでなければ通常取引 raise "options[:execution_expression] is required." if [:execution_expression] == nil type = ORDER_TYPE_NORMAL end raise "options[:expiration_type] is required." if [:expiration_type] == nil else # 成り行き type = ORDER_TYPE_MARKET_ORDER end #注文前の注文一覧 before = list_orders( ORDER_CONDITION_ON_ORDER ).inject(Set.new) {|s,o| s << o[0]; s } # レート一覧 result = link_click( "1" ) ClickClientScrap::Client.error( result ) if result.forms.empty? form = result.forms.first # 通貨ペア option = form.fields.find{|f| f.name == "P001" }..find {|o| to_pair( o.text.strip ) == currency_pair_code } raise "illegal currency_pair_code. currency_pair_code=#{currency_pair_code.to_s}" unless option option.select #注文方式 form["P100"] = type # 詳細設定画面へ result = @client.submit(form) ClickClientScrap::Client.error( result ) if result.forms.empty? form = result.forms.first case type when ORDER_TYPE_MARKET_ORDER # 成り行き form["P002"] = unit.to_s # 取り引き数量 form["P005.0"] = sell_or_buy == ClickClientScrap::FX::SELL ? "1" : "0" #売り/買い form["P007"] = [:slippage].to_s if ( && [:slippage] != nil ) # スリッページ when ORDER_TYPE_NORMAL # 指値 form["P003"] = [:rate].to_s # レート form["P005"] = unit.to_s # 取り引き数量 form["P002.0"] = sell_or_buy == ClickClientScrap::FX::SELL ? "1" : "0" #売り/買い exp = [:execution_expression] form["P004.0"] = exp == ClickClientScrap::FX::EXECUTION_EXPRESSION_REVERSE_LIMIT_ORDER ? "2" : "1" #指値/逆指値 set_expiration( form, , "P008", "P009" ) # 有効期限 when ORDER_TYPE_OCO # OCO form["P003"] = [:rate].to_s # レート form["P005"] = [:stop_order_rate].to_s # 逆指値レート form["P007"] = unit.to_s # 取り引き数量 form["P002.0"] = sell_or_buy == ClickClientScrap::FX::SELL ? "1" : "0" #売り/買い set_expiration( form, , "P010", "P011" ) # 有効期限 else raise "not supported yet." end # 確認画面へ result = @client.submit(form) ClickClientScrap::Client.error( result ) if result.forms.empty? result = @client.submit(result.forms.first) ClickClientScrap::Client.error( result ) unless result.body.toutf8 =~ /注文受付完了/ #注文前の一覧と注文後の一覧を比較して注文を割り出す。 #成り行き注文の場合、即座に約定するのでnilになる(タイミングによっては取得できるかも) tmp = list_orders( ORDER_CONDITION_ON_ORDER ).find {|o| !before.include?(o[0]) } return OrderResult.new( tmp ? tmp[1].order_no : nil ) end |
#set_expiration(form, options, input_type, input_date) ⇒ Object
有効期限を設定する
- form
-
フォーム
- options
-
パラメータ
- input_type
-
有効期限の種別を入力するinput要素名
- input_date
-
有効期限が日付指定の場合に、日付を入力するinput要素名
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/clickclient_scrap.rb', line 391 def set_expiration( form, , input_type, input_date ) case [:expiration_type] when ClickClientScrap::FX::EXPIRATION_TYPE_TODAY form[input_type] = "0" when ClickClientScrap::FX::EXPIRATION_TYPE_WEEK_END form[input_type] = "1" when ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED form[input_type] = "3" raise "options[:expiration_date] is required." unless [:expiration_date] form["#{input_date}.Y"] = [:expiration_date].year form["#{input_date}.M"] = [:expiration_date].month form["#{input_date}.D"] = [:expiration_date].day form["#{input_date}.h"] = [:expiration_date].respond_to?(:hour) ? [:expiration_date].hour : "0" else form[input_type] = "2" end end |
#settle(open_interest_id, unit, options = {}) ⇒ Object
決済注文を行います。
- open_interest_id
-
決済する建玉番号
- unit
-
取引数量
- options
-
決済注文のオプション。注文方法に応じて以下の情報を設定できます。
- <b>成り行き注文</b>
- <tt>:slippage</tt> .. スリッページ (オプション)
- <tt>:slippage_base_rate</tt> .. スリッページの基準となる取引レート(スリッページが指定された場合、必須。)
- <b>通常注文</b> ※注文レートが設定されていれば通常取引となります。
- <tt>:rate</tt> .. 注文レート(必須)
- <tt>:execution_expression</tt> .. 執行条件。ClickClientScrap::FX::EXECUTION_EXPRESSION_LIMIT_ORDER等を指定します(必須)
- <tt>:expiration_type</tt> .. 有効期限。ClickClientScrap::FX::EXPIRATION_TYPE_TODAY等を指定します(必須)
- <tt>:expiration_date</tt> .. 有効期限が「日付指定(ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED)」の場合の有効期限をDateで指定します。(有効期限が「日付指定」の場合、必須)
- <b>OCO注文</b> ※注文レートと逆指値レートが設定されていればOCO取引となります。
- <tt>:rate</tt> .. 注文レート(必須)
- <tt>:stop_order_rate</tt> .. 逆指値レート(必須)
- <tt>:expiration_type</tt> .. 有効期限。ClickClientScrap::FX::EXPIRATION_TYPE_TODAY等を指定します(必須)
- <tt>:expiration_date</tt> .. 有効期限が「日付指定(ClickClientScrap::FX::EXPIRATION_TYPE_SPECIFIED)」の場合の有効期限をDateで指定します。(有効期限が「日付指定」の場合、必須)
- 戻り値
-
なし
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/clickclient_scrap.rb', line 465 def settle ( open_interest_id, unit, ={} ) if ( [:rate] != nil && [:stop_order_rate] != nil ) # レートと逆指値レートが指定されていればOCO取引 raise "options[:expiration_type] is required." if [:expiration_type] == nil elsif ( [:rate] != nil ) # レートが指定されていれば通常取引 raise "options[:execution_expression] is required." if [:execution_expression] == nil raise "options[:expiration_type] is required." if [:expiration_type] == nil else # 成り行き if ( [:slippage] != nil ) raise "if you use a slippage, options[:slippage_base_rate] is required." if [:slippage_base_rate] == nil end end # 建玉一覧 result = link_click( "3" ) # 対象となる建玉をクリック link = result.links.find {|l| l.href =~ /[^"]*ORDERNO=([a-zA-Z0-9]*)[^"]*/ && $1 == open_interest_id } raise "illegal open_interest_id. open_interest_id=#{open_interest_id}" unless link result = @client.click(link) # 決済 form = result.forms.first form["P100"] = "00" # 成り行き TODO 通常(01),OCO取引(21)対応 result = @client.submit(form) ClickClientScrap::Client.error( result ) if result.forms.empty? # 設定 form = result.forms.first form["L111"] = unit.to_s form["P005"] = [:slippage].to_s if [:slippage] result = @client.submit(form) ClickClientScrap::Client.error( result ) if result.forms.empty? # 確認 form = result.forms.first result = @client.submit(form) ClickClientScrap::Client.error( result ) unless result.body.toutf8 =~ /完了/ end |