Great questions.
We're still working on getting historical data queried without first tracking data for a year - once that's solved you won't have to worry about your second question. As for an immediate solution, the easiest solution is simply accumulate a data panel day-over-day until it has enough data for you to do your calculations on.
The algorithm was created before schedule_function was released but you could just as easily replace it with schedule_function with an optional offset.
As for the shorts, it wasn't an error as much as that there were A LOT of stocks with scores less than 2. Go figure. The best way to offset this is simply limit the number of shorts that you find, like I do in this code:
def rebalance(context, data, scores):
"""
This method takes in the scores found by get_piotroski_scores and orders our portfolio accordingly
"""
#: Find which stocks we need to long and which ones we need to short
num_long = [stock for stock in scores if scores[stock] >= 7]
num_short = [stock for stock in scores if scores[stock] <= 2][:len(num_long)]
#: Stocks to long
for stock in num_long:
if stock in data:
log.info("Going long on stock %s with score %s" % (stock.symbol, scores[stock]))
order_target_percent(stock, 1.0/len(num_long))
#: Stocks to short
for stock in num_short:
if stock in data:
log.info("Going short on stock %s with score %s" % (stock.symbol, scores[stock]))
order_target_percent(stock, -1.0/len(num_short))
#: Exit any positions we might have
for stock in context.portfolio.positions:
if stock in data and stock not in num_long and stock not in num_short:
log.info("Exiting our positions on %s" % (stock.symbol))
order_target_percent(stock, 0)
record(number_long=len(num_long))
record(number_short=len(num_short))
Disclaimer
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.