最近对币圈有点兴趣,不懂金融,只知道定投比较适合小白,如果5年前开始定投,我现在是不是该发财了。所以写个程序分析一下
api
- 通过询问chatgpt,找到coingecko公开的api,用于获取币种走势,以及法币类型列表
- api文档地址:www.coingecko.com/en/api/docu…
收集数据
- 页面上需要收集:币种,法币,时间,定投策略,定投金额
- 然后通过api获取对应价格
处理数据
- 由于coingecko获取价格的接口,无法自定义粒度,粒度是自动的
- 小于1天,粒度为5分钟
- 小于90天,粒度为1小时
- 90天以上,粒度是1天
- 所以需要根据用户的选择的定投策略,处理粒度
let prices = res.prices // api返回的价格列表
const daySub = dayjs(form.date[1]).diff(dayjs(form.date[0]), "days")
if (daySub < 90) { // 小于90天,将粒度处理为1天
prices = prices.filter((_: any, index: any) => index % 24 === 0)
}
if (form.investRule === "week") { // 周定投,粒度为1周。form为表单收集的数据
prices = prices.filter((_: any, index: any) => index % 7 === 0)
} else if (form.investRule === "month") { // 月定投,粒度为1月
const curDay = dayjs(form.date[0]).date() // 当前定投日
prices = prices.filter((item: any) => dayjs(item[0]).date() === curDay)
}
计算并绘制图表
- 采用echarts绘制图标
- 代码不复杂,这里就不展示了