【理财类-01-08】20260716 ZFB积存金“多笔买入 一笔卖出的”收益计算(Python)保存CSV 回本平衡测算&目标收益定价&持仓损益结算
·

背景需求
等了一天,单笔买入的积存金没有达到回本金额,所以积存金快进快出赚差价只是理想状态,又一次套住了。
但是我看到之前套住了两笔积存金的均价从1027元降低到902元。
所以我想做一个“多笔买入、一次卖出”的回本价预估
一、“多笔买入一次卖出”的保本盈亏平衡计算器

'''
积存金多笔合并一笔卖出,保本盈亏平衡计算器(多笔一次性卖出)
1、支付宝积存金 卖出手续费固定0.4%
2、多笔持仓汇总,计算整仓保本卖出单价
豆包、阿夏
20260716
'''
def calc_multi_gold_break_even(buy_list):
"""
:param buy_list: 列表,元素为元组(买入单价, 克重),存放所有买入记录
"""
total_weight = 0.0
total_capital = 0.0
# 汇总全部持仓总克重、总投入本金
for p, w in buy_list:
total_capital += p * w
total_weight += w
if total_weight <= 0:
raise ValueError("总克重不能为0")
# 整仓平均成本价
avg_cost = total_capital / total_weight
# 保本公式:卖出价*(1-0.004) = 平均成本
sell_break_price = avg_cost / 0.996
rise_per_gram = sell_break_price - avg_cost
rise_rate = rise_per_gram / avg_cost * 100
total_sell_value = sell_break_price * total_weight
sell_fee = total_sell_value * 0.004
net_receive = total_sell_value - sell_fee
total_profit = net_receive - total_capital
profit_rate = total_profit / total_capital * 100
# 打印每一笔明细
print("===== 全部买入持仓明细 =====")
for idx, (p, w) in enumerate(buy_list, 1):
single_cost = p * w
print(f"第{idx}笔:单价{p:.2f}元/g,克重{w:.4f}g,投入{single_cost:.2f}元")
print("-" * 60)
print(f"合计总克重:{total_weight:.4f} g")
print(f"合计总投入本金:{total_capital:.2f} 元")
print(f"持仓平均成本价:{avg_cost:.2f} 元/g")
print("-" * 60)
print(f"整仓保本卖出单价:{sell_break_price:.2f} 元/克")
print(f"每克需要上涨:{rise_per_gram:.2f} 元")
print(f"上涨幅度:{rise_rate:.3f} %")
print("-" * 60)
print(f"保本卖出总市值:{total_sell_value:.2f} 元")
print(f"卖出手续费(0.4%):{sell_fee:.2f} 元")
print(f"卖出实际到手:{net_receive:.2f} 元")
print(f"保本总盈亏:{total_profit:.2f} 元,收益率:{profit_rate:.3f} %")
return {
"总克重": round(total_weight, 4),
"总本金": round(total_capital, 2),
"平均成本": round(avg_cost, 2),
"保本卖出价": round(sell_break_price, 2)
}
if __name__ == "__main__":
# 你的三笔买入记录
buy_records = [
(1040.63, 1.0282),
(1029.48, 1.0199),
(879.1, 11.3753),
(878.5, 11.3830),
]
res = calc_multi_gold_break_even(buy_records)
在代码最后部分,把多笔买入的 价格、克数,手动输入


目前四笔的均价是

运行后结果:
显示四笔均价是891.7元,需要再涨3.58元, 在895.28元卖出,可以保本。

二、“多笔买入一次卖出”的目标收益定价计算器
用同样思路,制作目标收益定价计算器

'''
第二版:多笔合并 目标收益定价计算器(指定每克纯利,求卖出价)
1、ZFB积存金ZSYH 多笔持仓一次性卖出,设定每克固定纯收入,计算目标卖出价
2、卖出手续费固定0.4%
新增:本金收益率=目标总纯利÷投入本金×100%
豆包、阿夏
20260716
'''
def calc_multi_gold_profit_target(buy_list, target_profit_per_g):
sell_fee_rate = 0.004
total_weight = 0.0
total_capital = 0.0
# 汇总所有买入记录
for p, w in buy_list:
total_capital += p * w
total_weight += w
if total_weight <= 0:
raise ValueError("总持仓克重不能为0或负数")
total_capital = round(total_capital, 2)
# 目标总纯利润
target_total_profit = target_profit_per_g * total_weight
# 卖出后需要到手的总现金
target_get_cash = total_capital + target_total_profit
# 推导公式:到手现金 = 卖出单价 * 总克重 * (1 - 手续费率)
need_sell_price = target_get_cash / (total_weight * (1 - sell_fee_rate))
avg_cost = total_capital / total_weight
rise_per_gram = need_sell_price - avg_cost
rise_percent = rise_per_gram / avg_cost * 100
total_sell_amount = need_sell_price * total_weight
deduct_fee = total_sell_amount * sell_fee_rate
# 目标收益率
target_profit_rate = target_total_profit / total_capital * 100 if total_capital != 0 else 0
# 打印每笔持仓明细
print("=" * 50)
print(" 支付宝浙商积存金【多笔合并盈利测算】")
print(f"卖出手续费固定:{sell_fee_rate*100}%")
print("=" * 50)
print("【持仓买入明细】")
for idx, (price, w) in enumerate(buy_list, 1):
single_cost = price * w
print(f"第{idx}笔|单价{price:.2f}元/g|克重{w:.4f}g|投入{single_cost:.2f}元")
print("-" * 60)
print(f"合计总持有克重:{total_weight:.4f} g")
print(f"合计总投入本金:{total_capital:.2f} 元")
print(f"整仓平均成本价:{avg_cost:.2f} 元/g")
print("-" * 60)
print(f"目标每克纯收益:{target_profit_per_g:.2f} 元")
print(f"目标全部总纯收益:{target_total_profit:.2f} 元")
print(f"目标本金收益率:{target_profit_rate:.3f} %")
print("-" * 60)
print(f"达成目标必须卖出单价:{need_sell_price:.2f} 元/克")
print(f"金价相对成本需上涨:{rise_per_gram:.2f} 元/克")
print(f"对应上涨幅度:{rise_percent:.3f} %")
print("-" * 60)
print(f"卖出总成交额:{total_sell_amount:.2f} 元")
print(f"卖出扣除手续费:{deduct_fee:.2f} 元")
print(f"卖出到手总现金:{target_get_cash:.2f} 元")
print("=" * 50)
result = {
"总克重": round(total_weight, 4),
"总本金": total_capital,
"平均成本": round(avg_cost, 2),
"目标每克纯利": target_profit_per_g,
"目标总利润": round(target_total_profit, 2),
"目标收益率%": round(target_profit_rate, 3),
"所需卖出单价": round(need_sell_price, 2),
"每克上涨金额": round(rise_per_gram, 2),
"涨幅%": round(rise_percent, 3),
"卖出手续费": round(deduct_fee, 2),
"到手金额": round(target_get_cash, 2)
}
return result
if __name__ == "__main__":
# 1. 填写你的多笔买入列表 (买入单价, 克重)
buy_records = [
(1040.63, 1.0282),
(1029.48, 1.0199),
(879.1, 11.3753),
(878.5, 11.3830),
]
# 设置目标每克纯收益
target_per_gain = 5
# 执行计算
res = calc_multi_gold_profit_target(buy_records, target_per_gain)
# ========== 交互式输入模式(取消注释即可使用) ==========
# try:
# count = int(input("一共有几笔持仓:"))
# records = []
# for i in range(1, count+1):
# p = float(input(f"第{i}笔买入单价:"))
# w = float(input(f"第{i}笔克重:"))
# records.append((p, w))
# target = float(input("目标每克纯收益(元):"))
# calc_multi_gold_profit_target(records, target)
# except Exception as e:
# print("输入错误:", e)


前文提到895.28元保本,加上5元=900.3元才能每克5元的收入

三、“多笔买入一次卖出”的持仓实际损益计算器

修改多笔参数,假设卖出价是910元

'''
第三版:多笔合并持仓实际损益计算器(多笔买入统一卖出,输入卖出价算盈亏,可负)
1、ZFB积存金ZSYH 多笔持仓一次性卖出,输入卖出单价自动计算整体净收益
2、卖出手续费固定0.4%
新增:收益率 = 总净收益 ÷ 投入本金 × 100%
豆包、阿夏
20260716
'''
def calc_multi_gold_actual_profit(buy_list, sell_price):
sell_fee_rate = 0.004 # 卖出手续费0.4%
total_weight = 0.0
total_capital = 0.0
# 汇总所有多笔买入:总克重、总投入本金
for p, w in buy_list:
total_capital += p * w
total_weight += w
if total_weight <= 0:
raise ValueError("总持仓克重不能为0或负数")
total_capital = round(total_capital, 2)
avg_cost = total_capital / total_weight
# 卖出总成交额
sell_total = sell_price * total_weight
# 卖出手续费
fee = sell_total * sell_fee_rate
# 卖出实际到手现金
get_cash = sell_total - fee
# 整体净盈亏(负数=亏损)
total_profit = get_cash - total_capital
# 每克平均净收益
profit_per_g = total_profit / total_weight
# 本金收益率
profit_rate = total_profit / total_capital * 100 if total_capital != 0 else 0
# 打印明细
print("=" * 55)
print(" 支付宝浙商积存金【多笔合并实际损益测算】")
print(f"卖出手续费:成交金额的{sell_fee_rate*100}%")
print("=" * 55)
print("【全部买入持仓明细】")
for idx, (price, w) in enumerate(buy_list, 1):
single_cost = price * w
print(f"第{idx}笔|买入单价{price:.2f}元/g|克重{w:.4f}g|单笔本金{single_cost:.2f}元")
print("-" * 60)
print(f"合计总持有克重:{total_weight:.4f} g")
print(f"合计投入总本金:{total_capital:.2f} 元")
print(f"整仓平均成本价:{avg_cost:.2f} 元/g")
print(f"统一卖出单价:{sell_price:.2f} 元/g")
print("-" * 60)
print(f"卖出总成交额:{sell_total:.2f} 元")
print(f"卖出扣除手续费:{fee:.2f} 元")
print(f"卖出到手总金额:{get_cash:.2f} 元")
print("-" * 60)
print(f"整体净收益:{total_profit:.2f} 元(负数代表亏损)")
print(f"每克平均净收益:{profit_per_g:.2f} 元/克")
print(f"本金收益率:{profit_rate:.3f} %(净收益÷总投入本金)")
print("=" * 55)
# 返回计算结果字典
res = {
"总克重": round(total_weight, 4),
"总本金": total_capital,
"平均成本": round(avg_cost, 2),
"卖出单价": sell_price,
"卖出成交额": round(sell_total, 2),
"卖出手续费": round(fee, 2),
"到手金额": round(get_cash, 2),
"总净盈亏": round(total_profit, 2),
"每克盈亏": round(profit_per_g, 2),
"收益率%": round(profit_rate, 3)
}
return res
if __name__ == "__main__":
# 1、固定多笔持仓数据(你的三笔买入记录)
buy_records = [
(1040.63, 1.0282),
(1029.48, 1.0199),
(879.1, 11.3753),
(878.5, 11.3830),
]
# 设置统一卖出单价,自行修改
sell_target_price = 910.0
# 执行计算
result = calc_multi_gold_actual_profit(buy_records, sell_target_price)
# ========== 交互式手动录入模式(取消注释启用) ==========
# try:
# num = int(input("一共有几笔积存金持仓:"))
# buy_data = []
# for i in range(1, num + 1):
# p_in = float(input(f"第{i}笔买入单价(元/g):"))
# w_in = float(input(f"第{i}笔克重(g):"))
# buy_data.append((p_in, w_in))
# sell_p = float(input("整仓统一卖出单价(元/g):"))
# calc_multi_gold_actual_profit(buy_data, sell_p)
# except Exception as err:
# print("输入异常:", err)

感悟:
积存金只能做地埋高卖(做多)的差价,所以风险极大。
四、降低平均价格,如何补仓
等了两天积存金价格越来越低(在876-878之间徘徊,),但我没有钱补仓了。我想计算一下,如果把平均价压到878元,还需要补多少克、多少钱?



'''
补仓计算
积存金多笔合并一笔卖出,保本盈亏平衡计算器 + 加仓摊薄成本测算
1、支付宝积存金 卖出手续费固定0.4%
2、多笔持仓汇总,计算整仓保本卖出单价
3、新增:给定加仓金价,计算需要再买多少克才能把平均成本压到目标价以内
豆包、阿夏
20260716
'''
def calc_multi_gold_break_even(buy_list):
"""
:param buy_list: 列表,元素为元组(买入单价, 克重),存放所有买入记录
"""
total_weight = 0.0
total_capital = 0.0
# 汇总全部持仓总克重、总投入本金
for p, w in buy_list:
total_capital += p * w
total_weight += w
if total_weight <= 0:
raise ValueError("总克重不能为0")
# 整仓平均成本价
avg_cost = total_capital / total_weight
# 保本公式:卖出价*(1-0.004) = 平均成本
sell_break_price = avg_cost / 0.996
rise_per_gram = sell_break_price - avg_cost
rise_rate = rise_per_gram / avg_cost * 100
total_sell_value = sell_break_price * total_weight
sell_fee = total_sell_value * 0.004
net_receive = total_sell_value - sell_fee
total_profit = net_receive - total_capital
profit_rate = total_profit / total_capital * 100
# 打印每一笔明细
print("===== 全部买入持仓明细 =====")
for idx, (p, w) in enumerate(buy_list, 1):
single_cost = p * w
print(f"第{idx}笔:单价{p:.2f}元/g,克重{w:.4f}g,投入{single_cost:.2f}元")
print("-" * 60)
print(f"合计总克重:{total_weight:.4f} g")
print(f"合计总投入本金:{total_capital:.2f} 元")
print(f"持仓平均成本价:{avg_cost:.2f} 元/g")
print("-" * 60)
print(f"整仓保本卖出单价:{sell_break_price:.2f} 元/克")
print(f"每克需要上涨:{rise_per_gram:.2f} 元")
print(f"上涨幅度:{rise_rate:.3f} %")
print("-" * 60)
print(f"保本卖出总市值:{total_sell_value:.2f} 元")
print(f"卖出手续费(0.4%):{sell_fee:.2f} 元")
print(f"卖出实际到手:{net_receive:.2f} 元")
print(f"保本总盈亏:{total_profit:.2f} 元,收益率:{profit_rate:.3f} %")
return {
"总克重": round(total_weight, 4),
"总本金": round(total_capital, 2),
"平均成本": round(avg_cost, 2),
"保本卖出价": round(sell_break_price, 2)
}
def calc_add_weight_to_lower_avg(total_capital, total_weight, target_avg, add_price):
"""
计算需要加仓多少克,才能把平均成本压到 target_avg 以内
:param total_capital: 原有总本金
:param total_weight: 原有总克重
:param target_avg: 目标平均成本(如878)
:param add_price: 接下来加仓的单价
:return: 字典包含所需加仓克重、加仓总金额等
"""
old_avg = total_capital / total_weight
print("\n========== 加仓摊薄成本测算 ==========")
print(f"当前持仓平均成本:{old_avg:.2f} 元/g")
print(f"目标平均成本上限:{target_avg:.2f} 元/g")
print(f"本次加仓买入单价:{add_price:.2f} 元/g")
print("-" * 50)
if add_price >= target_avg:
print("⚠️ 警告:加仓金价 ≥ 目标成本,无论买多少克,平均成本无法压到目标值以下!")
return {
"可行": False,
"需加仓克重": None,
"加仓总金额": None
}
# 计算公式推导得出最少加仓克重
numerator = target_avg * total_weight - total_capital
denominator = add_price - target_avg
min_add_x = numerator / denominator
# 向上取0.0001g(积存金最小单位精度)
import math
min_add_x = math.ceil(min_add_x * 10000) / 10000
add_total_money = min_add_x * add_price
# 验算加仓后新平均成本
new_total_cap = total_capital + add_total_money
new_total_w = total_weight + min_add_x
new_avg = new_total_cap / new_total_w
print(f"至少需要加仓克重:{min_add_x:.4f} g")
print(f"加仓需要投入金额:{add_total_money:.2f} 元")
print(f"加仓后总克重:{new_total_w:.4f} g")
print(f"加仓后新平均成本:{new_avg:.2f} 元/g")
print("-" * 50)
return {
"可行": True,
"最少加仓克重": round(min_add_x, 4),
"加仓投入金额": round(add_total_money, 2),
"加仓后总克重": round(new_total_w, 4),
"加仓后平均成本": round(new_avg, 2)
}
if __name__ == "__main__":
# 你的四笔买入记录
buy_records = [
(1040.63, 1.0282),
(1029.48, 1.0199),
(879.1, 11.3753),
(878.5, 11.3830),
]
res = calc_multi_gold_break_even(buy_records)
# ==================== 加仓测算参数 ====================
target_average = 874.0 # 目标平均成本压到878以内(878只是平均价,还要减去3.65的手续费 所以就是874左右)
add_buy_price = 872.0 # 假设接下来加仓金价875元/g(低于878才能摊薄)
# ======================================================
calc_add_weight_to_lower_avg(
total_capital=res["总本金"],
total_weight=res["总克重"],
target_avg=target_average,
add_price=add_buy_price
)
平均价压到878,但是还要扣掉3.62元手续费,所以压价到874(这个预估,没有测试过一定能再向下降低价格)


要再买20万,220克。
但是我觉得不太对,没有输入补仓的买入价


'''
积存金多笔合并一笔卖出,保本盈亏平衡计算器 + 加仓摊薄成本测算
1、支付宝积存金 卖出手续费固定0.4%
2、多笔持仓汇总,计算整仓保本卖出单价
3、新增:自定义补仓买入价,计算最少加仓克重,把平均成本压到874元以内
豆包、阿夏
20260716
'''
import math
def calc_multi_gold_break_even(buy_list):
"""
:param buy_list: 列表,元素为元组(买入单价, 克重),存放所有买入记录
"""
total_weight = 0.0
total_capital = 0.0
# 汇总全部持仓总克重、总投入本金
for p, w in buy_list:
total_capital += p * w
total_weight += w
if total_weight <= 0:
raise ValueError("总克重不能为0")
# 整仓平均成本价
avg_cost = total_capital / total_weight
# 保本公式:卖出价*(1-0.004) = 平均成本
sell_break_price = avg_cost / 0.996
rise_per_gram = sell_break_price - avg_cost
rise_rate = rise_per_gram / avg_cost * 100
total_sell_value = sell_break_price * total_weight
sell_fee = total_sell_value * 0.004
net_receive = total_sell_value - sell_fee
total_profit = net_receive - total_capital
profit_rate = total_profit / total_capital * 100
# 打印每一笔明细
print("===== 全部买入持仓明细 =====")
for idx, (p, w) in enumerate(buy_list, 1):
single_cost = p * w
print(f"第{idx}笔:单价{p:.2f}元/g,克重{w:.4f}g,投入{single_cost:.2f}元")
print("-" * 60)
print(f"合计总克重:{total_weight:.4f} g")
print(f"合计总投入本金:{total_capital:.2f} 元")
print(f"持仓当前平均成本价:{avg_cost:.2f} 元/g")
print("-" * 60)
print(f"整仓保本卖出单价:{sell_break_price:.2f} 元/克")
print(f"每克需要上涨:{rise_per_gram:.2f} 元")
print(f"上涨幅度:{rise_rate:.3f} %")
print("-" * 60)
print(f"保本卖出总市值:{total_sell_value:.2f} 元")
print(f"卖出手续费(0.4%):{sell_fee:.2f} 元")
print(f"卖出实际到手:{net_receive:.2f} 元")
print(f"保本总盈亏:{total_profit:.2f} 元,收益率:{profit_rate:.3f} %")
return {
"总克重": round(total_weight, 4),
"总本金": round(total_capital, 2),
"平均成本": round(avg_cost, 2),
"保本卖出价": round(sell_break_price, 2)
}
def calc_add_weight_to_lower_avg(total_capital, total_weight, target_avg, add_price):
"""
计算需要加仓多少克,才能把平均成本压到 target_avg 以内
:param total_capital: 原有总本金
:param total_weight: 原有总克重
:param target_avg: 目标平均成本上限(本次固定874)
:param add_price: 补仓的买入单价(自定义输入,必须小于目标均价才能摊薄)
:return: 字典包含所需加仓克重、加仓总金额等
"""
old_avg = total_capital / total_weight
print("\n========== 加仓摊薄成本测算 ==========")
print(f"当前持仓平均成本:{old_avg:.2f} 元/g")
print(f"目标平均成本上限:{target_avg:.2f} 元/g")
print(f"本次补仓买入单价:{add_price:.2f} 元/g")
print("-" * 50)
# 判断补仓价是否低于目标均价
if add_price >= target_avg:
print(f"⚠️ 警告:补仓金价 {add_price:.2f} ≥ 目标成本 {target_avg:.2f}")
print("无论买入多少克,平均成本都无法压到目标值以下,无需加仓!")
return {
"可行": False,
"需加仓克重": None,
"加仓总金额": None
}
# 计算公式求解最小加仓克重
numerator = target_avg * total_weight - total_capital
denominator = add_price - target_avg
min_add_x = numerator / denominator
# 向上取到0.0001g(积存金最小精度)
min_add_x = math.ceil(min_add_x * 10000) / 10000
add_total_money = min_add_x * add_price
# 验算加仓后新平均成本
new_total_cap = total_capital + add_total_money
new_total_w = total_weight + min_add_x
new_avg = new_total_cap / new_total_w
print(f"至少需要加仓克重:{min_add_x:.4f} g")
print(f"加仓需要投入金额:{add_total_money:.2f} 元")
print(f"加仓后总持有克重:{new_total_w:.4f} g")
print(f"加仓后新平均成本:{new_avg:.2f} 元/g")
print("-" * 50)
return {
"可行": True,
"最少加仓克重": round(min_add_x, 4),
"加仓投入金额": round(add_total_money, 2),
"加仓后总克重": round(new_total_w, 4),
"加仓后平均成本": round(new_avg, 2)
}
if __name__ == "__main__":
# 你的持仓记录
buy_records = [
(1040.63, 1.0282),
(1029.48, 1.0199),
(879.1, 11.3753),
(878.5, 11.3830),
]
# 先计算保本相关数据
res = calc_multi_gold_break_even(buy_records)
# ==================== 自定义参数区 ====================
target_average = 874.0 # 目标:均价压到874以内
add_buy_price = 870.0 # 【自行修改】补仓买入单价,必须小于874才有效
# ======================================================
# 执行加仓测算
calc_add_weight_to_lower_avg(
total_capital=res["总本金"],
total_weight=res["总克重"],
target_avg=target_average,
add_price=add_buy_price
)
# 交互式输入补仓价格(取消注释可手动输入实时金价)
# print("\n===== 手动输入实时补仓金价 =====")
# input_add_price = float(input("请输入当前积存金买入单价(元/g):"))
# calc_add_weight_to_lower_avg(res["总本金"], res["总克重"], target_average, input_add_price)

他说买入补仓价是870元*109克,才能让成本降到874元,也就是878补仓的话,没法把平均价压到874。
另外写一个EXCEL测试,无论补仓价位买多少克(10000000000G),平均价只会无限接近与补仓买入价(878元)

所以只能等伦敦金的波动,起到它会突然拉涨。
小红书说支付宝的积存金的预约单经常没法成交,还是要手动挂单才能生效。不过我设置的预约单成交了(已执行),不过不是第一时间成交(要排队等候成交),要金价来回摆动到这个价位几次才能成交。预约的好处就是能整数买入卖出。


更多推荐




所有评论(0)