work on clean up and switched all to use eodSearchCode
This commit is contained in:
parent
d68268b722
commit
e341cc0226
19 changed files with 206 additions and 127 deletions
|
|
@ -3,8 +3,36 @@ import { useState, useMemo, memo } from 'react';
|
|||
import { Chart } from '../../../components/charts/Chart';
|
||||
import { ChartContainer } from './ChartContainer';
|
||||
|
||||
interface ExtendedBacktestResult extends BacktestResult {
|
||||
equity?: Array<{ date: string; value: number }>;
|
||||
ohlcData?: Record<string, Array<{
|
||||
timestamp: number;
|
||||
open: number;
|
||||
high: number;
|
||||
low: number;
|
||||
close: number;
|
||||
volume?: number;
|
||||
}>>;
|
||||
runId?: string;
|
||||
}
|
||||
|
||||
interface ExtendedTrade {
|
||||
id: string;
|
||||
timestamp: string;
|
||||
symbol: string;
|
||||
side: 'buy' | 'sell';
|
||||
quantity: number;
|
||||
price: number;
|
||||
commission: number;
|
||||
pnl?: number;
|
||||
positionAfter: number;
|
||||
entryPrice?: number;
|
||||
entryDate?: string;
|
||||
date?: string;
|
||||
}
|
||||
|
||||
interface BacktestChartProps {
|
||||
result: BacktestResult | null;
|
||||
result: ExtendedBacktestResult | null;
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
|
|
@ -13,36 +41,43 @@ export const BacktestChart = memo(function BacktestChart({ result, isLoading }:
|
|||
const [selectedSymbol, setSelectedSymbol] = useState<string>('');
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
if (!result?.equity || !result?.ohlcData) return null;
|
||||
if (!result || (!result.equity && !result.performanceData) || (!result.ohlcData && !result.trades)) return null;
|
||||
|
||||
const symbols = Object.keys(result.ohlcData);
|
||||
const symbols = result.ohlcData ? Object.keys(result.ohlcData) : (result.trades ? [...new Set(result.trades.map(t => t.symbol))] : []);
|
||||
const symbol = selectedSymbol || symbols[0] || '';
|
||||
const ohlcData = result.ohlcData[symbol] || [];
|
||||
const ohlcData = result.ohlcData?.[symbol] || [];
|
||||
|
||||
// Remove excessive logging in production
|
||||
// Log only on significant changes
|
||||
if (process.env.NODE_ENV === 'development' && ohlcData.length > 0) {
|
||||
// Use a simple hash to detect actual data changes
|
||||
const dataHash = `${symbols.length}-${result.equity?.length}-${ohlcData.length}`;
|
||||
const dataHash = `${symbols.length}-${result.equity?.length || result.performanceData?.length}-${ohlcData.length}`;
|
||||
if ((window as any).__lastDataHash !== dataHash) {
|
||||
(window as any).__lastDataHash = dataHash;
|
||||
console.log('BacktestChart data updated:', {
|
||||
symbols,
|
||||
selectedSymbol,
|
||||
symbol,
|
||||
ohlcDataKeys: Object.keys(result.ohlcData),
|
||||
equityLength: result.equity?.length,
|
||||
ohlcDataKeys: result.ohlcData ? Object.keys(result.ohlcData) : [],
|
||||
equityLength: result.equity?.length || result.performanceData?.length,
|
||||
tradesLength: result.trades?.length
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const equityData = (result.equity || [])
|
||||
.filter(e => e && e.date && e.value != null)
|
||||
.map(e => ({
|
||||
time: new Date(e.date).getTime() / 1000,
|
||||
value: e.value
|
||||
}));
|
||||
const equityData = result.equity
|
||||
? result.equity
|
||||
.filter(e => e && e.date && e.value != null)
|
||||
.map(e => ({
|
||||
time: new Date(e.date).getTime() / 1000,
|
||||
value: e.value
|
||||
}))
|
||||
: result.performanceData
|
||||
?.filter(p => p && p.timestamp && p.portfolioValue != null)
|
||||
.map(p => ({
|
||||
time: new Date(p.timestamp).getTime() / 1000,
|
||||
value: p.portfolioValue
|
||||
})) || [];
|
||||
|
||||
// Find trades for this symbol
|
||||
const symbolTrades = result.trades?.filter(t => t.symbol === symbol) || [];
|
||||
|
|
@ -51,15 +86,17 @@ export const BacktestChart = memo(function BacktestChart({ result, isLoading }:
|
|||
const tradeMarkers = symbolTrades
|
||||
.filter(trade => {
|
||||
// Check multiple possible field names
|
||||
const hasPrice = trade.price != null || trade.entryPrice != null;
|
||||
const hasTime = trade.timestamp != null || trade.entryDate != null || trade.date != null;
|
||||
const extTrade = trade as ExtendedTrade;
|
||||
const hasPrice = extTrade.price != null || extTrade.entryPrice != null;
|
||||
const hasTime = extTrade.timestamp != null || extTrade.entryDate != null || extTrade.date != null;
|
||||
return hasPrice && hasTime;
|
||||
})
|
||||
.map(trade => {
|
||||
// Use whatever field names are present
|
||||
const price = trade.price || trade.entryPrice;
|
||||
const timestamp = trade.timestamp || trade.entryDate || trade.date;
|
||||
const side = trade.side || (trade.quantity > 0 ? 'buy' : 'sell');
|
||||
const extTrade = trade as ExtendedTrade;
|
||||
const price = extTrade.price || extTrade.entryPrice || 0;
|
||||
const timestamp = extTrade.timestamp || extTrade.entryDate || extTrade.date || '';
|
||||
const side = extTrade.side || (extTrade.quantity > 0 ? 'buy' : 'sell');
|
||||
|
||||
return {
|
||||
time: new Date(timestamp).getTime() / 1000,
|
||||
|
|
@ -140,8 +177,8 @@ export const BacktestChart = memo(function BacktestChart({ result, isLoading }:
|
|||
]}
|
||||
tradeMarkers={chartData.tradeMarkers}
|
||||
height={height}
|
||||
chartId={`backtest-${result?.runId || 'default'}`}
|
||||
key={result?.runId || 'default'}
|
||||
chartId={`backtest-${result?.runId || result?.id || 'default'}`}
|
||||
key={result?.runId || result?.id || 'default'}
|
||||
/>
|
||||
)}
|
||||
</ChartContainer>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue