Partial Take Profit Algorithm Development
Partial position closing (partial take profit) lets you lock part of profit as targets hit, leaving remaining position for further growth. Compromise between "sell all early" and "hold to end".
Partial Take Profit Logic
Instead of one target — multiple profit lock levels:
Entry: 1 BTC at $40,000
TP1 (25% position): $42,000 → sell 0.25 BTC, profit $500
TP2 (50% remaining): $44,000 → sell 0.375 BTC, profit $1,500
TP3 (remainder): trailing stop from $44k → sell 0.375 BTC on trigger
After TP1 move stop-loss to break-even. After TP2 — trailing stop.
Calculating Take Profit Levels
Fibonacci-based: levels at 127.2%, 161.8%, 261.8% of initial move.
R:R based: if stop $500 (1R), then TP1 = 1R ($500), TP2 = 2R ($1000), TP3 = 3R ($1500).
ATR-based: TP1 = entry + 1.5 × ATR, TP2 = entry + 3 × ATR, TP3 = trailing.
Implementation
class PartialTakeProfit:
def __init__(self, total_qty, take_profit_levels):
"""
take_profit_levels: [(price, pct_of_current), ...]
"""
self.remaining_qty = total_qty
self.levels = take_profit_levels
self.completed_levels = set()
def check_levels(self, current_price):
actions = []
for i, (tp_price, pct) in enumerate(self.levels):
if i not in self.completed_levels and current_price >= tp_price:
sell_qty = self.remaining_qty * pct
self.remaining_qty -= sell_qty
self.completed_levels.add(i)
actions.append({'action': 'partial_close', 'qty': sell_qty})
return actions
Break-even stop after first TP: on TP1 fill, move stop-loss to entry price. Remaining position — "free ride".
Develop partial take profit module with customizable levels, auto stop-loss move to break-even, optional trailing stop for final position part.







