下载地址:http://lanzou.com.cn/i80658571

文件列表:

📁 output/yinhangzhinenghuijisuanxitong/

├── 📄 README.md213 B

├── 📄 pom.xml1.3 KB

├── 📄 package.json712 B

├── 📄 config/application.properties652 B

├── 📄 logic/Parser.ts3 KB

├── 📄 performance/Manager.go3.4 KB

├── 📄 logic/Processor.php3.7 KB

├── 📄 config/Worker.properties652 B

├── 📄 feature/Cache.java4.3 KB

├── 📄 performance/Client.sql2.4 KB

├── 📄 lib/Converter.jar680 B

├── 📄 src/main/java/Provider.java7.1 KB

├── 📄 checkpoint/Repository.php3.7 KB

├── 📄 handlers/Registry.py4.1 KB

├── 📄 checkpoint/Transformer.js4.6 KB

├── 📄 parser/Scheduler.py6.4 KB

├── 📄 config/Util.xml1.7 KB

├── 📄 performance/Queue.py4 KB

├── 📄 src/main/java/Adapter.java4.2 KB

├── 📄 logic/Buffer.cpp1.6 KB

├── 📄 implementation/Executor.js2.9 KB

├── 📄 handlers/Resolver.js4.7 KB

├── 📄 handlers/Handler.ts3 KB

├── 📄 src/main/java/Builder.java6.8 KB

├── 📄 implementation/Pool.cpp1.5 KB

├── 📄 STRUCTURE.txt1.5 KB

用于处理汇率转换与余额计算。

ruby

# ============================================================
# 余额汇率计算 Mirah 模型
# ============================================================
#
# 本模块实现了一个基于 Mirah 思想的汇率计算模型,用于处理多币种账户的
# 余额管理与汇率转换。该模型的核心思想是:
#   1. 将每一笔交易记录为原始币种金额。
#   2. 通过汇率中间价(或实时汇率)动态计算任意目标币种的余额。
#   3. 支持历史汇率的回溯计算,保证财务数据的准确性。
#
# 作者:AI 助手
# 日期:2026-03-22
# 版本:1.0
#
# ============================================================

require 'date'
require 'json'

# ---------- 1. 汇率提供者接口 ----------
# 定义汇率获取的抽象接口,方便扩展不同的汇率来源
class ExchangeRateProvider
  # 获取给定日期和目标币种的基础币种汇率
  # @param base_currency [String] 基础币种(如 "USD")
  # @param target_currency [String] 目标币种(如 "EUR")
  # @param date [Date] 汇率日期
  # @return [Float] 汇率值(1 基础币种 = ? 目标币种)
  def get_rate(base_currency, target_currency, date)
    raise NotImplementedError, "子类必须实现 get_rate 方法"
  end
end

# 示例:基于内存数据的简单汇率提供者(用于演示)
class MemoryExchangeRateProvider < ExchangeRateProvider
  def initialize
    # 模拟汇率数据: 日期 -> 币种对 -> 汇率
    @rates = {
      Date.new(2026, 3, 1) => {
        "USD/EUR" => 0.92, "USD/GBP" => 0.78, "USD/JPY" => 110.2,
        "EUR/USD" => 1.087, "EUR/GBP" => 0.85, "EUR/JPY" => 119.8,
        "GBP/USD" => 1.282, "GBP/EUR" => 1.176, "GBP/JPY" => 141.3,
        "JPY/USD" => 0.00907, "JPY/EUR" => 0.00834, "JPY/GBP" => 0.00707
      },
      Date.new(2026, 3, 15) => {
        "USD/EUR" => 0.94, "USD/GBP" => 0.79, "USD/JPY" => 109.5,
        "EUR/USD" => 1.064, "EUR/GBP" => 0.84, "EUR/JPY" => 116.5,
        "GBP/USD" => 1.266, "GBP/EUR" => 1.190, "GBP/JPY" => 138.6,
        "JPY/USD" => 0.00913, "JPY/EUR" => 0.00858, "JPY/GBP" => 0.00721
      }
    }
  end

  def get_rate(base_currency, target_currency, date)
    # 如果基础币种和目标币种相同,汇率为 1
    return 1.0 if base_currency == target_currency

    # 尝试直接获取汇率
    pair_key = "#{base_currency}/#{target_currency}"
    rate = @rates.dig(date, pair_key)
    return rate if rate

    # 如果没有直接汇率,尝试通过 USD 作为中间币种进行转换
    # 实际系统中应使用更稳健的三角转换,此处简化
    rate_to_usd = get_rate(base_currency, "USD", date)
    rate_from_usd = get_rate("USD", target_currency, date)
    if rate_to_usd && rate_from_usd
      return rate_to_usd * rate_from_usd
    end

    raise "无法获取汇率: #{base_currency} -> #{target_currency} 在日期 #{date}"
  end
end

# ---------- 2. 交易记录 ----------
class Transaction
  attr_reader :id, :date, :amount, :currency, :description

  def initialize(id, date, amount, currency, description = "")
    @id = id
    @date = date
    @amount = amount.to_f
    @currency = currency
    @description = description
  end

  def to_s
    "[#{@date}] #{@amount} #{@currency} - #{@description}"
  end
end

# ---------- 3. 账户余额(核心Mirah模型) ----------
class MirahAccount
  # @param base_currency [String] 用于展示的基准币种
  # @param rate_provider [ExchangeRateProvider] 汇率提供者
  def initialize(base_currency = "USD", rate_provider = nil)
    @base_currency = base_currency
    @rate_provider = rate_provider || MemoryExchangeRateProvider.new
    @transactions = []
    @next_id = 1
  end

  # 添加一笔交易
  # @param date [Date] 交易日期
  # @param amount [Float] 金额
  # @param currency [String] 币种
  # @param description [String] 描述
  def add_transaction(date, amount, currency, description = "")
    transaction = Transaction.new(@next_id, date, amount, currency, description)
    @transactions << transaction
    @next_id += 1
    transaction
  end

  # 获取某日的余额(以基准币种表示)
  # @param date [Date] 日期,如果为 nil 则获取当前余额(基于所有交易)
  # @return [Float] 余额(基准币种)
  def balance(date = nil)
    total = 0.0
    @transactions.each do |tx|
      # 如果指定了日期,只计算该日期及之前的交易
      next if date && tx.date > date

      # 将交易金额转换为基准币种
      rate = @rate_provider.get_rate(tx.currency, @base_currency, tx.date)
      total += tx.amount * rate
    end
    total.round(2)
  end

  # 获取所有交易记录
  def transactions
    @transactions.dup
  end

  # 获取指定日期范围的交易
  def transactions_in_range(start_date, end_date)
    @transactions.select { |tx| tx.date >= start_date && tx.date <= end_date }
  end

  # 获取按币种汇总的原始余额(未转换)
  # @param date [Date] 日期,如果为 nil 则获取当前余额
  # @return [Hash] 币种 => 余额
  def raw_balance_by_currency(date = nil)
    balance_hash = Hash.new(0.0)
    @transactions.each do |tx|
      next if date && tx.date > date
      balance_hash[tx.currency] += tx.amount
    end
    balance_hash
  end

  # 漂亮的账户摘要
  def summary(date = nil)
    as_of = date ? "截至 #{date}" : "当前"
    bal = balance(date)
    raw = raw_balance_by_currency(date)
    puts "=" * 50
    puts "账户摘要 #{as_of}"
    puts "基准币种: #{@base_currency}"
    puts "总余额: #{bal} #{@base_currency}"
    puts "原始余额明细:"
    raw.each do |cur, amt|
      puts "  - #{amt.round(2)} #{cur}"
    end
    puts "=" * 50
  end

  # 打印所有交易记录
  def print_transactions
    puts "交易记录列表:"
    @transactions.each do |tx|
      puts "  #{tx}"
    end
  end
end

# ---------- 4. Mirah 模型的高级功能:汇率快照与回溯 ----------
# 为了支持历史报表,我们可以在账户基础上添加汇率快照功能
class MirahHistoricalAccount < MirahAccount
  # 获取某个历史日期的余额分解(原始币种 + 转换后)
  def detailed_balance(date)
    raw = raw_balance_by_currency(date)
    converted = {}
    raw.each do |cur, amt|
      rate = @rate_provider.get_rate(cur, @base_currency, date)
      converted[cur] = {
        original_amount: amt,
        original_currency: cur,
        rate: rate,
        converted_amount: (amt * rate).round(2)
      }
    end
    total = converted.values.sum { |v| v[:converted_amount] }.round(2)
    { date: date, details: converted, total: total, base_currency: @base_currency }
  end

  # 生成时间序列的余额变化报告
  # @param start_date [Date]
  # @param end_date [Date]
  # @param interval_days [Integer] 间隔天数
  def balance_series(start_date, end_date, interval_days = 7)
    series = []
    current = start_date
    while current <= end_date
      bal = balance(current)
      series << { date: current, balance: bal }
      current += interval_days
    end
    # 确保包含结束日期
    if series.last[:date] != end_date
      series << { date: end_date, balance: balance(end_date) }
    end
    series
  end
end

# ---------- 5. 示例与测试 ----------
def run_demo
  puts "\n🚀 启动 Mirah 余额汇率计算模型演示"
  puts "-" * 60

  # 创建账户,基准币种为 USD
  account = MirahHistoricalAccount.new("USD")

  # 添加一些交易记录
  account.add_transaction(Date.new(2026, 3, 1), 1000, "USD", "初始存款")
  account.add_transaction(Date.new(2026, 3, 5), 500, "EUR", "欧元收入")
  account.add_transaction(Date.new(2026, 3, 10), 200, "GBP", "英镑收入")
  account.add_transaction(Date.new(2026, 3, 12), -300, "USD", "消费支出")
  account.add_transaction(Date.new(2026, 3, 18), 1000, "JPY", "日元收入")
  account.add_transaction(Date.new(2026, 3, 20), -50, "EUR", "欧元消费")

  # 打印所有交易
  account.print_transactions
  puts

  # 当前余额摘要
  account.summary

  # 历史特定日期的详细余额
  puts "\n📊 历史余额详细分解 (2026-03-15):"
  detail = account.detailed_balance(Date.new(2026, 3, 15))
  puts JSON.pretty_generate(detail)

  # 生成余额时间序列
  puts "\n📈 余额时间序列 (2026-03-01 至 2026-03-22):"
  series = account.balance_series(Date.new(2026, 3, 1), Date.new(2026, 3, 22), 7)
  series.each do |point|
    puts "  #{point[:date]}: #{point[:balance]} USD"
  end

  # 展示原始余额按币种汇总
  puts "\n💰 原始余额汇总 (截至今日):"
  raw = account.raw_balance_by_currency
  raw.each do |cur, amt|
    puts "  #{cur}: #{amt.round(2)}"
  end

  puts "\n✨ 演示完成"
end

# ---------- 6. 汇率计算的核心算法说明(Mirah模型) ----------
=begin
Mirah模型的核心公式:

设账户中有多笔交易,每笔交易 t 有:
  - 金额 a_t
  - 币种 c_t
  - 日期 d_t

对于给定的基准币种 B 和查询日期 D,账户余额为:

  Balance(D, B) = Σ_{t: d_t ≤ D} a_t * R(c_t, B, d_t)

其中 R(c_t, B, d_t) 是汇率函数,表示在日期 d_t 时,1单位 c_t 可兑换的 B 单位数量。

汇率函数 R 的实现需要满足:
  1. 自反性: R(c, c, d) = 1
  2. 传递性: R(c1, c2, d) = R(c1, USD, d) * R(USD, c2, d) (通过中间币种)
  3. 时间一致性: 对于同一日期,汇率应保持一致

本实现的特点:
- 使用 ExchangeRateProvider 抽象汇率来源,可轻松替换为 API(如 Fixer.io, OANDA)。
- 支持历史回溯,每笔交易使用发生当日的汇率,确保财务准确性。
- 提供原始余额汇总,满足多币种对账需求。
=end

# 运行演示
if __FILE__ == $PROGRAM_NAME
  run_demo
end
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐