bbgo_origin/apps/backtest-report/components/ReportDetails.tsx

249 lines
7.2 KiB
TypeScript
Raw Normal View History

2022-05-17 17:53:48 +00:00
import React, {useEffect, useState} from 'react';
2022-05-18 18:11:43 +00:00
import moment from 'moment';
2022-05-17 17:53:48 +00:00
import TradingViewChart from './TradingViewChart';
import {BalanceMap, ReportSummary} from "../types";
2022-05-17 17:53:48 +00:00
2022-05-18 17:57:46 +00:00
import {
Badge,
Container,
createStyles,
Grid,
2022-05-18 17:57:46 +00:00
Group,
Paper,
SimpleGrid,
Skeleton,
Table,
2022-05-18 17:57:46 +00:00
Text,
ThemeIcon,
Title
} from '@mantine/core';
import {ArrowDownRight, ArrowUpRight,} from 'tabler-icons-react';
const useStyles = createStyles((theme) => ({
root: {
paddingTop: theme.spacing.xl * 1.5,
paddingBottom: theme.spacing.xl * 1.5,
},
label: {
fontFamily: `Greycliff CF, ${theme.fontFamily}`,
},
}));
interface StatsGridIconsProps {
data: {
title: string;
value: string;
diff?: number
dir?: string;
desc?: string;
}[];
}
function StatsGridIcons({data}: StatsGridIconsProps) {
const {classes} = useStyles();
const stats = data.map((stat) => {
const DiffIcon = stat.diff && stat.diff > 0 ? ArrowUpRight : ArrowDownRight;
const DirIcon = stat.dir && stat.dir == "up" ? ArrowUpRight : ArrowDownRight;
2022-06-05 05:46:43 +00:00
return (
2022-05-18 17:57:46 +00:00
<Paper withBorder p="xs" radius="md" key={stat.title}>
2022-06-05 05:46:43 +00:00
<Group position="left">
2022-05-18 17:57:46 +00:00
<div>
<Text
color="dimmed"
weight={700}
size="xs"
className={classes.label}
>
{stat.title}
2022-06-05 05:46:43 +00:00
{stat.dir ?
<ThemeIcon
color="gray"
variant="light"
sx={(theme) => ({color: stat.dir == "up" ? theme.colors.teal[6] : theme.colors.red[6]})}
size={16}
radius="sx"
>
<DirIcon size={16}/>
</ThemeIcon>
: null}
2022-05-18 17:57:46 +00:00
</Text>
2022-06-05 05:46:43 +00:00
<Text weight={700} size="xs">
2022-05-18 17:57:46 +00:00
{stat.value}
</Text>
</div>
{stat.diff ?
<ThemeIcon
color="gray"
variant="light"
sx={(theme) => ({color: stat.diff && stat.diff > 0 ? theme.colors.teal[6] : theme.colors.red[6]})}
size={38}
radius="md"
>
<DiffIcon size={28}/>
</ThemeIcon>
: null}
</Group>
{stat.diff ?
<Text color="dimmed" size="sm" mt="md">
<Text component="span" color={stat.diff && stat.diff > 0 ? 'teal' : 'red'} weight={700}>
{stat.diff}%
</Text>{' '}
{stat.diff && stat.diff > 0 ? 'increase' : 'decrease'} compared to last month
</Text> : null}
{stat.desc ? (
<Text color="dimmed" size="sm" mt="md">
{stat.desc}
</Text>
) : null}
</Paper>
);
});
return (
2022-06-05 05:46:43 +00:00
<div py="xl">
<SimpleGrid cols={5} breakpoints={[{maxWidth: 'sm', cols: 1, spacing: 'xl'}]}>
2022-05-18 17:57:46 +00:00
{stats}
</SimpleGrid>
</div>
);
}
2022-05-18 17:00:45 +00:00
2022-05-17 17:53:48 +00:00
interface ReportDetailsProps {
basePath: string;
runID: string;
}
const fetchReportSummary = (basePath: string, runID: string) => {
return fetch(
`${basePath}/${runID}/summary.json`,
)
.then((res) => res.json())
.catch((e) => {
console.error("failed to fetch index", e)
});
}
2022-05-18 17:57:46 +00:00
const skeleton = <Skeleton height={140} radius="md" animate={false}/>;
interface BalanceDetailsProps {
balances: BalanceMap;
}
const BalanceDetails = (props: BalanceDetailsProps) => {
const rows = Object.entries(props.balances).map(([k, v]) => {
return <tr key={k}>
<td>{k}</td>
<td>{v.available}</td>
</tr>;
});
2022-06-05 05:46:43 +00:00
return <Table verticalSpacing="xs" fontSize="xs">
<thead>
<tr>
<th>Currency</th>
<th>Balance</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</Table>;
};
2022-05-17 17:53:48 +00:00
const ReportDetails = (props: ReportDetailsProps) => {
const [reportSummary, setReportSummary] = useState<ReportSummary>()
useEffect(() => {
fetchReportSummary(props.basePath, props.runID).then((summary: ReportSummary) => {
console.log("summary", props.runID, summary);
setReportSummary(summary)
})
}, [props.runID])
if (!reportSummary) {
2022-05-18 17:00:45 +00:00
return <div>
<h2>Loading {props.runID}</h2>
2022-05-18 17:00:45 +00:00
</div>;
}
2022-06-05 05:46:43 +00:00
const strategyName = props.runID.split("_")[1]
const runID = props.runID.split("_").pop()
2022-05-18 18:11:43 +00:00
const totalProfit = Math.round(reportSummary.symbolReports.map((report) => report.pnl.profit).reduce((prev, cur) => prev + cur) * 100) / 100
const totalUnrealizedProfit = Math.round(reportSummary.symbolReports.map((report) => report.pnl.unrealizedProfit).reduce((prev, cur) => prev + cur) * 100) / 100
const totalTrades = reportSummary.symbolReports.map((report) => report.pnl.numTrades).reduce((prev, cur) => prev + cur) || 0
2022-05-18 17:57:46 +00:00
const totalBuyVolume = reportSummary.symbolReports.map((report) => report.pnl.buyVolume).reduce((prev, cur) => prev + cur) || 0
const totalSellVolume = reportSummary.symbolReports.map((report) => report.pnl.sellVolume).reduce((prev, cur) => prev + cur) || 0
2022-05-18 17:57:46 +00:00
const volumeUnit = reportSummary.symbolReports.length == 1 ? reportSummary.symbolReports[0].market.baseCurrency : '';
2022-05-18 17:00:45 +00:00
return <div>
2022-06-05 05:46:43 +00:00
<Container my="sx">
2022-05-18 17:57:46 +00:00
<div>
2022-06-05 05:46:43 +00:00
<Badge key={strategyName} color="teal">Strategy: {strategyName}</Badge>
{reportSummary.sessions.map((session) => <Badge key={session} color="teal">Exchange: {session}</Badge>)}
{reportSummary.symbols.map((symbol) => <Badge key={symbol} color="teal">Symbol: {symbol}</Badge>)}
2022-05-18 18:11:43 +00:00
2022-06-05 05:46:43 +00:00
<Badge color="teal">{reportSummary.startTime.toString()} {reportSummary.endTime.toString()} ~ {
2022-05-18 18:11:43 +00:00
moment.duration((new Date(reportSummary.endTime)).getTime() - (new Date(reportSummary.startTime)).getTime()).humanize()
}</Badge>
2022-06-05 05:46:43 +00:00
<Badge key={runID} color="gray" size="xs">Run ID: {runID}</Badge>
2022-05-18 17:57:46 +00:00
</div>
<StatsGridIcons data={[
2022-06-05 05:46:43 +00:00
{title: "Profit", value: "$" + totalProfit.toString(), dir: totalProfit >= 0 ? "up" : "down"},
2022-05-18 18:11:43 +00:00
{
2022-06-05 05:46:43 +00:00
title: "Unr. Profit",
value: totalUnrealizedProfit.toString() + "$",
2022-05-18 18:11:43 +00:00
dir: totalUnrealizedProfit > 0 ? "up" : "down"
},
2022-05-18 17:57:46 +00:00
{title: "Trades", value: totalTrades.toString()},
2022-06-05 05:46:43 +00:00
{title: "Buy Vol", value: totalBuyVolume.toString() + ` ${volumeUnit}`},
{title: "Sell Vol", value: totalSellVolume.toString() + ` ${volumeUnit}`},
2022-05-18 17:57:46 +00:00
]}/>
2022-06-05 05:46:43 +00:00
<Grid py="xl">
<Grid.Col xs={6}>
2022-06-05 05:46:43 +00:00
<Title order={6}>Initial Total Balances</Title>
<BalanceDetails balances={reportSummary.initialTotalBalances}/>
</Grid.Col>
<Grid.Col xs={6}>
2022-06-05 05:46:43 +00:00
<Title order={6}>Final Total Balances</Title>
<BalanceDetails balances={reportSummary.finalTotalBalances}/>
</Grid.Col>
</Grid>
{
2022-05-18 17:57:46 +00:00
/*
<Grid>
<Grid.Col span={6}>
<Skeleton height={300} radius="md" animate={false}/>
</Grid.Col>
<Grid.Col xs={4}>{skeleton}</Grid.Col>
</Grid>
*/
}
2022-05-18 17:57:46 +00:00
<div>
{
reportSummary.symbols.map((symbol: string, i: number) => {
return <TradingViewChart key={i} basePath={props.basePath} runID={props.runID} reportSummary={reportSummary}
2022-05-19 16:48:15 +00:00
symbol={symbol}/>
2022-05-18 17:57:46 +00:00
})
}
</div>
</Container>
2022-05-18 17:00:45 +00:00
</div>;
2022-05-17 17:53:48 +00:00
};
export default ReportDetails;