Class: RAPFLAG::History
- Inherits:
-
Object
- Object
- RAPFLAG::History
- Defined in:
- lib/rapflag/history.rb
Constant Summary collapse
- DATE_FORMAT =
'%Y.%m.%d'
- DATE_TIME_FORMAT =
'%Y.%m.%d %H:%M:%S'
Instance Attribute Summary collapse
-
#bfx_to_usd ⇒ Object
readonly
Returns the value of attribute bfx_to_usd.
-
#btc_to_usd ⇒ Object
readonly
Returns the value of attribute btc_to_usd.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#wallet ⇒ Object
readonly
Returns the value of attribute wallet.
Instance Method Summary collapse
- #create_csv_file ⇒ Object
- #create_summary ⇒ Object
-
#initialize(wallet = 'trading', currency = 'USD') ⇒ History
constructor
A new instance of History.
Constructor Details
#initialize(wallet = 'trading', currency = 'USD') ⇒ History
Returns a new instance of History.
13 14 15 16 |
# File 'lib/rapflag/history.rb', line 13 def initialize(wallet = 'trading', currency = 'USD') @wallet = wallet @currency = currency end |
Instance Attribute Details
#bfx_to_usd ⇒ Object (readonly)
Returns the value of attribute bfx_to_usd.
9 10 11 |
# File 'lib/rapflag/history.rb', line 9 def bfx_to_usd @bfx_to_usd end |
#btc_to_usd ⇒ Object (readonly)
Returns the value of attribute btc_to_usd.
9 10 11 |
# File 'lib/rapflag/history.rb', line 9 def btc_to_usd @btc_to_usd end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
9 10 11 |
# File 'lib/rapflag/history.rb', line 9 def currency @currency end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
9 10 11 |
# File 'lib/rapflag/history.rb', line 9 def history @history end |
#wallet ⇒ Object (readonly)
Returns the value of attribute wallet.
9 10 11 |
# File 'lib/rapflag/history.rb', line 9 def wallet @wallet end |
Instance Method Details
#create_csv_file ⇒ Object
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 |
# File 'lib/rapflag/history.rb', line 18 def create_csv_file out_file = "output/#{self.class.to_s.split('::').last.downcase}/#{@currency}_#{@wallet}.csv" FileUtils.makedirs(File.dirname(out_file)) CSV.open(out_file,'w', :write_headers=> true, :col_sep => ';', :headers => ['currency', 'amount', 'balance', 'description', 'date_time', ] #< column header ) do |csv| @history.each do | hist_item| csv << [ hist_item['currency'], hist_item['amount'], hist_item['balance'], hist_item['description'], Time.at(hist_item['timestamp'].to_i).strftime(DATE_TIME_FORMAT), ] end end sums = {} @history.each do | hist_item| key = /^[^\d]+/.match(hist_item['description'])[0].chomp value = hist_item['amount'].to_f if sums[key] sums[key] += value else sums[key] = value end end puts puts "Summary for #{@wallet} #{@currency} (#{@history.size} entries}" sums.each do |key, value| puts " #{sprintf('%40s', key)} is #{value}" end end |
#create_summary ⇒ Object
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rapflag/history.rb', line 61 def create_summary @daily = {} @history.sort{|x,y| x['timestamp'] <=> y['timestamp']}.each do | hist_item| date = Time.at(hist_item['timestamp'].to_i).strftime(DATE_FORMAT) info = Struct::Daily.new(date, hist_item['amount'].to_f, hist_item['balance'].to_f, hist_item['description']) amount = hist_item['amount'].to_f balance = hist_item['balance'].to_f if @daily[date] old_balance = @daily[date] existing = @daily[date] else info.income = 0.0 existing = info end if /Wire Withdrawal fee|Trading fees for|Margin Funding Payment on wallet/i.match( hist_item['description']) existing.income += amount end existing.balance = balance if balance != 0.0 @daily[date] = existing end out_file = "output/#{self.class.to_s.split('::').last.downcase}/#{@currency}_#{@wallet}_summary.csv" FileUtils.makedirs(File.dirname(out_file)) previous_date = nil saved_rate = nil saved_info = nil CSV.open(out_file,'w', :write_headers=> true, :col_sep => ';', :headers => ['currency', 'date', 'income', 'balance', 'rate', 'balance_in_usd', ] #< column header ) do |csv| @daily.each do |date, info| strings = date.split('.') fetch_date = Date.new(strings[0].to_i, strings[1].to_i, strings[2].to_i) rate = get_usd_exchange(fetch_date, @currency) (1..(fetch_date - previous_date -1).to_i).each do |j| intermediate = (previous_date + j).strftime('%Y.%m.%d') csv << [@currency, intermediate, saved_info.income, saved_info.balance, saved_rate ? saved_rate : nil, saved_rate ? info.balance * get_usd_exchange(intermediate, @currency) : nil, ] end if previous_date csv << [@currency, date, info.income, info.balance, rate ? rate : nil, rate ? info.balance * get_usd_exchange(fetch_date, @currency) : nil, ] previous_date = fetch_date saved_info = info saved_rate = nil end end end |