Back to Posts
Listen to Thread
Edited

Hi all,

I am trying to implement a simple algorithm that buys/sells a random security based on a coin flip (aka Cramer's algorithm). How can I select a stock at random? The following code does not work, and errors out saying sid takes only one parameter.

import random

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    pass

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that security.  
    # data.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.  
    thestock = int(random.randint(1,200))  
    stock = sid(thestock)  
    coin = random.random()  
    if (coin < 0.5):  
        order(stock, 1)  
    else:  
        order(stock, -1)        

Hi,

This is a fun idea!

The sid function is a common pitfall - it is really only intended for explicitly identifying a security at development time. We actually pre-parse the script to find all the sid calls, and use that information to configure the data sent to your algorithm. As a result, we don't allow variables to be passed into the function. Luckily, we provide an alternative to select a universe of stocks without explicitly identifying any of them. You make the selection using a builtin called set_universe. In the attached backtest, I select a random universe of stocks based on the DollarVolume traded. Then, I choose a random security from that universe in the handle_data method, and use your coin flip code to decide on buying or selling it.

thanks,
fawce

Clone Algorithm
42
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Thanks a lot for the quick reply and the sample code.

Also...3735% return based on randomness?? Wow.

Well, I didn't add any position limits, so it builds a pretty massive amount of leverage. I ran a bunch of samples and the returns are... random.

Hi Fawce,

Can the parameters 'bottom' & 'top' in set_universe vary within a program?

set_universe(universe.DollarVolumeUniverse(bottom, top))  

In other words, for example, could I get access to different chunks of the universe for each tic?

Grant

Grant,

Sorry, that's not possible. set_universe is in initialize, and only callable once.

The real fix for this is to let you call the whole universe rather than just part of it. We'll get there.

Dan

Dan,

No problem...I can try my algorithm with just a chunk of the universe. If I get it working, I'll post it. Basically looking to use a time series pattern matching algorithm that I developed to sort securities from most similar to least.

Grant

Edited

Very interesting experiment :) As I was thinking about it, if we simply choose one stock and randomly buy/sell it, why don't we try randomly picking two stocks and buy the one with lower price and sell the one with the higher price? It sounds like a very primitive version of pair trading, and I was hoping to get on average a better return. It did. Next step would be to make it more stable - a practical way of doing it is to run this algo a couple times using smaller amount of investments for each. Say you have $100, and you can split it into ten $10 trials of this algo and I believe since you have a positive expected return you can make money from this. Any ideas?

Clone Algorithm
44
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
Edited

The returns probably need to be averaged over many (> 1000) random seeds...maybe the sequence of random nums led to 3000 and 4000% returns. I am guessing such a strategy has returns with zero mean and high variance (with another random seed I lost 4000%).

EDIT: Can I embed images here??? http://i.imgur.com/gtvZTbL.png

Hey Ha, good point here. But I still think the expected value seems to be higher than zero since you are after all buying at a lower price and selling at a higher one - intuitively. I definitely agree that we need to have some sort of average over a larger set of randomized data.

The algos look fantastic but how do you get such volatility using just equities?

Log in to reply to this thread.
Not a member? Sign up!