-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculator.rb
62 lines (53 loc) · 1.66 KB
/
calculator.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
52
53
54
55
56
57
58
59
60
61
62
# coding: utf-8
require_relative "stock"
class Caculator
def initialize(account)
@account = account
end
def getChargesForBuy(stock)
stock.sum * @account.charges_ratio * 0.01 + @account.other_charge
end
def getChargesForSale(cur_info, stock)
cur_price = cur_info[:deal].to_f
cur_price * stock.buy_quantity * (@account.charges_ratio + @account.tax_ratio) * 0.01 + @account.other_charge
end
def getGrossProfit(cur_info, stock)
cur_price = cur_info[:deal].to_f
cur_price * stock.buy_quantity - stock.sum
end
def getProfit(cur_info, stock)
g_profit = self.getGrossProfit(cur_info, stock) #毛利润
buy_charges = self.getChargesForBuy(stock)
sale_charges = self.getChargesForSale(cur_info, stock)
profit = g_profit - buy_charges - sale_charges
end
def getProfitPercentage(profit, stock)
profit / (stock.buy_price * stock.buy_quantity) * 100
end
def getAllProfit(infos)
profits = {}
@account.all_stock.each do |stock|
profits[stock.code] = []
info = infos[stock.code]
profit = self.getProfit(info, stock)
profit_percentage = self.getProfitPercentage(profit, stock)
# profits[stock.code] = %w[#{profit} #{profit_percentage}]
profits[stock.code] << profit
profits[stock.code] << profit_percentage
end
return profits
end
def dumpInfo(infos)
values = infos.values
values.each do |value|
print "===================\n"
if value.length != @@inter_name.length
raise ArgumentError, "length error"
end
0.upto(value.length - 1) do |i|
print "#{@@inter_name[i]}:\t"
print "#{value[i]}\n"
end
end
end
end