First off, thanks for this community - I've only gotten to this point because of the posts on this forum and the great documentation.
What I'm trying to do is execute a strategy like this:
- Scan for the stocks I'm interested in, and
- At 10:00am, choose N stocks which meet my criteria, and place buy order(s), then
- Once the order is filled, place a LIMIT sell order for my target profit.
I've got a rough skeleton as follows:
def main(argv): cerebro = bt.Cerebro() cerebro.optstrategy(MyStrategy, <params go here>) cerebro.addsizer(MySizer) cerebro.addanalyzer(MyScreener) for contract in historical_data.GetContracts(): cerebro.adddata(MyFeed(contract)) cerebro.run()
How would you implement the scanner? There's an old blog post on stock screening, but it doesn't seem to be connected to a strategy - it's simply a screener.
If I had my way, it would look something like:
class MyStrategy(bt.Strategy): @overrides(bt.Strategy) def next(self): for data in self.datas: if not data.name in analyzer: continue # This feed has been selected, so use it. self.order = self.buy(...)
The question is, how should the
Strategy instances talk to one another? Best I can tell, the
Analyzer gets the same
next() calls as the
Strategy instances, and has references to them. So perhaps the Analyzer should write data to the Strategy instances?
So something like:
class MyScreener(bt.Analyzer): @overrides(bt.Analyzer) def next(self): eligible = set() for data in self.datas: if self.Eligible(data): eligible.add(data.name) # Tell the Strategy about the set of eligible feeds. self.strategy.SetEligibleFeeds(eligible)
Does this make sense, or am I thinking about it wrong?