月度归档:2014年05月

微信公众号公众平台开发模式

先要申请成为开发者,申请中,需要验证接口信息,上面写的很明白,就是一个80端口的网页+一个令牌,但是我把例子上传网站后,验证失败,显示不通过,大概意思是信息不匹配啥的。

但是帮助文档里没有什么特别说明,令牌也只说是自己设定的,买再说其它的。真是让人头疼!

后来看例程源代码,php的我也看不太懂。比如这句:define(“TOKEN”, “weixin”);

一开始我以为令牌就是TOKEN,结果不通过

然后把验证网页令牌写成weixin,通过了。 这样看来,这个define就相当于TOKEN=‘weixin’的副职语句了

建议微信文档中,应该标出来那里,告诉用户例程里的字符串要修改成网页验证里自己输入的令牌!

接入指南见这里:

http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97

公众号可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在开发模式中获得(需要已经成为开发者,且帐号没有异常状态)。注意调用所有微信接口时均需使用https协议。

后来看了下,好像订阅号需要认证才能拿到APPID和AppSecret

用scikits-learn对沪深300成份股做聚类分析

代码:

import datetime
import numpy
import sklearn.cluster
from matplotlib import finance
#1. Download price data

# 2011 to 2012
start = datetime.date(2013,5,15)
end = datetime.date(2014,5,15)
a='''0000001
0000002
0000009
0000012
0000024
0000039
0000046
0000060
0000061
0000063
0000069
0000100
0000156
0000157
0000333
0000338
0000400
0000401
0000402
0000423
0000425
0000528
0000536
0000538
0000562
0000568
0000581
0000596
0000598
0000623
0000625
0000629
0000630
0000651
0000656
0000686
0000703
0000709
0000718
0000725
0000728
0000729
0000750
0000758
0000768
0000776
0000778
0000783
0000792
0000793
0000800
0000826
0000831
0000839
0000858
0000869
0000876
0000878
0000895
0000933
0000937
0000960
0000961
0000963
0000970
0000983
0000999
0002001
0002007
0002024
0002038
0002051
0002065
0002069
0002081
0002106
0002129
0002142
0002146
0002155
0002202
0002230
0002236
0002241
0002269
0002294
0002299
0002304
0002310
0002344
0002353
0002375
0002385
0002399
0002415
0002422
0002431
0002450
0002456
0002500
0002570
0002594
0002603
0002653
0002673
1600000
1600008
1600009
1600010
1600011
1600015
1600016
1600018
1600019
1600027
1600028
1600029
1600030
1600031
1600036
1600048
1600050
1600058
1600060
1600062
1600066
1600068
1600079
1600085
1600089
1600096
1600100
1600104
1600108
1600109
1600111
1600115
1600118
1600123
1600143
1600150
1600153
1600157
1600160
1600166
1600170
1600177
1600188
1600196
1600208
1600216
1600219
1600221
1600252
1600256
1600259
1600266
1600267
1600271
1600276
1600309
1600315
1600316
1600332
1600340
1600348
1600352
1600362
1600369
1600372
1600376
1600383
1600395
1600403
1600406
1600415
1600436
1600489
1600497
1600498
1600516
1600518
1600519
1600528
1600535
1600546
1600547
1600549
1600582
1600583
1600585
1600588
1600597
1600598
1600600
1600637
1600642
1600648
1600649
1600655
1600660
1600663
1600664
1600674
1600688
1600690
1600694
1600703
1600705
1600718
1600739
1600741
1600783
1600795
1600804
1600809
1600811
1600827
1600832
1600837
1600839
1600859
1600863
1600873
1600875
1600886
1600887
1600893
1600895
1600900
1600970
1600971
1600997
1600999
1601001
1601006
1601009
1601018
1601088
1601098
1601099
1601101
1601106
1601111
1601117
1601118
1601139
1601158
1601166
1601168
1601169
1601186
1601231
1601238
1601258
1601288
1601299
1601318
1601328
1601333
1601336
1601369
1601377
1601390
1601398
1601555
1601600
1601601
1601607
1601618
1601628
1601633
1601666
1601668
1601669
1601688
1601699
1601717
1601766
1601800
1601808
1601818
1601857
1601866
1601888
1601898
1601899
1601901
1601918
1601928
1601933
1601939
1601958
1601988
1601989
1601991
1601992
1601998
1603000
1603993'''
#print(a)
aaa='''0000001
0000002
0000009
0000012
0000024
0000039
0000046
0000060
0000061
0000063
0000069
0000100
0000156
0000157
0000333
0000338
0000400
0000401
0000402
0000423
0000425
0000528
0000536
0000538
0000562
0000568
0000581
0000596
0000598
0000623
0000625
0000629
0000630
0000651
0000656
0000686
0000703
0000709
0000718
0000725
0000728
0000729
0000750
0000758
0000768
0000776
0000778
0000783
0000792
0000793
0000800
0000826
0000831
0000839
0000858
0000869
0000876
0000878
0000895
0000933
0000937
0000960
0000961
0000963
0000970
0000983
0000999
0002001
0002007
0002024
0002038
0002051
0002065
0002069
0002081
0002106
0002129
0002142
0002146
0002155
0002202
0002230
0002236
0002241
0002269
0002294
0002299
0002304
0002310
0002344
0002353
0002375
0002385
0002399
0002415
0002422
0002431
0002450
0002456
0002500
0002570
0002594
0002603
0002653
0002673'''

bbb="""1600000
1600008
1600009
1600010
1600011
1600015
1600016
1600018
1600019
1600027
1600028
1600029
1600030
1600031
1600036
1600048
1600050
1600058
1600060
1600062
1600066
1600068
1600079
1600085
1600089
1600096
1600100
1600104
1600108
1600109
1600111
1600115
1600118
1600123
1600143
1600150
1600153
1600157
1600160
1600166
1600170
1600177
1600188
1600196
1600208
1600216
1600219
1600221
1600252
1600256
1600259
1600266
1600267
1600271
1600276
1600309
1600315
1600316
1600332
1600340
1600348
1600352
1600362
1600369
1600372
1600376
1600383
1600395
1600403
1600406
1600415
1600436
1600489
1600497
1600498
1600516
1600518
1600519
1600528
1600535
1600546
1600547
1600549
1600582
1600583
1600585
1600588
1600597
1600598
1600600
1600637
1600642
1600648
1600649
1600655
1600660
1600663
1600664
1600674
1600688
1600690
1600694
1600703
1600705
1600718
1600739
1600741
1600783
1600795
1600804
1600809
1600811
1600827
1600832
1600837
1600839
1600859
1600863
1600873
1600875
1600886
1600887
1600893
1600895
1600900
1600970
1600971
1600997
1600999
1601001
1601006
1601009
1601018
1601088
1601098
1601099
1601101
1601106
1601111
1601117
1601118
1601139
1601158
1601166
1601168
1601169
1601186
1601231
1601238
1601258
1601288
1601299
1601318
1601328
1601333
1601336
1601369
1601377
1601390
1601398
1601555
1601600
1601601
1601607
1601618
1601628
1601633
1601666
1601668
1601669
1601688
1601699
1601717
1601766
1601800
1601808
1601818
1601857
1601866
1601888
1601898
1601899
1601901
1601918
1601928
1601933
1601939
1601958
1601988
1601989
1601991
1601992
1601998
1603000
1603993"""
ccc="""1600376
1600383
1600395
1600403
1600406
1600415
1600436
1600489
1600497
1600498
1600516
1600518
1600519
1600528
1600535
1600546
1600547
1600549
1600582
1600583
1600585
1600588
1600597
1600598
1600600
1600637
1600642
1600648
1600649
1600655
1600660
1600663
1600664
1600674
1600688
1600690
1600694
1600703
1600705
1600718
1600739
1600741
1600783
1600795
1600804
1600809
1600811
1600827
1600832
1600837
1600839
1600859
1600863
1600873
1600875
1600886
1600887
1600893
1600895
1600900
1600970
1600971
1600997
1600999"""
b=aaa.split('n')
#print(b)
c=[]
#print(b)
for x in b:
    #print (x,x[0],x[1:7])
    if x[0]=='0':
        temp=x[1:] + ".SZ"
        #print(temp)
        c.append(temp)
    if x[0]=="1":
        temp=x[1:]+".SS"
        c.append (temp)
#print(c)
#Dow Jones symbols
symbols = c

quotes = [finance.quotes_historical_yahoo(symbol, start, end, asobject=True)
          for symbol in symbols]

close = numpy.array([q.close for q in quotes]).astype(numpy.float)
print (close.shape)

#2. Calculate affinity matrix
logreturns = numpy.diff(numpy.log(close))
print (logreturns.shape)

logreturns_norms = numpy.sum(logreturns ** 2, axis=1)
S = - logreturns_norms[:, numpy.newaxis] - logreturns_norms[numpy.newaxis, :] + 2 * numpy.dot(logreturns, logreturns.T)

#3. Cluster using affinity propagation
aff_pro = sklearn.cluster.AffinityPropagation().fit(S)
labels = aff_pro.labels_
print(labels)
for i in range(len(labels)):
    print ('%s in Cluster %d' % (symbols[i], labels[i]))
temp=[]

输出:

[ 0  2 13  9  7  9  9  2 16 13  2  2  1  2  3  2 13  7  9  9  2  2  4  4  5
  9 10 16  9  2 13  2  2  9  7  6 13  2  7  9  2  2  9  2  9  2  2  2  2 16
 13  4 16  9  2  2  9  2  4  2  2  9  7  4  8  2  4  9  9 10 11 16 16 13 12
 13 10  2  7 16 13 14  4 15 16  4 13  9 17 18 11 16 19  9 20  9 13 13 21  2
 22 16 11 10  2]
000001.SZ in Cluster 0
000002.SZ in Cluster 2
000009.SZ in Cluster 13
000012.SZ in Cluster 9
000024.SZ in Cluster 7
000039.SZ in Cluster 9
000046.SZ in Cluster 9
000060.SZ in Cluster 2
000061.SZ in Cluster 16
000063.SZ in Cluster 13
000069.SZ in Cluster 2
000100.SZ in Cluster 2
000156.SZ in Cluster 1
000157.SZ in Cluster 2
000333.SZ in Cluster 3
000338.SZ in Cluster 2
000400.SZ in Cluster 13
000401.SZ in Cluster 7
000402.SZ in Cluster 9
000423.SZ in Cluster 9
000425.SZ in Cluster 2
000528.SZ in Cluster 2
000536.SZ in Cluster 4
000538.SZ in Cluster 4
000562.SZ in Cluster 5
000568.SZ in Cluster 9
000581.SZ in Cluster 10
000596.SZ in Cluster 16
000598.SZ in Cluster 9
000623.SZ in Cluster 2
000625.SZ in Cluster 13
000629.SZ in Cluster 2
000630.SZ in Cluster 2
000651.SZ in Cluster 9
000656.SZ in Cluster 7
000686.SZ in Cluster 6
000703.SZ in Cluster 13
000709.SZ in Cluster 2
000718.SZ in Cluster 7
000725.SZ in Cluster 9
000728.SZ in Cluster 2
000729.SZ in Cluster 2
000750.SZ in Cluster 9
000758.SZ in Cluster 2
000768.SZ in Cluster 9
000776.SZ in Cluster 2
000778.SZ in Cluster 2
000783.SZ in Cluster 2
000792.SZ in Cluster 2
000793.SZ in Cluster 16
000800.SZ in Cluster 13
000826.SZ in Cluster 4
000831.SZ in Cluster 16
000839.SZ in Cluster 9
000858.SZ in Cluster 2
000869.SZ in Cluster 2
000876.SZ in Cluster 9
000878.SZ in Cluster 2
000895.SZ in Cluster 4
000933.SZ in Cluster 2
000937.SZ in Cluster 2
000960.SZ in Cluster 9
000961.SZ in Cluster 7
000963.SZ in Cluster 4
000970.SZ in Cluster 8
000983.SZ in Cluster 2
000999.SZ in Cluster 4
002001.SZ in Cluster 9
002007.SZ in Cluster 9
002024.SZ in Cluster 10
002038.SZ in Cluster 11
002051.SZ in Cluster 16
002065.SZ in Cluster 16
002069.SZ in Cluster 13
002081.SZ in Cluster 12
002106.SZ in Cluster 13
002129.SZ in Cluster 10
002142.SZ in Cluster 2
002146.SZ in Cluster 7
002155.SZ in Cluster 16
002202.SZ in Cluster 13
002230.SZ in Cluster 14
002236.SZ in Cluster 4
002241.SZ in Cluster 15
002269.SZ in Cluster 16
002294.SZ in Cluster 4
002299.SZ in Cluster 13
002304.SZ in Cluster 9
002310.SZ in Cluster 17
002344.SZ in Cluster 18
002353.SZ in Cluster 11
002375.SZ in Cluster 16
002385.SZ in Cluster 19
002399.SZ in Cluster 9
002415.SZ in Cluster 20
002422.SZ in Cluster 9
002431.SZ in Cluster 13
002450.SZ in Cluster 13
002456.SZ in Cluster 21
002500.SZ in Cluster 2
002570.SZ in Cluster 22
002594.SZ in Cluster 16
002603.SZ in Cluster 11
002653.SZ in Cluster 10
002673.SZ in Cluster 2

现在的问题是:

1 不知道为什么,沪深300 的时候出错,而且是报错1600887 问题这个应该已经被转成600887.SS了啊! 单独换成上海的100多只股票,也是出错

2 对股票数太多的情况,应该修改输出形式,即一个Cluster 后面跟上所属的所有股票代码!

 

 

统计历史上某天的当日涨跌幅度

代码:

from datetime import date
import datetime
date1 = datetime.date( 2014, 4,28 )
date2 = datetime.date( 2014,5,4 )
#print(date1)
#rint(type(date1))
testd=datetime.date(1990,12,19)
testd=datetime.date(testd.year,testd.month,testd.day+1)
#print(testd)
lista=[]

# for line in open("D:/快盘/work/python/SH999999.csv"):
#     year1,so1,sh1,sl1,sc1,aa1,bbbb = line.split(",")
#     test=(year1,so1,sh1,sl1,sc1)
#     #print(test)
#     if year1==str(testd) or year1==str(date1):
#         pass
#         #print(year1,test)
da512=[]
for line in open("D:/快盘/work/python/SH999999.csv"):
    year1,so1,sh1,sl1,sc1,aa1,bbbb = line.split(",")
    test=(year1,so1,sh1,sl1,sc1,aa1,bbbb)
    #print(test)
    ii=0
    if year1.split('-')[1]=='05' and year1.split('-')[2]=='12':
        ii=ii+1
        #print(test)
        da512.append(test)

zs=0.0
zf=0.0
print(type(zs),type(zf))
for ld in da512:
#     if zs

 

输出:

>>> (executing lines 1 to 43 of “上证数据读.py”)

<class ‘float’> <class ‘float’>

1992-05-12 当日涨幅0.74%

1993-05-12 当日涨幅0.15%

1994-05-12 当日涨幅-0.68%

1995-05-12 当日涨幅2.72%

1997-05-12 当日涨幅0.64%

1998-05-12 当日涨幅-0.69%

1999-05-12 当日涨幅0.30%

2000-05-12 当日涨幅-0.30%

2003-05-12 当日涨幅0.58%

2004-05-12 当日涨幅2.25%

2005-05-12 当日涨幅-1.62%

2006-05-12 当日涨幅4.56%

2008-05-12 当日涨幅2.21%

2009-05-12 当日涨幅2.18%

2010-05-12 当日涨幅1.05%

2011-05-12 当日涨幅-0.73%

 

编程中一度想加上当日对前一日收盘价的涨跌,但是思路错了,变成了对去年的收盘价的涨跌了,同时编程中还碰到了一系列的问题:

1 浮点数==0问题,后来是用两种方法解决: int(浮点数)==0 或者 浮点数< 10 来解决。因为指数不会遇到< 10 的情况

2 最主要的就是逻辑错了,对前一日的涨跌,需要得到前一日的收盘价,这一点在yahoo上比较容易,可以直接取,在通达信数据里,则需要费点周折:

1) 把历史数据全部补上,休息日按无交易日处理

2) 取中间量,顺序遍历的时候,顺便用来存取前一日的收盘价。

3) 直接逻辑处理后,拿前一天的数据!

使用pyzo过程中,碰到输入时光标消失的问题,很讨厌

另外还碰到自动写的问题,比如输入abc,结果变成abcabc。。。。

通达信导出日K线数据测试

yahoo的上证指数日线数据有问题,没法用,所以转而选择通达信的数据。

刚开始还费力去编程转换通达信的存盘数据,后来发现,它竟然有导出功能,直接就能导出从1990.12.19日开始的日K线数据,真不错!

我倒出了csv格式,这样python处理起来就更简单了!选择的日线数据,精确复权(指数这里非必要,但个股一定要),文件名xxxxxx.csv,用逗号分隔,“生成导出头部”不勾选,日期格式yyyy-mm-dd .

导出后就是这样的格式:

2014-5-6,2024.26,2038.7,2021.49,2028.04,74609411,57548111872

2014-5-7,2023.15,2024.63,2008.45,2010.08,74360192,57558052864

2014-5-8,2006.85,2036.94,2005.69,2015.27,77865393,59529367552

2014-5-9,2016.5,2020.45,2011.78,2017.67,17104686,12630095872

分别是:日期,开盘,最高,最低,收盘,成交量,成交金额 (注意因为是今天导出,还没盘后维护,5.9日的数据只是盘中数据)

然后编程进行读取测试:

from datetime import date
import datetime
date1 = datetime.date( 2014, 4,28 )
date2 = datetime.date( 2014,5,4 )
#print(date1)
#rint(type(date1))
testd=datetime.date(1990,12,19)
testd=datetime.date(testd.year,testd.month,testd.day+1)
#print(testd)
lista=[]

for line in open("D:/快盘/work/python/SH999999.csv"):
    year1,so1,sh1,sl1,sc1,aa1,bbbb = line.split(",")
    test=(year1,so1,sh1,sl1,sc1)
    #print(test)
    if year1==str(testd) or year1==str(date1):
        print(year1)

输出:

>>> (executing lines 1 to 17 of “上证数据读.py”)

1990-12-20

2014-04-28

这就表明读取成功了!
但是中间也有波折,一直提示:

Traceback (most recent call last):

File “D:快盘workpython上证数据读.py”, line 13, in <module>

year1,so1,sh1,sl1,sc1,aa1,bbbb = line.split(“,”)

ValueError: need more than 1 value to unpack

后来通过debug,发现读完5.9日的数据之后才报错,仔细看,才发现数据文件最后一行写着:“数据来源通达信”

原来就是因为这句话报错的啊!浪费了我不少的时间…..

5.8 日涨跌统计程序

先看结果,看看大家能看懂不?

000001.SZ

2013-05-08 0.16 %

2012-05-08 -0.10 %

2009-05-08 2.81 %

2008-05-08 4.00 %

2007-05-08 3.97 %

2006-05-08 -0.26 %

 

000002.SZ

2013-05-08 0.17 %

2012-05-08 -0.33 %

2009-05-08 8.13 %

2008-05-08 3.67 %

2007-05-08 6.56 %

2006-05-08 8.78 %

 

600000.SS

2013-05-08 -0.69 %

2012-05-08 -0.53 %

2009-05-08 1.41 %

2008-05-08 2.39 %

2007-05-08 1.71 %

2006-05-08 0.00 %

 

600028.SS

2013-05-08 -0.17 %

2012-05-08 -0.49 %

2009-05-08 4.57 %

2008-05-08 1.95 %

2007-05-08 -3.31 %

2006-05-08 7.10 %

 
解释一下,ss代表上海交易所,SZ代表深圳交易所,所以上面是4只股票:深发展/万科/浦发银行/中石化

计算了他们近10年在5.8日那天,收盘价减去开盘价的数值,除以开盘价,即当天收盘价相对开盘价的涨跌幅!

由于yahoo数据原因,无法拿到上证指数的数据,同时浦发银行2006.5.8日是停盘的。还有就是碰到5.1长假而5.8日正好休市的情况下,数据也是不对的。即2003.5.8日的数据是不对的,已经提前去掉了。

 

基本可以看到5.8日上涨几率大约为2/3 .朋友们也可以去统计下上证指数的涨跌!

 

源码见这里:

from pylab import figure, show
from matplotlib.finance import quotes_historical_yahoo
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
import datetime
from datetime import date

date1 = datetime.date( 2012, 5,8 )
date2 = datetime.date( 2012,5,8 )
#date1=date.today()
#date2=date.today()

#daysFmt = DateFormatter('%m-%d-%Y')

#quotes = quotes_historical_yahoo('MFST',date1,date2)
gupiao=('000001.SZ','000002.SZ','600000.SS','600028.SS')
for j in gupiao:
    gu=j
    print(gu,)
    start=datetime.date(2013,5,8)
    #print (start,)
    for i in range(10):
        #print(i,)
        start1=datetime.date(start.year-i,start.month,start.day)
        end=datetime.date(start.year-i,start.month,start.day+1)
        #print(start1,)
        quotes = quotes_historical_yahoo(gu,start1,start1)
        if quotes:
            shouyang=[(q[1],q[2]) for q in quotes]
            zhenfu=(shouyang[0][1]-shouyang[0][0])/shouyang[0][0]
            #print (start1,shouyang,(shouyang[0][1]-shouyang[0][0])/shouyang[0][0])
            print ('%s %.2f %%' %(start1,zhenfu*100))
    print ('r')

yahoo 的数据比想象中要难用点,关键是:

1 无法拿到有效的上证指数数据,不知道是不是时间不对….

2 有时候停牌日或节假日,会有错误的数据。

 

autopy初探

想让python帮自己干点力气活,于是找到了autopy

AutoPy Introduction and Tutorial

还有这篇:

用Python制作游戏外挂

 

这就是一个移动鼠标的例子:

import autopy
import math
import time
import random

TWO_PI = math.pi * 2.0
def sine_mouse_wave():
    """
    Moves the mouse in a sine wave from the left edge of
    the screen to the right.
    """
    width, height = autopy.screen.get_size()
    print(width,height)
    height /= 2
    height -= 10 # Stay in the screen bounds.

    for x in xrange(width):
        y = int(height * math.sin((TWO_PI * x) / width) + height)
        autopy.mouse.move(x, y)
        time.sleep(random.uniform(0.001, 0.003))

sine_mouse_wave()
autopy.mouse.smooth_move(1280-1,1024-1)

我的屏幕是1280×1024的。

 

晚上又看了一下,按键方面主要有如下几个可能的应用:

key.type_string(string, wpm=0)

例子:>>> autopy.key.type_string(“hello”,120)

 

key.tap(key, [modifiers])

Convenience wrapper around toggle() that holds down and then releases the given key and modifiers.

例子:>>> key.tap(‘a’)

>>> key.tap(key.K_META)

 

鼠标操作:

mouse.get_pos()

Returns a tuple (x, y) of the current mouse position.

例子:

>>> mouse.get_pos()
(688, 775)

移动鼠标:

mouse.move(x, y)

Moves the mouse to the given (x, y) coordinate.

Exceptions:
  • ValueError is thrown if the point is out of index.

mouse.smooth_move(x, y)

Smoothly moves the mouse to the given (x, y) coordinate in a straight line.

Exceptions:
  • ValueError is thrown if the point is out of index.

上面那个程序就是移动鼠标的例子

 

鼠标点击:mouse.click(button=LEFT_BUTTON)

Convenience wrapper around toggle() that holds down and then releases the given mouse button.

例子:

>>> mouse.click()

 

screen.get_size()

Returns a tuple (width, height) of the size of the main screen.

例子:

>>> screen.get_size()
(1440, 900)

 

screen.get_color(x, y)

Returns hexadecimal value describing the RGB color at the given point.

例子:

>>> screen.get_color(0,0)
15132899

>>> color.hex_to_rgb(15132899)
(230, 232, 227)

 

在系统中可以直接help:help(autopy) >>> help(autopy.bitmap.Bitmap)

图片操作方面:

To save the screen capture to a file, we can use:

>>> import autopy
>>> autopy.bitmap.capture_screen().save('screengrab.png')
>>> import autopy
>>> autopy.bitmap.Bitmap.open('i-am-a-monkey-and-i-like-it.png')
<Bitmap object at 0x1001d5378>

http://www.autopy.org/documentation/api-reference/