Dicebot Programmer mode tutorial 04 - Martingale (and mini If tutorial)
In the previous tutorial, we made a basic martingale script. Lets expand on that script a bit using variables.
So the previous script:
function dobet()
if win then
nextbet=0.00000001
else
nextbet=previousbet*2
end
end
First, lets take a look at one of the built in variables, currentstreak.
From the variables appendix:
currentstreak, type double. Permission: Read Only. Shows the current winning or losing streak. When positive (>0), it's a winning streak. When negative (<0) it's a losing streak. Can never be 0. Only set after first bet.
Lets say we want to reset to our base bet when it's a 5 losing streak. We want to test if current streak is equal to -5.
if currentstreak==-5 then
Do something
end
Lets add that to our script, inside the dobet function:
function dobet()
if win then
nextbet=0.00000001
else
nextbet=previousbet*2
end
if currentstreak==-5 then
nextbet=0.00000001
end
end
Now there are two things to note in this script. The first is that it will reset only on exactly 5 losses in a row. The next 5 losses in a row (so a 10 losing streak), will not reset to base. Nor the third (15), fourth (20) or any other, until you win a bet and get a 5 losing streak again. We can of course add a new if for each one of them.
function dobet()
if win then
nextbet=0.00000001
else
nextbet=previousbet*2
end if currentstreak==-5 then
nextbet=0.00000001
end
if currentstreak==-10 then
nextbet=0.00000001
end
if currentstreak==-15 then
nextbet=0.00000001
end
if currentstreak==-20 then
nextbet=0.00000001
end
if currentstreak==-25 then
nextbet=0.00000001
end
end
Now you have a lot of code repetition, and what if you go one step further than you coded for? We'll look at that in a moment.
The second thing to note is that we're setting the "base" bet six times. Which means if we decide to change our base bet to something else, we'd need to remember to change in all six places. Imagine doing this if you had an if function for each series of 5 losses in a row up to 100 losing streak (like hunting x100 or something)? You'd need to change it in many many places every time you want to update it.
The solution to the second problem is easy, so lets take a look at it first. We use a variable. Lets define a local variable called baseBet (programming is case sensitive! Keep that in mind when naming variables and functions) and set it to 1 satoshi:
baseBet=0.00000001
Now we can use this variable instead of the number, and only update the variable when we want to change it. So in the script, we can use it like this:
function dobet()
baseBet=0.00000001
if win then
nextbet=baseBet
else
nextbet=previousbet*2
end
if currentstreak==-5 then
nextbet=baseBet
end
if currentstreak==-10 then
nextbet=baseBet
end
if currentstreak==-15 then
nextbet=baseBet
end
if currentstreak==-20 then
nextbet=baseBet
end
if currentstreak==-25 then
nextbet=baseBet
end
end
In one of the previous tutorials I mentioned scope.
scope refers to the area of the code where certain functions or variables are available. DiceBot Programmer mode tutorial 1.1 - Variables
In this script, the baseBet variables scope is the dobet function. That means it can only be used between the function dobet() line and the corresponding end line. It also means it gets set to 0.00000001 every time the dobet function gets called, which is every time a bet is placed.
So how can we reset the bot to base bet after every 5 losses in a row, even if you're at a 1000 bet losing streak? The easiest way to do this is using the modulus operator (%). The modulus operator divides the number before the operator with the number behind it, and gives you back the remaining.
This is like going back to elementary school maths, before we learnt about decimals and fractions. If we divide 5 by 2, we have 2 and 1 remaining. (2*2=4, 5-4=1)
10 modulus 4 = 2 (4*2=8, 10-8=2)
15%4=3 (3*4=12,15-12=3)
So how can this help us to determine if it's a 5 losing streak? Whenever a number is a factor of another number, the modulus operator will return 0. For example 10%5=0, because 10 is divisible by 5. This means that whenever anything modulus 5 is 0, it's factor of 5 and thus there's been a losing streak of 5 bets since the last reset.
So instead of having that large block of if statements, we can replace it with just the following:
function dobet()
baseBet=0.00000001
if win then
nextbet=baseBet
else
nextbet=previousbet*2
end
if currentstreak%5==0 then
nextbet=baseBet
end
end
Lets look at one more situation for this tutorial. Lets say, when our session wagered amount is between 0.1 and 1, our base bet is 10 times as large (I never said these scripts are going to be practical). First, code is executed from the top to bottom, so we need to change the base bet at the applicable place in the code to have an effect whenever it's reset to base (remember that it gets reset with every bet and is only available inside the dobet function). We also need to determine when to change it.
baseBet gets defined right at the start of the dobet function. We can only use and change it after it's defined, but we need to update it before it's used for calculations. So, we will need to add our code to update it directly after the definition.
We will need an if statement to determine whether it's between 0.1 and 1, and that if will look something like this:
if <wagered amount> > 0.1 then
if <wagered amount> < 1 then
update the value of basebet here
end
end
Check if the value is larger than 0.1, then check if the value is smaller than 1, then update it. While this works, it's not a great way to do it. Ifs have a few cool tricks you can use, like the AND and OR operators. Also, we need to track how much we wager!
Using an AND operator, we can condense the previous if into a shorter one:
if <wagered amount> > 0.1 and <wagered amount> < 1 then
update the value of basebet here
end
As you can expect, the AND operator takes the statement before and after it, and only returns true when BOTH are true at the same time.
Similarly, you can use the OR operator, for example:
if profit>0.001 or balance>1 then
do something
end
The OR returns true if either of the two statements are true. So your balance can be below 1 but if you profit is above 0.001, something will be done. Or vica versa.
Now we just need to track how much we've wagered. For this, we can't use a variable that gets reset after every bet, so it will need to be defined globally. This means outside of the dobet function. We also want to update it after every bet with the bet amount:
wagered=0
function dobet()
baseBet=0.00000001
newwagered = wagered + previousbet
wagered=newwagered
if wagered>0.1 and wagered<1 then
baseBet=0.0000001
end
if win then
nextbet=baseBet
else
nextbet=previousbet*2
end
if currentstreak%5==0 then
nextbet=baseBet
end
end
The two lines "newwagered = wagered + previousbet" and "wagered=newwagered" updates the wagered amount, by taking what it was, adding the amount of the latest bet to it and assigning it to a new variable, and then assign the old variable to the new one. You don't need that extra step and it can be done in a single line: wagered=wagered+previousbet
So our completed script:
wagered=0
function dobet()
baseBet=0.00000001
wagered = wagered + previousbet
if wagered>0.1 and wagered<1 then
baseBet=0.0000001
end
if win then
nextbet=baseBet
else
nextbet=previousbet*2
end
if currentstreak%5==0 then
nextbet=baseBet
end
end