const calcTrade = (kLineList: KLineData[]): BarTradeEntity[] => { return…
- User
const calcTrade = (kLineList: KLineData[]): BarTradeEntity[] => { return bigOrders.map((order) => { let positionX: number = 0;
for (let bar = 0; bar < kLineList.length; bar++) { const kLine = kLineList[bar]; if (order.timestamp <= kLine.timestamp) { if (positionX === 0) { positionX = bar - 1; } break; } } return { id: order.id, timestamp: order.timestamp, price: order.price, positionX, }; });};
Здесь нужно в начале запустить один отдельный цикл по KLineList, который создаст объект, где ключами будут timestamp, а значениями bar.
В этом же цикле найти шаг между двумя timestamp.
Затем запустить второй цикл уже по bigOrders. Для расчета positionX нужно будет расчитать существующий timestamp и получить значение из объекта первого цикла.
Для расчета существующего timestamp нужно: Math.ceil(order.timestamp / timestampStep) * timestampStep
- Assistant
const calcTrade = (kLineList: KLineData[], bigOrders: Order[]): BarTradeEntity[] => { const timestampStep = findTimestampStep(kLineList); // Find the step between two timestamps
// Create an object where the keys are timestamps and the values are bar numbers const timestampMap: { [key: number]: number } = {};
for (let bar = 0; bar < kLineList.length; bar++) { const kLine = kLineList[bar]; timestampMap[kLine.timestamp] = bar; }
return bigOrders.map((order) => { let positionX: number = 0; const existingTimestamp = Math.ceil(order.timestamp / timestampStep) * timestampStep;
if (timestampMap[existingTimestamp] !== undefined) { positionX = timestampMap[existingTimestamp] - 1; } return { id: order.id, timestamp: order.timestamp, price: order.price, positionX, };}); };
// Helper function to find the step between two timestamps const findTimestampStep = (kLineList: KLineData[]): number => { let timestampStep = Infinity; for (let i = 1; i < kLineList.length; i++) { const diff = kLineList[i].timestamp - kLineList[i - 1].timestamp; timestampStep = Math.min(timestampStep, diff); } return timestampStep; };
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.