Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jmerle committed Feb 28, 2024
1 parent c47d002 commit 980c512
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface TradingState {
observations: Observation;
}

export interface SandboxLogRow {
export interface AlgorithmDataRow {
state: TradingState;
orders: Record<ProsperitySymbol, Order[]>;
conversions: number;
Expand All @@ -100,7 +100,7 @@ export interface SandboxLogRow {
export interface Algorithm {
summary?: AlgorithmSummary;
activityLogs: ActivityLogRow[];
sandboxLogs: SandboxLogRow[];
data: AlgorithmDataRow[];
}

export type CompressedListing = [symbol: ProsperitySymbol, product: Product, denomination: Product];
Expand Down Expand Up @@ -144,7 +144,7 @@ export type CompressedTradingState = [

export type CompressedOrder = [symbol: ProsperitySymbol, price: number, quantity: number];

export type CompressedSandboxLogRow = [
export type CompressedAlgorithmDataRow = [
state: CompressedTradingState,
orders: CompressedOrder[],
conversions: number,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/visualizer/OrdersTable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Table } from '@mantine/core';
import { ReactNode } from 'react';
import { SandboxLogRow } from '../../models.ts';
import { AlgorithmDataRow } from '../../models.ts';
import { getAskColor, getBidColor } from '../../utils/colors.ts';
import { formatNumber } from '../../utils/format.ts';
import { SimpleTable } from './SimpleTable.tsx';

export interface OrdersTableProps {
orders: SandboxLogRow['orders'];
orders: AlgorithmDataRow['orders'];
}

export function OrdersTable({ orders }: OrdersTableProps): ReactNode {
Expand Down
8 changes: 4 additions & 4 deletions src/pages/visualizer/PositionChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function getLimit(algorithm: Algorithm, symbol: ProsperitySymbol): number {
// This code will be hit when a new product is added to the competition and the visualizer isn't updated yet
// In that case the visualizer doesn't know the real limit yet, so we make a guess based on the algorithm's positions

const product = algorithm.sandboxLogs[0].state.listings[symbol].product;
const product = algorithm.data[0].state.listings[symbol].product;

const positions = algorithm.sandboxLogs.map(row => row.state.position[product] || 0);
const positions = algorithm.data.map(row => row.state.position[product] || 0);
const minPosition = Math.min(...positions);
const maxPosition = Math.max(...positions);

Expand All @@ -29,7 +29,7 @@ function getLimit(algorithm: Algorithm, symbol: ProsperitySymbol): number {
export function PositionChart(): ReactNode {
const algorithm = useStore(state => state.algorithm)!;

const symbols = Object.keys(algorithm.sandboxLogs[0].state.listings).sort((a, b) => a.localeCompare(b));
const symbols = Object.keys(algorithm.data[0].state.listings).sort((a, b) => a.localeCompare(b));

const limits: Record<string, number> = {};
for (const symbol of symbols) {
Expand All @@ -41,7 +41,7 @@ export function PositionChart(): ReactNode {
data[symbol] = [];
}

for (const row of algorithm.sandboxLogs) {
for (const row of algorithm.data) {
for (const symbol of symbols) {
const position = row.state.position[symbol] || 0;
data[symbol].push([row.state.timestamp, (position / limits[symbol]) * 100]);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/visualizer/ProfitLossChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function ProfitLossChart(): ReactNode {
},
];

Object.keys(algorithm.sandboxLogs[0].state.listings)
Object.keys(algorithm.data[0].state.listings)
.sort((a, b) => a.localeCompare(b))
.forEach(symbol => {
const data = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Grid, Text, Title } from '@mantine/core';
import { ReactNode } from 'react';
import { ScrollableCodeHighlight } from '../../components/ScrollableCodeHighlight.tsx';
import { SandboxLogRow } from '../../models.ts';
import { AlgorithmDataRow } from '../../models.ts';
import { useStore } from '../../store.ts';
import { formatNumber } from '../../utils/format.ts';
import { ListingsTable } from './ListingsTable.tsx';
Expand All @@ -11,13 +11,13 @@ import { PositionTable } from './PositionTable.tsx';
import { ProfitLossTable } from './ProfitLossTable.tsx';
import { TradesTable } from './TradesTable.tsx';

export interface SandboxLogDetailProps {
row: SandboxLogRow;
export interface TimestampDetailProps {
row: AlgorithmDataRow;
}

export function SandboxLogDetail({
export function TimestampDetail({
row: { state, orders, conversions, traderData, algorithmLogs, sandboxLogs },
}: SandboxLogDetailProps): ReactNode {
}: TimestampDetailProps): ReactNode {
const algorithm = useStore(state => state.algorithm)!;

const profitLoss = algorithm.activityLogs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Slider, SliderProps, Text } from '@mantine/core';
import { useHotkeys } from '@mantine/hooks';
import { ReactNode, useState } from 'react';
import { SandboxLogRow } from '../../models.ts';
import { AlgorithmDataRow } from '../../models.ts';
import { useStore } from '../../store.ts';
import { formatNumber } from '../../utils/format.ts';
import { SandboxLogDetail } from './SandboxLogDetail.tsx';
import { TimestampDetail } from './TimestampDetail.tsx';
import { VisualizerCard } from './VisualizerCard.tsx';

export function SandboxLogsCard(): ReactNode {
export function TimestampsCard(): ReactNode {
const algorithm = useStore(state => state.algorithm)!;

const rowsByTimestamp: Record<number, SandboxLogRow> = {};
for (const row of algorithm.sandboxLogs) {
const rowsByTimestamp: Record<number, AlgorithmDataRow> = {};
for (const row of algorithm.data) {
rowsByTimestamp[row.state.timestamp] = row;
}

const timestampMin = algorithm.sandboxLogs[0].state.timestamp;
const timestampMax = algorithm.sandboxLogs[algorithm.sandboxLogs.length - 1].state.timestamp;
const timestampStep = algorithm.sandboxLogs[1].state.timestamp - algorithm.sandboxLogs[0].state.timestamp;
const timestampMin = algorithm.data[0].state.timestamp;
const timestampMax = algorithm.data[algorithm.data.length - 1].state.timestamp;
const timestampStep = algorithm.data[1].state.timestamp - algorithm.data[0].state.timestamp;

const [timestamp, setTimestamp] = useState(timestampMin);

Expand All @@ -35,7 +35,7 @@ export function SandboxLogsCard(): ReactNode {
]);

return (
<VisualizerCard title="Sandbox logs">
<VisualizerCard title="Timestamps">
<Slider
min={timestampMin}
max={timestampMax}
Expand All @@ -48,7 +48,7 @@ export function SandboxLogsCard(): ReactNode {
/>

{rowsByTimestamp[timestamp] ? (
<SandboxLogDetail row={rowsByTimestamp[timestamp]} />
<TimestampDetail row={rowsByTimestamp[timestamp]} />
) : (
<Text>No logs found for timestamp {formatNumber(timestamp)}</Text>
)}
Expand Down
6 changes: 3 additions & 3 deletions src/pages/visualizer/VisualizerPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AlgorithmSummaryCard } from './AlgorithmSummaryCard.tsx';
import { PositionChart } from './PositionChart.tsx';
import { PriceChart } from './PriceChart.tsx';
import { ProfitLossChart } from './ProfitLossChart.tsx';
import { SandboxLogsCard } from './SandboxLogsCard.tsx';
import { TimestampsCard } from './TimestampsCard.tsx';
import { VisualizerCard } from './VisualizerCard.tsx';
import { VolumeChart } from './VolumeChart.tsx';

Expand All @@ -27,7 +27,7 @@ export function VisualizerPage(): ReactNode {
}

const symbolColumns: ReactNode[] = [];
Object.keys(algorithm.sandboxLogs[0].state.listings)
Object.keys(algorithm.data[0].state.listings)
.sort((a, b) => a.localeCompare(b))
.forEach((symbol, i) => {
symbolColumns.push(
Expand Down Expand Up @@ -61,7 +61,7 @@ export function VisualizerPage(): ReactNode {
</Grid.Col>
{symbolColumns}
<Grid.Col span={12}>
<SandboxLogsCard />
<TimestampsCard />
</Grid.Col>
{algorithm.summary && (
<Grid.Col span={12}>
Expand Down
12 changes: 6 additions & 6 deletions src/utils/algorithm.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
ActivityLogRow,
Algorithm,
AlgorithmDataRow,
AlgorithmSummary,
CompressedAlgorithmDataRow,
CompressedListing,
CompressedObservations,
CompressedOrder,
CompressedOrderDepth,
CompressedSandboxLogRow,
CompressedTrade,
CompressedTradingState,
ConversionObservation,
Expand All @@ -16,7 +17,6 @@ import {
OrderDepth,
Product,
ProsperitySymbol,
SandboxLogRow,
Trade,
TradingState,
} from '../models.ts';
Expand Down Expand Up @@ -172,7 +172,7 @@ function decompressOrders(compressed: CompressedOrder[]): Record<ProsperitySymbo
return orders;
}

function decompressSandboxLogRow(compressed: CompressedSandboxLogRow, sandboxLogs: string): SandboxLogRow {
function decompressSandboxLogRow(compressed: CompressedAlgorithmDataRow, sandboxLogs: string): AlgorithmDataRow {
return {
state: decompressState(compressed[0]),
orders: decompressOrders(compressed[1]),
Expand All @@ -183,13 +183,13 @@ function decompressSandboxLogRow(compressed: CompressedSandboxLogRow, sandboxLog
};
}

function getSandboxLogs(logLines: string[]): SandboxLogRow[] {
function getSandboxLogs(logLines: string[]): AlgorithmDataRow[] {
const headerIndex = logLines.indexOf('Sandbox logs:');
if (headerIndex === -1) {
return [];
}

const rows: SandboxLogRow[] = [];
const rows: AlgorithmDataRow[] = [];
let nextSandboxLogs = '';

const sandboxLogPrefix = ' "sandboxLog": ';
Expand Down Expand Up @@ -239,7 +239,7 @@ export function parseAlgorithmLogs(logs: string, summary?: AlgorithmSummary): Al
return {
summary,
activityLogs,
sandboxLogs,
data: sandboxLogs,
};
}

Expand Down

0 comments on commit 980c512

Please sign in to comment.