I want to implement classic martingale using Python and Pandas in a betting system.
Say this DataFrame is defined as
df = pd.DataFrame(np.random.randint(0,2,100)*2-1, columns=['TossResults'])
therefore it contains a toss of results (-1 = lose 1 = win)
I would like to change the bet (the size of each bet) using the classic martingale.
The initial bid is 1.
If I lose the bet, it will be 2 times higher than the share (factor = 2).
If I win, the bet will be bet_initial
I made 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 use it effectively with 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 function (multi-parameter) along the 0 axis.
I do not know how to act!
I have ever seen the pandas.DataFrame.applymap function, but it seems to be just one parameter function.
Maybe I'm wrong and using shift function is not a good idea
working4coins
source share