INNER CODE UNIT · Python
profit
shashankvemuri/Finance · src/finance/backtesting/engine.py:185
profit = entries[j] * (1 + side * take_profit) if take_profit else None
stop_hit = stop is not None and (low[j] <= stop if side > 0 else high[j] >= stop)
profit_hit = profit is not None and (
high[j] >= profit if side > 0 else low[j] <= profit
)
profit_gap = profit is not None and (
price[j] >= profit if side > 0 else price[j] <= profit
)
if stop_hit or profit_hit:
if profit_gap:
fill, phase = price[j], "take_profit"
elif stop_hit:
fill = min(price[j], stop) if side > 0 else max(price[j], stop)
phase = "stop"
else:
fill, phase = profit, "take_profit"
quantity, prices = np.zeros(len(shares)), price.copy()
quantity[j], prices[j] = -shares[j], fill