Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _extract_trades_from_df(self, df):
- """Extract entry/exit trades from a per-bar signal DataFrame (for vectorized version)."""
- trades = []
- prev = 0
- entry_price = None
- entry_time = None
- for ts, row in df.iterrows():
- sig = row.signal
- sp = row.spread
- if sig == 1 and prev <= 0:
- entry_price = sp
- entry_time = ts
- if sig == 0 and prev == 1 and entry_price is not None:
- trades.append({
- "entry_time": entry_time,
- "exit_time": ts,
- "entry_price": entry_price,
- "exit_price": sp,
- "pnl": (sp - entry_price) * self.trade_size,
- "side": "long"
- })
- if sig == -1 and prev >= 0:
- entry_price = sp
- entry_time = ts
- if sig == 0 and prev == -1 and entry_price is not None:
- trades.append({
- "entry_time": entry_time,
- "exit_time": ts,
- "entry_price": entry_price,
- "exit_price": sp,
- "pnl": (entry_price - sp) * self.trade_size,
- "side": "short"
- })
- prev = sig
- return pd.DataFrame(trades)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement