162 lines
5.1 KiB
JavaScript
162 lines
5.1 KiB
JavaScript
"use strict";
|
|
/**
|
|
|
|
* Synthetic candle generation and stake-pool outcome resolution.
|
|
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.defaultSeedCandle = defaultSeedCandle;
|
|
exports.resolvePoolOutcome = resolvePoolOutcome;
|
|
exports.countConsecutiveDirectionalOutcomes = countConsecutiveDirectionalOutcomes;
|
|
exports.resolveAutoNoBetOutcome = resolveAutoNoBetOutcome;
|
|
exports.replicateNeutralCandle = replicateNeutralCandle;
|
|
exports.generateSyntheticCandle = generateSyntheticCandle;
|
|
function defaultSeedCandle(price) {
|
|
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,
|
|
};
|
|
}
|
|
function neutralOutcome(shortPct, longPct) {
|
|
let neutralColor = 'green';
|
|
if (shortPct < longPct)
|
|
neutralColor = 'red';
|
|
else if (longPct < shortPct)
|
|
neutralColor = 'green';
|
|
return { type: 'neutral', winner: null, neutralColor };
|
|
}
|
|
function resolvePoolOutcome(shortPool, longPool, rules, poolWinnerRule = 'minority') {
|
|
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);
|
|
}
|
|
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 };
|
|
}
|
|
const MAX_CONSECUTIVE_SAME_DIRECTION = 5;
|
|
/**
|
|
|
|
* Counts how many of the most recent directional outcomes (short/long) share the same type.
|
|
|
|
* Neutral outcomes break the streak and are skipped.
|
|
|
|
*/
|
|
function countConsecutiveDirectionalOutcomes(recentOutcomesNewestFirst) {
|
|
let type = 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 five consecutive candles of the same direction, the next must be the opposite.
|
|
|
|
*/
|
|
function resolveAutoNoBetOutcome(recentOutcomesNewestFirst) {
|
|
const { type, count } = countConsecutiveDirectionalOutcomes(recentOutcomesNewestFirst);
|
|
if (type && count >= MAX_CONSECUTIVE_SAME_DIRECTION) {
|
|
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.
|
|
|
|
*/
|
|
function replicateNeutralCandle(prev, colorHint) {
|
|
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.
|
|
|
|
*/
|
|
function generateSyntheticCandle(prev, outcome) {
|
|
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',
|
|
};
|
|
}
|
|
//# sourceMappingURL=candleGenerationService.js.map
|