首页 > 生活 > 基金购买方案回测
作者:ghostry 发布时间:2025-02-20 浏览: 28
转载注明出处: https://blog.ghostry.cn/mood/1111.html数据来源:https://kouchao.github.io/TiantianFundApi/apis/#%E8%8E%B7%E5%8F%96%E5%9F%BA%E9%87%91%E5%87%80%E5%80%BC
比如
https://tiantian-fund-api.vercel.app/api/action?action_name=fundMNHisNetList&pageIndex=1&FCODE=005918&pagesize=370
代码
# import requests
# 从 API 获取数据
# url = "https://tiantian-fund-api.vercel.app/api/action?action_name=fundMNHisNetList&pageIndex=1&FCODE=012414&pagesize=370"
# response = requests.get(url)
# data = response.json()
import json
def yuanshi(data):
# 提取日涨幅(JZZZL)
daily_changes = [float(item["JZZZL"]) for item in data["Datas"]]
daily_changes.reverse()
# print(daily_changes)
# 计算收益
initial_investment = 10000
total_profit = 10000
total_touru = 0
biaozhun = initial_investment
caozuo=200
for change in daily_changes:
daily_profit = total_profit * (change / 100)
total_profit += daily_profit
daily_profit = biaozhun * (change / 100)
biaozhun += daily_profit
# print(f"今日跌涨幅: {change} % , 价格 {total_profit:.2f} 元")
print(f"什么也不做")
print(f"最终金额: {total_profit:.2f} 元")
touru=initial_investment + total_touru
print(f"总投入: {touru:.2f} 元")
final_value = total_profit - touru
print(f"总收益: {final_value:.2f} 元")
return
def pingcang(data,beishu):
# 提取日涨幅(JZZZL)
daily_changes = [float(item["JZZZL"]) for item in data["Datas"]]
daily_changes.reverse()
# print(daily_changes)
# 计算收益
initial_investment = 10000
total_profit = 10000
total_touru = 0
biaozhun = initial_investment
caozuo=200
for change in daily_changes:
daily_profit = total_profit * (change / 100)
total_profit += daily_profit
daily_profit = biaozhun * (change / 100)
biaozhun += daily_profit
# print(f"今日跌涨幅: {change} % , 价格 {total_profit:.2f} 元")
# 涨caozuo就卖出,等跌了再买入
# if biaozhun - initial_investment > caozuo:
# total_touru -= total_profit
# total_profit = 0
# biaozhun = initial_investment
# print(f"卖出")
# if initial_investment-biaozhun>caozuo and total_profit==0:
# total_touru+=initial_investment
# total_profit=initial_investment
# biaozhun = initial_investment
# print(f"买入")
# 平仓法,始终维持在持仓initial_investment
daily_change = - initial_investment * change / 100 * beishu
if total_profit+daily_change>0:
total_profit += daily_change
total_touru += daily_change
# print(f"今日投入: {daily_change:.2f} 元 , 最终 {total_profit:.2f} 元")
print(f"平仓法")
print(f"最终金额: {total_profit:.2f} 元")
touru=initial_investment + total_touru
print(f"总投入: {touru:.2f} 元")
final_value = total_profit - touru
print(f"总收益: {final_value:.2f} 元")
return
with open('008888.json', 'r') as fcc_file:
print(f"0008888")
data = json.load(fcc_file)
yuanshi(data)
pingcang(data,10)
with open('414.json', 'r') as fcc_file:
print(f"414")
data = json.load(fcc_file)
yuanshi(data)
pingcang(data,10)
with open('005918.json', 'r') as fcc_file:
print(f"005918")
data = json.load(fcc_file)
yuanshi(data)
pingcang(data,10)
# import json
# # 读取JSON文件
# with open('414.json', 'r', encoding='utf-8') as f:
# data = json.load(f)
# # 提取Datas下的JZZZL
# jzzzl_list = [float(item['JZZZL']) / 100 for item in data.get('Datas', [])]
# jzzzl_list.reverse()
# # 初始化变量
# fund_amount = 10000 # 初始投入10000元
# total_investment = 10000 # 累计投入金额
# last_buy_high = 10000 # 上次买入后的最高值
# last_buy_index = 0 # 上次买入的索引
# # 遍历数据
# for i, jzzzl in enumerate(jzzzl_list):
# # 更新基金持有金额
# fund_amount *= (1 + jzzzl)
# # 更新上次买入后的最高值
# if i >= last_buy_index:
# last_buy_high = max(last_buy_high, fund_amount)
# # 判断是否需要买入
# if (last_buy_high - fund_amount) / last_buy_high > 0.05:
# print(f"买入 {fund_amount}")
# fund_amount += 500
# total_investment += 500
# last_buy_high = 0
# last_buy_index = i
# # 判断是否需要卖出
# elif (last_buy_high - fund_amount) / last_buy_high > 0.01:
# if fund_amount >= 500:
# print(f"卖出 {fund_amount}")
# fund_amount -= 500
# total_investment -= 500
# last_buy_high = 0
# last_buy_index = i
# # 输出结果
# print(f"最终基金持有金额: {fund_amount:.2f} 元")
# print(f"累计投入金额: {total_investment:.2f} 元")
经计算,平仓法比买了放在那什么都不做,要少赔些多赚些。但是感觉投入与回报不成正比。如果是程序自动操作还行。如果人工还不够麻烦的,不如仍在那不管。
上一篇: pve虚拟机只有ipv6通,ipv4不通下一篇: