Martingale and Anti-Martingale Algorithm Development
Martingale and Anti-Martingale are two opposite position sizing systems based on previous result. Martingale doubles stake after loss. Anti-Martingale after win. Both have mathematical basis and serious practical constraints.
Classic Martingale
Logic: after loss, double next position size. On first win, recover all previous losses plus earn base profit.
Trade 1: $100 → loss -$100
Trade 2: $200 → loss -$200
Trade 3: $400 → loss -$400
Trade 4: $800 → win +$800
Total: -100 -200 -400 + 800 = +$100
Mathematical problem: loss series grows exponentially. After 10 losses: $100 × 2^10 = $102,400. Exceeds deposit or exchange limits.
Implementation in crypto trading:
class MartingaleStrategy:
def __init__(self, base_qty, multiplier=2.0, max_orders=6):
self.base_qty = base_qty
self.multiplier = multiplier
self.max_orders = max_orders
self.current_level = 0
self.total_invested = 0
def get_next_qty(self, last_result):
if last_result == 'loss':
self.current_level = min(self.current_level + 1, self.max_orders)
else:
self.current_level = 0
return self.base_qty * (self.multiplier ** self.current_level)
Limited Martingale: max doublings = 4–6. After limit, fix loss and restart at base size. Turns mathematically dangerous system into manageable.
Anti-Martingale (Reverse Martingale)
Logic: increase size after win, decrease after loss. Allows aggressive use of "lucky streaks" while managing risk.
Application in trend: on series of profitable trades in trend — pyramid in trend direction. Turtle traders logic.
class AntiMartingaleStrategy:
def __init__(self, base_qty, multiplier=1.5, win_streak_limit=4):
self.base_qty = base_qty
self.multiplier = multiplier
self.win_streak = 0
self.win_streak_limit = win_streak_limit
def get_next_qty(self, last_result):
if last_result == 'win':
self.win_streak = min(self.win_streak + 1, self.win_streak_limit)
else:
self.win_streak = 0
return self.base_qty * (self.multiplier ** self.win_streak)
Profit lock: on reaching streak limit N — fix profit and return to base size. Don't give back accumulated.
Application in Crypto Trading
DCA-Martingale bots (popular pattern): on price fall, increase next buy size. Goal: lower average entry. Practically all "3Commas DCA bots" work on this principle.
Key DCA-Martingale bot parameters:
- Base order size: $100
- Safety orders: 6 (max levels)
- Price deviation: 2% (step down for next buy)
- Safety order multiplier: 1.5× (Anti-Martingale by volume)
- Take profit: 1.5%
Risk/Reward Analysis
| Parameter | Martingale | Anti-Martingale |
|---|---|---|
| Risk on loss series | Exponential | Linear |
| Max loss | Can wipe deposit | Limited to base_qty × N |
| Profit in trend | Low | High |
| Suitable for | Sideways | Trending |
Iron rule of Martingale: never without level limit. Unlimited Martingale is not strategy, it's gambling on credit.
Develop both strategies with configurable parameters, risk limits, position visualization and P&L, backtesting module for historical evaluation.







