diff --git a/apps/backtest-report/components/TradingViewChart.tsx b/apps/backtest-report/components/TradingViewChart.tsx index 7c1e08046..6d121aae1 100644 --- a/apps/backtest-report/components/TradingViewChart.tsx +++ b/apps/backtest-report/components/TradingViewChart.tsx @@ -113,6 +113,19 @@ const fetchPositionHistory = (basePath: string, runID: string, filename: string) }); }; +const selectPositionHistory = (data: PositionHistoryEntry[], since: Date, until: Date): PositionHistoryEntry[] => { + const entries: PositionHistoryEntry[] = []; + for (let i = 0; i < data.length; i++) { + const d = data[i]; + if (d.time < since || d.time > until) { + continue + } + + entries.push(d) + } + return entries +} + const fetchOrders = (basePath: string, runID: string) => { return fetch( `${basePath}/${runID}/orders.tsv`, @@ -124,6 +137,19 @@ const fetchOrders = (basePath: string, runID: string) => { }); } +const selectOrders = (data: Order[], since: Date, until: Date): Order[] => { + const entries: Order[] = []; + for (let i = 0; i < data.length; i++) { + const d = data[i]; + if (d.time && (d.time < since || d.time > until)) { + continue + } + + data.push(d); + } + return entries +} + const parseInterval = (s: string) => { switch (s) { case "1m": @@ -390,7 +416,7 @@ const TradingViewChart = (props: TradingViewChartProps) => { const resizeObserver = useRef(); const intervals = props.reportSummary.intervals || []; - intervals.sort((a,b) => { + intervals.sort((a, b) => { const as = parseInterval(a) const bs = parseInterval(b) if (as < bs) { @@ -403,7 +429,6 @@ const TradingViewChart = (props: TradingViewChartProps) => { const [currentInterval, setCurrentInterval] = useState(intervals.length > 0 ? intervals[intervals.length - 1] : '1m'); const [showPositionBase, setShowPositionBase] = useState(false); - const [showCanceledOrders, setShowCanceledOrders] = useState(false); const [showPositionAverageCost, setShowPositionAverageCost] = useState(false); const [orders, setOrders] = useState([]); @@ -423,6 +448,7 @@ const TradingViewChart = (props: TradingViewChartProps) => { const fetchers = []; const ordersFetcher = fetchOrders(props.basePath, props.runID).then((orders: Order[] | void) => { if (orders) { + // const markers = ordersToMarkers(currentInterval, selectOrders(orders, selectedTimeRange[0], selectedTimeRange[1])); const markers = ordersToMarkers(currentInterval, orders); chartData.orders = orders; chartData.markers = markers; @@ -436,7 +462,8 @@ const TradingViewChart = (props: TradingViewChartProps) => { const manifest = props.reportSummary?.manifests[0]; if (manifest && manifest.type === "strategyProperty" && manifest.strategyProperty === "position") { const positionHistoryFetcher = fetchPositionHistory(props.basePath, props.runID, manifest.filename).then((data) => { - chartData.positionHistory = data; + chartData.positionHistory = selectPositionHistory(data as PositionHistoryEntry[], selectedTimeRange[0], selectedTimeRange[1]); + // chartData.positionHistory = data; }); fetchers.push(positionHistoryFetcher); } @@ -594,7 +621,7 @@ const TradingViewChart = (props: TradingViewChartProps) => { format(new Date(ms), 'M d HH')} step={1000 * parseInterval(currentInterval)} onChange={(tr: any) => { @@ -661,12 +688,12 @@ const createLegendUpdater = (legend: HTMLDivElement, prefix: string) => { } } -const formatDate = (d : Date) : string => { +const formatDate = (d: Date): string => { return moment(d).format("MMM Do YY hh:mm:ss A Z"); } const createOHLCLegendUpdater = (legend: HTMLDivElement, prefix: string) => { - return (param: any, time : any) => { + return (param: any, time: any) => { if (param) { const change = Math.round((param.close - param.open) * 100.0) / 100.0 const changePercentage = Math.round((param.close - param.open) / param.close * 10000.0) / 100.0;