feat: signal abstraction layer + sizer + HS300 universe + PnL/IC reports
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
"""Signal-driven multi-stock strategy."""
|
||||
import backtrader as bt
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class AlphaStrategy(bt.Strategy):
|
||||
"""Trade each feed based on its precomputed ``signal`` line.
|
||||
|
||||
The strategy delegates the buy/sell/hold decision to a portfolio builder
|
||||
(e.g. ``ThresholdBuilder``) and sizes entries with ``order_target_percent``.
|
||||
"""
|
||||
|
||||
def __init__(self, builder):
|
||||
self.builder = builder
|
||||
|
||||
def next(self):
|
||||
for data in self.datas:
|
||||
sig = data.signal[0]
|
||||
if pd.isna(sig):
|
||||
continue
|
||||
in_position = bool(self.getposition(data).size)
|
||||
action = self.builder.build(float(sig), in_position)
|
||||
if action.action == "buy":
|
||||
self.order_target_percent(data=data, target=action.size_pct)
|
||||
elif action.action == "sell":
|
||||
self.close(data=data)
|
||||
Reference in New Issue
Block a user