rapflag 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -2
- data/History.md +4 -0
- data/bin/rapflag +0 -1
- data/lib/rapflag/poloniex.rb +71 -37
- data/lib/rapflag/version.rb +1 -1
- data/rapflag.gemspec +1 -1
- data/spec/poloniex_spec.rb +13 -3
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ffaa4c17111d1c37c02118a096dff0930a93819
|
4
|
+
data.tar.gz: 2787ec4a5db18aee84c1947aeb2c5cfeac173a08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae3137d9aee15c69368d7d61d98effb9d8c383ad4cd338a4e63bdeb33264348af3de3d57b5a4175bc09c9250b18efecc798de10eab82c53eae4a4c74f064a336
|
7
|
+
data.tar.gz: 893844bbc5508ce8effda45098b1de91baaae9834ebb86d7359926662d901d248e5adbacbdfabe37cda8880429575151535d1757b85887a7faab220521191f4a
|
data/Gemfile
CHANGED
data/History.md
CHANGED
data/bin/rapflag
CHANGED
data/lib/rapflag/poloniex.rb
CHANGED
@@ -11,7 +11,8 @@ module RAPFLAG
|
|
11
11
|
|
12
12
|
class Poloniex < History
|
13
13
|
attr_reader :complete_balances, :active_loans, :lending_history, :deposits, :withdrawals, :deposit_addresses,
|
14
|
-
:trade_history, :available_account_balances, :open_orders, :tradable_balances
|
14
|
+
:trade_history, :available_account_balances, :open_orders, :tradable_balances,
|
15
|
+
:output_prefix
|
15
16
|
|
16
17
|
@@btc_to_usd = {}
|
17
18
|
@@bfx_to_usd = {}
|
@@ -51,7 +52,7 @@ module RAPFLAG
|
|
51
52
|
def dump_history
|
52
53
|
load_history_info
|
53
54
|
FileUtils.makedirs('output') unless File.directory?('output')
|
54
|
-
CSV.open(
|
55
|
+
CSV.open("#{@output_prefix}/trade_history.csv",'w+',
|
55
56
|
:col_sep => ';',
|
56
57
|
:write_headers=> true,
|
57
58
|
:headers => [ 'currency_pair'] + @trade_history.values.first.first.to_h.keys
|
@@ -62,7 +63,7 @@ module RAPFLAG
|
|
62
63
|
end
|
63
64
|
end
|
64
65
|
end
|
65
|
-
CSV.open(
|
66
|
+
CSV.open("#{@output_prefix}/lending_history.csv",'w+',
|
66
67
|
:col_sep => ';',
|
67
68
|
:write_headers=> true,
|
68
69
|
:headers => @lending_history.first.to_h.keys
|
@@ -71,7 +72,7 @@ module RAPFLAG
|
|
71
72
|
csv << info.to_h.values
|
72
73
|
end
|
73
74
|
end
|
74
|
-
CSV.open(
|
75
|
+
CSV.open("#{@output_prefix}/tradable_balances.csv",'w+',
|
75
76
|
:col_sep => ';',
|
76
77
|
:write_headers=> true,
|
77
78
|
:headers => [ 'from_currency', 'to_from_currency', ]
|
@@ -82,7 +83,7 @@ module RAPFLAG
|
|
82
83
|
end
|
83
84
|
end
|
84
85
|
end
|
85
|
-
CSV.open(
|
86
|
+
CSV.open("#{@output_prefix}/complete_balances.csv",'w+',
|
86
87
|
:col_sep => ';',
|
87
88
|
:write_headers=> true,
|
88
89
|
:headers => [ 'currency', 'available', 'onOrders', 'btcValue' ]
|
@@ -91,7 +92,7 @@ module RAPFLAG
|
|
91
92
|
csv << [balance[0]] + balance[1].values
|
92
93
|
end
|
93
94
|
end
|
94
|
-
CSV.open(
|
95
|
+
CSV.open("#{@output_prefix}/active_loans.csv",'w+',
|
95
96
|
:col_sep => ';',
|
96
97
|
:write_headers=> true,
|
97
98
|
:headers => [ 'key', 'id', 'currency', 'rate', 'amount', 'duration', 'autoRenew', 'date', 'fees', ]
|
@@ -103,7 +104,7 @@ module RAPFLAG
|
|
103
104
|
end
|
104
105
|
end
|
105
106
|
|
106
|
-
CSV.open(
|
107
|
+
CSV.open("#{@output_prefix}/available_account_balances.csv",'w+',
|
107
108
|
:col_sep => ';',
|
108
109
|
:write_headers=> true,
|
109
110
|
:headers => [ 'key', 'currency', 'balance']
|
@@ -114,7 +115,7 @@ module RAPFLAG
|
|
114
115
|
end
|
115
116
|
end
|
116
117
|
end
|
117
|
-
CSV.open(
|
118
|
+
CSV.open("#{@output_prefix}/deposit_addresses.csv",'w+',
|
118
119
|
:col_sep => ';',
|
119
120
|
:write_headers=> true,
|
120
121
|
:headers => [ 'currency', 'id']
|
@@ -123,7 +124,7 @@ module RAPFLAG
|
|
123
124
|
csv << [currency, id]
|
124
125
|
end
|
125
126
|
end
|
126
|
-
CSV.open(
|
127
|
+
CSV.open("#{@output_prefix}/withdrawals.csv",'w+',
|
127
128
|
:col_sep => ';',
|
128
129
|
:write_headers=> true,
|
129
130
|
:headers => @deposits.first.to_h.keys
|
@@ -132,7 +133,7 @@ module RAPFLAG
|
|
132
133
|
csv << info.to_h.values
|
133
134
|
end
|
134
135
|
end
|
135
|
-
CSV.open(
|
136
|
+
CSV.open("#{@output_prefix}/deposits.csv",'w+',
|
136
137
|
:col_sep => ';',
|
137
138
|
:write_headers=> true,
|
138
139
|
:headers => @withdrawals.first.to_h.keys
|
@@ -191,55 +192,56 @@ module RAPFLAG
|
|
191
192
|
Time.parse(@lending_history.collect{|x| x.close}.min).to_i,
|
192
193
|
@withdrawals.collect{|x| x.timestamp}.min,].min
|
193
194
|
|
194
|
-
min_date = Time.at(min_time).to_date
|
195
|
+
min_date = Time.at(min_time).to_date - 1
|
195
196
|
max_date = Time.at(max_time).to_date
|
196
197
|
|
197
198
|
puts "We start using the available_account_balances"
|
198
199
|
pp @available_account_balances
|
199
|
-
puts
|
200
|
-
puts "Should we better start using the complete_balances"
|
201
200
|
pp @complete_balances
|
202
|
-
puts "How should I handle the following currency_pair"
|
203
|
-
pp @trade_history.keys
|
204
201
|
@available_account_balances.each do |key, balances|
|
205
202
|
balances.each do |currency, balance|
|
206
203
|
puts "Calculation history for #{key} #{currency} with current balance of #{balance}"
|
207
|
-
out_name = "
|
204
|
+
out_name = "#{@output_prefix}/#{key}_#{currency}.csv"
|
208
205
|
FileUtils.makedirs(File.dirname(out_name)) unless File.exists?(File.dirname(out_name))
|
209
206
|
@history = []
|
210
|
-
current_day =
|
211
|
-
current_balance =
|
212
|
-
while (current_day
|
207
|
+
current_day = min_date
|
208
|
+
current_balance = 0.0
|
209
|
+
while (current_day < max_date)
|
210
|
+
puts "#{key}: at #{current_day}" if current_day.day == 1 && current_day.month % 3 == 1
|
213
211
|
entry = OpenStruct.new
|
214
212
|
entry.current_day = current_day
|
215
|
-
entry.
|
213
|
+
entry.balance_BEG = current_balance
|
216
214
|
|
217
215
|
deposits = find_per_currency_and_day(@deposits, currency,current_day)
|
218
216
|
sum_deposits = 0.0; deposits.each{ |x| sum_deposits += x.amount.to_f }
|
219
|
-
entry.deposits = sum_deposits
|
220
217
|
|
221
218
|
withdrawals = find_per_currency_and_day(@withdrawals, currency, current_day)
|
222
219
|
sum_withdrawals = 0.0; withdrawals.each{ |x| sum_withdrawals += x.amount.to_f }
|
223
|
-
entry.withdrawals = sum_withdrawals
|
224
220
|
|
225
221
|
lendings = find_lending_info_day(currency, current_day)
|
226
|
-
|
227
|
-
|
228
|
-
entry.fees = sum_fee
|
222
|
+
# fee field is negative, therefore we let sum_fee be negative, too
|
223
|
+
income = 0.0; sum_fee = 0.0; lendings.each{ |x| income += x.earned.to_f; sum_fee += x.fee.to_f }
|
229
224
|
|
230
225
|
# End_of_Day_Balance = End_of_Day_Balance(-1) + Deposits - Withdrawals + Lending_Income - Trading_Fees + Purchases - Sales
|
231
226
|
sales = find_day_trade(currency, current_day, 'buy')
|
232
227
|
sum_sales = 0.0; sales.each{ |sale| sum_sales += sale.amount.to_f*sale.rate.to_f }
|
233
|
-
entry.sales = sum_sales
|
234
228
|
|
235
229
|
purchases = find_day_trade(currency, current_day, 'sell')
|
236
|
-
sum_purchase = 0.0; purchases.each{ |purchase| sum_purchase += purchase.amount.to_f
|
230
|
+
sum_purchase = 0.0; purchases.each{ |purchase| sum_purchase += purchase.amount.to_f*purchase.rate.to_f }
|
231
|
+
diff_day = sum_deposits - sum_withdrawals + sum_purchase - sum_sales + income
|
232
|
+
|
233
|
+
entry.deposits = sum_deposits
|
234
|
+
entry.income = income
|
235
|
+
entry.withdraw = sum_withdrawals
|
236
|
+
entry.sales = sum_sales
|
237
237
|
entry.purchases = sum_purchase
|
238
|
-
entry.
|
238
|
+
entry.balance_END = current_balance + diff_day
|
239
|
+
entry.fees = sum_fee
|
240
|
+
entry.day_difference = diff_day
|
239
241
|
@history << entry
|
240
|
-
current_day
|
242
|
+
current_day += 1
|
241
243
|
# balance for previous day
|
242
|
-
current_balance = entry.
|
244
|
+
current_balance = entry.balance_END
|
243
245
|
end
|
244
246
|
next unless @history.size > 0
|
245
247
|
CSV.open(out_name,'w+',
|
@@ -256,6 +258,7 @@ module RAPFLAG
|
|
256
258
|
end
|
257
259
|
private
|
258
260
|
def check_config
|
261
|
+
@output_prefix = 'output/poloniex'
|
259
262
|
@spec_data = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec', 'data'))
|
260
263
|
['poloniex_api_key',
|
261
264
|
'poloniex_secret',
|
@@ -269,24 +272,57 @@ module RAPFLAG
|
|
269
272
|
nil
|
270
273
|
end
|
271
274
|
private
|
275
|
+
|
276
|
+
def load_lending_history
|
277
|
+
# we must load this history in pieces, as we cannot return all items at once
|
278
|
+
end_time = Time.now.to_i
|
279
|
+
limit = 1000
|
280
|
+
full_history = []
|
281
|
+
while true
|
282
|
+
@lending_history = []
|
283
|
+
puts "After #{full_history.size} items. Loading upto #{limit} lending_history items before #{Time.at(end_time)}"
|
284
|
+
lendings = load_or_save_json(:lending_history, {:start => 0, :end_time => end_time, :limit => limit})
|
285
|
+
break if lendings.size == 0
|
286
|
+
lendings.each_with_index {|item| full_history.push(OpenStruct.new(item.clone)) };
|
287
|
+
min_time = lendings.collect{|x| x['close']}.min
|
288
|
+
min_time_i = Time.parse(min_time).to_i
|
289
|
+
end_time = min_time_i-1
|
290
|
+
break if defined?(RSpec) # We don't simulate loading more than 1 json file
|
291
|
+
end
|
292
|
+
@lending_history = full_history
|
293
|
+
puts "Loaded #{@lending_history.size} lending_history items"
|
294
|
+
end
|
295
|
+
|
272
296
|
def load_or_save_json(name, param = nil)
|
297
|
+
load_json = nil
|
273
298
|
json_file = File.join(@spec_data, name.to_s + '.json')
|
274
299
|
parse_body = "@#{name} = JSON.parse(body)"
|
275
|
-
load_json = param ? "::Poloniex.#{name.to_s}('#{param}').body" : "::Poloniex.#{name.to_s}.body"
|
276
300
|
body = nil
|
277
301
|
if File.directory?(@spec_data) && File.exist?(json_file) && defined?(RSpec)
|
278
302
|
body = IO.read(json_file)
|
279
303
|
else
|
304
|
+
if param
|
305
|
+
if param.is_a?(String)
|
306
|
+
load_json = "::Poloniex.#{name.to_s}('#{param}').body"
|
307
|
+
else
|
308
|
+
load_json = "::Poloniex.#{name.to_s}(#{param.values.join(',')}).body"
|
309
|
+
end
|
310
|
+
else
|
311
|
+
load_json = "::Poloniex.#{name.to_s}.body"
|
312
|
+
end
|
313
|
+
# puts "Poloniex version #{::Poloniex::VERSION}: Will call '#{load_json}' for #{name}"
|
280
314
|
body = eval(load_json)
|
281
|
-
File.
|
315
|
+
FileUtils.makedirs(File.dirname(json_file))
|
316
|
+
File.open(json_file, 'w+') { |f| f.write(body)} if body && defined?(RSpec)
|
282
317
|
end
|
283
318
|
eval(parse_body) if body
|
284
319
|
rescue => error
|
285
|
-
puts "Calling '#{load_json}' for #{name} failed with error: #{error}"
|
320
|
+
puts "Calling version #{::Poloniex::VERSION} '#{load_json}' for #{name} failed with error: #{error}"
|
286
321
|
# puts "Backtrace #{error.backtrace.join("\n")}"
|
287
322
|
exit(1)
|
288
323
|
end
|
289
324
|
def load_history_info
|
325
|
+
return if @balances && @balances.size > 0
|
290
326
|
check_config
|
291
327
|
begin
|
292
328
|
@balances = load_or_save_json(:balances)
|
@@ -297,6 +333,7 @@ module RAPFLAG
|
|
297
333
|
puts "Backtrace #{error.backtrace.join("\n")}"
|
298
334
|
exit 1
|
299
335
|
end
|
336
|
+
load_lending_history
|
300
337
|
@active_loans = load_or_save_json(:active_loans)
|
301
338
|
@available_account_balances = load_or_save_json(:available_account_balances)
|
302
339
|
all = load_or_save_json(:complete_balances)
|
@@ -309,10 +346,6 @@ module RAPFLAG
|
|
309
346
|
@deposits_withdrawals['deposits'].each {|x| @deposits << OpenStruct.new(x) };
|
310
347
|
@withdrawals =[]
|
311
348
|
@deposits_withdrawals['withdrawals'].each {|x| @withdrawals << OpenStruct.new(x) };
|
312
|
-
info = load_or_save_json(:lending_history)
|
313
|
-
@lending_history = []
|
314
|
-
info.each {|x| @lending_history << OpenStruct.new(x) };
|
315
|
-
|
316
349
|
@open_orders = load_or_save_json(:open_orders, 'all')
|
317
350
|
info = load_or_save_json(:trade_history, 'all')
|
318
351
|
@trade_history = {}
|
@@ -326,6 +359,7 @@ module RAPFLAG
|
|
326
359
|
@active_loans # key
|
327
360
|
@provided_loans = []; @active_loans['provided'].each {|x| @provided_loans << OpenStruct.new(x) };
|
328
361
|
@used_loans = []; @active_loans['used'].each {|x| @used_loans << OpenStruct.new(x) };
|
362
|
+
|
329
363
|
end
|
330
364
|
end
|
331
365
|
end
|
data/lib/rapflag/version.rb
CHANGED
data/rapflag.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency '
|
20
|
+
spec.add_dependency 'ypoloniex', '>= 1.0.2'
|
21
21
|
spec.add_dependency 'bitfinex-rb'
|
22
22
|
spec.add_dependency 'trollop'
|
23
23
|
|
data/spec/poloniex_spec.rb
CHANGED
@@ -20,13 +20,23 @@ describe RAPFLAG::Poloniex do
|
|
20
20
|
expect(@rap.currency).to eql('BTC')
|
21
21
|
expect(File.exist?(Poloniex_CSV_Test_Lending_File)).to eql(true)
|
22
22
|
lines = IO.readlines(Poloniex_CSV_Test_Lending_File)
|
23
|
-
expect(lines.first.chomp).to eql('current_day;
|
23
|
+
expect(lines.first.chomp).to eql('current_day;balance_BEG;deposits;income;withdraw;sales;purchases;balance_END;fees;day_difference')
|
24
|
+
|
24
25
|
first_trade = lines.find{|line| /2017-03-25/.match(line)}.chomp
|
25
26
|
day_difference = first_trade.split(';')[-1]
|
26
|
-
expect(day_difference.to_f).to eql(-
|
27
|
+
expect(day_difference.to_f).to eql(-0.9895374700000007)
|
28
|
+
|
27
29
|
second_trade = lines.find{|line| /2017-03-24/.match(line)}.chomp
|
28
30
|
day_difference = second_trade.split(';')[-1]
|
29
|
-
expect(day_difference.to_f).to eql(-3.
|
31
|
+
expect(day_difference.to_f).to eql(-3.6381863327881176)
|
32
|
+
|
33
|
+
third_trade = lines.find{|line| /2016-06-17/.match(line)}.chomp
|
34
|
+
day_difference = third_trade.split(';')[-1]
|
35
|
+
expect(day_difference.to_f).to eql(7.177347194443261)
|
36
|
+
income1 = first_trade.split(';')[3]
|
37
|
+
expect(income1.to_f).to eql(0.01046259)
|
38
|
+
income2 = second_trade.split(';')[3]
|
39
|
+
expect(income2.to_f).to eql(0.03696987000000001)
|
30
40
|
end
|
31
41
|
it 'should have correct size' do
|
32
42
|
expect(@rap.history.size).to eql(368)
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rapflag
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zeno R.R. Davatz, Niklaus Giger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: ypoloniex
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.0.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 1.0.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bitfinex-rb
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|