"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
"""Strategy SignalEngine template for Strategy Development Manager.
Generated by sdm_register for strategy: {STRATEGY_NAME}
Source: {SOURCE_PAPER}
Signal definition: {SIGNAL_DEFINITION}
Entry rules: {ENTRY_RULES}
Exit rules: {EXIT_RULES}
Position sizing: {POSITION_SIZING}
This template satisfies the backtest runner SignalEngine contract.
"""
from __future__ import annotations
import pandas as pd
class SignalEngine:
"""Rule-based strategy signal engine.
Implements entry/exit rules with position sizing from the source paper.
"""
def __init__(
self,
entry_threshold: float = 0.3,
exit_threshold: float = -0.1,
max_position: float = 1.0,
stop_loss: float = -0.05,
lookback: int = 20,
) -> None:
self.entry_threshold = entry_threshold
self.exit_threshold = exit_threshold
self.max_position = max_position
self.stop_loss = stop_loss
self.lookback = lookback
def generate(self, data_map: dict[str, pd.DataFrame]) -> dict[str, pd.Series]:
"""Generate strategy position signals.
Args:
data_map: symbol -> OHLCV DataFrame.
Returns:
symbol -> pd.Series of target positions (float, clipped to [-1, 1]).
"""
signals: dict[str, pd.Series] = {}
for symbol, df in data_map.items():
position = pd.Series(0.0, index=df.index, dtype=float)
# TODO: Replace with actual strategy logic from paper
# Example: Mean-reversion strategy (Avramov & Chordia 2006)
# Compute signal indicator
returns = df["close"].pct_change(self.lookback, fill_method=None)
volatility = (
df["close"].pct_change(fill_method=None).rolling(self.lookback).std()
)
# Normalized signal: return / volatility
normalized = returns / volatility.replace(0, float("nan"))
# Entry: when normalized signal crosses below -entry_threshold (buy dip)
# Exit: when normalized signal crosses above exit_threshold (take profit)
in_position = False
entry_price = 0.0
for i, date in enumerate(df.index):
if i < self.lookback:
continue
sig = normalized.loc[date] if date in normalized.index else 0.0
price = df["close"].loc[date]
if not in_position:
# Entry condition
if pd.notna(sig) and sig < -self.entry_threshold:
in_position = True
entry_price = price
position.loc[date] = self.max_position
else:
# Exit conditions
pnl = (price - entry_price) / entry_price
if (pd.notna(sig) and sig > self.exit_threshold) or pnl < self.stop_loss:
in_position = False
position.loc[date] = 0.0
else:
position.loc[date] = self.max_position
signals[symbol] = position.clip(-1.0, 1.0)
return signals