Ruby:使用Hash跟踪股票交易 [英] Ruby: Keeping track of Stock Transaction using Hash

查看:97
本文介绍了Ruby:使用Hash跟踪股票交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby的新手,我正在尝试编写一个简单"系统的代码,因此我可以跟踪股票交易.计算平均价格,将来我将尝试获取股息信息.

I am new to Ruby, and I am trying to code a "simple" system, so I can keep track of stock transaction. Calculating the average price, and in the future I will try to get dividends information.

到目前为止,我的代码看起来像这样(请随意提出更好的方法来编写我的代码,正如我所说的,我是新来的.)

So far, my code looks like this (PLEASE feel free to propose better ways to do my code, as I said, I am new).

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

例如,我可以创建资产并进行买卖:

For example, I can create the asset, and make buys and sells:

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 

到目前为止,还可以(但我认为有一种更简洁,更易读的方式来执行此操作……不确定).

So far, it is OK (but I think that there is a cleaner and more readable way to do it... not sure).

但是假设我要查看卖"类型的所有交易. 我怎样才能做到这一点?如何查看ciel3.transaction数组的内部,该数组具有哈希值:type ?? Tnks

But lets suppose I want to view all the transactions of the type "SELL". How can I do this? How to look inside the ciel3.transaction array, that has the hash :type ?? Tnks

推荐答案

您可能需要一个Transaction类,而不是使用哈希.

Instead of using a hash, you probably want a Transaction class.

如果使用数据库支持它并使用ActiveRecord,则搜索将非常简单.

If you back it with a DB and use ActiveRecord, then searching will be very simple.

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

If not, you can do ciel3.transactions.select{|t| t[:type] == 'SELL'}

这篇关于Ruby:使用Hash跟踪股票交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆