Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with partial profits & stop-loss

I'm having difficulties with what I'm trying to do would appreciate any help!

  1. I'm working only on TSLA and I want to enter only after 10am based on its moving averages if I have no position. Just after I enter I am putting a stop loss with a stop price at the low of the day and I want to risk only $200 so that determines how many shares I will buy. Until here it seems to work...

  2. Then if my paper profit is equal or greater than $400 I want to sell half my position and have a stop loss for the remainder of the position at the price where I bought the stock originally.

This is were I'm having a lot of issues:

  • context.portfolio.positions[stock].cost_basis goes down as I sell so my code doesn't work - any other way to say: once I have sold half my position I do not want to sell any other qty of the stock unless it hits my stop loss.
  • how do I create the stop loss in 2 since selling shares changes the value of context.portfolio.positions[stock].cost_basis and then how do I cancel the original stop loss in 1?

Hope it's clear thanks in advance for your help!

Max

2 responses

Hi Max,

I took a look at your code and here is what I found. One problem is you're nesting your stop-loss loops inside of your ordering loops.

Here's how your algo runs: Handle_data gets called once per minute. After 10AM, if there are no positions, it will place an order to buy the stock according to your MA logic. However the order is filled in the next minute bar and is subject to the slippage model. Since the order is placed in one bar and filled in the next bar, code on line 60 never gets triggered. It will never be true that

if number_of_shares == 0 then if (context.portfolio.positions[stock].amount != 0):

I updated your code and have attached the backtest. The logic now says:

  • Enter position at or after 10AM according to the MA logic
  • Once we have a position, set a stop-loss at today's low. If triggered, this will exit the entire position
  • If have a profit that equals or exceeds $400, sell half of the portfolio

To note, it's more robust to use order_target_percent, which is more flexible with changes in your portfolio cash. Let me know if this makes sense and if its the behavior that you expected.

Cheers,
Alisa

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.

great thanks a lot. the orders were not triggered when trying to sell half the shares but it's working by moving

elif (current_price - stock_avg_cost)*number_of_shares >= 400:  

inside the previous loop
elif context.portfolio.positions[stock].amount != 0: Now the problem is that as I sell some shares the value of stock_avg_cost goes down and as a result my profit condition current_price - stock_avg_cost)*number_of_shares >= 400: is true many times and the algo keeps on selling half my positions.

I also don't understand why after I have 0 shares left I keep getting log saying sold 0 shares: how could that log happen when my condition current_price - stock_avg_cost)*number_of_shares >= 400 should be false since number_of_shares = 0

Also once I sell half my shares how do I make sure that the original stop order is now for just half of the shares? And how can I move it to stock_avg_cost from todays_low?

thanks so much for your help and sorry for my ignorance!

Max