GEMESYS Canada - (Click here to jump back to Site Top Page
Research & Consulting Services in Advanced Technology & Artificial Intelligence

*** Martingale Trading Simulator - First built on Z80 SBC, Using Z80-Basic (screenshots) ***
*** And Reverse-Martingale Simulator - New Code for X11Basic Interpreter on Linux (source text) ***

Menu
  • GEMESYS Research-Log
  • Return to Index Page

Martingale and Reverse-Martingale Simulator Program Details

I built this code, by first asking GROK AI to write a Martingale Simulator in Basic, so I could run it on my Z80, just as an experiment. It's proved so fascinating, that I found a good BASIC interpreter, called X11Basic, and built and installed it from source, on several Linux machines. The Martingale and Reverse-Martingale simulators were brought to my attention, by the blow-up of "Captain Condor" (David Chau), who on Christmas Eve, in 2025, he himself, and along with his followers, were using the "Iron Condor" options strategy, to make leveraged options bets on the SPX, when realized volatility and implied volatility were not in alignment. If their trade lost, they would triple (!) bet size, and do the trade again. I recalled the "Martingale" betting system, where a gambler doubles-down after each loss, is generally regarded as a strategy which eventually destroys gamblers. And this was the case, where the scaled-up "Iron Condor" trade lost roughly $50 million, wiping out a large number of traders who were following "Captain Condor", using this strategy.

It's really worth the time, to play with trading simulators, and try different parameters, and these two programs allow this. You can see surprising results.

The screen-shots are all from my Z80 stuff - but now, as of Feb. 7, 2026, at the page bottom, I have modern X11Basic code, for MARTGLX11.BAS (the Martingale Simulator), and REVMTGLX.BAS (the Reverse-Martingale Simulator), in plain-text. If you install the X11Basic interpreter on a Linux machine, and just copy/paste this program code into a couple of different files, you can load, run and play with these programs, and enter different initial amounts of capital, bet-size, win-probability (the trader's "edge"), and the number of trades to simulate - 1000, 10000, 100000 or whatever. The evolution of the trading (betting) action is very interesting to observe.

[ Martingale Trading Simulation Program, in BASIC (part1 of 2) - Jan. 13, 2026 ]

I asked the Grok.com AI to write a Martingale Trading Simulation program, in BASIC, so I could run it on my homemade Z80 SBC (Single Board Computer), which has a BASIC interpreter. There were some curious logic errors, but for the most part, the program was fully written, and with some debugging and fixes, and some extra display information as the program runs (so I can see how it is trading, and the bets it is making) the program is a fun exercise to experiment with.
This screen shot shows the first part of the program. Below, is the image of rest of program. (Click on image to expand, ESC or click outside image to exit)

[ Martingale Trading Simulation Program, in BASIC (part-2) - Jan. 13, 2026 ]

This screen shot shows the second part of the program. Please excuse the awful resolution, which looked much better on the iPhone image. I will try to find time to clean this image up at some point. (NOTE: Further down, is code for REVERSE Martingale Trading Simulator) (Click on image to expand, ESC or click outside image to exit)

[ Martingale Trading Simulation Program, Run of 100,000 trades - Jan. 13, 2026 ]

This shows image of the Martingale Trading Simulator results, which was run on the Z80 SBC (Single Board Computer). The little program in file MARTINGL.BAS is loaded using a simple shell script, with "loadtoZ80ttyS1 MARTINGL.BAS" which assumes that you have used Kermit to connect to the Z80, and have started the Z80-Basic interpreter. The BASIC commands LIST and RUN are used to list the program, and then run it, respectively. As it is running, you can use "Ctrl-s" and "Ctrl-q" to pause and then continue the running simulation. You can see in this simulation, with starting capital of $100,000, and initial bet size of $100, the Martingale system ran the capital balance up to $5,593,300. But note the drawdown of $409,500! This was the result after 100,000 trades. Note there was a run of 12 losses in a row.
(Click on image to expand, ESC or click outside image to exit)

[ Martingale Trading Simulation Program, Run of 10,000 Trades - Jan. 13, 2026 ]

This shows image shows results of the Martingale Trading Simulator on a second run of 10,000 trades. The same parameters were used, but here we got a run of 13 losses in a row. The trading capital goes from $522,220 down to a $-295,400 balance. The simulated trader lost all his capital, and quite a bit more, because the system required a $409,600 bet be made to recover the string of losses. But it did not work, and generated a loss so large that capital went to negative $295,400. With the use of margin, these kinds of results are actually possible.

This run highlights the risk of the Martingale strategy.
The key thing to realize, is that the probability of win and the probability of loss is the same, each time. A long string of losses in a row, is a very low probability event, but it can occur, and it does occur. If your loss probability is 0.48, then the probability of 12 losses in a row, is 0.48 ** 12, which is a small value of 0.00014959. But in a run of 10,000 trades, that means that you can expect roughly 1.5 events of this type, and so one event of this low likelyhood is actually expected to occur, roughly once in 10,000 times. And in this run, it does, and the trader is wiped out, just like happened to the folks following "Captain Condor".

I think a *lot* of market actively is following strategies that are not dis-similar from this "Martingale" betting strategy. There is more risk in modern markets, than we can see. It only shows up very rarely, but it does show up.
(Click on image to expand, ESC or click outside image to exit)

[ The "loadtoZ80ttyS1" Program which is used to load BASIC program onto the Z80 SBC - Jan. 13, 2026 ]

This little program is a BASH shell script that takes any filename, and loads it line by line, to the Z80 (or any serial device attached to the Linux P/C serial port, and has a slight pause, sufficient to allow the Z80-Basic to save and register each line of the program. This little script works really well, and with it, and the Kermit "log session FILENAME.BAS" feature of Kermit (which lets you just capture the contents of a Kermit session, and write it to a file), one can load program to the Z80, and save them from the Z80, into Linux files on any Linux machine.

Kermit can be built on any Linux, if one has a C-compiler (I used gcc 4.8.5 on an old Linux box to build and install Kermit). To save the Basic program from Z80 to a Linux file, one escapes from the Z80 Kermit session with "ctrl-/" and "c", to "C-Kermit:", enters "log session FILENAME.BAS", returns to Kermit with "connect", enter "LIST" to list the whole program, then "ctrl-/" and "c" again to return to "C-Kermit:" prompt, and enter "close session".

The Basic program will be in file called "FILENAME.BAS". This little shell script shown here, can be run from a shell sub-session, from the "C-Kermit:" prompt as follows: Make the link to the Z80 with: the three commands at "C-Kermit:" prompt: "set line /dev/ttyS1", "set baud 38400", "set carrier-watch off" and then "connect". This connects to the Z80 serial port (you have to use a null-modem cable, which switches the RS-232 transmit and receive lines).

You boot Z80-Basic with "B", and then return to "C-Kermit:" prompt by entering the escape sequence ("ctrl-/" and "c"), and then enter "push" to get to a shell sub-session, and enter "./loadtoZ80ttyS1 MARTINGL.BAS" to load the Martingale Trading Simulator.

When it's done (you see each program line load, in the Linux command line shell), enter "exit" to close the BASH shell, and return to "C-Kermit:", where you enter "connect" to return to the Z80, and you will see your program. You run it with the Basic "RUN" command, and enter the trading simulation parameters, for capital, bet-size, number-of-trades. Note that you also must enter a random number to seed the pseudo-random-number generator. (Click on image to expand, ESC or click outside image to exit)

[ Randomness Generator Check Resulting Histogram - Jan. 15, 2026 ]

I wrote this program to check the quality of the pseudo-random values that the RND(x) function generates. The RND function is "seeded" with an initial value, each time the program is run, so that a different evolutionary path of results is generated. It is very difficult to get randomness on a computer, since anything you do, will tend to be deterministic. You have to have some confidence in your pseudo-random results being generated.

To check the RND function, I wrote a program that can generate arbitrarily sized boxes, filled with dots and spaces, and then count the number of dots for each trial, and produce a scaled average between 0 and 1.

This nice histogram shows all is pretty good. I had the program generate 1000 boxes of 5 rows by 80 columns, and "randomly" wrote either a dot or a space in each cell. Then, for each box, I range-scaled the result onto a 130 column by 80 row histogram display. One should get a nice bell-curve (a "Gaussian" curve), with a mean value around 0.50. And this is pretty much what we get, so it means I can trust the quality of the pseudo-random numbers, as being close enough to random, for any simulator which needs random outcomes, to work correctly.

(Click on image to expand, ESC or click outside image to exit)

[ The Martingale Simulator Making 5 Million Dollars - Jan. 15, 2026 ]

Here is screen-shot of final run result of Martingale Simulator making over 5 million dollars, starting with 100,000 dollars, and an initial bet-size of $100, and a win probability of 0.52 (a very small edge), and 100,000 trades.

Note the there is a drawdown of over 3 million dollars, and a run of 15 losses in a row. Could any human make (and lose) a 7 figure bet, and withstand the 15 losses in a row?

Probably not.

There are reasons why this strategy is difficult to put into practice, despite the successful runs it can have. (Click on image to expand, ESC or click outside image to exit)

[ The Martingale Simulator Making 72 Million Dollars - Jan. 15, 2026 ]

Here is screen-shot of final run result of Martingale Simulator making over 72 million dollars (!), starting with 100,000 dollars, and an initial bet-size of $1000, and a win probability of 0.72 (a very *BIG* edge), and 100,000 trades.

Note the there is now only a drawdown of over 127,000 dollars, and a run of only 7 losses in a row.

If you really have an edge, you can maybe use the Martingale approach, despite it's risks. The key here, is that the 72% probability of win, means the probability of loss is reduced to 28%. If you take 0.28 ** (to the exponent of) 10, you get 0.00000295. And 0.00000295 times 100,000 trails, means you can expect to get only 0.29619 events of 10-losses in a row, which is less than once per 100,000 trades.

Raising the win-probability (and reducing the loss probability), makes a big difference, because you can see the simulator's biggest run of losses was only 7, not 15 like the previous 100,000 trades run. So, with the bigger edge, we can dial up the bet-size, and really goose the process.

A lot of algorithmic-trading market activity now, is probably being driven by this sort of push-it-to-the-limit kind of betting strategy. And what one finds, is that you can flip the idea of the Martingale betting strategy around, and instead of increasing bet size when the process is losing money (to recover loses), you can instead increase the bet size when the algorithmic process has made a win.

The old smart traders did this. When they were right, and on a roll, they *increased* their bet sizes, and added to their trades. When the results went against them, and they were proven by the market to be wrong, they would reduce their bet sizes after closing their losing positions.

I want to simulate this, because I think it is a better strategy to use, in the real world, with real data, where unlimited variance seems to be the case. We can find our "edge" suddenly disappears, right when we need it most!

(Click on image to expand, ESC or click outside image to exit)

[ REVERSE Martingale Trading Simulation Program, in BASIC (part1 of 2) - Jan. 26, 2026 ]

This is part 1 of 2 of the code for the little BASIC program that simulates the REVERSE Martingale betting process, where the simulated trader INCREASES his bet size (doubles it), when he makes a winning bet. Unlike a classical double-down-on-a-loss Martingale, this time we double up bet size after a WIN, so that any string of WINNING bets will cause a profitable run. Note that we have to have a BET-Limit value, which I typically set at $100,000.
If you run a simulation with $100,000 dollars initial capital, a starting bet-size of $300, a Win-Probability of 0.55 (a small edge), and a BET-Limit of $100,000, and a run of 1000 trades (or bets), the simulation will sometimes do quite well, despite most of the time, just grinding along and losing a little bit as the simulation progresses. It's really interesting to see the process evolve.
This screen shot shows the first part of the program. Below, is the image of rest of program. (Click on image to expand, ESC or click outside image to exit)

[REVERSE Martingale Trading Simulation Program, in BASIC (part-2) - Jan. 26, 2026 ]

This screen shot shows the second part of the program. (Click on image to expand, ESC or click outside image to exit)


Update Using X11Basic Interpreter on Linux to run Simulator Programs


Ok, here an update. Look a the Research Log for Feb 7, 2026, and see how I got the X11Basic interpreter running on the Linux boxes. It's a pretty good piece of software.

Here is much improved code, which can run in the X11Basic interpreter. There are two programs, the Martingale Simulator (that will make money, but often (always?) blows up and bankrupts the simulated trader(gambler).

The second program, is the Reverse Martingale program, where you scale up on wins, not on losses. This is what rational traders do in the real world, so they don't get carried out in a basket, having lost more than all they have. (This Happens, really.)

It is the magic one, which if run with the right parameters, can make a lot of money, without almost always bankrupting the simulated trader. Play with it, and learn. Yes, the effective distribution of possible results is pretty close to gausian, but it is the sequence of runs, where the extreme conditions reside. In a Reverse Martingale, the unlikely sequence of wins is used to create the extreme gains for the trader. Most of the trading activity just grinds down the simulated trader's funds - again, much like the way the real world operates.

Here are the two X11Basic programs as plain-text (I found the html PRE operative...). They can just be copy/pasted into text file, and loaded and run with the X11Basic interpreter. See the Feb. 7th, 2026 Research Log entry for details on how to build and install the X11Basic interpreter from source, on any modern 64-bit Linux or even an old 32-bit Linux, if you have an old machine or laptop with an installed Linux distro.

 MARTGLX11.BAS
REM Martingale Simulator for X11BASIC  -- Note: This one WORKS, X11Basic uses labels, not line numbers.
REM ---------------------------------     Also, IF   ENDIF, with  and ENDIF on 
REM                                       seperate lines.  There is no "THEN" subcommand.

L05:
 PRINT " " 
 PRINT "Martingale Trading Simulation" 
 PRINT "============================="
 PRINT " "
 REM ======================================= 
 REM     Simple Martingale Trading Simulation 
 REM ======================================= 
 INPUT "Enter number to seed RND function: ";X 
 N = RND(X)
 PRINT " " 
 PRINT "MARTINGALE TRADING SIMULATION" 
 PRINT "-----------------------------" 
 PRINT " "
 INPUT "Initial Capital ($): "; CAP
 REM --- INPUT  CAP 
 PRINT "Starting bet size ($): "
 INPUT  BET 
L85: 
 PRINT "Win probability (0.5 to 0.7 typically): "
 INPUT PWIN 
 PRINT "How many trades to simulate: "
 INPUT TMXTRDS 
 PRINT "Max Trades: ", TMXTRDS 
 IF PWIN <= 0 OR PWIN >= 1
         GOTO L85
       ENDIF 
 PRINT " " 
 PRINT "Starting simulation..." 
 PRINT "Initial balance: ", CAP 
 PRINT " "
 LET BAL = CAP 
 LET CURRBET = BET 
 LET MXDRAWDN = 0 
 LET PEAK = CAP 
 LET WINS = 0 
 LET LOSSES = 0 
 LET LUNGESTLOS = 0 
 LET NCURRLOS = 0 
 LET NLOS = 0 
 LET NWIN = 0 
 REM ------- 
 FOR TRADE = 1 TO TMXTRDS 
 REM Generate random outcome (0=loss, 1=win) 
 LET R = RND(X) 
 LET X = X + 1 
 IF R <= PWIN 
     GOTO L320 
    ENDIF 
REM === LOSS === 
 LET BAL = BAL - CURRBET 
 PRINT " Trade Number: ", TRADE
 PRINT "    Loss ... and Balance: ", BAL 
 REM --- OUT 10,99 : OUT 16,99 
 LET NCURRLOS = NCURRLOS + 1 
 LET NLOS = NLOS + 1 
 GOTO L370 
L320: 
REM === WIN === 
 LET BAL = BAL + CURRBET 
 PRINT " Trade Number: ", TRADE
 PRINT "    Win .... and Balance: ", BAL 
 REM --- OUT 10,8 : OUT 16,8 
 LET WINS = WINS + 1 
 LET NCURRLOS = 0 
 LET NWIN = NWIN + 1 
 REM --- reset after win 
 LET CURRBET = BET 
L370:
 REM Track statistics 
 IF BAL > PEAK 
    PEAK = BAL 
   ENDIF 
LET DRAWDOWN = PEAK - BAL 
 IF DRAWDOWN > MXDRAWDN 
    MXDRAWDN = DRAWDOWN 
   ENDIF
 IF NCURRLOS > LUNGESTLOS 
    LUNGESTLOS = NCURRLOS 
   ENDIF
 REM Next bet size (classic Martingale) 
 IF NCURRLOS=0 
     CURRBET=BET 
   ENDIF 
 IF NCURRLOS>0 
     CURRBET=CURRBET*2 
   ENDIF
 PRINT "Current Bet: "; CURRBET 
 REM Emergency stop - bankrupt 
 IF BAL <= 0 
    GOTO L600 
   ENDIF 
NEXT TRADE 
 REM --------- 
L500:
 REM ==================== RESULTS ==================== 
 PRINT 
 PRINT "=== SIMULATION FINISHED ===" 
 PRINT "Trades completed: "; TRADE-1 
 PRINT 
 PRINT  "Final balance ........: $ ", BAL 
 PRINT  "Net profit/loss ......: $ ", BAL-CAP 
 PRINT  "Max drawdown .........: $ ", MXDRAWDN 
 PRINT  "Peak balance .........: $ ", PEAK 
 PRINT  "Return ...............: % ", (BAL-CAP)/CAP*100 
 PRINT  "Number of Losses ....:  ", NLOS 
 PRINT  "Number of Wins ......:  ", NWIN 
 PRINT  "Starting Bet .... "; BET 
 PRINT  "Win Probability   ";PWIN 
 PRINT 
 PRINT "Wins / Losses .........: ", WINS, "/", (TRADE-1)-WINS 
 PRINT "Longest losing streak .: ", LUNGESTLOS 
 GOTO L700 
L600: 
 REM Bankrupt 
 PRINT 
 PRINT "!!! BANKRUPT !!!" 
 PRINT "Stopped at trade #: ", TRADE 
 PRINT "Final balance: ", BAL 
 PRINT "Longest losing streak was: ", LUNGESTLOS 
 PRINT " "
 GOTO L500
L700: 
  PRINT 
 INPUT "Run again (Y/N): ", AGAIN$
 IF AGAIN$="Y" OR AGAIN$="y"
     GOTO L05 
   ENDIF
 END 

And now, the one that really is worth playing with, and learning about, because it uses the extreme conditions (unlikely runs of same result in a row), to benefit, not destroy, the simulated trader...

 REVMTGLX.BAS

REM --- OUT 10,56 : OUT 16,94

L05:
   PRINT " "
   PRINT "REVERSE Martingale Simulation - Increase Bet if WIN" 
   PRINT "==================================================="
   PRINT " "
   REM  REVERSE Martingale Trading Simulation - Increase Bet if Win" 
 REM =============================================================
 INPUT "Enter number to seed RND function (nn.nnnn): ";X
 N = RND(X) 
 PRINT " "
 PRINT "Reverse Martingale Simulation" 
 PRINT "-----------------------------" 
 INPUT "Initial capital ($): "; CAP 
 INPUT "Starting bet size ($): "; BET 
L90:
   INPUT "Win probability (0.4-0.6 typical): "; PWIN 
 INPUT "How many trades to simulate: "; TMXTRDS 
 INPUT "Enter BETLIMIT to trigger Bet size reset: "; BLIMIT
 PRINT "Max Trades: "; TMXTRDS 
    IF PWIN <= 0 OR PWIN >= 1 
       GOTO L90 
    ENDIF
 PRINT " " 
 PRINT "Starting simulation..." 
 PRINT "Initial balance: "; CAP 
 PRINT 
 BAL = CAP 
 CURRBET = BET 
 MXDRAWDN = 0 
 PEAK = CAP 
 WINS = 0 
 LOSSES = 0 
 LUNGESTLOS = 0 
 NCURRLOS = 0 
 NLOS = 0 
 NWIN = 0 
 REM ------- 
 FOR TRADE = 1 TO TMXTRDS 
 REM Generate random outcome (0=loss, 1=win) 
 R = RND(X) 
 X = X + 1 
 IF R <= PWIN 
       GOTO L320 
    ENDIF
 REM === LOSS === 
 BAL = BAL - CURRBET 
 PRINT "Loss ... and Balance: ", BAL, "  Trade Number: ", TRADE 
 REM --- display loss: OUT 10,99 : OUT 16,99 
 NCURRLOS = NCURRLOS + 1 
 NLOS = NLOS + 1 
 REM --- reset betsize after loss (rev. mtngl) 
 CURRBET = BET 
 GOTO L370 
L320:
    REM === WIN === 
 BAL = BAL + CURRBET 
 PRINT "Win .... and Balance: ", BAL, "  Trade Number: ", TRADE 
 REM --- display win: OUT 10,8 : OUT 16,8 
 WINS = WINS + 1 
 NCURRLOS = 0 
 NWIN = NWIN + 1 
 REM --- Do not reset after win, reset after loss (this is Rev. Mrtni)
 REM CURRBET = BET 
L370:
     REM Track statistics 
 IF BAL > PEAK 
     PEAK = BAL 
    ENDIF 
DRAWDOWN = PEAK - BAL 
 IF DRAWDOWN > MXDRAWDN 
     MXDRAWDN = DRAWDOWN
    ENDIF 
 IF NCURRLOS > LUNGESTLOS 
     LUNGESTLOS = NCURRLOS 
    ENDIF
 REM Next bet size (REVERSE Martingale) 
 IF NCURRLOS=0 
     CURRBET=CURRBET*2 
    ENDIF
 IF NCURLOSS>0 
     CURRBET=BET 
    ENDIF
 REM --- Once BETSIZE gets bigger than BETLIMIT, reset it to BET orig
 IF CURRBET > BLIMIT 
     CURRBET=BET
    ENDIF
 PRINT "Current Bet: "; CURRBET 
 REM Emergency stop - bankrupt 
 IF BAL <= 0 
      GOTO L600
    ENDIF 
 NEXT TRADE 
 REM --------- 
L500:
 REM ==================== RESULTS ==================== 
 PRINT 
 PRINT "=== SIMULATION FINISHED ===" 
 PRINT "Trades completed: "; TRADE-1 
 PRINT 
 PRINT  "Final balance ........: $ "; BAL 
 PRINT  "Net profit/loss ......: $ "; BAL-CAP 
 PRINT  "Max drawdown .........: $ "; MXDRAWDN 
 PRINT  "Peak balance .........: $ "; PEAK 
 PRINT  "Return ...............: % "; (BAL-CAP)/CAP*100 
 PRINT  "Number of Losses ....:  "; NLOS 
 PRINT  "Number of Wins ......:  "; NWIN 
 PRINT  "Starting Bet .... "; BET 
 PRINT  "Win Probability   ";PWIN 
 PRINT  "Bet Reset Limit .....: "; BLIMIT
 PRINT 
 PRINT "Wins / Losses .........: "; WINS; " / "; (TRADE-1)-WINS 
 PRINT "Longest losing streak .: "; LUNGESTLOS 
 GOTO L700 
L600:
    REM Bankrupt 
 PRINT " "
 PRINT "!!! BANKRUPT !!!" 
 PRINT "Stopped at trade #"; TRADE 
 PRINT "Final balance: "; BAL 
 PRINT "Longest losing streak was: "; LUNGESTLOS 
 PRINT " "
 GOTO L500
L700:
    PRINT " "
 INPUT "Run again (Y/N): "; AGAIN$ 
 IF AGAIN$="Y" OR AGAIN$="y" 
      GOTO L05
    ENDIF 
 END 
 

So, these two programs can be just copied into text files, and then given names and loaded into the X11Basic interpreter, and run with various parameters supplied by the user. They are really interesting and educational. Try using a starting capital of 100000, a starting bet size of 100, a slight edge (a 0.52 win probability), and either 1000 or 10000 or 100000 trades, and note how the trading action unfolds.

The simulation is driven by the results of biased randomness (a win is when the RND function returns less than or equal to the win probability number). You can check that the biased-random results of wins and losses is very close (usually) to the probability expected by the win probability value.

The magic in the "Reverse Martingale" simulation, comes partly from the value entered for the "Bet Limit", where scaled-up betting is stopped. Try a value of 50000 or even 125000, where the scale-up-bet-size on win, is stopped, and reset.

Goto Gemesys Research Log
Goto Gemesys Research Main Page