Created
August 1, 2019 06:44
-
-
Save dsetzer/b15e26cfacf63946354812128b0f1bd8 to your computer and use it in GitHub Desktop.
Complete rewrite of apexograph v2 with added trailing stop (v2.1).
This file contains hidden or 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 = { | |
baseBet: { type: 'balance', label: 'Added Base Bet', value: 10000 }, // Extra amount added to base bet. | |
betBalance: { type: 'multiplier', label: 'Max Bettable Bal', value: 0.03 },// Max portion of bal to bet with. | |
maxPayout: { type: 'multiplier', label: 'Payout Max Clamp', value: 3 }, // Upper bound limit on payout. | |
minPayout: { type: 'multiplier', label: 'Payout Min Clamp', value: 1.5 }, // Lower bound limit on payout. | |
debug: { type: 'checkbox', label: 'Verbose Log Output', value: false }, // Outputs additional information. | |
}; | |
const debug = config.debug.value, startBalance = userInfo.balance; | |
let hmrsv = 3, results = [], loses = [], vectorApex = 0, nextBet = 0; | |
let trailDist = 0.6 * startBalance, trailBalance = (startBalance - trailDist); | |
try { engine.getState().history.forEach(h => results.push(h.bust)); } catch(e) {} | |
function calcTarget(hmrsv, results) { | |
const pLen = results.length | |
if (pLen < hmrsv) { | |
if (debug) { log("[DEBUG] Not enough input to slice. Returning.. "); } return; | |
} | |
const r = results.slice(-hmrsv).sort((a, b) => a - b), len = r.length, mid = Math.floor(len / 2) | |
return clampTarget((len % 2 ? r[mid] : ((r[mid - 1] + r[mid]) / 2))); | |
} | |
function roundBit(bet) { | |
return Math.max(100, Math.round(bet / 100) * 100); | |
} | |
function calcBet() { | |
const betBalPerc = config.betBalance.value, lossCount = loses.length || 0; | |
let baseBet = 0 + config.baseBet.value + (userInfo.balance * (betBalPerc / (200 - lossCount))); | |
vectorApex = calcTarget(hmrsv, results); | |
if (!lossCount) { | |
if (debug) log(`[DEBUG] Base bet ${baseBet/100}, no losses.`); | |
return baseBet; | |
} | |
const losses = (loses.reduce((a, b) => a + b) * 100); | |
nextBet = roundBit((baseBet + losses) / (vectorApex - 1)); | |
if (debug) log(`[DEBUG] Next ${nextBet/100}, (base ${baseBet/100}+(${losses/100}/${vectorApex} losses)`); | |
return nextBet; | |
} | |
engine.on('GAME_STARTING', () => { | |
if (results.length < hmrsv) return; | |
nextBet = roundBit(calcBet()), vectorApex = calcTarget(hmrsv, results) | |
let losses = loses.length ? (loses.reduce((a, b) => a + b) * 100) : 0; | |
if((userInfo.balance - (losses + nextBet)) <= trailBalance){ | |
log(`[WARN] Stopped before betting past trail amount, next bet was ${nextBet/100}. Retained ${(userInfo.balance-startBalance)/100} profit.`); | |
stop('Trailing-Stop Triggered'); | |
} | |
engine.bet(nextBet, vectorApex); | |
log(`[INFO] Balance: ${userInfo.balance/100} bits. Betting ${nextBet/100} bits @ ${vectorApex}x next.`) | |
}); | |
engine.on('GAME_ENDED', () => { | |
const lastGame = engine.history.first(); | |
results.push(lastGame.bust); | |
if (results.length < hmrsv) { | |
if (debug) log(`[DEBUG] gathering initial data (${results.length}/${hmrsv} results)`); | |
return; | |
} | |
if (lastGame.wager){ | |
if(lastGame.cashedAt) { | |
loses = [] | |
if((userInfo.balance - trailDist) > trailBalance){ | |
trailBalance = (userInfo.balance - trailDist) | |
if(debug) log(`[DEBUG] Trail balance raised to ${trailBalance/100} bits`); | |
} | |
const profit = lastGame.wager * lastGame.cashedAt - lastGame.wager; | |
log(`[INFO] (WON) Bust at ${lastGame.bust}x, Cashed at ${lastGame.cashedAt}x for ${profit/100} bits profit.`); | |
} else { | |
loses.push(nextBet / 100); | |
const loss = loses.reduce((a, b) => a + b); | |
log(`[INFO] (LOST) Bust at ${lastGame.bust}x, Current losses at ${loss} bits`) | |
} | |
} | |
}); | |
function clampTarget(target) { | |
return Math.max(config.minPayout.value, Math.min(config.maxPayout.value, target)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment