Class: RAPFLAG::Poloniex
Constant Summary collapse
- @@btc_to_usd =
{}
- @@bfx_to_usd =
{}
Constants inherited from History
History::DATE_FORMAT, History::DATE_TIME_FORMAT
Instance Attribute Summary collapse
-
#active_loans ⇒ Object
readonly
Returns the value of attribute active_loans.
-
#available_account_balances ⇒ Object
readonly
Returns the value of attribute available_account_balances.
-
#complete_balances ⇒ Object
readonly
Returns the value of attribute complete_balances.
-
#deposit_addresses ⇒ Object
readonly
Returns the value of attribute deposit_addresses.
-
#deposits ⇒ Object
readonly
Returns the value of attribute deposits.
-
#lending_history ⇒ Object
readonly
Returns the value of attribute lending_history.
-
#open_orders ⇒ Object
readonly
Returns the value of attribute open_orders.
-
#output_prefix ⇒ Object
readonly
Returns the value of attribute output_prefix.
-
#tradable_balances ⇒ Object
readonly
Returns the value of attribute tradable_balances.
-
#trade_history ⇒ Object
readonly
Returns the value of attribute trade_history.
-
#withdrawals ⇒ Object
readonly
Returns the value of attribute withdrawals.
Attributes inherited from History
#bfx_to_usd, #btc_to_usd, #currency, #history, #wallet
Instance Method Summary collapse
- #create_csv_file ⇒ Object
- #dump_history ⇒ Object
- #fetch_csv_history ⇒ Object
-
#find_day_trade(currency, date, type) ⇒ Object
type is either buy or sell.
- #find_lending_info_day(currency, date) ⇒ Object
- #find_per_currency_and_day(items, currency, date) ⇒ Object
- #get_usd_exchange(date_time = Time.now, from = 'BTC') ⇒ Object
Methods inherited from History
Constructor Details
This class inherits a constructor from RAPFLAG::History
Instance Attribute Details
#active_loans ⇒ Object (readonly)
Returns the value of attribute active_loans.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def active_loans @active_loans end |
#available_account_balances ⇒ Object (readonly)
Returns the value of attribute available_account_balances.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def available_account_balances @available_account_balances end |
#complete_balances ⇒ Object (readonly)
Returns the value of attribute complete_balances.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def complete_balances @complete_balances end |
#deposit_addresses ⇒ Object (readonly)
Returns the value of attribute deposit_addresses.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def deposit_addresses @deposit_addresses end |
#deposits ⇒ Object (readonly)
Returns the value of attribute deposits.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def deposits @deposits end |
#lending_history ⇒ Object (readonly)
Returns the value of attribute lending_history.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def lending_history @lending_history end |
#open_orders ⇒ Object (readonly)
Returns the value of attribute open_orders.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def open_orders @open_orders end |
#output_prefix ⇒ Object (readonly)
Returns the value of attribute output_prefix.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def output_prefix @output_prefix end |
#tradable_balances ⇒ Object (readonly)
Returns the value of attribute tradable_balances.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def tradable_balances @tradable_balances end |
#trade_history ⇒ Object (readonly)
Returns the value of attribute trade_history.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def trade_history @trade_history end |
#withdrawals ⇒ Object (readonly)
Returns the value of attribute withdrawals.
13 14 15 |
# File 'lib/rapflag/poloniex.rb', line 13 def withdrawals @withdrawals end |
Instance Method Details
#create_csv_file ⇒ Object
178 179 180 |
# File 'lib/rapflag/poloniex.rb', line 178 def create_csv_file puts "create_csv_file: already done" end |
#dump_history ⇒ Object
52 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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rapflag/poloniex.rb', line 52 def dump_history load_history_info FileUtils.makedirs('output') unless File.directory?('output') CSV.open("#{@output_prefix}/trade_history.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => [ 'currency_pair'] + @trade_history.values.first.first.to_h.keys ) do |csv| @trade_history.each do |currency_pair, trades| trades.each do |trace| csv << [ currency_pair] + trace.to_h.values end end end CSV.open("#{@output_prefix}/lending_history.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => @lending_history.first.to_h.keys ) do |csv| @lending_history.each do |info| csv << info.to_h.values end end CSV.open("#{@output_prefix}/tradable_balances.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => [ 'from_currency', 'to_from_currency', ] ) do |csv| @tradable_balances.each do |currency_pair, balance| balance.each do |info| csv << [ currency_pair] + info end end end CSV.open("#{@output_prefix}/complete_balances.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => [ 'currency', 'available', 'onOrders', 'btcValue' ] ) do |csv| @complete_balances.each do |balance| csv << [balance[0]] + balance[1].values end end CSV.open("#{@output_prefix}/active_loans.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => [ 'key', 'id', 'currency', 'rate', 'amount', 'duration', 'autoRenew', 'date', 'fees', ] ) do |csv| @active_loans.each do |key, loans| loans.each do | loan | csv << [key] + loan.values end end end CSV.open("#{@output_prefix}/available_account_balances.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => [ 'key', 'currency', 'balance'] ) do |csv| @available_account_balances.each do |key, balances| balances.each do |currency, balance| csv << [key, currency, balance] end end end CSV.open("#{@output_prefix}/deposit_addresses.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => [ 'currency', 'id'] ) do |csv| @deposit_addresses.each do |currency, id| csv << [currency, id] end end CSV.open("#{@output_prefix}/withdrawals.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => @deposits.first.to_h.keys ) do |csv| @deposits.each do |info| csv << info.to_h.values end end CSV.open("#{@output_prefix}/deposits.csv",'w+', :col_sep => ';', :write_headers=> true, :headers => @withdrawals.first.to_h.keys ) do |csv| @withdrawals.each do |info| csv << info.to_h.values end end end |
#fetch_csv_history ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/rapflag/poloniex.rb', line 182 def fetch_csv_history load_history_info @trade_history.values.collect{|values| values.collect{|x| x.date} }.max max_time = [ @deposits.collect{|x| x.}.max, Time.parse(@trade_history.values.collect{|values| values.collect{|x| x.date} }.max.max).to_i, Time.parse(@lending_history.collect{|x| x.close}.max).to_i, @withdrawals.collect{|x| x.}.max,].max min_time = [ @deposits.collect{|x| x.}.min, Time.parse(@trade_history.values.collect{|values| values.collect{|x| x.date} }.min.min).to_i, Time.parse(@lending_history.collect{|x| x.close}.min).to_i, @withdrawals.collect{|x| x.}.min,].min min_date = Time.at(min_time).to_date - 1 max_date = Time.at(max_time).to_date puts "We start using the available_account_balances" pp @available_account_balances pp @complete_balances @available_account_balances.each do |key, balances| balances.each do |currency, balance| puts "Calculation history for #{key} #{currency} with current balance of #{balance}" out_name = "#{@output_prefix}/#{key}_#{currency}.csv" FileUtils.makedirs(File.dirname(out_name)) unless File.exists?(File.dirname(out_name)) @history = [] current_day = min_date current_balance = 0.0 while (current_day < max_date) puts "#{key}: at #{current_day}" if current_day.day == 1 && current_day.month % 3 == 1 entry = OpenStruct.new entry.current_day = current_day entry.balance_BEG = current_balance deposits = find_per_currency_and_day(@deposits, currency,current_day) sum_deposits = 0.0; deposits.each{ |x| sum_deposits += x.amount.to_f } withdrawals = find_per_currency_and_day(@withdrawals, currency, current_day) sum_withdrawals = 0.0; withdrawals.each{ |x| sum_withdrawals += x.amount.to_f } lendings = find_lending_info_day(currency, current_day) # fee field is negative, therefore we let sum_fee be negative, too income = 0.0; sum_fee = 0.0; lendings.each{ |x| income += x.earned.to_f; sum_fee += x.fee.to_f } # End_of_Day_Balance = End_of_Day_Balance(-1) + Deposits - Withdrawals + Lending_Income - Trading_Fees + Purchases - Sales sales = find_day_trade(currency, current_day, 'buy') sum_sales = 0.0; sales.each{ |sale| sum_sales += sale.amount.to_f*sale.rate.to_f } purchases = find_day_trade(currency, current_day, 'sell') sum_purchase = 0.0; purchases.each{ |purchase| sum_purchase += purchase.amount.to_f*purchase.rate.to_f } diff_day = sum_deposits - sum_withdrawals + sum_purchase - sum_sales + income entry.deposits = sum_deposits entry.income = income entry.withdraw = sum_withdrawals entry.sales = sum_sales entry.purchases = sum_purchase entry.balance_END = current_balance + diff_day entry.fees = sum_fee entry.day_difference = diff_day @history << entry current_day += 1 # balance for previous day current_balance = entry.balance_END end next unless @history.size > 0 CSV.open(out_name,'w+', :col_sep => ';', :write_headers=> true, :headers => @history.first.to_h.keys ) do |csv| @history.each do |info| csv << info.to_h.values end end end end end |
#find_day_trade(currency, date, type) ⇒ Object
type is either buy or sell
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rapflag/poloniex.rb', line 164 def find_day_trade(currency, date, type) found = [] @trade_history.each do |currency_pair, trades| next unless /^#{currency}/.match(currency_pair) trades.each do |trade| if trade.type.eql?(type) && date.eql?(Date.parse(trade.date)) trade.currency_pair = currency_pair found << trade end end end puts "find_day_trade: #{date} #{currency} found #{found.size}" if $VERBOSE found end |
#find_lending_info_day(currency, date) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/rapflag/poloniex.rb', line 156 def find_lending_info_day(currency, date) puts "Searching lending (close) for #{currency} in day #{date}" if $VERBOSE found = @lending_history.find_all { |item| item.currency.eql?(currency) && date.eql?(Date.parse(item.close)) } puts "find_lending_info_day: found #{found.size} #{found.first} #{found.last}" if $VERBOSE found end |
#find_per_currency_and_day(items, currency, date) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/rapflag/poloniex.rb', line 147 def find_per_currency_and_day(items, currency, date) puts "Searching for #{currency} in day #{date}" if $VERBOSE start_time = date.to_time.to_i end_time = (date + 1).to_time.to_i found = items.find_all{|item| item.currency.eql?(currency) && item. >= start_time && item. < end_time} puts "find_per_currency_and_day: found #{found.size} #{found.first} #{found.last}" if $VERBOSE found end |
#get_usd_exchange(date_time = Time.now, from = 'BTC') ⇒ Object
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 |
# File 'lib/rapflag/poloniex.rb', line 20 def get_usd_exchange(date_time = Time.now, from='BTC') return 1.0 if from == 'USD' daily = ::Poloniex.get_all_daily_exchange_rates('BTC_GNT') 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 = 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 => err puts "Err #{err}" end |