|
| 1 | +import { |
| 2 | + VictoryChart, |
| 3 | + VictoryGroup, |
| 4 | + VictoryArea, |
| 5 | + VictoryLine, |
| 6 | + VictoryAxis, |
| 7 | + VictoryLabel, |
| 8 | +} from 'victory' |
| 9 | +import { LimitBet } from 'common/bet' |
| 10 | +import { CPMMBinaryContract, PseudoNumericContract } from 'common/contract' |
| 11 | +import { getDisplayProbability } from 'common/calculate' |
| 12 | + |
| 13 | +interface DepthChartProps { |
| 14 | + contract: CPMMBinaryContract | PseudoNumericContract |
| 15 | + yesBets: LimitBet[] |
| 16 | + noBets: LimitBet[] |
| 17 | +} |
| 18 | + |
| 19 | +export function DepthChart(props: DepthChartProps) { |
| 20 | + const { contract, yesBets, noBets } = props |
| 21 | + |
| 22 | + // Won't display a depth chart for numeric contracts, only binary contracts right now |
| 23 | + if (contract.outcomeType === 'PSEUDO_NUMERIC') { |
| 24 | + return <div className="depth-chart" /> |
| 25 | + } |
| 26 | + |
| 27 | + const yesData = cumulative(yesBets) |
| 28 | + const noData = cumulative(noBets) |
| 29 | + const maxAmount = Math.max( |
| 30 | + yesData[yesData.length - 1].y, |
| 31 | + noData[noData.length - 1].y |
| 32 | + ) |
| 33 | + let minX = yesData[yesData.length - 1].x |
| 34 | + let maxX = noData[noData.length - 1].x |
| 35 | + const xRange = maxX - minX |
| 36 | + minX -= xRange * 0.1 |
| 37 | + if (minX < 0) { |
| 38 | + minX = 0 |
| 39 | + } |
| 40 | + maxX += xRange * 0.1 |
| 41 | + if (maxX > 1) { |
| 42 | + maxX = 1 |
| 43 | + } |
| 44 | + yesData.unshift({ x: minX, y: yesData[0].y }) |
| 45 | + noData.push({ x: maxX, y: noData[noData.length - 1].y }) |
| 46 | + |
| 47 | + const currentValue = getDisplayProbability(contract) |
| 48 | + |
| 49 | + return ( |
| 50 | + <div className="depth-chart"> |
| 51 | + <h2>Market depth chart</h2> |
| 52 | + <VictoryChart |
| 53 | + minDomain={{ x: minX, y: 0 }} |
| 54 | + maxDomain={{ x: maxX, y: maxAmount }} |
| 55 | + padding={{ top: 50, bottom: 50, left: 100, right: 50 }} |
| 56 | + domainPadding={30} |
| 57 | + > |
| 58 | + <VictoryAxis |
| 59 | + tickFormat={(t) => `${Math.round(t * 100)}%`} |
| 60 | + tickCount={10} |
| 61 | + label="Chance" |
| 62 | + axisLabelComponent={<VictoryLabel dy={10} />} |
| 63 | + /> |
| 64 | + <VictoryAxis |
| 65 | + tickFormat={(t) => `${Math.round(t)}` + 'M'} |
| 66 | + tickCount={6} |
| 67 | + dependentAxis={true} |
| 68 | + axisLabelComponent={<VictoryLabel dy={-30} />} |
| 69 | + label={'Amount'} |
| 70 | + /> |
| 71 | + <VictoryGroup |
| 72 | + style={{ |
| 73 | + data: { strokeWidth: 1, fillOpacity: 0.4 }, |
| 74 | + }} |
| 75 | + > |
| 76 | + {/* // Line that shows the current value */} |
| 77 | + <VictoryLine |
| 78 | + style={{ |
| 79 | + data: { stroke: 'gray' }, |
| 80 | + }} |
| 81 | + data={[ |
| 82 | + { x: currentValue, y: 0 }, |
| 83 | + { x: currentValue, y: maxAmount }, |
| 84 | + ]} |
| 85 | + /> |
| 86 | + {/* First vertical line of the "yes" bets */} |
| 87 | + <VictoryLine |
| 88 | + style={{ |
| 89 | + data: { stroke: '#059669' }, |
| 90 | + }} |
| 91 | + data={[ |
| 92 | + { x: yesData[yesData.length - 1].x, y: 0 }, |
| 93 | + { |
| 94 | + x: yesData[yesData.length - 1].x, |
| 95 | + y: yesData[yesData.length - 1].y, |
| 96 | + }, |
| 97 | + ]} |
| 98 | + /> |
| 99 | + {/* First vertical line of the "no" bets */} |
| 100 | + <VictoryLine |
| 101 | + style={{ |
| 102 | + data: { stroke: 'red' }, |
| 103 | + }} |
| 104 | + data={[ |
| 105 | + { x: noData[0].x, y: 0 }, |
| 106 | + { x: noData[0].x, y: noData[0].y }, |
| 107 | + ]} |
| 108 | + /> |
| 109 | + {/* // Area that shows the yes bets */} |
| 110 | + <VictoryArea |
| 111 | + style={{ |
| 112 | + data: { fill: '#6EE7B7', stroke: '#059669' }, |
| 113 | + }} |
| 114 | + data={yesData} |
| 115 | + interpolation="stepBefore" |
| 116 | + /> |
| 117 | + {/* // Area that shows the no bets */} |
| 118 | + <VictoryArea |
| 119 | + style={{ |
| 120 | + data: { fill: 'red', stroke: 'red' }, |
| 121 | + }} |
| 122 | + data={noData} |
| 123 | + interpolation="stepAfter" |
| 124 | + /> |
| 125 | + </VictoryGroup> |
| 126 | + </VictoryChart> |
| 127 | + </div> |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +type Coordinate = { x: number; y: number } |
| 132 | + |
| 133 | +// Converts a list of LimitBets into a list of coordinates to render into a depth chart. |
| 134 | +// Going in order of probability, the y value accumulates each order's amount. |
| 135 | +function cumulative(bets: LimitBet[]): Coordinate[] { |
| 136 | + const result: Coordinate[] = [] |
| 137 | + let totalAmount = 0 |
| 138 | + |
| 139 | + for (let i = 0; i < bets.length; i++) { |
| 140 | + totalAmount += bets[i].orderAmount |
| 141 | + result.push({ x: bets[i].limitProb, y: totalAmount }) |
| 142 | + } |
| 143 | + |
| 144 | + return result |
| 145 | +} |
0 commit comments