外贸FOB-CIF报价换算引擎·专业版
输入 / 输出
输入:EXW成本、国内费用、海运费(USD)、保险费率、利润率、汇率、投保加成、数量;输出:FOB/CFR/CIF 的 CNY 与 USD 单价、保险费(report 字典)。
使用示例
from trade_quotation import Quotation
q = Quotation(exw_cost_cny=100000, domestic_fee_cny=2000,
freight_usd=150, insurance_rate=0.001,
profit_rate=0.08, fx_rate=7.15, quantity=20)
print(q.report()) # FOB/CFR/CIF 人民币与美元
引擎代码:trade_quotation.py
# -*- coding: utf-8 -*-
"""外贸报价换算引擎:EXW成本 → FOB / CFR / CIF 单价(人民币与美元)。
标准公式(每计量单位,如每吨):
FOB = EXW成本 + 国内费用 + 利润
CFR = FOB + 海运费
CIF = CFR / (1 - 投保加成 × 保险费率) # 投保加成通常 1.10(按 CIF 价的110%投保)
保险费 = CIF × 投保加成 × 保险费率
"""
class Quotation:
def __init__(self, exw_cost_cny, domestic_fee_cny=0.0, freight_usd=0.0,
insurance_rate=0.0, profit_rate=0.0, fx_rate=7.15,
insured_coverage=1.10, quantity=1):
if exw_cost_cny < 0 or domestic_fee_cny < 0 or freight_usd < 0:
raise ValueError("费用不能为负")
if not (0 <= insurance_rate < 1):
raise ValueError("保险费率应在 [0,1)")
if profit_rate < 0 or fx_rate <= 0 or quantity <= 0:
raise ValueError("利润率非负、汇率为正、数量为正")
if not (1.0 <= insured_coverage <= 1.3):
raise ValueError("投保加成通常在 1.0~1.3")
self.exw = float(exw_cost_cny)
self.domestic = float(domestic_fee_cny)
self.freight_usd = float(freight_usd)
self.ins_rate = float(insurance_rate)
self.profit_rate = float(profit_rate)
self.fx = float(fx_rate)
self.coverage = float(insured_coverage)
self.qty = int(quantity)
def _per_unit_domestic(self):
return self.domestic / self.qty
def fob_cny(self):
base = self.exw + self._per_unit_domestic()
return round(base * (1 + self.profit_rate), 2)
def fob_usd(self):
return round(self.fob_cny() / self.fx, 2)
def _freight_cny_per_unit(self):
return self.freight_usd * self.fx / self.qty
def cfr_cny(self):
return round(self.fob_cny() + self._freight_cny_per_unit(), 2)
def cfr_usd(self):
return round(self.cfr_cny() / self.fx, 2)
def cif_cny(self):
cfr = self.cfr_cny()
denom = 1 - self.coverage * self.ins_rate
if denom <= 0:
raise ValueError("保险费率过高,CIF 无法计算")
return round(cfr / denom, 2)
def cif_usd(self):
return round(self.cif_cny() / self.fx, 2)
def insurance_cny(self):
return round(self.cif_cny() * self.coverage * self.ins_rate, 2)
def report(self):
return {
"FOB_CNY": self.fob_cny(), "FOB_USD": self.fob_usd(),
"CFR_CNY": self.cfr_cny(), "CFR_USD": self.cfr_usd(),
"CIF_CNY": self.cif_cny(), "CIF_USD": self.cif_usd(),
"insurance_CNY": self.insurance_cny(),
}
测试(6 正例 + 4 边界(test_trade_quotation.py 实跑全过):纯EXW、加利润、CIF 标准公式数值验算(10000→10011.01)、海运费与国内费用按数量摊、report 字段完整;负费用、非法费率、费率过高致分母≤0、非法汇率数量均报错。)
硬性规则
- 确定性输出:相同输入必得相同结果,时间/随机量通过参数注入;
- 非法输入(负数、越界、格式错误)一律抛 ValueError,不静默吞掉;
- 本引擎为算法/计算参考,集成到业务时请结合真实参数与压测,不替代专业判断。