目录

1、模块、包和库

模块

2、Python标准库介绍

3、Python内置数据类型与操作

List类型与操作 

Tuple类型与操作

Range类型与操作

字符串操作

字典类型操作

集合操作

习题


1、模块、包和库

模块

模块是.py为后缀的文件。

该文件定义了一些常量和函数。以满足某种功能。

举例:

做一个处理价格序列的模块

def OpenPrice(priceSeq):
    Open = priceSeq[0]
    return(Open)
   
def HighPrice(priceSeq):
    High = priceSeq[0]
    for price in priceSeq:
        if price > High:
            High = price;
    return(High)
def LowPrice(priceSeq):
    Low = priceSeq[0]
    for price in priceSeq:
        if price < Low:
            Low = price;
    return(Low)
   
def ClosePrice(priceSeq):
    Close = priceSeq[-1]
    return(Close)

python量化回测代码 python 量化入门_学习

生成价格序列,然后查询开高低收

python量化回测代码 python 量化入门_学习_02

简化一下查询开高低收的代码

写一个c9_pmath的模块

def findMax(seq):
    maxValue=seq[0]
    for a in seq:
        if a > maxValue:
            maxValue = a
    return(maxValue)
   
def findMin(seq):
    minValue=seq[0]
    for a in seq:
        if a < minValue:
            minValue = a
    return(minValue)

python量化回测代码 python 量化入门_python量化回测代码_03

再重新写一个查询开高低收的模块c9_priceAnalysis2

引入c9_math这个模块

最高价和最低价的查询部分,就实现了代码的优化和简化

import c9_pmath as pmath
def OpenPrice(priceSeq):
    Open = priceSeq[0]
    return(Open)
   
def HighPrice(priceSeq):
    High = pmath.findMax(priceSeq)
    return(High)
def LowPrice(priceSeq):
    Low = pmath.findMin(priceSeq)
    return(Low)
   
def ClosePrice(priceSeq):
    Close = priceSeq[-1]
    return(Close)

python量化回测代码 python 量化入门_学习_04

最后做一下测试:

import c9_priceAnalysis2 as pa2
priceSeq=[19,18,20,22,17,21]
pa2.OpenPrice(priceSeq)
pa2.HighPrice(priceSeq)
pa2.LowPrice(priceSeq)
pa2.ClosePrice(priceSeq)

python量化回测代码 python 量化入门_开发语言_05

结果完美。

包,体现了模块的结构化管理的思想。

包(本质上可以理解为一个文件夹)由具有相关联的功能的模块文件构成。

比如,处理股票的一个包,处理期货的一个包

包文件由__init__.py和其他py文件组成。

同样可以由import语句来导入包和包中的模块

没有特别的定义,在Python中强调其功能性。

模块由函数构成

包由模块构成

库由包、模块、函数构成。

2、Python标准库介绍

Python自带的函数库。

介绍了内置函数、内置常量、内置数据类型、内置异常,和其他功能的函数组的模块。

这个没啥好说的,照着书上的代码打一遍就行。

3、Python内置数据类型与操作

List类型与操作 

通过list()或[ ]可以创建空列表。

用append()添加元素,一次只能添加一个。如果添加一个List,则这个List会作为一个元素。

python量化回测代码 python 量化入门_开发语言_06

用extend()添加多个元素,将元素进行迭代后,进行添加。

python量化回测代码 python 量化入门_python量化回测代码_07

用+来添加元素,创建一个新的list,不改变原有List

python量化回测代码 python 量化入门_开发语言_08

用+=来添加元素,创建一个新的list,并将其赋值给原来的变量。

python量化回测代码 python 量化入门_学习_09

插入列表元素

使用insert()来插入,第一个参数为索引值,第二个参数为要插入的元素。

python量化回测代码 python 量化入门_python_10

删除列表元素

使用pop(),删除list的最后一个元素,并输出被删除的元素。

python量化回测代码 python 量化入门_python量化回测代码_11

使用pop(i),i为列表索引值。指定删除该索引的元素。并返回该元素。

python量化回测代码 python 量化入门_python_12

使用remove(),删除制定的元素。若元素不存在,则报错。

python量化回测代码 python 量化入门_学习_13

在使用remove之前,先使用count来统计要删除的元素是否存在于列表中。

若count的返回值为0,则美必要使用remove来删除。

python量化回测代码 python 量化入门_学习_14

使用sort()对列表进行升序排列

使用reverse()对列表进行降序排列。

使用index(x,start, end)来对列表进行切片。

X返回x元素第一次出现的索引值。

Start,end生成对应索引值的切片。

Tuple类型与操作

Tuple属于序列类型,具有不可变的性质。

使用tuple(), ,或()三种方法都可以创建tuple类型数据

python量化回测代码 python 量化入门_开发语言_15

Tuple类型的数据可以进行迭代。

python量化回测代码 python 量化入门_开发语言_16

python量化回测代码 python 量化入门_学习_17

Range类型与操作

使用range生成等间隔的整数序列

Range(start, stop, step),start,初始值,stop,结束之,setp步长

Range的值无法直接查看

使用list或tuple等序列的方式,可以显示

python量化回测代码 python 量化入门_python量化回测代码_18

修改步长为2

python量化回测代码 python 量化入门_学习_19

字符串操作

用单引号,双引号,三引号,str()创建字符串。

python量化回测代码 python 量化入门_python_20

创建的字符串中,包含单引号或双引号时,要放在三引号中。

python量化回测代码 python 量化入门_Python_21

用str来创建字符串

python量化回测代码 python 量化入门_Python_22

用len查询字符串长度

python量化回测代码 python 量化入门_python量化回测代码_23

字符串切片,可以用序列索引来操作。

python量化回测代码 python 量化入门_python_24

用+来连接字符串

python量化回测代码 python 量化入门_python_25

用split来分割字符串,默认以空格进行分割。

python量化回测代码 python 量化入门_学习_26

其他的都是一些简单的字符串操作。需要用的时候,临时查一下语法即可。

字典类型操作

用花括号{}或dict()来创建字典。

python量化回测代码 python 量化入门_python量化回测代码_27

字典的item, key 和value查询

python量化回测代码 python 量化入门_python量化回测代码_28

Key和value都有可迭代性质。都可以用list转变成列表类型。

python量化回测代码 python 量化入门_python_29

key做为索引,使用get来查询对应的值

python量化回测代码 python 量化入门_开发语言_30

使用update来进行更新

python量化回测代码 python 量化入门_python_31

集合操作

集合有set()和frozenset()两种类型。Set可变,frozenset不可变。

python量化回测代码 python 量化入门_学习_32

python量化回测代码 python 量化入门_开发语言_33

python量化回测代码 python 量化入门_Python_34

使用花括号{ }来创建set

python量化回测代码 python 量化入门_学习_35

使用add()方法来增加元素

使用remove()删除特定元素

Frozenset由于是不可变集合,所以不能使用add和remove

python量化回测代码 python 量化入门_python_36

用len()来查询集合中元素的个数(不重复的)

python量化回测代码 python 量化入门_学习_37

使用for循环,对集合元素进行迭代

python量化回测代码 python 量化入门_python量化回测代码_38

集合运算包括:

合并:union

交集:intersection

差集:difference

是否属于:<

习题

1、

python量化回测代码 python 量化入门_python量化回测代码_39

2、

3、list(range(2,102,2))

4、

python量化回测代码 python 量化入门_开发语言_40