Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Solved, Turtle Trading strategy

It based on Richard Donchian strategy
We calculate the N using ATR(Average True Range) from Talib
Unit =0.01 of the account size / ATR


Here is the strategy and Add strategy is the core:

1st Buy: First time break Donchian upper limit(last 20 days max), buy 1 unit
2nd Buy: When price went up by 0.5N, we buy another 1 unit.
3rd Buy: Same, When price went up by 0.5N, we buy another 1 unit.
4th Buy: Same, but this is the last buy. We do not add anymore
SELL: when current price drop below 20 days min, we sell to 0.

Problems are:

  1. I record the 1st buy construct a DataFrame called 'recordme' but by the time we reach 2nd buy, 'recordme' is still empty for some reason. I cannot find why it is not recording?
2015-02-09 16:30 rebalance:61 INFO Empty DataFrame  
Columns: [add_time, last_buy_price, symbols]  
Index: []  

2.following strategy went wrong when it sees a empty Series. Here I was trying to substract a value instead of a Series. I don't know if we fix the 1 problem, this will be solved accordingly.


I've asked help once of this strategy, but it doesn't seem to fix the problem
from: if cur_price > add_price and add_unit < 4:
to: if cur_price > add_price[-1] and add_unit[-1] < 4:
I don't think its the prime problem though.
Notes:
True Range = Max(High-Low,abs(High-PreClose),abs(PreClose-Low))
ATR = MA(TrueRange, 20)
N = (PreN[-19:] + True Range) / 20

2 responses

I'll try to answer your questions.

I record the 1st buy construct a DataFrame called 'recordme' but by the time we reach 2nd buy, 'recordme' is still empty for some reason. I cannot find why it is not recording?
The issue is the following code. recordme keeps getting set to a new empty dataframe at the beginning of rebalance. That is why it always appears empty. Probably want to do this only once in initialize.

def rebalance(context, data):  
    global recordme  
    hist = data.history(context.securities, ['high', 'low', 'close'], 200, '1d')  
    account_size = context.portfolio_size  
    recordme = pd.DataFrame({'symbols':[],'add_time':[],'last_buy_price':[]})  

2.following strategy went wrong when it sees a empty Series. Here I was trying to subtract a value instead of a Series. I don't know if we fix the 1 problem, this will be solved accordingly.
Yes, the issue should be solved if recordme isn't set to a new empty dataframe each time the algo runs rebalance. However, you should never store prices like this. If there is a stock split one will end up comparing the split price with the previous un-split price. Maybe consider using the cost_basis attribute of a position. This will be the split-adjusted weighted average price of an open position. There's info on that attribute here.

Try those things and reply if you hit any roadblocks.

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.

@Dan, Thanks a lot! I think its fixed.

I have tried using last_price= context.portfolio.positions[context.securities].cost_basis, but it has problems
1.last_price= context.portfolio.positions[symbol('AAPL')].cost_basis,
SPY doesn't work here. I think either it doesn't take SPY as input because its a ETF.

2.context.portfolio.positions[context.securities].cost_basis.
somehow, I cannot using 'for loop' in it.

I will dig deeper, bigger chance is that I didn't research enough. But I am really happy its runs. Its been bugging me too long.
Thank you again