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
注文をキャンセルします。.
-
#each_page(page) ⇒ Object
すべてのページを列挙する。.
-
#get_margin ⇒ 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要素名.
- #set_sell_or_buy(form, sell_or_buy) ⇒ Object
-
#settle(open_interest_id, unit, options = {}) ⇒ Object
決済注文を行います。.
Constructor Details
#initialize(client, links, options = {}) ⇒ FxSession
Returns a new instance of FxSession.
199 200 201 202 203 |
# File 'lib/clickclient_scrap.rb', line 199 def initialize( client, links, ={} ) @client = client @links = links = end |
Class Method Details
.convert_rate(str) ⇒ Object
12.34-35 形式の文字列をbidレート、askレートに変換する。
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/clickclient_scrap.rb', line 229 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] elsif str =~ /^([\d]+\.[\d]+)\-([\d]+\.[\d]+)$/ return [$1.to_f,$2.to_f] end end |
Instance Method Details
#cancel_order(order_no) ⇒ Object
注文をキャンセルします。
- order_no
-
注文番号
- 戻り値
-
なし
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/clickclient_scrap.rb', line 434 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 = nil each_page( result ) {|page| link = page.links.find {|l| l.href =~ /[^"]*GKEY=([a-zA-Z0-9]*)[^"]*/ && $1 == order_no } break if link } 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 |
#each_page(page) ⇒ Object
すべてのページを列挙する。
607 608 609 610 611 612 613 614 615 616 617 |
# File 'lib/clickclient_scrap.rb', line 607 def each_page( page ) current_page=1 while ( page ) yield page current_page+=1 link_to_next = page.links.find{|i| i.text == current_page.to_s } page = link_to_next ? @client.click( link_to_next ) : nil end end |
#get_margin ⇒ Object
余力情報を取得します。
- 戻り値
-
ClickClientScrap::FX::Marginのハッシュ。
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
# File 'lib/clickclient_scrap.rb', line 624 def get_margin result = link_click( "7" ) list = result.body.toutf8.scan( /【([^<]*)[^>]*>[^>]*>([^<]*)</m ) values = list.inject({}) {|r,i| if ( i[0] == "証拠金維持率】" ) r[i[0]] = i[1] else r[i[0]] = i[1].gsub(/,/, "").to_i end r } return Margin.new( values["時価評価総額】"], values["建玉評価損益】"], values["口座残高】"], values["証拠金維持率】"], values["余力】"], values["拘束証拠金】"], values["必要証拠金】"], values["注文中必要証拠金】"], values["振替可能額】"] ) end |
#list_open_interests(currency_pair_code = nil) ⇒ Object
建玉一覧を取得します。
- currency_pair_code
-
通貨ペアコード。※未実装
- 戻り値
-
建玉IDをキーとするClickClientScrap::FX::OpenInterestのハッシュ。
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/clickclient_scrap.rb', line 582 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 = [] each_page( result ) {|page| list += page.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のハッシュ。
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'lib/clickclient_scrap.rb', line 544 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 = [] each_page( result ) {|page| list += page.body.toutf8.scan( /<a href="[^"]*GKEY=([a-zA-Z0-9]*)">([A-Z]{3}\/[A-Z]{3}) ([^<]*)<\/a><br>[^;]*;([^<]*)<font[^>]*>([^<]*)<\/font>([^@]*)@([\d\.]*)([^\s]*) ([^<]*)<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 = to_execution_expression(i[7]) stop_order_rate = i[13] ? i[13].to_f : nil stop_order_execution_expression = i[14] ? to_execution_expression(i[14]) : nil tmp[order_no] = Order.new( order_no, trade_type, order_type, execution_expression, \ sell_or_buy, pair, count, rate, i[8], stop_order_rate, stop_order_execution_expression) } return tmp end |
#list_rates ⇒ Object
レート一覧を取得します。
- 戻り値
-
通貨ペアをキーとするClickClientScrap::FX::Rateのハッシュ。
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/clickclient_scrap.rb', line 208 def list_rates result = link_click( "1" ) if !@last_update_time_of_swaps \ || Time.now.to_i - @last_update_time_of_swaps > ([: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]*([\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]}-#{l[2]}" 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のハッシュ。
249 250 251 252 253 254 255 256 |
# File 'lib/clickclient_scrap.rb', line 249 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].sub(/,/,"").to_i, l[2].sub(/,/,"").to_i ); r } end |
#logout ⇒ Object
ログアウトします。
649 650 651 652 653 654 655 656 657 |
# File 'lib/clickclient_scrap.rb', line 649 def logout begin @client.click( @links.find {|i| i.text == "\303\233\302\270\303\236\302\261\302\263\303\204" \ || i.text == "ログアウト" }) rescue end 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> - 戻り値
-
ClickClientScrap::FX::OrderResult
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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/clickclient_scrap.rb', line 303 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["P003"] = unit.to_s # 取り引き数量 set_sell_or_buy( form, sell_or_buy ) form["P005"] = [:slippage].to_s if ( && [:slippage] != nil ) # スリッページ when ORDER_TYPE_NORMAL # 指値 form["P003"] = [:rate].to_s # レート form["P005"] = unit.to_s # 取り引き数量 set_sell_or_buy( form, sell_or_buy ) exp = [:execution_expression] == ClickClientScrap::FX::EXECUTION_EXPRESSION_REVERSE_LIMIT_ORDER ? "2" : "1" form.(:name => 'P004.0', :value => exp )[0].check 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 # 取り引き数量 set_sell_or_buy( form, sell_or_buy ) 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要素名
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/clickclient_scrap.rb', line 405 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 |
#set_sell_or_buy(form, sell_or_buy) ⇒ Object
423 424 425 426 |
# File 'lib/clickclient_scrap.rb', line 423 def set_sell_or_buy( form, sell_or_buy ) value = sell_or_buy == ClickClientScrap::FX::SELL ? "1" : "0" #売り/買い form.(:name => 'P002.0', :value => value )[0].check end |
#settle(open_interest_id, unit, options = {}) ⇒ Object
決済注文を行います。
- open_interest_id
-
決済する建玉番号
- unit
-
取引数量
- options
-
決済注文のオプション。注文方法に応じて以下の情報を設定できます。
- <b>
- 戻り値
-
なし
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/clickclient_scrap.rb', line 488 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 = nil each_page( result ) {|page| link = page.links.find {|l| l.href =~ /[^"]*ORDERNO=([a-zA-Z0-9]*)[^"]*/ && $1 == open_interest_id } break if link } 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["P003"] = 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 |