Class: RAPFLAG::Bitfinex

Inherits:
History show all
Defined in:
lib/rapflag/bitfinex.rb

Constant Summary collapse

@@btc_to_usd =
{}
@@bfx_to_usd =
{}

Constants inherited from History

History::DATE_FORMAT, History::DATE_TIME_FORMAT

Instance Attribute Summary

Attributes inherited from History

#bfx_to_usd, #btc_to_usd, #currency, #history, #wallet

Instance Method Summary collapse

Methods inherited from History

#create_csv_file, #create_summary, #initialize

Constructor Details

This class inherits a constructor from RAPFLAG::History

Instance Method Details

#fetch_csv_historyObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rapflag/bitfinex.rb', line 53

def fetch_csv_history
  @history = []
  check_config
  client = ::Bitfinex::Client.new
  timestamp = Time.now.to_i + 1
  while true
    begin
      partial = client.history(@currency, { :limit => 500, :until => timestamp, :wallet => @wallet})
      break unless partial && partial.size > 0
      if partial.is_a?(Hash)
        puts "Got #{partial['error']} while fetching #{@wallet} #{@currency} until #{Time.at(timestamp)}"
        exit 3
      end
      first_time = Time.at(partial.first['timestamp'].to_i).strftime(DATE_TIME_FORMAT)
      last_time = Time.at(partial.last['timestamp'].to_i).strftime(DATE_TIME_FORMAT)
      puts "Feched #{partial.size} @history entries #{first_time} -> #{last_time}"  if $VERBOSE
      timestamp = (partial.last['timestamp'].to_i - 1)
      @history = @history | partial
      break if partial.size <= 1
    rescue => error
      puts "error #{error}"
    end
  end
  puts "Feched #{@history.size} history entries" if $VERBOSE
end

#get_usd_exchange(date_time = Time.now, from = 'BTC') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rapflag/bitfinex.rb', line 13

def get_usd_exchange(date_time = Time.now, from='BTC')
  return 1.0 if from == 'USD'
  key = date_time.strftime(DATE_FORMAT)
  return @@btc_to_usd[key] if from.eql?('BTC') && @@btc_to_usd.size > 0
  return @@bfx_to_usd[key] if from.eql?('BFX') && @@bfx_to_usd.size > 0

  ms =  (date_time.is_a?(Date) ? date_time.to_time : date_time).to_i*1000
  ms_next_date = ms + (3*24*3600)*1000
  # this does not work
  # url = "https://api.bitfinex.com/v2/candles/trade:1D:t#{from}USD/hist?start:#{ms}?end:#{ms_next_date}"
  url = "https://api.bitfinex.com/v2/candles/trade:1D:t#{from}USD/hist?start:#{ms}?end:#{ms_next_date}"
  # therefore we just return the most uptodate
  url = "https://api.bitfinex.com/v2/candles/trade:1D:t#{from}USD/hist?start:#{ms}"
  puts "Fetching #{date_time}: #{url} #{@@btc_to_usd.size} BTC #{@@bfx_to_usd.size} BFX" if $VERBOSE
  response = Faraday.get(url)
  items = eval(response.body)
  rates = {}
  items.each do |item|
    if item.first.eql?(:error)
      puts "Fetching returned #{item}. Aborting"
      exit(1)
    end
    # http://docs.bitfinex.com/v2/reference#rest-public-candles
    # field definitions for  [ MTS, OPEN, CLOSE, HIGH, LOW, VOLUME ],
    # MTS   int   millisecond time stamp
    # OPEN  float   First execution during the time frame
    # CLOSE   float   Last execution during the time frame
    # HIGH  integer   Highest execution during the time frame
    # LOW   float   Lowest execution during the timeframe
    # VOLUME  float   Quantity of symbol traded within the timeframe
    # [[1489363200000,1224.4,1211.2,1238,1206.7,6157.96283895],
    timestamp = Time.at(item.first/1000).strftime(DATE_FORMAT)
    rates[timestamp] = item[2]
  end;
  from.eql?('BTC') ? @@btc_to_usd = rates.clone : @@bfx_to_usd = rates.clone
  rates[key] ? rates[key] : nil
rescue => err
  puts "Err #{err}"
end