Class: RAPFLAG::Bitfinex
Constant Summary collapse
- Delay_in_seconds =
30
- @@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_history ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rapflag/bitfinex.rb', line 60 def fetch_csv_history @history = [] check_config client = ::Bitfinex::Client.new = Time.now.to_i + 1 while true begin partial = nil while true partial = client.history(@currency, { :limit => 1000, :until => , :wallet => @wallet}) if partial.is_a?(Hash) && (partial.size > 0) && partial['error'].eql?('ERR_RATE_LIMIT') puts "Got #{partial['error']} while fetching #{@wallet} #{@currency} #{client.history} items" puts " Will wait #{Delay_in_seconds} seconds before retrying" sleep(Delay_in_seconds) end break if partial && partial.is_a?(Array) end break if partial.size <= 1 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 "Fetched #{partial.size} @history entries #{first_time} -> #{last_time}" if $VERBOSE = (partial.last['timestamp'].to_i - 1) @history = @history | partial rescue => error puts "error #{error}" puts " backtrace: #{error.backtrace[0..10].join("\n")}" end end puts "Feched #{@history.size} history entries" if $VERBOSE end |
#get_usd_exchange(date_time = Time.now, from = 'BTC') ⇒ Object
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 52 53 54 55 56 57 58 |
# File 'lib/rapflag/bitfinex.rb', line 14 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}" # therefore we just return the most uptodate url = "https://api.bitfinex.com/v2/candles/trade:1D:t#{from}USD/hist?start:#{ms}" rates = {} while true 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) if items && items.size > 0 && items.first.first.eql?(:error) puts "#{Time.now}: Fetching #{url} returned #{items.first}." puts " Retrying in #{Delay_in_seconds} seconds" sleep(Delay_in_seconds) else break end end items.each do |item| # 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], = Time.at(item.first/1000).strftime(DATE_FORMAT) rates[] = item[2] end; from.eql?('BTC') ? @@btc_to_usd = rates.clone : @@bfx_to_usd = rates.clone rates[key] ? rates[key] : nil rescue => error puts "error #{error}" puts " backtrace: #{error.backtrace[0..10].join("\n")}" require 'pry'; binding.pry end |