Class: JIJI::Position

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

Overview

ポジション

Constant Summary collapse

SELL =

売り/買い区分:売り

0
BUY =

売り/買い区分:買い

1
STATE_WAITING =

状態:注文中

0
STATE_START =

状態:新規

1
STATE_FIX_WAITING =

状態:決済注文中

2
STATE_FIXED =

状態:決済済み

3
STATE_LOST =

状態:約定前にシステムが再起動された

4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position_id, sell_or_buy, count, unit, date, rate, pair, trader, operator, open_interest_no = "", order_no = "") ⇒ Position

コンストラクタ

sell_or_buy

売りor買い

count

数量

unit

取引単位

date

取引日時

rate

レート

pair

通貨ペア

trader

取引実行者

operator

operator

open_interest_no

建玉番号

order_no

注文番号



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/jiji/operator.rb', line 163

def initialize( position_id, sell_or_buy, count, unit, date, rate, pair,
  trader, operator, open_interest_no="", order_no="" ) #:nodoc:
  @position_id = position_id
  @sell_or_buy = sell_or_buy
  @count = count
  @unit = unit
  @price = (count*unit*rate).to_i
  @date = date
  @profit_or_loss = 0
  @state =STATE_START
  @rate = rate
  @pair = pair
  @trader = trader

  @open_interest_no = open_interest_no
  @order_no = order_no

  @operator = operator
  @info = {}
  @swap = 0
  @swap_time = Time.local( date.year, \
    date.month, date.day, operator.conf.get([:swap_time], 5 ))
  @swap_time += 60*60*24 if date > @swap_time
end

Instance Attribute Details

#countObject (readonly)

取引数量



263
264
265
# File 'lib/jiji/operator.rb', line 263

def count
  @count
end

#current_priceObject (readonly)

現在価値



267
268
269
# File 'lib/jiji/operator.rb', line 267

def current_price
  @current_price
end

#dateObject (readonly)

購入日時



259
260
261
# File 'lib/jiji/operator.rb', line 259

def date
  @date
end

#fix_dateObject (readonly)

決済日時



261
262
263
# File 'lib/jiji/operator.rb', line 261

def fix_date
  @fix_date
end

#fix_rateObject (readonly)

決済時のレート



273
274
275
# File 'lib/jiji/operator.rb', line 273

def fix_rate
  @fix_rate
end

#operatorObject (readonly)

クライアント



246
247
248
# File 'lib/jiji/operator.rb', line 246

def operator
  @operator
end

#pairObject (readonly)

通貨ペア



277
278
279
# File 'lib/jiji/operator.rb', line 277

def pair
  @pair
end

#position_idObject (readonly)

一意な識別子



249
250
251
# File 'lib/jiji/operator.rb', line 249

def position_id
  @position_id
end

#priceObject (readonly)

取得金額



265
266
267
# File 'lib/jiji/operator.rb', line 265

def price
  @price
end

#profit_or_lossObject (readonly)

現在の損益



269
270
271
# File 'lib/jiji/operator.rb', line 269

def profit_or_loss
  @profit_or_loss
end

#rateObject (readonly)

購入時のレート



271
272
273
# File 'lib/jiji/operator.rb', line 271

def rate
  @rate
end

#raw_position_idObject

プラグインが返す識別子



251
252
253
# File 'lib/jiji/operator.rb', line 251

def raw_position_id
  @raw_position_id
end

#sell_or_buyObject (readonly)

売りか買いか?



254
255
256
# File 'lib/jiji/operator.rb', line 254

def sell_or_buy
  @sell_or_buy
end

#stateObject (readonly)

状態



256
257
258
# File 'lib/jiji/operator.rb', line 256

def state
  @state
end

#swapObject (readonly)

決済時のレート



275
276
277
# File 'lib/jiji/operator.rb', line 275

def swap
  @swap
end

#traderObject (readonly)

取引を行ったエージェント名



279
280
281
# File 'lib/jiji/operator.rb', line 279

def trader
  @trader
end

Instance Method Details

#[](key) ⇒ Object

:nodoc:



219
220
221
# File 'lib/jiji/operator.rb', line 219

def [](key) #:nodoc:
  @info[key]
end

#[]=(key, value) ⇒ Object

:nodoc:



222
223
224
# File 'lib/jiji/operator.rb', line 222

def []=(key, value) #:nodoc:
  @info[key] = value
end

#_commitObject

:nodoc:



188
189
190
191
192
193
# File 'lib/jiji/operator.rb', line 188

def _commit #:nodoc:
  raise "illegal state" if @state != STATE_START
  @state = STATE_FIXED
  @fix_date = @current_date
  @fix_rate = @current_rate
end

#lostObject

:nodoc:



195
196
197
# File 'lib/jiji/operator.rb', line 195

def lost #:nodoc:
  @state = STATE_LOST
end

#next(rates) ⇒ Object

現在価格を更新



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/jiji/operator.rb', line 200

def next(rates) #:nodoc:
  return if @state == STATE_FIXED
  rate = rates[@pair]
  @current_rate = @sell_or_buy == BUY ? rate.bid : rate.ask
  @current_price = (@count * @unit * @current_rate).to_i

  # swap
  if @swap_time <= rates.time
    @swap += @sell_or_buy == BUY ? rate.buy_swap : rate.sell_swap
    @swap_time += 60*60*24
  end

  @profit_or_loss = @sell_or_buy == BUY \
      ? @current_price - @price + @swap\
      : @price - @current_price + @swap

  @current_date = rates.time
end

#valuesObject

:nodoc:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/jiji/operator.rb', line 226

def values #:nodoc:
  {
    :position_id => position_id,
    :raw_position_id => raw_position_id,
    :sell_or_buy =>  sell_or_buy == JIJI::Position::SELL ? :sell : :buy,
    :state => state,
    :date => date.to_i,
    :fix_date => fix_date.to_i,
    :count => count ,
    :price => price,
    :profit_or_loss => profit_or_loss.to_i,
    :rate => rate,
    :fix_rate => fix_rate,
    :swap=> swap,
    :pair => pair,
    :trader => trader
  }
end