Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help With Trailing Stop Implementation

I'm having some issues implementing a trailing stop. Basically I am trying to do something like this:

# place stoplimit order for apple  
order(context.aapl, amount, style=StopLimitOrder(limit_price, stop_price))  
# Get My Open Order for apple  
open_aapl_orders = get_open_orders(context.aapl)  
# Adjust my stop limit based on the new price of apple (psuedo code is below)  
if data.current(context.appl, 'price')  > "Existing Stop_Price on my order":  
          Cancel order and place a new one  

Basically just trying to figure out what code I need to use to get the existing stop price in my open order so I can compare it against the current price of apple. Is there a way to pull the stop price I set in my open order out of "open_appl_orders"?.....Any suggestions would be much appreciated.

3 responses

Bump

Hi Frank,

In the code you put in your post, you would be able to access the order's existing stop price by doing open_aapl_orders[0]['stop']. The 0 is to index into the list of orders.

The way I figured this out was by simply running your code and having it print out open_aapl_orders. Then I was able to see that the item in the list is pretty much a dictionary with a key called 'stop' pointing to the stop price. So if you're ever looking for hidden attributes like this, give printing a try.

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.

Thank you Nathan! I was printing, but my dictionary comprehension skills were lacking. Your tidbit about the index was the difference maker.