Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
DIA Trend Within Trend

This is a concept that takes the basic turtle trend model and puts a little spin on it. The orders are on stock in the dow. Besides for the basic trend of the stock, it also includes the trend of the dow when starting. Pyramiding and moving stops based on N is included, or at least I think they are correctly included.

As you can see, its pretty volatile but when its working well its great. I need to figure out how to stop the bleeding when things are going poorly and I think it might be a good model. It can also easy be applied to other indexes as well.

9 responses

Yeah, I know but I figure you could use the same basic idea and use other close information to help prop up the trends.

I fixed an issue with the stop and I think its in better shape.

A question, is the way that I set up how I set and update stops good or is there a better way to do it?

Hello John,

I've run the algo but from looking at the full backtest it only trades once on 2010-01-04.

P.

Peter,

Yeah, you are right, I just see this algo hanging on to the stocks long term and not letting go. Thats why I was wondering about the stops and if I'm doing them right. More specifically, if I'm updating the stops correctly on line 99 in the above backtest. The weird thing is that in even when testing on longer periods, it still performs pretty well, see the attached back test. With that said, I'm pretty sure its also over leveraged and I need to look into that as well.

Thanks for the input, keep it coming. I'm new to this community and Python so keep in coming.

John

Hello John,

I'm looking at stop and limit orders myself and I think the documentation is not clear. I believe that the order and the stop are placed separately as in:

price = data[sid(24)].close_price  
order(sid(24), 110)  
order(sid(24), -110, stop_price = price * 0.95)  

which results in two orders i.e. a filled market order and an open stop order.

The stupid thing is I made a tiny contribution to zipline relating to stop and limit orders but that was so long ago I've forgotten most of what I did.

P.

Hello AM,

Thanks. How should the attached be better implemented?

P.

Hello AM,

Thanks - your test is attached for others to see. I've asked for feedback from Quantopian about the syntax of stop and limit orders. Looking back at some zipline work I did I was writing this i.e. a market order followed by a stop order:

from datetime import datetime  
import pytz  
from zipline.transforms.utils import EventWindow

from zipline.algorithm import TradingAlgorithm  
from zipline.utils.factory import load_from_yahoo


class BuyApple(TradingAlgorithm):

    def initialize(self):  
        self.firstOrderplaced = False

    def handle_data(self, data):  
        print "--------------------"  
        print "Datetime in handle_data : " + str(self.datetime)  
        print data['AAPL'].price  
        print self.portfolio.cash  
        if not self.firstOrderplaced:  
            self.order('AAPL', 100,)  
            self.order('AAPL', -100, stop_price=118.00)  
            #slf.order('AAPL', -100, limit_price=131.00)  
            #self.order('AAPL', -100, limit_price=123.00, stop_price=125.60)  
            self.firstOrderplaced = True  


if __name__ == '__main__':  
    start = datetime(2008, 1, 26, 0, 0, 0, 0, pytz.utc)  
    end = datetime(2008, 4, 1, 0, 0, 0, 0, pytz.utc)  
    data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start,  
                           end=end)  
    simple_algo = BuyApple(instant_fill=True)  
    results = simple_algo.run(data)  

When run in IDLE the output is:

>>>  
AAPL  
--------------------  
Datetime in handle_data : 2008-01-28 00:00:00+00:00  
125.71  
100000.0  
--------------------  
Datetime in handle_data : 2008-01-29 00:00:00+00:00  
127.19  
87413.429  
--------------------  
Datetime in handle_data : 2008-01-30 00:00:00+00:00  
127.81  
87413.429  
--------------------  
Datetime in handle_data : 2008-01-31 00:00:00+00:00  
130.88  
87413.429  
--------------------  
Datetime in handle_data : 2008-02-01 00:00:00+00:00  
129.32  
87413.429  
--------------------  
Datetime in handle_data : 2008-02-04 00:00:00+00:00  
127.29  
87413.429  
--------------------  
Datetime in handle_data : 2008-02-05 00:00:00+00:00  
125.08  
87413.429  
--------------------  
Datetime in handle_data : 2008-02-06 00:00:00+00:00  
117.96  
87413.429  
--------------------  
Datetime in handle_data : 2008-02-07 00:00:00+00:00  
117.23  
99194.633  
--------------------  

P.

Hello John,

Apologies for high-jacking your thread. I'll start a new one on order syntax at https://www.quantopian.com/posts/usage-and-syntax-of-stop-orders.

P.

No problem at all. Feel free to high-jack my thread. It helps me learn. Thanks for the help so far.