harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-2252harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-2252PSEUDOCODE TASK DESCRIPTION
BEGIN TASK: Build a minimal, self-contained backtesting framework in a new Python package named "backtest".
DEFINE PriceSeries AS List of PricePoint
DEFINE Trade FIELDS: entry_index: int exit_index: int entry_price: float exit_price: float quantity: int profit_loss: float is_open: bool
METHOD is_closed() RETURNS bool
RETURN NOT is_open
DEFINE EquityTracking FUNCTION compute_equity(cash: float, open_qty: int, current_close: float) RETURNS float RETURN cash + (open_qty * current_close)
END DATA MODEL
DEFINE CLASS MovingAverageCrossoverStrategy INHERITS Strategy PARAMETERS: short_window: int, long_window: int STATE: short_window, long_window history of closes for SMA computation
METHOD initialize(price_series: PriceSeries) RETURNS None
// Prepare moving average state from price_series
METHOD next_signal(index: int, price_series: PriceSeries) RETURNS Optional[String]
// Compute short_ma = average of last short_window closes
// Compute long_ma = average of last long_window closes
// If short_ma crosses above long_ma -> return "buy"
// If short_ma crosses below long_ma -> return "sell"
// Else -> return None
END STRATEGY INTERFACE
METHOD init(price_series: PriceSeries, strategy: Strategy, initial_cash: float = 10000.0) RETURNS None ASSIGN fields accordingly
METHOD run() RETURNS Dict INITIALIZE: trades: List[Trade] = [] equity_curve: List[float] = [] cash: float = initial_cash open_trade: Trade or None = None current_index: int = 0
IF price_series is empty
equity_curve.APPEND(initial_cash)
final_equity := initial_cash
RETURN {"trades": trades, "equity_curve": equity_curve, "final_equity": final_equity}
FOR index FROM 0 TO length(price_series) - 1
point := price_series[index]
signal := strategy.next_signal(index, price_series)
// BUY condition
IF signal == "buy" AND open_trade IS None
price := point.close
quantity := FLOOR(cash / price)
IF quantity > 0
cash := cash - (quantity * price)
open_trade := Trade(
entry_index = index,
exit_index = -1,
entry_price = price,
exit_price = 0.0,
quantity = quantity,
profit_loss = 0.0,
is_open = True
)
ENDIF
ENDIF
// SELL condition
IF signal == "sell" AND open_trade IS NOT None
price := point.close
exit_quantity := open_trade.quantity
cash := cash + (exit_quantity * price)
profit := (price - open_trade.entry_price) * exit_quantity
APPEND Trade(
entry_index = open_trade.entry_index,
exit_index = index,
entry_price = open_trade.entry_price,
exit_price = price,
quantity = exit_quantity,
profit_loss = profit,
is_open = False
) TO trades
open_trade := None
ENDIF
// Equity calculation after this step
current_equity := compute_equity(cash, (open_trade.quantity IF open_trade IS NOT None ELSE 0), point.close)
APPEND current_equity TO equity_curve
END FOR
// End of data: close any open position at last price
IF open_trade IS NOT None
last_price := price_series[-1].close
exit_quantity := open_trade.quantity
cash := cash + (exit_quantity * last_price)
profit := (last_price - open_trade.entry_price) * exit_quantity
APPEND Trade(
entry_index = open_trade.entry_index,
exit_index = length(price_series) - 1,
entry_price = open_trade.entry_price,
exit_price = last_price,
quantity = exit_quantity,
profit_loss = profit,
is_open = False
) TO trades
open_trade := None
final_equity := cash
ELSE
final_equity := LAST_ELEMENT_OF equity_curve
ENDIF
RETURN {"trades": trades, "equity_curve": equity_curve, "final_equity": final_equity}
EDGE CASES
REQUIREMENTS - All code uses only Python standard library - Docstrings and type hints included - No file I/O in core logic - Importable without errors in fresh environment
USAGE EXAMPLE (in module docstring or comment) price_series = [ {'date': '2020-01-01', 'close': 100.0}, ... ] strat = MovingAverageCrossoverStrategy(short_window=5, long_window=20) bt = Backtester(price_series, strat, initial_cash=10000) results = bt.run() results['final_equity']
END TASK