The following code places bets on the results of flipping coins. You start at £ 100 and risk 5% on each flip, but because my code calculates the size of the bet based on your starting balance, the bet is always 5.
import pandas
import matplotlib.pyplot as plt
start_bal = 100.0
risk = 0.05
a = pandas.DataFrame()
a['Result'] = [1,1,1,1,-1,-1,1,1,1,-1,-1,1,1,-1,-1,-1,1,1]
a['bet'] = start_bal*risk
a['pnl'] = a.Result * a.bet
a['bal'] = start_bal + a.pnl.cumsum()
plt.plot(a.bal)
What I would like to do is recalculate the size of the bet after each bet depending on your balance while you bet more when your balance increases and less when it decreases. This will mean that "bal" depends on the "bet", which, in turn, depends on the "bal", so I end up in a circular relationship.
Can this be done? Do I need to iterate over data one row at a time, recounting the “score” and “bid” in this particular index?
Thank.