Back to Community
Any help with why this is not trading?

Hi - can someone please take a look at this and let me know why its not trading anything? Seriously spent over 4 hrs trying a whole bunch of different things before resorting to asking Q Crowd to assist this newbie. Took code from a few different places and somehow lost track along the way (wish some version control functionalities can be introduced at some point ... or I should just be a bit more careful)...

Summary of what I am trying:
- take predefined universe
- get 2 /30 EMA
- get delta between EMA's
- sort to get the top 5 delta's
- calculate a weight for the top 5
- purchase those 5 positions based on the calculated weight

Currently just want to try it long only, but tried to build framework around in case wanted to start testing shorts also ... any assistance would be much appreciated

Clone Algorithm
47
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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: 55e0ea2cb7bae90de959497b
There was a runtime error.
5 responses

Hi Umar,

Welcome to Quantopian. Some advice on debugging:

When something is going wrong, cut everything out to make it as simple as possible. Cut down the number of stocks. print everything you can think of. Have it backtest over just one or two days, and of course, use the debugger and step through every line of code.

Of course, you may have tried some of that ;)

In def sort_returns(prices) you have updowndelta which is a pandas series, the index is stocks and the value is short EMA - long EMA (It's already the 'most recent returns' because you've used .iloc[-1]) last_delta will contain the 'last' stock in that series, which could be anything, but in this case was just 'CARB'. I think what you actually need here is just updowndelta.sort() (which returns None because it sorts inplace, if you want to return a sorted copy you need updowndelta.order()) and return.

I didn't look closely at the other functions, but I think that gives you some food for thought. Let us know how it goes :)

James

Thanks James - was able to piece together a few things and get it work - attached is the daily back test for a short time period.

Two Issues I am having now is:
1) that I am trying to stop it trading when the SPY moves 2% - I tried this on line 64 but it does not appear to be kicking in for some reason - you can see the impact from Aug 18/15 onwards

2) backtests in minute mode are taking me over 45 minutes and some even longer - not sure how I can 'clean' the code to avoid this.

Thanks again for your help - would it be inappropriate (Q rules or just bad taste) if I offered a token gesture of $25 to your favorite charity for helping me out? Either way, appreciate the help

Clone Algorithm
47
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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: 55e35c409bb6350de3e7b909
There was a runtime error.

ok looking quite a bit better - got it to 'almost' stop based on SPY movement and also backtesting much faster ... stats looking ok at 2015 YTD 129% with 17.6% DD but need to do realism test on them at some point (cash/leverage/commission etc. etc.) but more interested in just figuring out logic of coding right now

but still need to figure out:
1) how to run two different history pulls - one will be used for trading on a day period interval, and the other is on the previous 3 hrs on a minute interval which tracks the SPY movement that I can use as a gauge to turn off trading/alter trading parameters... cannot figure out how to calculate the minute SPY movement in one def and call it in a another def (like I can calculate it one def that loops on X minutes but do not know how to pull it another def that runs once a day)
2) how can I put some sort of trading guard to avoid volatility ETF's - not all ETFs, just the volatility one - is there a way without manually listing them all out?

Clone Algorithm
47
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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: 55e3da827d81260dd2473c66
There was a runtime error.

one more just from 1July15 to date backtest @ 55% in just two months with 10% DD

Clone Algorithm
47
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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: 55e3da25e6c3c50de3721f7a
There was a runtime error.

Hi Umar,

1) I think you've worked out the 2% logic now, but I've changed it a little. You have to check for open orders.

2) To increase backtest speed, you ideally need to remove all loops. Some loops are unavoidable, but otherwise try to use all pandas/numpy functions that work on arrays. At the moment, you have context.N = 900 days (about 42 months), which is going to be slow with a DollarVolumeUniverse. No way of getting around that really. You could reduce this to 750 days and still get the required 30 months of prices. If you could define a different universe using fundamental data instead (https://www.quantopian.com/help#sample-fundamentals) that might be quicker.

That's very nice of you - help comes for free - but if you feel so inclined, you may pick any cancer research charity (I'm not familiar with US charities).

3) I'm not 100% on what you mean there, but see the example. You can get the past 3 hrs of minutes (or less, if the day just started) and do something with that. If you want to check on that every minute, it will be slow to test.

4) volatile ETFs - that's tricky, but you can rule out most of the leveraged ETFs by checking you don't order any sid in security_lists.leveraged_etf_list. Although I don't think historical leveraged ETFs are included in that list.

What's with the prices.resample(... closed='left') ?

I believe the ranges and rangesD are the same but reversed in order (ignoring sign), i.e. ranges.iloc[0:10] == rangesD.iloc[-10:]. See example.

Note that context.account.leverage is gross leverage.

See what you think. Do check my logic.

Clone Algorithm
6
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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: 55e45967059d0b0de0593035
There was a runtime error.