mirror of
https://github.com/c9s/bbgo.git
synced 2024-09-20 08:11:08 +00:00
backtest: add time range selector support to orders data and position data
This commit is contained in:
parent
6ce9f6a2b7
commit
90aac7e469
|
@ -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) => {
|
const fetchOrders = (basePath: string, runID: string) => {
|
||||||
return fetch(
|
return fetch(
|
||||||
`${basePath}/${runID}/orders.tsv`,
|
`${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) => {
|
const parseInterval = (s: string) => {
|
||||||
switch (s) {
|
switch (s) {
|
||||||
case "1m":
|
case "1m":
|
||||||
|
@ -390,7 +416,7 @@ const TradingViewChart = (props: TradingViewChartProps) => {
|
||||||
const resizeObserver = useRef<any>();
|
const resizeObserver = useRef<any>();
|
||||||
const intervals = props.reportSummary.intervals || [];
|
const intervals = props.reportSummary.intervals || [];
|
||||||
|
|
||||||
intervals.sort((a,b) => {
|
intervals.sort((a, b) => {
|
||||||
const as = parseInterval(a)
|
const as = parseInterval(a)
|
||||||
const bs = parseInterval(b)
|
const bs = parseInterval(b)
|
||||||
if (as < bs) {
|
if (as < bs) {
|
||||||
|
@ -403,7 +429,6 @@ const TradingViewChart = (props: TradingViewChartProps) => {
|
||||||
|
|
||||||
const [currentInterval, setCurrentInterval] = useState(intervals.length > 0 ? intervals[intervals.length - 1] : '1m');
|
const [currentInterval, setCurrentInterval] = useState(intervals.length > 0 ? intervals[intervals.length - 1] : '1m');
|
||||||
const [showPositionBase, setShowPositionBase] = useState(false);
|
const [showPositionBase, setShowPositionBase] = useState(false);
|
||||||
const [showCanceledOrders, setShowCanceledOrders] = useState(false);
|
|
||||||
const [showPositionAverageCost, setShowPositionAverageCost] = useState(false);
|
const [showPositionAverageCost, setShowPositionAverageCost] = useState(false);
|
||||||
const [orders, setOrders] = useState<Order[]>([]);
|
const [orders, setOrders] = useState<Order[]>([]);
|
||||||
|
|
||||||
|
@ -423,6 +448,7 @@ const TradingViewChart = (props: TradingViewChartProps) => {
|
||||||
const fetchers = [];
|
const fetchers = [];
|
||||||
const ordersFetcher = fetchOrders(props.basePath, props.runID).then((orders: Order[] | void) => {
|
const ordersFetcher = fetchOrders(props.basePath, props.runID).then((orders: Order[] | void) => {
|
||||||
if (orders) {
|
if (orders) {
|
||||||
|
// const markers = ordersToMarkers(currentInterval, selectOrders(orders, selectedTimeRange[0], selectedTimeRange[1]));
|
||||||
const markers = ordersToMarkers(currentInterval, orders);
|
const markers = ordersToMarkers(currentInterval, orders);
|
||||||
chartData.orders = orders;
|
chartData.orders = orders;
|
||||||
chartData.markers = markers;
|
chartData.markers = markers;
|
||||||
|
@ -436,7 +462,8 @@ const TradingViewChart = (props: TradingViewChartProps) => {
|
||||||
const manifest = props.reportSummary?.manifests[0];
|
const manifest = props.reportSummary?.manifests[0];
|
||||||
if (manifest && manifest.type === "strategyProperty" && manifest.strategyProperty === "position") {
|
if (manifest && manifest.type === "strategyProperty" && manifest.strategyProperty === "position") {
|
||||||
const positionHistoryFetcher = fetchPositionHistory(props.basePath, props.runID, manifest.filename).then((data) => {
|
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);
|
fetchers.push(positionHistoryFetcher);
|
||||||
}
|
}
|
||||||
|
@ -594,7 +621,7 @@ const TradingViewChart = (props: TradingViewChartProps) => {
|
||||||
|
|
||||||
<TimeRangeSlider
|
<TimeRangeSlider
|
||||||
selectedInterval={selectedTimeRange}
|
selectedInterval={selectedTimeRange}
|
||||||
timelineInterval={timeRange}
|
timelineInterval={reportTimeRange}
|
||||||
formatTick={(ms: Date) => format(new Date(ms), 'M d HH')}
|
formatTick={(ms: Date) => format(new Date(ms), 'M d HH')}
|
||||||
step={1000 * parseInterval(currentInterval)}
|
step={1000 * parseInterval(currentInterval)}
|
||||||
onChange={(tr: any) => {
|
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");
|
return moment(d).format("MMM Do YY hh:mm:ss A Z");
|
||||||
}
|
}
|
||||||
|
|
||||||
const createOHLCLegendUpdater = (legend: HTMLDivElement, prefix: string) => {
|
const createOHLCLegendUpdater = (legend: HTMLDivElement, prefix: string) => {
|
||||||
return (param: any, time : any) => {
|
return (param: any, time: any) => {
|
||||||
if (param) {
|
if (param) {
|
||||||
const change = Math.round((param.close - param.open) * 100.0) / 100.0
|
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;
|
const changePercentage = Math.round((param.close - param.open) / param.close * 10000.0) / 100.0;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user