Hi Brandon,
We definitely have implementing a realistic margin model on our work queue. In the meantime you do need to do some defensive coding to prevent your algo from over ordering. This is especially true in a case like your shared algo where you are running in Daily backtest mode but looping over a number of stocks. The cash management logic you put in place was assuming that orders were being filled and taken out of the available cash pool as they were being placed -- however the backtester actually fills order on the NEXT bar of the simulation. So you were queuing orders up and just re-checking the static starting cash value from the beginning of the day.
One way to handle that is to add a convenience method that will keep track of how much cash you've allocated out to pending open orders so far. So for example you could do:
pending_order_value = open_order_value(context,data)
##The available cash for placing new orders is the cash on hand MINUS the open orders already placed but not filled.
cash = context.portfolio.cash - pending_order_value
Where the pending_order_value is computed as follows:
def open_order_value(context,data):
#Return the $ pending in open orders
pending_order_value = 0
for stock in context.stocks:
orders = get_open_orders(stock)
if orders:
for oo in orders:
shares = oo.amount
price = data[stock].price
value = shares * price
pending_order_value = pending_order_value + value
return pending_order_value
Let me know if this is helpful and if you come across other comments or feedback!
Best regards,
Jess
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.