Skip to content

Instantly share code, notes, and snippets.

@ruzli
Last active October 20, 2020 09:57
Show Gist options
  • Save ruzli/24bf0b70dd8cb636695037fad15140ea to your computer and use it in GitHub Desktop.
Save ruzli/24bf0b70dd8cb636695037fad15140ea to your computer and use it in GitHub Desktop.
Simple martingale with stop loss and stop profit
var config = {
baseBet: { label: "Base bet", type: "balance", value: 100 }, // how many satoshis to bet initially
target: { label: "Target", type: "multiplier", value: 2 }, // target multiplier
betMultiplier: { label: "Bet multiplier", type: "multiplier", value: 2 }, // what to multiply the bet size by when we lose a wager
stopBalance: { label: "Stop if profit above[0 = OFF]", type: "balance", value: 100000 },
stopLoss: { label: "Stop if lose more than[0 = OFF]", type: "balance", value: -100000 },
}
var engine = this
var highBalance = this.balance;
var lprofit = function(){
return highBalance + config.stopLoss.value;
};
var startBR = this.balance;
var profit = function() {
return Math.round(engine.balance - startBR).toFixed(2)
}
let lossCount = 0
this.log(`Starting martingale with a base bet of ${config.baseBet.value} satoshis.`)
while (true) {
checkConditions(this);
// make the bet and wait for the result
const { multiplier } = await this.bet(betSize(lossCount), config.target.value)
if (multiplier < config.target.value) { // loss
lossCount++
this.log(`Lost bet. Multiplying bet size by ${config.betMultiplier.value} for new bet size of ${betSize(lossCount)} satoshis.`)
} else { // win
lossCount = 0
this.log(`Won bet. Setting bet size to ${config.baseBet.value} satoshis.`)
}
}
function betSize(lossCount) {
const bet = config.baseBet.value * Math.pow(config.betMultiplier.value, lossCount)
return Math.round(bet / 100) * 100
}
function checkConditions(context){
if (config.stopLoss.value != 0) {
if (context.balance > highBalance) {
highBalance = context.balance;
} else if (context.balance < lprofit()) {
context.log("Stop loss triggered");
context.stop();
}
}
if (profit() > config.stopBalance.value && config.stopBalance.value != 0) {
context.log("Stop profit triggered")
context.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment