Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Good and Bad Baskets as criteria for long/short positions. Repeat transaction problem

Hi. Here's my latest stab at buying and selling a basket of "good" and "bad" stocks. I will later add bells and whistles, such
as using stop limits, and target sells, as well as varying percentage of shorts to longs based on the status of the S&P. But before I do ,
I'm confronted with the issue that it is repeatedly buying the same position (see CASI) instead of skipping over any stocks that are
already short or long or for which orders are pending. What did I miss?

Thanks

2 responses

Have you tried using order_targets ? That way, you can just point to an absolute position amount and it will automatically be adjusted. Another alternative is to simply check if you have any preexisting position in the stock with something like context.portfolio.positions[symbol('AAPL')] .

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.

Thanks Gus,

I fixed it as follows:
if get_open_orders(stock):
log.info('stock already has pending order: stock: {} looping' .format(stock.symbol))
transactionPnL = 0
if sharesHeld < 0:
transactionPnL = (new_price - cost_basis ) * sharesHeld * -1
elif sharesHeld >0:
transactionPnL = (new_price - cost_basis ) * sharesHeld
log.info('Security: {} Cost basis: {:+.2f} Close price {:+.2f} shares traded: {:+.0f} unrealized P&L:{:+.2f}' .format(stock.symbol, cost_basis, new_price, sharesHeld,transactionPnL ))
continue