Hi,
I was wondering if there was a way to code the algorithm to not go long at all. To just use the money available in my account, or that's specified.
Thanks (:
Hi,
I was wondering if there was a way to code the algorithm to not go long at all. To just use the money available in my account, or that's specified.
Thanks (:
Hi Josh,
Based on your second sentence, I'm assuming that you are trying to code an algorithm that goes long and does not borrow any money?
To clarify, The term "go long" means you are holding the stock in expectation that it's price is going to rise, thereby earning a profit.
"Go short" is the opposite, you believe the stock is over-valued and the price is going to fall. If you short a stock, you are borrowing it from your broker to sell - you don't actually have to own the stock. You're hoping to buy the stock back when the price falls, earning a profit. During this window that you're waiting for the stock price to drop, you're paying interest to your broker. When you short a stock you're holding negative shares. You don't own these shares; you borrowed them and need to return them once your position is covered.
So to answer your question,
To go long, add the trading guard set_long_only() in the intialize() function of your code.
To only use the cash available in your account, add a check in your ordering logic. The example below leaves a 5% cash buffer.
# If the current price is 1% above the 5-day average price AND we have enough cash, then we will order.
if current_price > (1.01 * average_price) and cash > current_price:
# Need to calculate how many shares we can buy
# Keep 10% of cash available as a buffer
number_of_shares = int((0.95*cash) / current_price)
# Place the buy order (positive means buy, negative means sell)
order(context.security, number_of_shares)
Also take a look at this thread with a longer explanation: https://www.quantopian.com/posts/negative-cash. Let me know if that helps!
Cheers,
Alisa
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.