Simple strategy using industry leaders in tech sector

This is basically the first program/strategy that I have implemented in Quantopian. It is very simple. It basically buy when majority of stocks' prices in the list is 1% higher than those of the previous days.
And, sell when the prices today is 1 % lower than that of the previous days.

The list I have put here are apple, HPQ, IBM, ACN, CSC, YHOO, MSFT, GOOG_L, Facebook and AOL, which are industry leaders according to the Bloomberg.

I understand that there is survivorship bias in this program.. But, this will do for now. Any feedback or comment for improvement is greatly appreciated. ..

21
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
# this strategy buy stocks when 75 % of stocks in the list is up 1 percent
# and sell when 75 % of stocks in the list is down 1 percent

# this long only strategy

from collections import Counter
import math

def initialize(context):

# the tech stocks here are apple, HPQ, IBM, ACN, CSC, YHOO, MSFT, GOOG_L, Facebook and AOL

context.stocks = [sid(24), sid(3735), sid(3766), sid(25555),sid(1898),sid(14848),sid(5061),sid(5692),sid(26578),sid(42950),sid(38989)]
# now we are setting this strategy only long only... will give an error if we try to short

set_long_only()
# setting commission to be realistic

context.funds=[sid(19658)]

def handle_data(context, data):

decider=moved_a_lot(context,data)

#record(portfolio_values=context.portfolio.portfolio_value,position_values=context.portfolio.positions_value,cash=context.portfolio.cash)
for stock in context.stocks:

if decider == 1 and context.portfolio.positions[stock].amount==0 and context.portfolio.cash > 10* data[stock].price :
order(stock,10)
print ' '
print get_datetime()

elif decider == -1 and context.portfolio.positions[stock].amount > 0:
print ' '

order(stock,-10)
print get_datetime()
log.info(' Selling'+str( stock.symbol)+'at price '+str(data[stock].price))
print get_datetime()

def moved_a_lot(context,data):

price_history = history(bar_count=5, frequency='1d', field='price')

per=[]
movement=[]

# calculating the percentage of each stock in the list
for stock in context.stocks:
prev_bar=price_history[stock][-2]
curr_bar=price_history[stock][-1]
percentage=  (curr_bar-prev_bar )/prev_bar
per.append(percentage)

# converting the percentage to list like this [ 1,1,1,0,-1,0]
# 1 if percentage is greater than 0.005
# -1 if percentage is less than -0.005
# 0 otherwise

for x in per:
if x > 0.01:
movement.append(1)
elif x< -0.01:
movement.append(-1)
else:
movement.append(0)

# finding the mode and returning it if it occours more han 2/3 of the stocks

if Counter(movement).most_common(1)[0][1] > (2/3.0)*len(movement):
return Counter(movement).most_common(1)[0][0]

else:
return 0


There was a runtime error.
2 responses

You can try to avoid the bias of cherry picking stocks by using set_universe, which creates a basket of stocks for you.

I cloned your algo and ran it with a universe of approximately 40 stocks, and it looks the algorithm returns are heavily influenced by the stocks traded!

Using set_universe or Fetcher's custom universe are great ways to test your strategy against a larger group of stocks.

8
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
# this strategy buy stocks when 75 % of stocks in the list is up 1 percent
# and sell when 75 % of stocks in the list is down 1 percent

# this long only strategy

from collections import Counter
import math

def initialize(context):

# the tech stocks here are apple, HPQ, IBM, ACN, CSC, YHOO, MSFT, GOOG_L, Facebook and AOL

#context.stocks = [sid(24), sid(3735), sid(3766), sid(25555),sid(1898),sid(14848),sid(5061),sid(5692),sid(26578),sid(42950),sid(38989)]
set_universe(universe.DollarVolumeUniverse(98, 98.5))
# now we are setting this strategy only long only... will give an error if we try to short

set_long_only()
# setting commission to be realistic

# context.funds=[sid(19658)]

def handle_data(context, data):

decider=moved_a_lot(context,data)

#record(portfolio_valcontext.stocksues=context.portfolio.portfolio_value,position_values=context.portfolio.positions_value,cash=context.portfolio.cash)
for stock in data:

if decider == 1 and context.portfolio.positions[stock].amount==0 and context.portfolio.cash > 10* data[stock].price :
order(stock,10)
print ' '
print get_datetime()

elif decider == -1 and context.portfolio.positions[stock].amount > 0:
print ' '

order(stock,-10)
print get_datetime()
log.info(' Selling'+str( stock.symbol)+'at price '+str(data[stock].price))
print get_datetime()

def moved_a_lot(context,data):

price_history = history(bar_count=5, frequency='1d', field='price')

per=[]
movement=[]

# calculating the percentage of each stock in the list
for stock in data:
prev_bar=price_history[stock][-2]
curr_bar=price_history[stock][-1]
percentage=  (curr_bar-prev_bar )/prev_bar
per.append(percentage)

# converting the percentage to list like this [ 1,1,1,0,-1,0]
# 1 if percentage is greater than 0.005
# -1 if percentage is less than -0.005
# 0 otherwise

for x in per:
if x > 0.01:
movement.append(1)
elif x< -0.01:
movement.append(-1)
else:
movement.append(0)

# finding the mode and returning it if it occours more han 2/3 of the stocks

if Counter(movement).most_common(1)[0][1] > (2/3.0)*len(movement):
return Counter(movement).most_common(1)[0][0]

else:
return 0


There was a runtime error.
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.

Hi Alisa,

I also have tested different stocks on different values of percentage and realize this is a horrible strategy. It just does not work. :) But, that is ok since it was good coding practice in Quantopian. :)

I have used set_universe on other strategies. It does indeed remove survivorship bias. Thank you for suggesting custom universe. I didn't know about it and I will try to use it in the future.