Class: JIJI::Operator

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/operator.rb

Overview

オペレーター

Direct Known Subclasses

RmtOperator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trade_result_dao = nil, money = nil) ⇒ Operator

コンストラクタ

money

保証金



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jiji/operator.rb', line 16

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

#confObject

Returns the value of attribute conf.



129
130
131
# File 'lib/jiji/operator.rb', line 129

def conf
  @conf
end

#drawObject (readonly)

引き分け



127
128
129
# File 'lib/jiji/operator.rb', line 127

def draw
  @draw
end

#fixed_profit_or_lossObject (readonly)

現在の確定済み損益



118
119
120
# File 'lib/jiji/operator.rb', line 118

def fixed_profit_or_loss
  @fixed_profit_or_loss
end

#loseObject (readonly)

負け数



125
126
127
# File 'lib/jiji/operator.rb', line 125

def lose
  @lose
end

#positionsObject (readonly)

建て玉



120
121
122
# File 'lib/jiji/operator.rb', line 120

def positions
  @positions
end

#profit_or_lossObject (readonly)

現在の損益



116
117
118
# File 'lib/jiji/operator.rb', line 116

def profit_or_loss
  @profit_or_loss
end

#winObject (readonly)

勝ち数



123
124
125
# File 'lib/jiji/operator.rb', line 123

def win
  @win
end

Instance Method Details

#buy(count, pair = :EURJPY, trader = "") ⇒ Object

購入する

count

購入する数量

pair

通貨ペア(:EURJPYなど)

trader

取引実行者識別用の名前

return

Position



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jiji/operator.rb', line 50

def buy(count, pair=:EURJPY, trader="")
  rate = @rate[pair]
  unit = @rate.pair_infos[pair].trade_unit
  p = Position.new( UUIDTools::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

取引を確定する



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jiji/operator.rb', line 80

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

#flushObject

すべてのポジションデータを保存する。



109
110
111
112
113
# File 'lib/jiji/operator.rb', line 109

def flush
  @positions.each_pair {|k, v|
    @trade_result_dao.save( v ) if @trade_result_dao
  }
end

#next_rates(rate) ⇒ Object

レートを更新する。



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jiji/operator.rb', line 29

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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jiji/operator.rb', line 67

def sell(count, pair=:EURJPY, trader="")
  rate = @rate[pair]
  unit = @rate.pair_infos[pair].trade_unit
  p = Position.new( UUIDTools::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

#stopObject

未約定のポジションデータを“ロスト”としてマークする。



101
102
103
104
105
106
# File 'lib/jiji/operator.rb', line 101

def stop
  @positions.each_pair {|k, v|
    v.lost
    @trade_result_dao.save( v ) if @trade_result_dao
  }
end

#win_rateObject

勝率



96
97
98
# File 'lib/jiji/operator.rb', line 96

def win_rate
  win > 0 ? win / (win+lose+draw) : 0.0
end