409 lines
7.1 KiB
TypeScript
409 lines
7.1 KiB
TypeScript
/**
|
||
|
||
* Synthetic candle generation and stake-pool outcome resolution.
|
||
|
||
*/
|
||
|
||
|
||
|
||
export type RoundOutcome =
|
||
|
||
| { type: 'short_win'; winner: 'short'; neutralColor: null }
|
||
|
||
| { type: 'long_win'; winner: 'long'; neutralColor: null }
|
||
|
||
| { type: 'neutral'; winner: null; neutralColor: 'red' | 'green' };
|
||
|
||
|
||
|
||
export type GeneratedCandle = {
|
||
|
||
open: number;
|
||
|
||
high: number;
|
||
|
||
low: number;
|
||
|
||
close: number;
|
||
|
||
colorHint: 'red' | 'green';
|
||
|
||
};
|
||
|
||
|
||
|
||
export type PrevCandleOhlc = {
|
||
|
||
open: number;
|
||
|
||
high: number;
|
||
|
||
low: number;
|
||
|
||
close: number;
|
||
|
||
};
|
||
|
||
|
||
|
||
export function defaultSeedCandle(price: number): PrevCandleOhlc {
|
||
|
||
const ref = price > 0 ? price : 100;
|
||
|
||
const bodyMove = ref * 0.0025;
|
||
|
||
const wick = ref * 0.0012;
|
||
|
||
const open = ref;
|
||
|
||
const close = ref + bodyMove;
|
||
|
||
return {
|
||
|
||
open,
|
||
|
||
close,
|
||
|
||
high: close + wick,
|
||
|
||
low: open - wick * 0.35,
|
||
|
||
};
|
||
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
|
||
* Resolves round outcome from stake pools.
|
||
|
||
* Winner side is chosen by pool_winner_rule when pools are not neutral; otherwise neutral (push).
|
||
|
||
*/
|
||
|
||
export type PoolWinnerRule = 'minority' | 'majority';
|
||
|
||
|
||
|
||
export type PoolNeutralityRules = {
|
||
|
||
/** When |short% − long%| <= this value, round is neutral (pools too close). */
|
||
|
||
minTolerancePercent: number;
|
||
|
||
/** When max(short%, long%) >= this value, round is neutral (pools too one-sided). */
|
||
|
||
maxTolerancePercent: number;
|
||
|
||
minToleranceEnabled: boolean;
|
||
|
||
maxToleranceEnabled: boolean;
|
||
|
||
};
|
||
|
||
|
||
|
||
function neutralOutcome(shortPct: number, longPct: number): RoundOutcome {
|
||
let neutralColor: 'red' | 'green' = 'green';
|
||
if (shortPct < longPct) neutralColor = 'red';
|
||
else if (longPct < shortPct) neutralColor = 'green';
|
||
return { type: 'neutral', winner: null, neutralColor };
|
||
}
|
||
|
||
/**
|
||
* Shared neutrality checks used by majority/minority and daily-revenue modes.
|
||
* Returns a neutral outcome when rules apply; otherwise null.
|
||
*/
|
||
export function resolvePoolNeutrality(
|
||
shortPool: number,
|
||
longPool: number,
|
||
rules: PoolNeutralityRules
|
||
): RoundOutcome | null {
|
||
const total = shortPool + longPool;
|
||
if (total <= 0) {
|
||
return { type: 'neutral', winner: null, neutralColor: 'green' };
|
||
}
|
||
|
||
const shortPct = (shortPool / total) * 100;
|
||
const longPct = (longPool / total) * 100;
|
||
const gap = Math.abs(shortPct - longPct);
|
||
const majorityPct = Math.max(shortPct, longPct);
|
||
|
||
if (rules.minToleranceEnabled && gap <= rules.minTolerancePercent) {
|
||
return neutralOutcome(shortPct, longPct);
|
||
}
|
||
|
||
// Max imbalance (e.g. 95/5 or solo 100/0): when enabled, extreme one-sided
|
||
// pools are neutral (refund), including a single participant at 100%.
|
||
if (rules.maxToleranceEnabled && majorityPct >= rules.maxTolerancePercent) {
|
||
return neutralOutcome(shortPct, longPct);
|
||
}
|
||
|
||
// Exact equal pools are always a push (refund), even if min-tolerance is off.
|
||
if (shortPool === longPool) {
|
||
return neutralOutcome(shortPct, longPct);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
export function resolvePoolOutcome(
|
||
shortPool: number,
|
||
longPool: number,
|
||
rules: PoolNeutralityRules,
|
||
poolWinnerRule: PoolWinnerRule = 'minority'
|
||
): RoundOutcome {
|
||
const neutral = resolvePoolNeutrality(shortPool, longPool, rules);
|
||
if (neutral) return neutral;
|
||
|
||
const shortWins =
|
||
poolWinnerRule === 'majority' ? shortPool > longPool : shortPool < longPool;
|
||
|
||
if (shortWins) {
|
||
return { type: 'short_win', winner: 'short', neutralColor: null };
|
||
}
|
||
|
||
return { type: 'long_win', winner: 'long', neutralColor: null };
|
||
}
|
||
|
||
|
||
|
||
export type DirectionalOutcomeType = 'short_win' | 'long_win';
|
||
|
||
|
||
|
||
/** Default streak cap when admin config is missing / unset. */
|
||
export const DEFAULT_MAX_CONSECUTIVE_SAME_DIRECTION = 5;
|
||
|
||
/**
|
||
* Clamp admin value: `0` disables the cap; otherwise `1..50`.
|
||
*/
|
||
export function normalizeMaxConsecutiveSameDirection(value: unknown): number {
|
||
const n = typeof value === 'number' ? value : Number(value);
|
||
if (!Number.isFinite(n) || n <= 0) return 0;
|
||
return Math.min(50, Math.max(1, Math.floor(n)));
|
||
}
|
||
|
||
/**
|
||
|
||
* Counts how many of the most recent directional outcomes (short/long) share the same type.
|
||
|
||
* Neutral outcomes break the streak and are skipped.
|
||
|
||
*/
|
||
|
||
export function countConsecutiveDirectionalOutcomes(
|
||
|
||
recentOutcomesNewestFirst: Array<'short_win' | 'long_win' | 'neutral'>
|
||
|
||
): { type: DirectionalOutcomeType | null; count: number } {
|
||
|
||
let type: DirectionalOutcomeType | null = null;
|
||
|
||
let count = 0;
|
||
|
||
|
||
|
||
for (const outcome of recentOutcomesNewestFirst) {
|
||
|
||
if (outcome !== 'short_win' && outcome !== 'long_win') {
|
||
|
||
break;
|
||
|
||
}
|
||
|
||
if (type === null) {
|
||
|
||
type = outcome;
|
||
|
||
count = 1;
|
||
|
||
continue;
|
||
|
||
}
|
||
|
||
if (outcome === type) {
|
||
|
||
count += 1;
|
||
|
||
continue;
|
||
|
||
}
|
||
|
||
break;
|
||
|
||
}
|
||
|
||
|
||
|
||
return { type, count };
|
||
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
|
||
* Random short/long outcome for rounds with no player predictions.
|
||
|
||
* After `maxConsecutiveSameDirection` consecutive candles of the same direction,
|
||
|
||
* the next must be the opposite. Pass `0` to disable the cap (pure random).
|
||
|
||
*/
|
||
|
||
export function resolveAutoNoBetOutcome(
|
||
|
||
recentOutcomesNewestFirst: Array<'short_win' | 'long_win' | 'neutral'>,
|
||
|
||
maxConsecutiveSameDirection: number = DEFAULT_MAX_CONSECUTIVE_SAME_DIRECTION
|
||
|
||
): Extract<RoundOutcome, { type: DirectionalOutcomeType }> {
|
||
|
||
const maxConsecutive = normalizeMaxConsecutiveSameDirection(maxConsecutiveSameDirection);
|
||
|
||
const { type, count } = countConsecutiveDirectionalOutcomes(recentOutcomesNewestFirst);
|
||
|
||
|
||
|
||
if (maxConsecutive > 0 && type && count >= maxConsecutive) {
|
||
|
||
return type === 'short_win'
|
||
|
||
? { type: 'long_win', winner: 'long', neutralColor: null }
|
||
|
||
: { type: 'short_win', winner: 'short', neutralColor: null };
|
||
|
||
}
|
||
|
||
|
||
|
||
if (Math.random() < 0.5) {
|
||
|
||
return { type: 'short_win', winner: 'short', neutralColor: null };
|
||
|
||
}
|
||
|
||
return { type: 'long_win', winner: 'long', neutralColor: null };
|
||
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
|
||
* Neutral = exact copy of previous OHLC (same body, wicks, and price level).
|
||
|
||
* Only colorHint differs for display.
|
||
|
||
*/
|
||
|
||
export function replicateNeutralCandle(
|
||
|
||
prev: PrevCandleOhlc,
|
||
|
||
colorHint: 'red' | 'green'
|
||
|
||
): GeneratedCandle {
|
||
|
||
return {
|
||
|
||
open: prev.open,
|
||
|
||
high: prev.high,
|
||
|
||
low: prev.low,
|
||
|
||
close: prev.close,
|
||
|
||
colorHint,
|
||
|
||
};
|
||
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
|
||
* Builds OHLC for the synthetic candle following the resolved outcome.
|
||
|
||
* Long candles anchor at the top-right of the previous body; short at the bottom-right.
|
||
|
||
*/
|
||
|
||
export function generateSyntheticCandle(
|
||
|
||
prev: PrevCandleOhlc,
|
||
|
||
outcome: RoundOutcome
|
||
|
||
): GeneratedCandle {
|
||
|
||
if (outcome.type === 'neutral') {
|
||
|
||
return replicateNeutralCandle(prev, outcome.neutralColor);
|
||
|
||
}
|
||
|
||
|
||
|
||
const ref = prev.close > 0 ? prev.close : 100;
|
||
|
||
const bodyMove = ref * 0.0025;
|
||
|
||
const wick = ref * 0.0012;
|
||
|
||
const prevTop = Math.max(prev.open, prev.close);
|
||
|
||
const prevBottom = Math.min(prev.open, prev.close);
|
||
|
||
|
||
|
||
if (outcome.type === 'short_win') {
|
||
|
||
const open = prevBottom;
|
||
|
||
const close = open - bodyMove;
|
||
|
||
return {
|
||
|
||
open,
|
||
|
||
high: open + wick * 0.35,
|
||
|
||
low: close - wick,
|
||
|
||
close,
|
||
|
||
colorHint: 'red',
|
||
|
||
};
|
||
|
||
}
|
||
|
||
|
||
|
||
const open = prevTop;
|
||
|
||
const close = open + bodyMove;
|
||
|
||
return {
|
||
|
||
open,
|
||
|
||
high: close + wick,
|
||
|
||
low: open - wick * 0.35,
|
||
|
||
close,
|
||
|
||
colorHint: 'green',
|
||
|
||
};
|
||
|
||
}
|
||
|
||
|