由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
_pennystock版 - Re: 【Stragety论坛】Trading with correlations. (转载)
相关主题
【Stragety论坛】Trading with correlations. (转载)Trading strategy: Buying Candlestick Bullish Reversal Patt (转载)
Re: 新手的都进来看看吧 (转载)【技术贴-大盘股小组】谈谈今天的大型股
ATPG07/22 收盤行情 -- 美股開高走高 大漲作收
all "REE" stock is buy and holdSpecial Reports Jul 23, 2010
new stragety for "REE" play正式开始走下坡路的股
all my pick only my personal opinion. if lost money by following, don't blame meTop 10 Dow 30 (DJIA) Stocks with Highest Upside
Events and ERs for Week 4/19 - 4/23 (转载)updated weekly list (转载)
我的抄底的想法 (转载)Next Week Highlighted Earnings Releases
相关话题的讨论汇总
话题: adjclose话题: date话题: symbol话题: str话题: enterprice
1 (共1页)
e*n
发帖数: 1511
1
【 以下文字转载自 Stock 讨论区 】
发信人: mitbbs2020 (bbc), 信区: Stock
标 题: Re: 【Stragety论坛】Trading with correlations.
发信站: BBS 未名空间站 (Sun Jul 25 19:52:04 2010, 美东)
用option当然可以,但不是必须,
用option可以得到更高的leverage,
但是缺点是散户量小,不容易实现market neutral。
比如1.37的hedge ratio,你要long 137和short 100个才能实现。
13和10就不那么好,而且不容易adjust,看附图就知道不怎么neutral的情况。
说stock不便于对冲,这点我不懂你什么意思。
这本身就是market neutral的strategy。
我测试过DISCA和AXP(paper account),追踪了至少2个月,涨跌幅从来没超过3%的,
但是这反过来说明这不是一对好的pair。
别的pair,没有任何leverage,一年可以做到100%以上的也有,如果我实际做stock到
这个%,
已经很满意了。
这是我之前测试的code:
from numpy import *
def conv2list(results):
temp = []
for result in results:
temp.append(result[0])
return temp
def getrank(num, list):
r = 1
for a in list:
if a > num:
r = r + 1
return r
def pairtrade(symbol_a, symbol_b, length, enter_threshold,
exit_threshold):
import pyodbc, os, string
cnxn = pyodbc.connect('DSN=stock')
cursor = cnxn.cursor()
adjclose_aq = "select top " + str(length) + " AdjClose\
from dbo.all_daily\
where symbol = '" + symbol_a + "' order by Date Desc"
cursor.execute(adjclose_aq)
adjclose_a = conv2list(cursor.fetchall())
adjclose_a.reverse()
adjclose_bq = "select top " + str(length) + " AdjClose\
from dbo.all_daily\
where symbol = '" + symbol_b + "' order by Date Desc"
cursor.execute(adjclose_bq)
adjclose_b = conv2list(cursor.fetchall())
adjclose_b.reverse()
# check if open price is adjusted is done seperately in databse
# only need Openprice from lookback + 2 to total length day
lookback = 260
openlength = length - lookback

adjopen_aq = "select top " + str(openlength) + " OpenPrice\
from dbo.all_daily\
where symbol = '" + symbol_a + "' order by Date Desc"
cursor.execute(adjopen_aq)
adjopen_a = conv2list(cursor.fetchall())
adjopen_a.reverse()
adjopen_bq = "select top " + str(openlength) + " OpenPrice\
from dbo.all_daily\
where symbol = '" + symbol_b + "' order by Date Desc"
cursor.execute(adjopen_bq)
adjopen_b = conv2list(cursor.fetchall())
adjopen_b.reverse()
# for pair trading, a,b share the same date, only openprice range is
needed
date_abq = "select top " + str(length) + " Date\
from dbo.all_daily\
where symbol = '" + symbol_a + "' order by Date Desc"
cursor.execute(date_abq)
date_ab = conv2list(cursor.fetchall())
date_ab.reverse()

# 0 hold, 1 long a short b, -1 long b short a
status = 0
# total investment is $10000 short $5000, long $5000
investment = 10000
long, short = investment/2, -investment/2
trades, winner, loser = 0, 0, 0
stats = []

#print "%s, %s" % (symbol_a, symbol_b)
#print "Enter Date, Enter PriceA, Enter PriceB, ShareA, ShareB, Exit
Date, Exit PriceA, Exit PriceB, Gain, %Gain"
# count from the 251th days
for i in range(lookback, length):
rank_a = getrank(adjclose_a[i], adjclose_a[i-lookback:i])
rank_b = getrank(adjclose_b[i], adjclose_b[i-lookback:i])
diff = rank_a - rank_b
# rank a is too high and rank b is too low, means a is
underpriced and b is overpriced,
# so long a short b
if(status == 0 and diff >= enter_threshold):
status = 1
# i = lookback, then buy at the first adjopen date, already
shifted
enterprice_a, enterprice_b = adjclose_a[i], adjclose_b[i]
share_a, share_b = long/enterprice_a, short/enterprice_b
trade = "%s, %f, %f, %f, %f" % (date_ab[i], adjclose_a[i],
adjclose_b[i], share_a, share_b)
elif(status == 0 and diff <= - enter_threshold):
status = -1
enterprice_a, enterprice_b = adjclose_a[i], adjclose_b[i]
share_a, share_b = short/enterprice_a, long/enterprice_b
trade = "%s, %f, %f, %f, %f" % (date_ab[i], adjclose_a[i],
adjclose_b[i], share_a, share_b)
# assume it will not go from long_a_short_b signal to
long_b_short_a signal in a day
elif((status == 1 and diff <= exit_threshold) or (status == -1
and diff >= -exit_threshold)):
status = 0
trades = trades + 1
exitprice_a, exitprice_b = adjclose_a[i], adjclose_b[i]
# to be checked the close date
gain = share_a*(exitprice_a-
enterprice_a)+share_b*(exitprice_b-enterprice_b)

stats.append(gain)

if gain > 0:
winner = winner + 1
else:
loser = loser + 1
#print "%s, %s, %f, %f, %f, %f" % (trade, date_ab[i],
exitprice_a, exitprice_b, gain, gain/(long-short))
#print "total gain: %f%%" % totalgain
#print "total trades: %d" % trades
#print "winner trades: %d" % winner
#print "loser trades: %d" % loser
#print "maximum gain: %f%%" % maxgain
#print "maximum loss: %f%%" % maxloss
if trades >= 2:
if max(stats) > 0.0:
maxgain = max(stats)/investment
else:
maxgain = 0.0
if min(stats) < 0.0:
maxloss = min(stats)/investment
else:
maxloss = 0.0

totalgain = sum(stats)/investment
winpercent = winner/trades
arraystats = array(stats)
sharpe = average(stats)/arraystats.std()
#print "winner percentage: %f%%\n" % winpercent
return "%d|%d|%d|%f|%f|%f|%f" %(trades, winner, loser, maxgain,
maxloss, totalgain, sharpe)
else:
return "0|0|0|0|0|0|0"
def conv2array(results):
list = []
for result in results:
list.append(result[0])
return array(list)
import pyodbc, os, string
import time, datetime
from scipy.stats import *
t0 = time.clock()
cnxn = pyodbc.connect('DSN=stock')
cursor = cnxn.cursor()
today = (datetime.date.today() - datetime.timedelta(2)).strftime("%Y-%m-
%d")
print today
minlen = 500
result = open("nasdaq_correlation.csv","w")
result.writelines("sector|industry|symbola|symbolb|correlation|trades|wi
nner|loser|maxgain|maxloss\n")

getsector = "select distinct sector from dbo.nasdaq_sector where
isnull(sector,'')<>''"
cursor.execute(getsector)
sectors = cursor.fetchall()
sectorsize = len(sectors)
for sector in sectors:
getindustry ="select distinct industry from dbo.nasdaq_sector
where sector = '" + sector[0] + "'"
cursor.execute(getindustry)
industries = cursor.fetchall()
for industry in industries:
print "processing ", sector[0], industry[0]
getsymbol = "select b.symbol \
from dbo.nasdaq_sector a\
join dbo.nasdaq b on a.symbol = b.symbol \
where a.sector = '" + sector[0] + "'" \
+ " and a.industry = '" + industry[0] + "'"\
+ " and b.volume > 0 \
group by b.symbol \
having max(b.date) = '" + today + "'"\
" and count(b.date) > " + str(minlen)
cursor.execute(getsymbol)
symbols = cursor.fetchall()
symbolsize = len(symbols)
for i in range(symbolsize-1):
for j in range(i+1, symbolsize):
symbola = symbols[i][0]
symbolb = symbols[j][0]
print "processing %s with %s" %
(symbola, symbolb)
getadjclosea = "select top " +
str(minlen) + " adjclose \
from dbo.nasdaq \
where symbol = '" + symbola +
"'"\
+ " order by date desc"
cursor.execute(getadjclosea)
adjclose_a = cursor.fetchall()
adjclosea = conv2array(adjclose_a)

getadjcloseb = "select top " +
str(minlen) + " adjclose \
from dbo.nasdaq \
where symbol = '" + symbolb +
"'"\
+ " order by date desc"
cursor.execute(getadjcloseb)
adjclose_b = cursor.fetchall()
adjcloseb = conv2array(adjclose_b)
correlation = spearmanr(adjclosea,
adjcloseb)[0]
traderesult = pairtrade(symbola,
symbolb, minlen, 10, 0)
result.writelines(str(sector[0]) + '|' +
str(industry[0]) + '|' + str(symbola) + '|' + str(symbolb)+ '|' +
str(correlation) + '|' + traderesult + '\n')
print str(time.clock() - t0) + ' took'
result.close()
t********s
发帖数: 4503
2
楼主啥专业啊?太厉害了。程序是啥写的啊?
e*n
发帖数: 1511
3
转的
python 写的

【在 t********s 的大作中提到】
: 楼主啥专业啊?太厉害了。程序是啥写的啊?
1 (共1页)
相关主题
Next Week Highlighted Earnings Releasesnew stragety for "REE" play
ZT: Biotech cash worksheetall my pick only my personal opinion. if lost money by following, don't blame me
ARNA 4.00 put expires at close Fri, Jul 16, 2010 为什么量那么大?有什么重要的事件安排在那时发布吗?Events and ERs for Week 4/19 - 4/23 (转载)
怎么在stockcharts加两个以上的symbol?我的抄底的想法 (转载)
【Stragety论坛】Trading with correlations. (转载)Trading strategy: Buying Candlestick Bullish Reversal Patt (转载)
Re: 新手的都进来看看吧 (转载)【技术贴-大盘股小组】谈谈今天的大型股
ATPG07/22 收盤行情 -- 美股開高走高 大漲作收
all "REE" stock is buy and holdSpecial Reports Jul 23, 2010
相关话题的讨论汇总
话题: adjclose话题: date话题: symbol话题: str话题: enterprice