《量化交易之路:用Python做股票量化分析》第2部分

第2章:量化语言–Python
2.1数据结构

*2.1.1基本类型
i = 1
print type(i)
if isinstance(i, str):
print “i is str type”

2.1.2字符串和容器
字符串是不可变对象,对字符串操作会返回新的对象
str = ‘I love you’
print id(str)
str_new = str.replace(’ ', ‘’)
print id(str_new)

2.2函数
zip()函数:同时迭代
dict = {‘key1’:‘value1’, ‘key2’: ‘value2’}
min(zip(dict.values(), dict.keys()))
输出:(‘value1’, ‘key1’)

lambda函数:
y = lambda x: x*x
print y(5)
输出:25

map()函数: 将序列元素代入函数
x_list = [1,2,3,4,5]
y = map( lambda x: x*x , x_list)
print y
输出:[1, 4, 9, 16, 25]

filter()函数:把序列元素代入函数。返回值是True则保留序列元素
def is_odd(n):
return n % 2 == 1
x = [1, 2, 3, 4, 5]
y = filter(is_odd, x)
print y
输出:[1, 3, 5]

reduce()函数:把序列元素代入函数。函数得有两个参数,先对序列中第1,2个参数操作,得到的结果再与第3个操作,依次
x = [1, 2, 3, 4, 5]
y = reduce(lambda a, b: a*b, x)
print y

偏函数:通过partial()设置函数的默认参数
import functools
print int(‘10’, base=10)
print int(‘10’, base=2 )
int2 = functools.partial(int,base = 2)
print int2(‘10’)
输出:10 2 2

2.3面向对象

类和实例
class student(object):
def init(self, name, score):
self.name = name
self.score = score

xiao_ming = student(‘xiao_ming’, 100)
print xiao_ming.name, xiao_ming.score

保护类的属性不被外部访问:在属性前面加上‘__’, 通过方法获取属性,如果直接print xiao_ming.__name或则__score会报错,因为python解释器会把__name改成其他的名字。加一个下划线的属性可以访问,但要注意不要随意访问。

class student(object):
    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def get_name(self):
        return self.__name
    def get_score(self):
        return self.__score

xiao_ming = student('xiao_ming', 100)
try:
    print xiao_ming.__name, xiao_ming.__score
except AttributeError:
    print xiao_ming.get_name()
    print xiao_ming.get_score()
else:
    print 'no error'

继承类:子类要重新初始化属性只能重新定义,不然只能继承父类的__init__

class student(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score

    def get_name(self):
        return self.name
    def get_score(self):
        return self.score

class adult(student):
    def __init__(self, name, score, pay):
        self.pay = pay
        self.name = name
        self.score = score
    def get_pay(self):
        return self.pay

da_ming = adult('da_ming', 100, 10000)
print da_ming.get_name(), da_ming.get_score(), da_ming.get_pay()

输出:da_ming 100 10000

多态: da_ming是student,也是adult
print isinstance(da_ming,student)
print isinstance(da_ming, adult)
输出:True True

@property: 把方法变成属性调用

class student(object):
    @property
    def score_property(self):
        return self._score
    @score_property.setter
    def score_property(self, value):
        if 0<=value<=100:
            self._score = value
        else:
            print 'value must be integer and 0 <= value <=100'

xiao_ming = student()
xiao_ming.score_property = 100
print xiao_ming.score_property

输出:100

@classmethody与@staticmethod

2.4性能效率
itertools的使用
多进程和多线程
使用编译库提高效率

2.5代码调试
print和logging 模块

第3章:量化工具–Numpy

3.1基础操作

初始化

print np.arange(10)
print np.zeros(10)
print np.ones(10)
print np.ones((3,2,4))
print np.linspace(2,10,5)

输出:[0 1 2 3 4 5 6 7 8 9]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]

[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]

[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]]

[ 2. 4. 6. 8. 10.]

类型转换
print np.linspace(2,10,5).astype(int)
输出:[ 2 4 6 8 10]

筛选数据
序列函数
存储和读取

3.2正态分布
3.3伯努利分布

第4章:量化工具–Pandas
4.1基本操作方法
4.2基本数据分析示例
4.3实例1:寻找股票异动涨跌幅阀值
4.4实例2:星期几第这个股票的“好日子”
4.5实例3:跳空缺口
4.6 pandas三维面板的使用

第5章:量化工具–可视化
5.1使用Matplotlib可视化数据
5.2使用Bokeh交互可视化
5.3使用pandas可视化数据
5.4使用Seaborn可视化数据
5.5实例1:可视化量化策略的交易区间及卖出原因
5.6实例2:标准化两个股票的观察周期
5.7实例3: 黄金分割线
5.8技术指标的可视化

第6章:量化工具–数学
6.1回归与插值
6.2蒙特卡罗方法与凸优化
6.3线性代数

猜你喜欢

转载自blog.csdn.net/q386538588/article/details/86690019