Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
I am having problem with my first algo.

I have no intention using this but I am interested in using a algo that works on a day-trade time.
So i thought I would start with something easy. I am still learning python.

The algo buys and sell at the levels i want it to however I checked on a stock chart and the results differ from the results I should get . The transaction details are different from what i would expect to get looking at a stock chart.
The algo should of bought earlier then it did.

Can someone please help me or tell me why this is?

Thanks in advance Andrew

6 responses

any help?

Hello Andrew,

I'm mystified by the StopLimitOrder. Here's the output I got after modifying your algorithm:

2013-12-03PRINT15.4635  
2013-12-03PRINTEvent({'status': 1, 'limit_reached': True, 'created': datetime.datetime(2013, 11, 21, 17, 50, tzinfo=), 'stop': None, 'reason': None, 'stop_reached': False, 'commission': 3.0, 'amount': 100, 'limit': 15.48, 'sid': Security(700, symbol='BAC', security_name='BANK OF AMERICA CORP', exchange='NEW YORK STOCK EXCHANGE', start_date=datetime.datetime(1993, 1, 4, 0, 0, tzinfo=), end_date=datetime.datetime(2014, 9, 22, 0, 0, tzinfo=), first_traded=None), 'id': 'a2c6b8a421c24a6cac370811fd93b82c', 'dt': datetime.datetime(2013, 12, 3, 18, 23, tzinfo=), 'filled': 100})  
2014-01-02PRINT16.0025  
2014-01-02PRINTEvent({'status': 1, 'limit_reached': True, 'created': datetime.datetime(2013, 11, 25, 14, 44, tzinfo=), 'stop': None, 'reason': None, 'stop_reached': False, 'commission': 3.0, 'amount': -100, 'limit': 16.0, 'sid': Security(700, symbol='BAC', security_name='BANK OF AMERICA CORP', exchange='NEW YORK STOCK EXCHANGE', start_date=datetime.datetime(1993, 1, 4, 0, 0, tzinfo=), end_date=datetime.datetime(2014, 9, 22, 0, 0, tzinfo=), first_traded=None), 'id': 'eb0bc78919cf4d159f0c4fffabbf3bfa', 'dt': datetime.datetime(2014, 1, 2, 16, 26, tzinfo=), 'filled': -100})  

My confusion is that your buy order should have both a limit price and a stop price listed, but I only see the limit price of 15.48. It seems that your stop price of 15.52 should be listed, too.

Grant

It looks like your algorithm submits a StopLimit order on 11/21/2013 and submits a Limit order on 11/25/2013. The StopLimit position is then filled on 12/3/2013 and the Limit position remains open. The Limit order was placed before the StopLimit order was filled!

What might have been confusing is that there isn't a check for open orders. When you submit a StopLimit order on 11/21/2013, the backtester converts the stop order to a limit order, waiting for the price to trigger at 15.48. So between 11/21 - 12/3 context.order_placed=1 but the order is not filled. The order is then filled on 12/3/2013.

To fix this logic, you should add a check for open orders. Your Limit position should check there are no open orders and if context.order_placed=1.

Here is a sample algo that has a check for open orders: https://www.quantopian.com/posts/rebalance-algo-9-sector-etfs

One thing to note is there's a slight difference between backtesting and live trading. In general, we try to keep the behavior closely aligned, and this is one of the few differences. In live trading, all open orders get cancelled at 4PM EOD. This is a precautionary measure against wild price swings during non-market hours. In backtesting, open orders persist from day-to-day. In the future we'll work to make the behavior identical, but in the meantime you can write your own code to cancel the orders if its 4:00PM. Hope that helps and let me know if you have other questions!

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.

Hi Alisa,

Shouldn't the order event for the StopLimitOrder that I print out above show both the stop price and the limit price? And shouldn't it indicate that the stop was reached? Or was there a separate stop order that is inaccessible, and all I'm seeing is the resulting limit order?

Grant

Thanks for your help.

The problem is about the execution of the trade it should of been executed at 11/23/13 When looking at stock chart at freestocks.com.

I thought this may of been because the stock gaped up before the open however I check this another stock and it seems to happen to it appears to me this is because the price that is recorded at the bottom is not a minute by minute price it appears the be rounded. I changed it to yahoo for Wednesday trading day and it was still not executed.

The back test I ran was From 2013-11-10 to 2014-09-26 with $1,000,000 initial capital on (minute data) on Alisa Deychman updated version.
The result where
2013-12-03 - Buys $1,549.40 (1 transactions)
6:23 PM
BAC
BUY
100
$15.49 $1,549.40 2014-01-02 - Sells $1,597.20 (1 transactions)
4:26 PM
BAC
SELL
-100
$15.97 ($1,597.20

I am confused why is it not executing the first chance it gets to make the trade?

Andrew,

Yeah, It's a little confusing on how the stop-limit orders work but hopefully I can help clear things up:

#: StopLimitOrder(limitprice, stopprice)  
context.stoplimit_order = order(context.bac, 100, style=StopLimitOrder(15.48, 15.52))  

This line asks that the stop price be set to 15.52 and the limit price be set to 15.48. So first, the stock's price has to be higher than 15.52 which happens on 2013-11-21 and next, because now the stop-limit order has been converted into a limit order, the stock's price has to be lower than 15.48 in order for the trade to execute. And the first time this series of conditions are met are on 2013-12-03 which is when your log shows that 100 shares of BAC were bought.

I've attached an algo that shows you the logs of when these conditions are met, but feel free to ask more questions about it. Also, this thread may help clear up the stop-limit order a bit more.

Seong

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.