Is it possible to buy stock by cash and not shares?

My program always over-buys and spends more money than the portfolio contains.
So instead of buying 200 shares when a signal is reached i would prefer to buy a certain amount of
cash worth of stock. Is this possible? I'm talking about "order(context.stock, +200)" and how it orders
shares as opposed to spending cash.

9 responses

Hi Isaac,

Check out the order_target_value method, you can do something like:

order_target_value(context.stock,1000)


to order $1,000 worth of context.stock Alternatively you could use the order_target_percent method to allocate a fixed percent of your total portfolio value, for example: order_target_percent(context.stock,0.10)  Will place an order of context.stock equal to 10% of your total portfolio value. One gotcha with these convenience methods is that they do not check for open orders, so if you are simulating with illiquid stocks using a conservative slippage model you may still hit an over-ordering scenario. We're working on ways to make that easier, but careful backtesting and defensive coding are good best practice for now. Best wishes, 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. Wow thanks! However i noticed that even in minute mode, even if the signal says to "buy" for an hour that code you shared with me only executes the transaction once, i want it to check every minute for a signal and execute a 1000$ order every time the signal is turned on.
Is this possible? Thanks Jess your'e a great help!

Ah ok, if you want to order $1,000 every time (as opposed to ordering the number of shares of$ to reach a 'target') then you're looking for order_value instead.

order_value(context.stock,1000)


will order \$1,000 of context.stock every time it is invoked.

Cool it worked! Thanks! Now a side question: For selling i used "order_target_value(context.stock, 0)" which i know is supposed to sell all shares of a stock, however when i use it, it over-sells and by doing that started shorting shares. Is there an easier method for selling all owned shares in the portfolio? again thank you

I also tried using "current_shares = context.portfolio.positions[context.stock].amount" and then "order(context.stock,-1*current_shares)" to sell and i get an error saying "Runtime exception: NameError: global name 'current_shares' is not defined" i don't get whats wrong...

Hi Isaac,

Those both sound like the right idea, if you can share the algo here (or send me a collab invite privately to [email protected]) I'd be happy to try to get it working for you!

Cheers,
Jess

Hey Jessica! I added you as a collaborator to my GGS TEST2 Algorithm, can you see if you can help me with my issue with selling?

Hi Isaac,

I think your algo was placing duplicate orders because there was no check for open orders, this can happen if you try to trade more than 25% of the trade volume for a stock on a given bar with the default slippage model enabled.

Adding a check and guard against ordering with pending open orders seemed to fix this issue, but please check it out and see if its behaving as you expect now!

Best wishes, Jess

Wow, worked great! Anyway i can do the same thing for buying shares too? Because even though i buy with cash and not with shares it over-buys. This is really weird.