Class: JIJI::Operator
- Inherits:
-
Object
- Object
- JIJI::Operator
- Defined in:
- lib/jiji/operator.rb
Overview
オペレーター
Direct Known Subclasses
Instance Attribute Summary collapse
-
#conf ⇒ Object
Returns the value of attribute conf.
-
#draw ⇒ Object
readonly
引き分け.
-
#fixed_profit_or_loss ⇒ Object
readonly
現在の確定済み損益.
-
#lose ⇒ Object
readonly
負け数.
-
#positions ⇒ Object
readonly
建て玉.
-
#profit_or_loss ⇒ Object
readonly
現在の損益.
-
#win ⇒ Object
readonly
勝ち数.
Instance Method Summary collapse
-
#buy(count, pair = :EURJPY, trader = "") ⇒ Object
購入する count:: 購入する数量 pair:: 通貨ペア(:EURJPYなど) trader:: 取引実行者識別用の名前 return:: Position.
-
#commit(position) ⇒ Object
取引を確定する.
-
#flush ⇒ Object
すべてのポジションデータを保存する。.
-
#initialize(trade_result_dao = nil, money = nil) ⇒ Operator
constructor
コンストラクタ money:: 保証金.
-
#next_rates(rate) ⇒ Object
レートを更新する。.
-
#sell(count, pair = :EURJPY, trader = "") ⇒ Object
売却する count:: 売却する数量 pair:: 通貨ペア(:EURJPYなど) trader:: 取引実行者識別用の名前 return:: Position.
-
#stop ⇒ Object
未約定のポジションデータを“ロスト”としてマークする。.
-
#win_rate ⇒ Object
勝率.
Constructor Details
#initialize(trade_result_dao = nil, money = nil) ⇒ Operator
コンストラクタ
- money
-
保証金
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jiji/operator.rb', line 17 def initialize( trade_result_dao=nil, money=nil ) @rate = nil @money = money @profit_or_loss = 0 @fixed_profit_or_loss = 0 @positions = {} @draw = 0 @lose = 0 @win = 0.0 @trade_result_dao = trade_result_dao end |
Instance Attribute Details
#conf ⇒ Object
Returns the value of attribute conf.
130 131 132 |
# File 'lib/jiji/operator.rb', line 130 def conf @conf end |
#draw ⇒ Object (readonly)
引き分け
128 129 130 |
# File 'lib/jiji/operator.rb', line 128 def draw @draw end |
#fixed_profit_or_loss ⇒ Object (readonly)
現在の確定済み損益
119 120 121 |
# File 'lib/jiji/operator.rb', line 119 def fixed_profit_or_loss @fixed_profit_or_loss end |
#lose ⇒ Object (readonly)
負け数
126 127 128 |
# File 'lib/jiji/operator.rb', line 126 def lose @lose end |
#positions ⇒ Object (readonly)
建て玉
121 122 123 |
# File 'lib/jiji/operator.rb', line 121 def positions @positions end |
#profit_or_loss ⇒ Object (readonly)
現在の損益
117 118 119 |
# File 'lib/jiji/operator.rb', line 117 def profit_or_loss @profit_or_loss end |
#win ⇒ Object (readonly)
勝ち数
124 125 126 |
# File 'lib/jiji/operator.rb', line 124 def win @win end |
Instance Method Details
#buy(count, pair = :EURJPY, trader = "") ⇒ Object
購入する
- count
-
購入する数量
- pair
-
通貨ペア(:EURJPYなど)
- trader
-
取引実行者識別用の名前
- return
-
Position
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jiji/operator.rb', line 51 def buy(count, pair=:EURJPY, trader="") rate = @rate[pair] unit = @rate.pair_infos[pair].trade_unit p = Position.new( UUID.random_create().to_s, Position::BUY, count, unit, @rate.time, rate.ask, pair, trader, self ) p.next( @rate ) @profit_or_loss += p.profit_or_loss @positions[p.position_id] = p @trade_result_dao.save( p ) if @trade_result_dao return p end |
#commit(position) ⇒ Object
取引を確定する
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jiji/operator.rb', line 81 def commit(position) position._commit @trade_result_dao.save( position ) if @trade_result_dao @positions.delete position.position_id @fixed_profit_or_loss += position.profit_or_loss if position.profit_or_loss == 0 @draw+=1 elsif position.profit_or_loss < 0 @lose+=1 else @win+=1 end end |
#flush ⇒ Object
すべてのポジションデータを保存する。
110 111 112 113 114 |
# File 'lib/jiji/operator.rb', line 110 def flush @positions.each_pair {|k, v| @trade_result_dao.save( v ) if @trade_result_dao } end |
#next_rates(rate) ⇒ Object
レートを更新する。
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jiji/operator.rb', line 30 def next_rates( rate ) @rate = rate result = @positions.inject({:total=>0.0,:profit=>0}) {|r,e| p = e[1] p.next(rate) r[:total] += p.price if p.state == Position::STATE_START r[:profit] += p.profit_or_loss r } @profit_or_loss = result[:profit] + @fixed_profit_or_loss if @money && (( @money + result[:profit] ) / result[:total]) <= 0.007 raise "loss cut" end @trade_result_dao.next( self, rate.time ) if @trade_result_dao end |
#sell(count, pair = :EURJPY, trader = "") ⇒ Object
売却する
- count
-
売却する数量
- pair
-
通貨ペア(:EURJPYなど)
- trader
-
取引実行者識別用の名前
- return
-
Position
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jiji/operator.rb', line 68 def sell(count, pair=:EURJPY, trader="") rate = @rate[pair] unit = @rate.pair_infos[pair].trade_unit p = Position.new( UUID.random_create().to_s, Position::SELL, count, unit, @rate.time, rate.bid, pair, trader, self ) p.next( @rate ) @profit_or_loss += p.profit_or_loss @positions[p.position_id] = p @trade_result_dao.save( p ) if @trade_result_dao return p end |
#stop ⇒ Object
未約定のポジションデータを“ロスト”としてマークする。
102 103 104 105 106 107 |
# File 'lib/jiji/operator.rb', line 102 def stop @positions.each_pair {|k, v| v.lost @trade_result_dao.save( v ) if @trade_result_dao } end |
#win_rate ⇒ Object
勝率
97 98 99 |
# File 'lib/jiji/operator.rb', line 97 def win_rate win > 0 ? win / (win+lose+draw) : 0.0 end |