Back to Community
reverse scale trading algorithm

Hi All,

I've not been on Quantopian for a long time, so i'm still pretty new to it all but a managed to build an reverse scale trading algorithm based on an artikle i once read on the internet. The idea is that you buy more if a stock rises above a certain level, and sell when it goes below it. Then these levels adopt to the new price. With this you try to build a portfolio of continually rising stocks, by removing the ones that do not rise. Then when these also go below the adopted low level you hope to have made a profit.
Finaly i have made a profitable algorithm out of it, but it still contains some bugs, because at some point it buys way to much stocks at one day. Also i think that the selection method for buying algorithms could be improved, maybe even with a fundamental method. So far i have not been ablo to come up with a good method that helps the reverse scale method select good initial stocks. If anyone has some pointers or tips that could improve this algorithm that would be great!

Feel free to copy and improve this algorithm, and if you improve it it would be great if you can post your improved version here also.

Regards,
Dennis

Clone Algorithm
42
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: 55a0ba5a628c88125979d471
There was a runtime error.
8 responses

Hi Dennis,

You kinda have a breakout trend following/momentum system going on. I'm liking it. It has problems with leverage, but first take a look at the following:

def reevaluate(context, data):  
    for stock in data:  
        if stock in security_lists.leveraged_etf_list:  # skip bar if in not to trade list  
            return # do you want "continue"?  
        if get_open_orders(stock): # Skip bar if there are open orders  
            return # do you want "continue"?  
        if context.portfolio.positions[stock].amount != 0:  # do you mean ==0 ?  
            return # do you want "continue"?  

From Nov 2012 the short positions are getting knackered by the sustained bull market, but before then they were smoothing out the equity curve.

Just some of my thoughts,
James

Hi James,

Thanks for your insight, i think the bad thing with the short positions could be overcome with a better initial selection method(although i do not have figured out what yet :P) however i do not fully understand your code snip.
If i'm correct with this part:
if context.portfolio.positions[stock].amount != 0: # do you mean ==0 ?
i am not reevaluating the stock if it is alraedy bought(not equal to 0) because if you have bought it, you want to stick to the sell and buy limits set at that buy moment. The reevaluate is for non bought stocks as an initial long/short system.
The other two # do you want "continue"? i do not fully understand.

Thanks again.

Regards,
Dennis

Hi Dennis.

This is very similar to something I've been working on, but your code is more elegant! That said, am I wrong or is there an error in this part of the logic:
elif context.portfolio.positions[stock].amount < 0: # short
if current <= context.more_price[stock.symbol] and shorts <= longs*1:
to_buy = int(context.step_USD/current)*-1
order(stock, to_buy)
context.bought[stock.symbol] = 1
context.dump_price[stock.symbol] = current * 1.85
context.more_price[stock.symbol] = current * 0.15
log.info('>>added short {} of {}'.format(to_buy,stock.symbol))

Don't you want to add to the shorts if it is making a lower low? In that case you would want the dump_price and more_price variables to be
context.dump_price[stock.symbol] = current * 1.15
context.more_price[stock.symbol] = current * 0.85
so that you only add more if the stock moves lower.

    I have not tried this yet but I' m pretty sure it will further improve results.

Serge

Hi Serge,

That is definatly a typo! thx

Regards,
Dennis

If anyone would have some nice ideas about initial selection methods i could look into that would be great!

Hi Dennis

I made a few small changes to your code that were discussed above, which improve returns for the periods I tested. The beta values on this are still very high - way too high for the Q fund , but perhaps not for an aggressive trader.

My suggestion for improvement would be to cause a reset on THAT PARTICULAR STOCK, whenever the stock reaches a 3 fold standard deviation.
Right now the stocks initial direction (long or short) stays in that direction permanently so the stock will
only ever be traded in that direction. So if it drops 40 percent it will never go long again until that 40 percent breach is recovered. This could be improved upon , I believe. Theoretically you might want to consider going long again when it has dropped by at least 2/3 of the previous long term high low.

So for example if stock has gone up by 3 standard deviations, you might want to reset the dump_price and more_price values for that stock once the stock has regressed back to its mean

Obviously this would need to be tested to determine if it really helps.

Can you point me to the original website espousing this scaled trading methodology?

Thanks
Serge

Clone Algorithm
15
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: 56c21a08f2165b12f6cb2e86
There was a runtime error.

Hi Serge,

Thank you for your reply!

Hereby the link where i first found this idea:
http://www.investopedia.com/university/fiveminute/fiveminute8.asp

Your ideas are really interesting, and i will definatly look into them when i have some more spare time. When i have some results i'll post them here.

Kind regards,
Dennis

*Hi Dennis,

Thanks for your response with the link. I like the article, as it corresponds to something I've been practicing for a number of years, without really having formulated the logical theory behind it to my satisfaction. This article does. it. I too, will continue to tinker with your algo, and will keep you abreast of any positive or negative findings.

Thanks
Serge