Back to Community
Quantified Strategy buy at open, sell on close, .. why am I getting different results in Quantopian...?

According to the study... @quantified.. the strategy based on the ticker XLP I am applying it in quantopian to check for correctness of the study.

Yesterday must have been a down day of at least 0.25%.  
If XLP opens down more than 0.1% today, go long and exit on the close.

Fairly simple rules. Here is the result from 2005 until present:

          **P/L       76.03854  
                #wins   147  
                Avg         0.34**  

source: http://www.quantifiedstrategies.com/trade-the-boring-consumer-stocks-when-they-open-down-and-yesterday-was-a-down-day/

AM I... missing something ? pls help...

13 responses

Firstly, you are not applying your rules, you just buying and selling at certain times during the day.

Secondly, you are buying and selling so much that you're paying a lot commissions, which is killing any profits.

You can disable commissions as shown below.

Clone Algorithm
8
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: 568d2813e64b541174ddfe66
There was a runtime error.

it does not outperform... as shown...in there study....

What you ... posted ... is a ... completely different ... s ... trategy ... to the one ... you ... desc ... ribed ...

This algo more close to what is described in so called "paper".
It has low volatility, low beta ,very high consistency and stability (of loosing) .
Those four may bring it to front page of Quantopian open.
But.................... ...................... ........................... ...........

Clone Algorithm
2
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: 568dcebfcfc31c0d2acf3eb1
There was a runtime error.

@Simon.. its closely related.... than you think.... can you build an algo to prove your point of a better closely related to the study.... you can.. recycle my code... if needed... thanks....

@chan, pls edit your first msg and correct the title, it's buy at open, sell on close, thanks.
None of these backtests are actually doing that by the way, they always specify running some number of minutes away from open and close.
However, if you set one to try to do so (below), it barely does beat the benchmark yet would be worse with commissions.

However, I have a question aside from that:
Setting a breakpoint inside on_open(), the debugger will show a time of day relevant to your time zone.
As we know, the first minute of the trading day does not run for us to be able to enter an order then.
One would expect the minute that shows up in on_open() then to be :31.
Instead, it will be :35, :33, :34 etc. On 2005-01-31 it is :37.

The question is:
Assuming that's because XLP is thinly traded, is the backtester trying to be helpful and smart by not running on_open() until the first minute that a security in the universe is traded, and, do we always miss the first trade using schedule_function() this way ? (We can enter an order in that minute, however to trade in that minute we would have had to have entered the order in the previous minute or before)
Or will time_rules.market_open() cause the first run to be the minute before a first trade?
Assuming this is on target, can someone check online please, was the first trade of XLP on 2005-01-31 at US/Eastern 9:37 or 9:38? Thanks.

def initialize(context):  
    context.xlp = symbol('XLP')  
    set_slippage(slippage.FixedSlippage(spread=.00))  
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0))  
    schedule_function(on_open, date_rules.every_day(), time_rules.market_open())  
    schedule_function(on_clos, date_rules.every_day(), time_rules.market_close())

def on_open(context, data):  
    if get_open_orders(): return  
    xlp = context.xlp  
    closes = history(3, '1d', 'close_price')[xlp]

    # From http://www.quantifiedstrategies.com/trade-the-boring-consumer-stocks-when-they-open-down-and-yesterday-was-a-down-day/  
    # "Yesterday must have been a down day of at least 0.25%."  
    # "If XLP opens down more than 0.1% today, go long and exit on the close."  
    if closes[-2] < closes[-3] * (100 - .25 / 100):  
        if data[xlp].price < closes[-2] * (100 - .1 / 100):  
            order_target_percent(xlp, 1.0)

def on_clos(context, data):  
    # This is a side-problem by the way, it should not wait 24 hrs to sell all, it should check again every minute  
    if get_open_orders(): return  # Live/real, if the sell doesn't go thru, the sell order would be canceled. Should re-open next morning, must do manually. I don't think anyone has been doing that, have not seen a function for it, and it didn't occur to me until now.  
    order_target_percent(context.xlp, 0)

def handle_data(context, data):  
    return  

@garyha.... I thought it was from "close to open...". since it w s replicated in Ipy thon notebook. ....as shown here...."http://nbviewer.ipython.org/urls/dl.dropboxusercontent.com/u/11352905/notebooks/trading%20with%20python%20example.ipynb " most of the returns ... come from... close to open... I don't know...if the same code... can be implemented in research.... or can we replicated it in research.... to achieve... the same results....

Gary,
This is a backtest of yours code with disabled slippage and commissions.

Clone Algorithm
4
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: 568e2455a606b6117ccae6c0
There was a runtime error.

And this is the same one with Quantopian default slippage and commissions.
Nether one is realistic.

Clone Algorithm
4
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: 568e1f53bdfd1e118370152c
There was a runtime error.

"Nether one is realistic."

Correct! Way to nail that one.
And if you start it when the author stated, it's worse. Don't bother.

My question again (and the point) was:
Assuming this is on target, can someone check online please, was the first trade of XLP on 2005-01-31 at US/Eastern 9:37 or 9:38? Thanks

Chan's link at ipython.org says: "The rules of -0.25% / -0.1% can be further improved to achieve a Sharpe of 1.58. However, the strategy performance does not vary that much, it is very stable and performs well for a wide range of settings".

So it would seem odd that we can't replicate the good results reported by multiple other sources. Given (as Vladimir illustrated) this thing can be that widely sensitive to merely slippage and commissions, I would like to know whether that question I'm asking is relevant, because it might be harming people in live/real money, thus also hampering Quantopian's overall success the longer it sits around unnoticed/undealt with, possibly resulting in false conclusions in backtesting, steering developers down wrong paths and might be fixable.

Gary,
I found miscalculation in yours code:
This two lines

if closes[-2] < closes[-3] * (100 - .25 / 100):  
        if data[xlp].price < closes[-2] * (100 - .1 / 100):  

should be as

if closes[-2] < closes[-3] * (100 - .25 / 100)/100:  
        if data[xlp].price < closes[-2] * (100 - .1 / 100)/100:  

and it makes performance without commissions and slippage worse.

Clone Algorithm
4
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: 568e30fe7faecd11912808f6
There was a runtime error.

with Quantopian default slippage and commissions

Clone Algorithm
4
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: 568e35198efdb71192e15341
There was a runtime error.