2016-07-21 9 views
1

Rubyを初めて使いました。「シンプル」なシステムをコーディングしようとしていますので、株式取引を追跡できます。平均価格を計算すると、将来私は配当情報を取得しようとします。Ruby:ハッシュを使って株式取引を追跡する

これまでのところ、私のコードは次のようになっています(私が言ったように、私のコードを行うためのよりよい方法を自由に提案してください)。

require 'money' 
require 'money/bank/google_currency' 
require 'monetize' 
require 'date' 
require 'ystock' 
require 'ostruct' 

# (optional) 
# set the seconds after than the current rates are automatically expired 
# by default, they never expire 
Money::Bank::GoogleCurrency.ttl_in_seconds = 86400 
I18n.enforce_available_locales = false #erro no formatting... 
# set default bank to instance of GoogleCurrency 
Money.default_bank = Money::Bank::GoogleCurrency.new 

class Stock 

attr_accessor :code, :quantity, :price, :transactions, :spotprice 

    def initialize(code:) 
     @code = code 
     @quantity =00 
     @price = 00.to_money(:BRL) 
     @transactions = [] 
     @spotprice = 0.to_money(:BRL) 
    end  

    def spotprice 
     begin 
      asset_temp = Ystock.quote(@code.to_s + ".sa") # since it is South America. 
      asset_info = OpenStruct.new asset_temp # organize it. 
      @spotprice = asset_info.price.to_money(:BRL) # get the price. And transform it to Money, local currency 
     rescue => @error #Is there an TCP/IP error? 
      @spotprice = 0.to_money(:BRL) 
     end 
    end 

    def buy (quantity:, price:, fees:, date:0) 
     transactions.push type: "BUY", date: Date.strptime(date.to_s, '%d/%m/%Y'), quantity: quantity, price: price.to_money(:BRL), fees: fees.to_money(:BRL) 
     #Lets calculate the average price that we bought: 
     new_price = (((@quantity * @price.to_money(:BRL))) + ((quantity * price.to_money(:BRL)) + fees.to_money(:BRL)))/(@quantity + quantity) 
     @quantity += quantity 
     @price = new_price.to_money(:BRL) # new price is the average price. 
    end 

    def sell (quantity:,price:, fees:, date:) 
     transactions.push type: "SELL", date: Date.strptime(date.to_s,  '%d/%m/%Y'), quantity: quantity, price: price.to_money(:BRL), fees:  fees.to_money(:BRL) 
     @quantity -= quantity 
    end 
end 

例えば、私は、資産を作成し、買物を作り、販売していることができます。

ciel3 = Stock.new(code: "CIEL3") 
ciel3.buy(quantity: 100, price: 9.00, fees: 21.5, date: "12/05/2015") 
p ciel3 
ciel3.buy(quantity: 100,price: 12, fees: 21.7, date: "12/06/2015") 
ciel3.sell(quantity: 50,price: 11.5,fees: 20.86, date: "20/06/2015") 
p ciel3 
ciel3.buy(quantity: 200,price: 15,fees: 23.6, date: "12/07/2015") 
puts ciel3.price.format 
puts 
puts 
# puts ciel3.spotprice.format 
p ciel3.transactions 

これまでのところ、それはOKです(私は行うにはよりクリーンで読みやすい方法があると思いますそれは...確かではない)。

しかし、 "売る"タイプのすべての取引を表示するとします。 どうすればいいですか?ハッシュを持つciel3.transaction配列を調べる方法は? Tnks

答えて

3

ハッシュを使用する代わりに、おそらくTransactionクラスが必要です。

DBを使用してActiveRecordを使用すると、検索が非常に簡単になります。そうでない場合

することは、あなたはciel3.transactions.select{|t| t[:type] == 'SELL'}

+0

B.七、ありがとうございました!!それは完璧です。私はトランザクションクラスについて(まだ)分かりません。あとで見てみます。あなたは私のコードに他のヒントを与えますか? –

+0

RubyのTransactionクラスが見つかりませんでした。 Railsの場合のみ、それは同じですか? –

+0

独自の 'Transaction'クラス' class Transaction'を作成します。 –

0

を行うことができます私はRubyで簡単なコマンド・ライン・在庫管理アプリを書きました。おそらく、あなたのプロジェクトにはたくさんのコードを再利用することができます。

それは私のGitHubの上だ:https://github.com/brunofacca/udacity-inventory-manager

+0

"初心者の方には多くの可能性があり、専門家の心にはいくつかあります。" ;-) –

関連する問題