Back to Community
Help (on what I think is a simple Algo)

All I am trying to do is grab the open -> submit a limit order -> set a take profit and stop loss.

But for some reason this thing ends up short?

def initialize(context):  
    context.stock = symbol('AA')  
    context.TP = 1.02  
    context.SL = .90  
    schedule_function(record_first_bar, date_rules.week_start(), time_rules.market_open())  
    schedule_function(long_entry, date_rules.week_start(), time_rules.market_open())  

def record_first_bar(context, data):  
    context.first_min_close = data.current(context.stock, 'open')  

def long_entry(context, data):  
    order_target_percent(context.stock, .33, style=LimitOrder(context.first_min_close))  

def close_profitable_longs(context, data):  
    is_long = (context.portfolio.positions[context.stock].amount > 0)  
    if (is_long):  
        profit = (context.first_min_close * context.TP)  
        stop = (context.first_min_close * context.SL)  
        if (profit <= data.current(context.stock,'price')):  
            order_target_percent(context.stock, 0)  
        if (stop >= data.current(context.stock,'price')):  
            order_target_percent(context.stock, 0)  

def handle_data(context, data):  
    close_profitable_longs(context, data)  
2 responses

Hi Tyler -

I added some logging to your algo. The problem is that you're not checking to make sure that the order is filled before you place a new order - you end up with orders stacked on top of each other. Clone the attached algo and check the logging. (Also - tip for next time - it helps if you share a backtest rather than the code. The backtest object has lots of helpful info in it for debugging).

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 59a425c8aa3dbc510f28dc2d
There was a runtime error.

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.

Dan, thank you. I knew I was missing something simple. I've added the get_open_orders() check and now everything looks good.