量化交易 1.基础语法与数据结构

#基本类型
'''
def xrange(x):
    n=0
    while n<x:
        yield n
        n+=1
'''

i=1;f=1.1;b=(1>2);
price_str = "30.14,20.18,22.88,45.99"
print(1,b)
print(2,type(i),type(f),type(b),type(price_str))

#基本语法 if else ellif while for def class
#isinstance()函数被用来验证某个对象是否是某个类型
#逻辑运算与或非,and,or not区别其他语言&& || !

if not isinstance(price_str,str):
    #not 代表逻辑非,如果不是字符串,转换为字符串
   price_str = str(price_str)
print(3,price_str)

'''
if isinstance(price_str,int) and price_str > 0:
    #and代表逻辑与,如果是int类型且是正数
   #price_str += 1
elif isinstance(price_str,float) or price_str < 0:
    #or 代表逻辑或,如果是float或者小于0
   price_str += 1.0
else:
    raise TypeError('price_str is str type!')
'''

#字符串和容器
print(4,'旧的price_str id = {}'.format(id(price_str)))
price_str = price_str.replace(' ','')
print (5,'新的price_str id = {}'.format(id(price_str)))
print(6,price_str)

#容器
#列表是一种有序的容器,可以对元素进行增,删,改操作
#split以逗号分隔字符串,返回数组price_array
price_array = price_str.split(',')
print(7,price_array)
#price_array尾部append一个重复的32.82
price_array.append('45.99')
print(8,price_array)
#集合是一个无序的容器,且集合中的元素无重复
set(price_array)
print(9,price_array)
price_array.remove('45.99')
print(10,price_array)

#循环控制
#需求1:收盘价+日期
date_array = []
date_base = 20170118
'''
#这里用for只是为了计数,无用的变量Python建议使用‘_'声明
for _ in xrange(0,len(price_array)):
    date_array.append(str(date_base))
    #本节只是简单的实例,不考虑日期的进位
    date_base +=1
print(11,date_array)
'''
price_cnt = len(price_array)-1
while price_cnt >0:
    date_array.append(str(date_base))
    date_base+=1
    price_cnt -=1
print(12,date_array)

#4列表推导式
date_base= 20170119
date_array = [str(date_base+ind) for ind,_ in enumerate(price_array)]
print(13,date_array)
#元组tuple
stock_tuple_list = [(date,price) for date,price in zip(date_array,price_array)]
#tuple 访问使用索引
print (14,'20170119日价格:{}'.format(stock_tuple_list[1][1]))
print(15,stock_tuple_list)
'''.format使用‘{}’.format()'''
#5可命名元组:namedtuple
#以上代码通过tuple访问使用索引,欠缺灵活性,可以通过namedtuple改进,代码如下:
from collections  import namedtuple
stock_namedtuple = namedtuple('stock',('date','price'))
stock_namedtuple_list = [stock_namedtuple(date,price) for date,price in zip(date_array,price_array)]
#nametuple 访问使用price
print(16,'20170119日价格:{}'.format(stock_namedtuple_list[1].price))
print(17,stock_namedtuple_list)

#6使用字典推导式
#使用字典dict来完成这个任务,同样使用字典推导式完成,代码跟简洁
#字典推导式:{key:value for in }
stock_dict = {date:price for date,price in zip(date_array,price_array)}
print(18,'20170119日价格:{}'.format(stock_dict['20170119']))
print(19,stock_dict)
#dict使用key-value存储,特点是根据key查询value,速度快,使用keys()和values()函数可以分别返回字典中的key列表与value列表

print(20,stock_dict.keys(),21,stock_dict.values())

#7有序字典:OrderedDict
#仔细观察上面的keys()函数可以发现,其返回的结果和之前date_array的顺序并没有保持一致,字典的存储是无序的
#需求2:下面需要这个字典按照时间顺序返回。所以这里不能用普通dict,应该使用OrderedDict,它将按照插入的顺序排列,代码如下:
from collections import OrderedDict
stock_dict = OrderedDict((date,price) for date,price in zip(date_array,price_array))
print(21,stock_dict.keys())

输出如下
1 False
2 <class 'int'> <class 'float'> <class 'bool'> <class 'str'>
3 30.14,20.18,22.88,45.99
4 旧的price_str id = 2351468010568
5 新的price_str id = 2351468010568
6 30.14,20.18,22.88,45.99
7 ['30.14', '20.18', '22.88', '45.99']
8 ['30.14', '20.18', '22.88', '45.99', '45.99']
9 ['30.14', '20.18', '22.88', '45.99', '45.99']
10 ['30.14', '20.18', '22.88', '45.99']
12 ['20170118', '20170119', '20170120']
13 ['20170119', '20170120', '20170121', '20170122']
14 20170119日价格:20.18
15 [('20170119', '30.14'), ('20170120', '20.18'), ('20170121', '22.88'), ('20170122', '45.99')]
16 20170119日价格:20.18
17 [stock(date='20170119', price='30.14'), stock(date='20170120', price='20.18'), stock(date='20170121', price='22.88'), stock(date='20170122', price='45.99')]
18 20170119日价格:30.14
19 {'20170119': '30.14', '20170120': '20.18', '20170121': '22.88', '20170122': '45.99'}
20 dict_keys(['20170119', '20170120', '20170121', '20170122']) 21 dict_values(['30.14', '20.18', '22.88', '45.99'])
21 odict_keys(['20170119', '20170120', '20170121', '20170122'])

Process finished with exit code 0

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值