用python量化交易 -- 笔记 1

jupyter notebook f:/stock/book/abu-master_20200318

视频教程:
https://v.qq.com/x/page/g0555b9k6ge.html

page181
如果你的Anaconda可以运行,那么试着在cmd中运行 conda install ipython-notebook ,anaconda会把ipython notebook安装在你的电脑上,然后继续在cmd中运行 ipython notebook ,应该就可以了
用python量化交易 -- 笔记 1_第1张图片

import matplotlib.pyplot as plt
from collections import namedtuple
from collections import OrderedDict
from functools import reduce
import numpy as np
plt.plot(a_investor)
import matplotlib.pyplot as plt
from abupy import ABuSymbolPd
tsla_df = ABuSymbolPd.make_kl_df(‘usTSLA’, n_folds=2)
tsla_df.tail()

使用的环境是:
Ipython Notebook
https://github.com/bbfamily/abu
page5
www.hzbook.com
微信公众号:abu_quant

virtualenvwrapper-win
1.安装 virtualenvwrapper-win

pip3 install virtualenvwrapper-win

再次运行workon,目录中没有虚拟环境了,因为默认目录已经改变

https://blog.csdn.net/sophiezjz/article/details/82838951
虚拟环境搭建

IPython与IPython Notebook(Jupyter)安装
https://blog.csdn.net/sophiezjz/article/details/82838951

python -m pip install --upgrade pip

部署
推荐使用Anaconda部署Python环境,详见 量化环境部署

测试
import abupy

https://www.anaconda.com/
https://www.anaconda.com/distribution/#download-section
用python量化交易 -- 笔记 1_第2张图片
Anaconda Prompt(Anaconda3)

我选择了加入系统变量,我们可以看到我的python显示为Anaconda版
anconda使用conda作为包管理工具,也就是anaconda把一些常用的python包统一管理,可以方便的安装、更新和卸载包。
conda常用命令

conda --version 查看conda版本
conda -V
conda --help 获取帮助
conda -h
conda update --help
conda remove --help

Anaconda环境管理
接下来我们先研究单一环境下的Anaconda环境管理。
conda env --help
刚刚系统默认创建了名叫base的默认环境,我们可以使用conda命令查看当前有多少环境
conda env list 查看所有环境
或者
conda info --envs

anaconda ipython notebook 在哪

如果你的Anaconda可以运行,那么试着在cmd中运行 conda install ipython-notebook ,anaconda会把ipython notebook安装在你的电脑上,然后继续在cmd中运行 ipython notebook ,应该就可以了

用python量化交易 -- 笔记 1_第3张图片
用python量化交易 -- 笔记 1_第4张图片
用python量化交易 -- 笔记 1_第5张图片
在这里插入图片描述
用python量化交易 -- 笔记 1_第6张图片
用python量化交易 -- 笔记 1_第7张图片

https://www.cnblogs.com/rangger/p/9520123.html
ipython notebook教程

用python量化交易 -- 笔记 1_第8张图片
用python量化交易 -- 笔记 1_第9张图片
page33

i=1
type(i)
int

f=1.1
type(f)
float

b = ( 1 > 2)
print(b)
type(b)
False
bool


price_str = '30.14, 29.58, 26.36, 32.56, 32.82'
type(price_str)
print(price_str)
30.14, 29.58, 26.36, 32.56, 32.82
 
 
print(id(price_str))
85943664

price_str = price_str.replace(' ','')
print(id(price_str))
print(price_str)
85972352
30.14,29.58,26.36,32.56,32.82


price_array = price_str.split(',')
print(price_array)
['30.14', '29.58', '26.36', '32.56', '32.82']

print('hello')
hello

price_array.append('32.82')
print(price_array)
['30.14', '29.58', '26.36', '32.56', '32.82', '32.82']

print(set(price_array))
{'32.82', '32.56', '26.36', '29.58', '30.14'}

print(price_array)
['30.14', '29.58', '26.36', '32.56', '32.82', '32.82']

price_array.remove('32.82')
print(price_array)
['30.14', '29.58', '26.36', '32.56', '32.82']

date_array = []
date_base = 20170118
for _ in range(0, len(price_array)):
    date_array.append(str(date_base))
    date_base += 1
date_array
['20170118', '20170119', '20170120', '20170121', '20170122']

date_array = []
date_base = 20170118
price_cnt = len(price_array) -1
while price_cnt > 0 :
    date_array.append(str(date_base))
    date_base += 1 
    price_cnt -= 1
date_array
['20170118', '20170119', '20170120', '20170121']


date_base = 20170118
date_array = [str(date_base + ind) for ind, _ in enumerate(price_array)]
date_array
['20170118', '20170119', '20170120', '20170121', '20170122']


stock_tuple_list = [(date,price) for date, price in zip(date_array, price_array)]
stock_tuple_list
[('20170118', '30.14'),
 ('20170119', '29.58'),
 ('20170120', '26.36'),
 ('20170121', '32.56'),
 ('20170122', '32.82')]



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)]
stock_namedtuple_list
[stock(date='20170118', price='30.14'),
 stock(date='20170119', price='29.58'),
 stock(date='20170120', price='26.36'),
 stock(date='20170121', price='32.56'),
 stock(date='20170122', price='32.82')]


stock_dict = {date: price for date, price in zip(date_array, price_array)}
stock_dict
{'20170118': '30.14',
 '20170119': '29.58',
 '20170120': '26.36',
 '20170121': '32.56',
 '20170122': '32.82'}


stock_dict.keys(), stock_dict.values()
(dict_keys(['20170118', '20170119', '20170120', '20170121', '20170122']),
 dict_values(['30.14', '29.58', '26.36', '32.56', '32.82']))


from collections import OrderedDict
stock_dict = OrderedDict(
    (date,price) for date,price in zip(date_array,price_array))

stock_dict.keys()
odict_keys(['20170118', '20170119', '20170120', '20170121', '20170122'])


min(stock_dict)
'20170118'
min(zip(stock_dict.values(),stock_dict.keys()))
('26.36', '20170120')


def find_second_max(dict_array):
    stock_prices_sorted = sorted(zip(dict_array.values(), dict_array.keys()))
    return stock_prices_sorted[-2]

if callable(find_second_max):
    print(find_second_max(stock_dict))
('32.56', '20170121')

find_second_max_lambda = lambda dict_array:\
sorted(zip(dict_array.values(),dict_array.keys() ))[-2]

find_second_max_lambda(stock_dict)
('32.56', '20170121')


def find_max_and_min(dict_array):
    stock_prices_sorted = sorted(
    zip(dict_array.values(), dict_array.keys()) )
    return stock_prices_sorted[0], stock_prices_sorted[-1]
find_max_and_min(stock_dict)
(('26.36', '20170120'), ('32.82', '20170122'))
 

用python量化交易 -- 笔记 1_第10张图片
用python量化交易 -- 笔记 1_第11张图片

你可能感兴趣的:(python)