【问题标题】:Ruby: Keeping track of Stock Transaction using HashRuby:使用哈希跟踪股票交易
【发布时间】:2016-07-22 04:06:18
【问题描述】:

我是 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 

到目前为止,还可以(但我认为有一种更清洁、更易读的方法……不确定)。

但假设我想查看所有类型为“SELL”的交易。 我怎样才能做到这一点?如何查看具有哈希的 ciel3.transaction 数组内部:type ?? 坦克

【问题讨论】:

    标签: ruby hash store


    【解决方案1】:

    您可能需要Transaction 类,而不是使用哈希。

    如果你用数据库支持它并使用 ActiveRecord,那么搜索将非常简单。

    如果没有,你可以ciel3.transactions.select{|t| t[:type] == 'SELL'}

    【讨论】:

    • B.七,谢谢!!这十分完美。我(还)不知道 Transaction 类。我会看一下。您能给我的代码提供任何其他提示吗?
    • 我找不到 Ruby 的 Transaction 类。只针对 Rails,是一样的吗?
    • 你会创建自己的Transaction 类:class Transaction
    • 就其他提示而言,我推测您找到了此代码。我认为您可以通过编写自己的代码来了解更多信息。如果您阅读了一些 Ruby 教程,那么应该可以编写自己的代码来执行此操作。
    • 不,我没有找到这个代码。这是我做的。我会把这当作一种恭维! :D 我刚刚使用了现成的代码(宝石)到货币(所以我将能够将其转换为美元、欧元等)和宝石从雅虎检索股票价格。课程和方法由我完成。我明白了,您对我的建议是编写 Transaction 类,并使其成为 ActiveRecord 的子类。所以我可以在 DB 得到它。但这仅适用于 Rails 还是适用于 Ruby? (Rails 我还没有开始)我正在阅读一些关于 Ruby 的书籍。有什么建议吗?
    【解决方案2】:

    我用 Ruby 编写了一个简单的命令行库存管理应用程序。您可能可以为您的项目重用大量代码。

    在我的 GitHub 上:https://github.com/brunofacca/udacity-inventory-manager

    【讨论】:

    • "在初学者的头脑中有很多可能性,在专家的头脑中很少。" ;-)
    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多