all right let me set the context for the video in this video we'll be coding a popular gambling strategy called the Martingale system on a gambling website called stake Martingale is a fairly common betting strategy it's also very easy to understand and it works on most days this is one of the reasons that casinos have limits on their tables minimum and maximum amount of money that you can bet on any table is restricted to prevent some of these strategies we'll understand what exactly is this strategy we'll try to run this strategy by hand on stake and then eventually we'll be coding the strategy and letting it run for 30 minutes I will be profitable in the first 30 minutes and then we'll let it run for a day we'll see is this a profitable strategy what makes it profitable or unprofitable why do casinos even allow this strategy if it is profitable and how can you do some basic mathematics to realize U whether or not this will work in the long run without any further Ado let's get right into the video all right let's try to understand amongst the many games that exist on a gambling website for example a website like the one that you're seeing on screen um can you trick the system a little bit are there strategies that might be profitable in the long run um you might have heard of card card counting in uh Blackjack that is one strategy that a few people are able to use in online casinos of course online casinos also try to make sure you cannot do it but other than that you know what are some simple strategies that you can use uh to try and not fake a casino but you know increase your probability of winning at the very least um if you look at the games over here we'll be going through this game called dice which you know it's basically 50/50 um you bet a certain amount let's say I bet $1 or and then if this number that you know come anywhere between 0 to 100 if it's less than 50 then I lose the $1 if it's more than 50 then I make some money so if I bet one more time I again lose if I bet one more time I lost three and now I made back $1 because it's more than 50% I made back $1 again because it's more than 50% so as you can see it says the roll over is 50.50 which means if it's above 50.50 I will make money if it is below 50.50 I will lose money so what's my probability of winning my probability of winning or my winning chance is 49.5% this is how the casino makes money right the casino will only make if I put it at 50/50 then you know I will not make 2x I will make slightly lesser because the casino also needs to make money um in the longer run if you're playing this game again and again you will lose money because half of the times it will come here half of the times it will come here whenever it comes here you go down to zero whenever it comes here you don't double up you go up to 1.98 of your original amount slightly less than 2x so that the casino makes money in the longer run because in the longer run 50% of the times it will be here 50% of the times it will be here if I increase this number you know if I increase the slider to go up to 51 then my win chance goes down I will only win 49% of the times but I will you know my multiplier goes up so the amount of money that I will build will be much higher but either way always the probability of me making money will be less than one in this case if you multiply 49 by 2.24 2.24 into 49 you will see that number comes down to 99.76% less than 100 which means you're going to lose money question is what is that strategy that will make you make money here and the answer is this weird strategy that you might have thought of sometime in your free time I don't know I I think of a lot of these things in my free time let's say this is the game which has you know almost 50% win probability let's say 49% even if not 50% that is fine the game has a win probability of 49% which means if I send it $1 there is a 50% probability goes down to zero 50% probability it goes down up to $2 the strategy goes something like this you will bet $1 and if it goes down to zero you will now bet $2 I repeat you will now bet $2 and if it again goes down to zero then you will bet $4 if it again goes down to Zer then you will bet $8 so on and so forth you will keep doubling the amount that you're sending and at some point of course you will win you can't keep losing until internity and whenever you win you will make your money back if you win the first time your $1 goes up to $2 if you win the second time your 1 +2 $3 goes up to $4 so your net profit here also is $1 here also is $1 if you win the third time 1 + 2 + 4 will go up to8 and in this case also your net profit will be $1 so it doesn't matter when you win as long as you have enough liquidity to support multiple transactions and keep doubling up you will be fine right that makes some mathematical sense the same logic can be applied to a game like roulette so if I go to roulette over here if You' ever tried to play roulette I can bet either on red or black if I bet on red the probability of red coming here is slightly less than 50% right because half of them are red half of them are black but then is one that is green as well so slightly less than 50% of the times black will come slightly less than 50% of the times red will come if I bet on red if I bet it came to red and I bet on red so my money went up by a bit um here again you know if it would have died I could have just bet twice the amount and then kept on going um so this is you know a commonly used strategy um question is does it work the answer is it won't in the long run than else everyone would make it but why does it not work we will see very soon first we will code it let's open the dice game and let's try to let's try to simulate the strategy first by hand if I do a 0.1 over here and I bet it it won great I'm in profit it lost I'm going to double the amount and bet again it lost double the amount bet again I have won I'm going to reset to 0.1 I'm going to bet I lost I'm going to double up and bet I won again then I will go back to 0.1 bet won 0.1 I lost I will double up and bet again I won lost double up backed double up backed double up backed double up backed double up backed and I won again so as you can see I've been profitable I me technically if you look at it every game that I've played I have won if I lost I just doubled up bet again doubled up bet Again by the end of it I was always in a profit of 0.1 as long as I have enough liquidity as long as when this amount goes up it goes up very quickly it will reach 512 which is you know very close to how much money I have so I can't keep doing this infinitely but how many times do you think you know less than 50% would come in a row it probably won't come more than 10 times is what you would think probability says it it can but you would think of course if I keep betting again and again not more than 10 times will it come so I just need to make sure if I have to bet 10 times in those 10 times also $118 is enough the maximum amount amount of money that I have to bet is let's say $100 so what should be my amount that I should be dealing with probably 0.1 because if I double it up once twice third three times four times five times six times seven times eight times nine times 10 times it reaches $102 so as long as I can be sure less than 50 will not come 10 times in a row this strategy will work for a $100 and with a starting amount of 01 but if it does not come if if less than 50 comes 10 times in a row then of course I'm screwed I lose $100 cool let's implement this strategy programmatically I can do this by hand but as you can see it feels like we're doing great it's just very tiring for me to do this again and again so I want to write some code that can U automatically do it that's going to be the next part of this video all right let's see how we can automate this process and run this algorithm you know via code and keep it running overnight and we'll see tomorrow how much profit we make a lot of times games exchanges you know if you're trading they provide you apis so you can do this programmatically I don't think stake does that and that is why we will have to inspect some of their requests look at exactly what is the request that goes out when you're trying to speculate or bet and then based on that we'll be able to you know convert this into a program let's try to bet 0.1 and when I do as you can see a request goes out to a graphql server now what is graphql don't worry too much about it's just another way for you to interact to your back end and it probably makes sense for a website like stake that's receiving a lot of requests uh to use or you know to move to something like graphql overest if you look at the payload that we sent this is how graphql queries look like if you don't understand it high level it's very similar to rest calls you send some data over to the back end and then the back end responds with you know some response in this case the data that we're sending specifically the you know thing that we're the variables that we're worried about are the amount that the user is betting U the condition whether or not the user will make money when it's above 50 or below 50 um the currency that they're currently playing with the target at which you know um they want to make or lose money which in our case is 50.5 I don't know what this identifier is hopefully it Remains the Same let's try this out one more time let's see if it Remains the Same across requests and the answer is it does not so this might come to bite us I don't know what this identify is if it is automatically generated or not um but either way let's see you know if we just send requests with the same identifier does it work or not how can you take a request from your browser and run it via code there are a few ways the easiest way is just right click click on copy and copy it as curl what does copying as curl do curl is a standard command line interface or not a CLI it's like a bash command that you can run that lets you send HTTP requests har too much jargon yes a FLH command basically means something like this if you type curl space the URL that you're sending the request to a bunch of headers which you know have my authentication information I will log out before this video goes onto YouTube but if I wouldn't have logged out then you could have just sent this request from your terminal and you know you would have been able to trade on my account thankfully I will log out before I release the video or we'll just blur out the cookies that I have over here given this request if I press enter let's see what the server responds with and the server responds with result 12.25 Target 50.5 this is the response that the server gave me which pretty much tells me can most probably lost this bet I was able to programmatically send out a request I was able to programmatically play the game and the server is sped with whether I won or lost in this case since the result is 12.25 I lost let's try that one more time in this case the result is 99.48 that probably means I won if I go over here and you know look at my wallet balance it's 118.7 n right now let me send the request one more time and it goes down to 69 which means I lost and as you can see the result was 48.8 which means I lost if I do that one more time result was 80 and the amount of money that I have goes up to 118.7 n now now that we have the request as curl what we have to do is convert it you know into some language of our choice not Jess golang rust doesn't matter and once we do that we'll just run an infinite Loop that runs our algorithm so first let's convert this request over to some basic nodejs code then we'll build our logic around it how can you do that you can write it from scratch if you want so I mean the one thing you can do is something like this let me initialize an empty note project you can write typescript or JavaScript code doesn't really matter it's like less than probably 50 lines of code here so I'm just going to stick to JavaScript if you want to get fancy feel free to use rust goang typescript doesn't really matter let me open this in Visual Studio code let me now start to convert my requests over to a nodejs request um there are many libraries in nodejs for example you know axio is a famous one so let me install axio let me require it at the top of my file conio equal to require AOS and now let me write the logic U to you know just simply send a request for now all right this is what the final code looks like um you import the axios library which we'll be using to send requests this is optional you can also send requests in nodejs using the fetch API you don't need this um I separate the cookie out since it's a very long string just to keep the code below this cleen I have it in a separate variable and then I Define an async function called main that sends a request using axus it's a post request this is the URL where the request goes to these are the headers that we sent and this is the data that we sent to the back end if you look at this this is exactly the same as the curl request that we were playing with the while back this is the URL these were all the headers and this was the final data that we were sending to the back end the same thing I've just converted to nodejs code in the end I log response.data do data basically response. data gives you everything that the backend returned and in our case the backend returns us a key called Data if I go to the curl request and show you after I send the curl request I get back the data or I get back the payload from the back end which itself has the data field which is why I've done a response. data do data response. data gives you whatever the backend returned you response. data. data is only because the response from the backend itself has the data key if I run node index.js you will see the request is going out I'm getting some payload from the back end in this case the result is 75.1 3 so we're good to go which basically means I made money if I run the same request again 63 is still above 50 so I will still make money if I run it again 5019 is below 50.5 so I lost 0.1 cents or you know whatever I was betting which in this case is 0.1 now let's see how can we convert this into our strategy let's convert this main function to you know maybe something like send request which takes the amount as the input and let's take this amount and replace 0.1 to be that specific amount how can you do that there are various ways this is the easiest one you use a dynamic variable like this dollar brackets amount where you know this will get replaced B with the actual value of amount if this feels very jarring you can also do something like this plus amount plus this is also fine the earlier syntax that I showed you is just slightly cleaner syntax to implement the same thing what am I doing the data that I sent to the back end I'm making this amount configurable so that I can change the amount based on if a request of mine fails I was also just calling the main function or you know the send request function now I need to call the send request function based on the response that I get from the back end so let me Define a main function that I will call and inside this main function I will have an infinite Loop which does what it will send a request let amount equal to 0.1 that is the amount that we will start with and it will send a request with the current amount and wait for the response if and let's say the response is what we get back from here so response. data. data is what this function returns if response do um dice what was the output that we got let's look at the curl request if response dot result no do state. result so response do state. result is greater than how did it know 50 how is AI so good 50.5 then oh there you go it know knows what I'm trying to do into 2 lse amount equal to 0.1 default underscore amount equal to 0.1 that should most probably do it um yeah I think that code looks right let me also put a bunch of logs um and also maybe sleep between iterations so while a wait there we go I've defined a function called main where I initialize amount to be 0.1 or this constant that I've defined over here and infinitely run a loop where I'm first betting that amount and based on the response from the server if the response says that I won then I should be something like this if the response says I have lost is when I just double the amount and then you know in the next iteration I will be betting twice the amount if I do win then I just reset the amount back to default amount and the circle sort of continues let's see if this works if this is doing the same thing I was doing a while back in the UI is it dice roll. state. result let's see betting 0.1 one betting 0.2 lost okay that looks long I think the logs here are incorrect this is lost and this is one let's try that one more time I bet 0.1 I lost and so I bet 0.2 and I won bet 0.1 I lost bet 0.2 I won lost one I mean basically looks fine it's not going Beyond 0.2 I'm basically winning a lot now it reached 0.4 but again I won so I do lose I reach 0.4 in this case I reach 0.8 1.6 3.2 and M1 so as you can see it's get beginning to get scary already um I was able to you know reach 3.2 if I would have lost a few more times I might have you know run out of all the money I have so even though it feels as profitable it isn't we'll go through the math very soon but let me just run this for a while and let's see how our balance changes like what is our current um balance if I go to Brave let me take a screenshot of this so I don't forget it's 12.59 let me run this for a while let me also decrease the base amount by a bit um so you know even if we lose just Trying to minimize us losing because I want this to run for a day or so uh this will fail in the long run but I want this to at least work for let's say you know the next few hours so I can show you know this is actually a decent Prof strategy in the short term but not in the long run okay I'm going to read this for now and I will see you guys in a few hours e all right uh I've been running it for 30 minutes now um and as you can see the amount has gone up to $130 uh if you remember the original amount let me quickly open the screenshot that I took a while back the original amount that we kick thing kicked things off with was $120 it's gone up to $130 in a span of half an hour so it might feel like this is you know infinite money printing machine um and it might be if you're a lucky person but if I leave this running for a day what do you think happens just take a guess here I'm going to do that very soon I'm going to turn this logic a little bit U to you know basically exit if there are 15 losses in a row U let me quickly write that logic I want this server to just stop running or you know just the process to exit if there are 15 losses in a row so that if that does happen uh tomorrow I'm able to track that exact event so how do you do that let uh losses in a row equal to zero and then anytime there's a loss I just do a losses in a row Plus+ and I check over here if losses in a row is greater than equal to 15 if I've lost 15 times in a row then of course you want to just exit the process right there um and if you ever win then losses in a row goes down to zero something like this uh I think that looks right hopefully that is right initialize it to zero anytime you lose you increase this number anytime you win you make this number uh take it back down to zero and if you have more than 15 losses in a row you exit right there and there I'm now going to push this onto a server of mine um and let this run the whole night and then we'll go from there we'll see um by tomorrow which basically means in like 12 hours or so let's say this is either going up to $300 $250 or this is going down to zero there's no in the middle um it might not go down to zero it might go down to like $30 or something like that uh because we're starting off from 0.01 we're doubling up if I double this up 15 times what is 0.01 into 2 ra to the power 15 that's $327 I don't have $327 so I need to decrease the number of losses in a row to be around 12 I would think uh greater than equal to 12 because that's $40 um yeah so in the worst case I lose $40 I can actually make this 13 as well yeah that looks right um in the worst case I lose $80 so by tomorrow you're either going to see this balance go down to $50 something like that or it'll go up to $200 maybe $300 if I'm being honest if I keep this running I ran this for half an hour it went up by $10 if I keep it running for 12 hours it'll probably go up to $250 let's see which one of the two happens let me give you a spoiler it all depends on my luck um it is equivalent to me just betting $130 in a single go that is the probability of me either doubling up tomorrow or going down to zero um I could just take this $130 and bet it all at once the same is the probability of you this algorithm running the whole night and me not hitting the case of you know 15 losses in a or 30 losses in a row but we'll see that tomorrow we'll understand what the placebo effect over here is and why people think this strategy works but it most probably will not in the long run let me move this to my server all right if I run it on my server error keeps on coming can I run it locally I can so it seems like uh it won't let me run it on my server because my server has a specific IP that it just most probably blocking uh this cookie is most probably Associated to my IP the cookie the cookie that I'm sending over here uh can probably only be sent from the IP that I'm currently on um which is probably why these requests are failing over here um and hence I probably cannot run it on my server which is fine I will just run it on my laptop and you know leave my laptop on for the whole night and tomorrow we will see what happens uh I will see you guys in a few hours and we'll try to number one uh do some basic math as to is this really weighing strategy and number two see how much money we make my speculation is we go up to $200 uh by the time I wake up we will see though have a good night all righty welcome to the next day I've already seen the result what do you think has happened I was running an infinite game um again and again for almost 7 hours I've run it for and in these 7 hours have I been able to hit a case where I lost 12 times the answer is yes if I go back to the stake dashboard over here you will see my amount is down to $95 um which basically means it went up to $150 and then in a single row I lost 12 times I lost around $50 and went down to 95 the game stopped right there that was the strategy that I wrote right if you lose more than 12 times I would just stop the game and that's exactly what happened I unfortunately got rid of the Vlogs here when I was recording or when I woke up in the morning but there were 12 games that got lost together um that is why this strategy isn't profitable um you might think harir the probability of one loss happening or 12 losses happening in a row is so low if you look at it the probability of one loss happening in 13 times in a row is 0.00 1 2% um but 0.12% becomes significant if you're playing thousands of game the problem with this strategy is that you're only increasing your Capital by only a little bit U if your starting amount is 0.1 then you're only making you know 0.1 0.1 you're slowly upping your game in our case we went from $120 to $130 in a span of around 30 minutes that means you'll have to play a lot of games for you to double up for me to go from $100 to $200 I would have had to play maybe 10,000 games and amongst those 10,000 games there is of course some probability um that you know 12 games are losses together um and that will lead to you know you losing money in the long run can the strategy work it works in the short run if you play this for you know half an hour or maybe 10 minutes then there's a very low probability of that event happening um and the problem is okay you will not make a lot of money on $100 you'll make $1 in 15 minutes so it's not worth pursuing it for 1% there is of course still some risk um but the risk is much lower the risk of going down to zero is much lower but the upside is also not huge the upside is just 1% which is why if you have a lot of capital maybe a million dollars might make sense you make 1% that's $10,000 you sleep the next morning you try again over there also if you keep doing this again and again every day at some point you will hit the edge case and in that case you will lose a million dollars so the reason the strategy doesn't work is because when you win you win only a little bit but when you lose you lose all of your Capital that is why even though you're winning most of the times you're not increasing your Capital by a bit and when you lose you lose you know 100% of the money that you have that is why you should not gamble that is why the strategy is you know fairly dumb um and it feels like Placebo it feels like it can never happen that I there are 13 Reds in a row if you're playing blackjack or 13 you know times less than 50 coming but as I said there is always some probability you will hit that probability in the longer run cool what did we learn from this video number one do not gamble if you do gamble know that you're going to lose in the long run maybe play shortterm games and you know cash out as soon as possible um if you really want to you know sort of fool the casino even though you can can never the only way to do that is not playing for long runs betting with a lot of capital 100K and when you bet bet with a lot of capital and you know run these strategies most probably you'll end up making some money and then just cash out don't get addicted to the game don't play it for longer terms the reason casinos work is because people play it for a long time and if you do play it for a long time you will lose every game has probability stacked against you with that let's end it I will see you guys in the next one bye-bye