|
|
@@ -4,11 +4,12 @@ import type { TradeRecord, DailyProfit, ProfitSummary, StockData } from '../type
|
|
|
export function useProfitCalculator(stockData: StockData[], trades: TradeRecord[]) {
|
|
|
const [currentDate, setCurrentDate] = useState<string>('');
|
|
|
|
|
|
- // 计算每日收益
|
|
|
+ // 计算每日收益(采用答题卡的计算方式:单日涨幅简单相加)
|
|
|
const dailyProfits = useMemo(() => {
|
|
|
const profitMap = new Map<string, DailyProfit>();
|
|
|
let accumulatedProfit = 0; // 累计收益
|
|
|
|
|
|
+ // 按日期分组交易
|
|
|
trades.forEach(trade => {
|
|
|
const date = trade.date;
|
|
|
if (!profitMap.has(date)) {
|
|
|
@@ -21,29 +22,57 @@ export function useProfitCalculator(stockData: StockData[], trades: TradeRecord[
|
|
|
|
|
|
const dailyProfit = profitMap.get(date)!;
|
|
|
dailyProfit.trades.push(trade);
|
|
|
-
|
|
|
- if (trade.type === 'SELL' && trade.buyPrice !== undefined) {
|
|
|
- // 本次收益 = (当天收盘价 - 买入收盘价) / 买入收盘价
|
|
|
- const currentProfit = (trade.price - trade.buyPrice) / trade.buyPrice;
|
|
|
- // 累计收益 = 之前的累计收益 + 本次收益
|
|
|
- accumulatedProfit += currentProfit;
|
|
|
- // 记录当日收益为累计收益
|
|
|
- dailyProfit.profit = accumulatedProfit;
|
|
|
- }
|
|
|
});
|
|
|
|
|
|
return Array.from(profitMap.values());
|
|
|
}, [trades]);
|
|
|
|
|
|
- // 计算当日行情
|
|
|
+ // 计算当日行情(采用答题卡的计算方式:基于价格变化百分比)
|
|
|
const profitSummary = useMemo(() => {
|
|
|
- // 获取累计收益
|
|
|
- const totalProfit = trades.reduce((sum, trade) => {
|
|
|
- if (trade.type === 'SELL' && trade.buyPrice !== undefined) {
|
|
|
- return sum + (trade.price - trade.buyPrice) / trade.buyPrice;
|
|
|
+ // 获取累计收益(单日涨幅简单相加)
|
|
|
+ let totalProfit = 0;
|
|
|
+
|
|
|
+ // 如果有交易记录,计算基于价格变化的收益
|
|
|
+ if (stockData.length > 1 && trades.length > 0) {
|
|
|
+ // 按日期排序股票数据
|
|
|
+ const sortedStockData = [...stockData].sort((a, b) =>
|
|
|
+ new Date(a.d).getTime() - new Date(b.d).getTime()
|
|
|
+ );
|
|
|
+
|
|
|
+ // 按日期排序交易记录
|
|
|
+ const sortedTrades = [...trades].sort((a, b) =>
|
|
|
+ new Date(a.date).getTime() - new Date(b.date).getTime()
|
|
|
+ );
|
|
|
+
|
|
|
+ // 计算每日收益(答题卡方式:基于价格变化百分比)
|
|
|
+ // 这里需要根据用户的买卖决策来计算
|
|
|
+ let isHolding = false; // 是否持股
|
|
|
+
|
|
|
+ for (let i = 1; i < sortedStockData.length; i++) {
|
|
|
+ const prevDate = sortedStockData[i - 1].d;
|
|
|
+ const currentDate = sortedStockData[i].d;
|
|
|
+ const prevPrice = parseFloat(sortedStockData[i - 1].c);
|
|
|
+ const currentPrice = parseFloat(sortedStockData[i].c);
|
|
|
+
|
|
|
+ // 检查该日期是否有交易
|
|
|
+ const dateTrades = sortedTrades.filter(trade => trade.date === prevDate);
|
|
|
+
|
|
|
+ // 处理该日期的交易
|
|
|
+ dateTrades.forEach(trade => {
|
|
|
+ if (trade.type === 'BUY') {
|
|
|
+ isHolding = true;
|
|
|
+ } else if (trade.type === 'SELL') {
|
|
|
+ isHolding = false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 如果持股,计算当日收益(答题卡方式:基于连续两天的价格变化)
|
|
|
+ if (isHolding) {
|
|
|
+ const dailyChange = ((currentPrice - prevPrice) / prevPrice);
|
|
|
+ totalProfit += dailyChange;
|
|
|
+ }
|
|
|
}
|
|
|
- return sum;
|
|
|
- }, 0);
|
|
|
+ }
|
|
|
|
|
|
// 获取当日行情数据
|
|
|
// 如果没有指定currentDate,则使用最新一天的数据
|