Last active
June 30, 2021 01:16
-
-
Save zundra/3547e6883dd7f90e66f77cd7cacb4cb2 to your computer and use it in GitHub Desktop.
median_based.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = { | |
payout: { | |
value: 2.1, | |
type: "multiplier", | |
label: "payout", | |
} | |
}; | |
var stateVars = { | |
roundResults: [], | |
bustBelowMedianCount: 0, | |
runNext: false, | |
runningCountSize: 0, | |
attemptCount: 0, | |
avg: 0, | |
historyLoaded: false, | |
stdDev: 0, | |
median: 0, | |
increaseValues: [5, 15, 35, 75, 125, 300, 700], | |
} | |
log("Script is running.."); | |
engine.on("GAME_STARTING", onGameStarted); | |
engine.once("GAME_STARTING", () => engine.on("GAME_ENDED", onGameEnded)); | |
function onGameStarted() | |
{ | |
if (!stateVars.historyLoaded) | |
{ | |
log("Loading history..."); | |
loadHistory(); | |
stateVars.historyLoaded = true; | |
} | |
if (stateVars.runNext) | |
{ | |
log("calculateNextPayout ", calculateNextPayout()); | |
makeBet(calculateNextBet(), calculateNextPayout()); | |
stateVars.attemptCount = stateVars.attemptCount + 1; | |
} | |
} | |
function onGameEnded() { | |
if (!stateVars.historyLoaded) | |
{ | |
return; | |
} | |
var lastGame = engine.history.first(); | |
stateVars.roundResults.pop(); | |
stateVars.roundResults.push(lastGame.bust); | |
if (lastGame.cashedAt) | |
{ | |
var profit = (config.wager.value * lastGame.cashedAt - config.wager.value) / 100; | |
log("Won", profit.toFixed(2), "bits"); | |
} | |
stateVars.avg = calculateAverage(); | |
stateVars.stdDev = calculateStandardDeviation(); | |
stateVars.median = calculateMedian(); | |
log("getLastBust() > stateVars.median", (getLastBust() > stateVars.median)); | |
log("stateVars.median ", stateVars.median); | |
log("getLastBust() ", getLastBust()); | |
if (getLastBust() < stateVars.median) | |
{ | |
stateVars.bustBelowMedianCount++; | |
if (stateVars.bustBelowMedianCount == 2 && stateVars.bustBelowMedianCount < stateVars.increaseValues.length) | |
{ | |
stateVars.runNext = true; | |
} | |
} | |
else | |
{ | |
stateVars.bustBelowMedianCount = 0; | |
stateVars.attemptCount = 0; | |
stateVars.runNext = false; | |
} | |
printLogs(); | |
} | |
// Helper functions | |
function loadHistory() | |
{ | |
var history = engine.history.toArray().reverse(); | |
for (var i = 0; i < history.length; i++) | |
{ | |
stateVars.roundResults.push(history[i].bust); | |
} | |
} | |
function calculateNextBet() | |
{ | |
return roundBit(stateVars.increaseValues[stateVars.attemptCount] * 100); | |
} | |
function calculateNextPayout() | |
{ | |
if (stateVars.median < config.payout.value) | |
{ | |
return roundValueTwoDecimaal(stateVars.median) | |
} | |
else | |
{ | |
return config.payout.value; | |
} | |
} | |
function getLastBust() | |
{ | |
return stateVars.roundResults[stateVars.roundResults.length - 1]; | |
} | |
function calculateStandardDeviation() { | |
var array = stateVars.roundResults; | |
const n = array.length | |
if (n == 0) | |
return 0; | |
const mean = array.reduce((a, b) => a + b) / n; | |
return Math.sqrt(array.map(x => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n) | |
} | |
function calculateMedian() | |
{ | |
var array = stateVars.roundResults.slice(); | |
if(array.length ===0) return 0; | |
array.sort(function(a,b) | |
{ | |
return a-b; | |
}); | |
var half = Math.floor(array.length / 2); | |
if (array.length % 2) | |
return array[half]; | |
return (array[half - 1] + array[half]) / 2.0; | |
} | |
function calculateAverage() | |
{ | |
var runningTotal = 0; | |
if (stateVars.roundResults.length == 0) | |
{ | |
return 0; | |
} | |
for(var i = 0; i < stateVars.roundResults.length; i++) | |
{ | |
runningTotal += stateVars.roundResults[i]; | |
} | |
return runningTotal / stateVars.roundResults.length; | |
} | |
function roundBit(bit) | |
{ | |
return Math.round(bit / 100) * 100; | |
} | |
function roundValue(value) | |
{ | |
return Math.round(value / 100); | |
} | |
function roundValueTwoDecimaal(value) | |
{ | |
return Math.round(value * 100) / 100; | |
} | |
function makeBet(betVal, payoutVal) | |
{ | |
engine.bet(betVal, payoutVal); | |
log( | |
"betting", | |
roundValue(betVal), | |
"on", | |
payoutVal, | |
"x", | |
"Attempt [" + stateVars.attemptCount + "]" | |
); | |
} | |
function printLogs() | |
{ | |
log("runNext = " + stateVars.runNext); | |
log("THE IS AVERAGE = ", stateVars.avg , "OVER", stateVars.roundResults.length, "GAMES"); | |
log("THE MEDIAN = " + stateVars.median); | |
log("THE STANDARD DEV = " + stateVars.stdDev); | |
log("DIFF = " + (stateVars.avg - stateVars.stdDev)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment