I am trying to design a program that buys and sells programs not based on a timed rebalance like most of the Quantopian Strategies. I am trying to buy stocks everyday if they make a new yearly high, and sell them once they hit their ATR stop loss. I currently have to reweigh the portfolio every day as I get new positions. I added a momentum score also, for speed, and volatility to find stocks with lower standard deviations.
E.g.
Day 1:
I have 10 positions
Day 2:
Add 3, lose 2
so the weight would start on day 1 as 10%,
but now I have 11, i.e. (10+3)-2, So I rebalance the portfolio to 1/11 or 9% to keep the equal weight
This gives me the equation.
long_weight=1.0/(new+old-context.ts)
where I loop through context.buy
context.buy=context.longs+context.exist
I know there are probably a lot of better ways to do this. How do I add positions daily, and sell them after an indicator signal?
Thanks,
Eric