Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:51:16+00:00 2026-06-18T12:51:16+00:00

I want to implement a classic martingale using Python and Pandas in a betting

  • 0

I want to implement a classic martingale using Python and Pandas in a betting system.

Let’s say that this DataFrame is defined like this

df = pd.DataFrame(np.random.randint(0,2,100)*2-1, columns=['TossResults'])

so it contains toss results (-1=lose 1=win)

I would like to change stake (the amount I bet every bet) using classic martingale.

Initial stake is 1.

If I lose stake will be 2 times previous stake (multiplier=2).

If I win stake will be stake_initial

I did a function

def stake_martingale_classical(stake_previous, result_previous, multiplier, stake_initial):
    if (result_previous==-1): # lose
        stake = stake_previous*multiplier
    elif (result_previous==1):
        stake = stake_initial
    else:
        raise(Exception('Error result_previous must be equal to 1 (win) or -1 (lose)'))
    return(stake)

but I don’t know how to implement it efficiently using Pandas.
I tried this :

initial_stake = 1
df['Stake'] = None
df['Stake'][0] = initial_stake
df['TossResultsPrevious'] = self.df['TossResults'].shift(1) # shifting-lagging
df['StakePrevious'] = self.df['Stake'].shift(1) # shifting-lagging

but now, I need to apply this (multiparameters) function along 0-axis.

I don’t know how to proceed !

I ever saw pandas.DataFrame.applymap function but it seems to be 1 parameter function only.

Maybe I’m wrong and using shift function is not a good idea

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:51:17+00:00Added an answer on June 18, 2026 at 12:51 pm

    One slight interpretation change is you need to mark a loss as a 1 and a win as a 0.

    The first step is to find the edges of the losing runs, (steps + edges). You then need to take the difference of the sizes of the steps and shove those values back into the original data. When you take a cumsum of toss2 it gives you the current length of your losing streak. Your bet is then 2 ** cumsum(toss2).

    The numpy version is faster than the pandas version, but the factor depends on N (~8 for N=100 and ~2 for N > 10000).


    pandas

    Using pandas.Series:

    import pandas as pd
    toss = np.random.randint(0,2,100)
    
    toss = pd.Series(toss)
    
    steps = (toss.cumsum() * toss).diff() # mask out the cumsum where we won [0 1 2 3 0 0 4 5 6 ... ]
    edges = steps < 0 # find where the cumsum steps down -> where we won
    dsteps = steps[edges].diff() # find the length of each losing streak
    dsteps[steps[edges].index[0]] = steps[edges][:1] # fix length of the first run which in now NaN
    toss2 = toss.copy() # get a copy of the toss series
    toss2[edges] = dsteps # insert the length of the losing streaks into the copy of the toss results
    bets = 2 ** (toss2).cumsum() # compute the wagers
    
    res = pd.DataFrame({'toss': toss,
                        'toss2': toss2,
                        'runs': toss2.cumsum(),
                        'next_bet': bets})
    

    numpy

    This is the pure numpy version (my native language is it were). There is a bit of fineagling to get the arrays to line up that pandas does for you

    toss = np.random.randint(0,2,100)
    
    steps = np.diff(np.cumsum(toss) * toss)
    edges = steps < 0
    edges_shift = np.append(False, edges[:-1])
    init_step = steps[edges][0]
    toss2 = np.array(toss)
    toss2[edges_shift] = np.append(init_step, np.diff(steps[edges]))
    bets = 2 ** np.cumsum(toss2)
    
    fmt_dict = {1:'l', 0:'w'}
    for t, b in zip(toss, bets):
        print fmt_dict[t] + '-> {0:d}'.format(b)
    

    pandas output

    In [65]: res
    Out[65]: 
        next_bet  runs  toss  toss2
    0          1     0     0      0
    1          2     1     1      1
    2          4     2     1      1
    3          8     3     1      1
    4         16     4     1      1
    5          1     0     0     -4
    6          1     0     0      0
    7          2     1     1      1
    8          4     2     1      1
    9          1     0     0     -2
    10         1     0     0      0
    11         2     1     1      1
    12         4     2     1      1
    13         1     0     0     -2
    14         1     0     0      0
    15         2     1     1      1
    16         1     0     0     -1
    17         1     0     0      0
    18         2     1     1      1
    19         1     0     0     -1
    20         1     0     0      0
    21         1     0     0      0
    22         2     1     1      1
    23         1     0     0     -1
    24         2     1     1      1
    25         1     0     0     -1
    26         1     0     0      0
    27         1     0     0      0
    28         2     1     1      1
    29         4     2     1      1
    30         1     0     0     -2
    31         2     1     1      1
    32         4     2     1      1
    33         1     0     0     -2
    34         1     0     0      0
    35         1     0     0      0
    36         1     0     0      0
    37         2     1     1      1
    38         4     2     1      1
    39         1     0     0     -2
    40         2     1     1      1
    41         4     2     1      1
    42         8     3     1      1
    43         1     0     0     -3
    44         1     0     0      0
    45         1     0     0      0
    46         1     0     0      0
    47         2     1     1      1
    48         1     0     0     -1
    49         2     1     1      1
    50         1     0     0     -1
    51         1     0     0      0
    52         1     0     0      0
    53         1     0     0      0
    54         1     0     0      0
    55         2     1     1      1
    56         1     0     0     -1
    57         1     0     0      0
    58         1     0     0      0
    59         1     0     0      0
    60         1     0     0      0
    61         2     1     1      1
    62         1     0     0     -1
    63         2     1     1      1
    64         4     2     1      1
    65         8     3     1      1
    66        16     4     1      1
    67        32     5     1      1
    68         1     0     0     -5
    69         2     1     1      1
    70         1     0     0     -1
    71         2     1     1      1
    72         4     2     1      1
    73         1     0     0     -2
    74         2     1     1      1
    75         1     0     0     -1
    76         1     0     0      0
    77         2     1     1      1
    78         4     2     1      1
    79         1     0     0     -2
    80         1     0     0      0
    81         2     1     1      1
    82         1     0     0     -1
    83         1     0     0      0
    84         1     0     0      0
    85         1     0     0      0
    86         2     1     1      1
    87         4     2     1      1
    88         8     3     1      1
    89        16     4     1      1
    90        32     5     1      1
    91        64     6     1      1
    92         1     0     0     -6
    93         1     0     0      0
    94         1     0     0      0
    95         1     0     0      0
    96         2     1     1      1
    97         1     0     0     -1
    98         1     0     0      0
    99         1     0     0      0
    

    numpy output

    (different seed than panadas results)

    (result -> next bet):
    w->  1
    l->  2
    w->  1
    w->  1
    l->  2
    w->  1
    l->  2
    w->  1
    l->  2
    l->  4
    w->  1
    l->  2
    w->  1
    l->  2
    l->  4
    w->  1
    w->  1
    w->  1
    l->  2
    l->  4
    l->  8
    w->  1
    l->  2
    l->  4
    w->  1
    l->  2
    l->  4
    w->  1
    w->  1
    l->  2
    w->  1
    w->  1
    w->  1
    w->  1
    l->  2
    l->  4
    w->  1
    w->  1
    l->  2
    l->  4
    l->  8
    w->  1
    w->  1
    l->  2
    l->  4
    w->  1
    w->  1
    w->  1
    w->  1
    w->  1
    w->  1
    l->  2
    w->  1
    l->  2
    w->  1
    l->  2
    w->  1
    w->  1
    w->  1
    w->  1
    w->  1
    w->  1
    l->  2
    l->  4
    l->  8
    l->  16
    w->  1
    l->  2
    l->  4
    w->  1
    w->  1
    w->  1
    w->  1
    l->  2
    w->  1
    w->  1
    l->  2
    w->  1
    w->  1
    w->  1
    l->  2
    w->  1
    w->  1
    w->  1
    w->  1
    w->  1
    w->  1
    l->  2
    l->  4
    l->  8
    w->  1
    w->  1
    l->  2
    l->  4
    l->  8
    w->  1
    l->  2
    l->  4
    w->  1
    l->  2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a very general question, that not only applies to this example. Let's say
i starter in jqgrid, i want implement inline edit in jqgrid i have this
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement login using facebook in my windows phone 7.1 application When
to prevent CSRF I want to implement the Synchronizer Token Pattern in my classic
everyone! My english is poor and sorry fot that. I want implement a function
I have an ASP.NET MVC 4 Application that I want to implement Unit of
I have WSDL and XSD files and want implement a webservice based on this
I want to start this question by saying that it is paradigm-related and that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.