准备利用rqalpha做一个诊股系统,当然先要将funcat插件调试好,然后即可将同花顺上的易语言搬到rqalpha中使用了,根据一定规则将各股票进行打分,看起来可以勉强使用了。只有一点,得到的数据不够新,一般总是滞后一天,需要将爬取的实时数据保存到系统中,然后利用系统进行诊股。
首先需要考虑如何在ndarray中添加元素,以下为方法,最后将之保存到pandas中,再保存回bcolz数据中
1 单维数组添加
dtype = np.dtype([('date', 'uint32')])
result = np.empty(shape=(3,), dtype=dtype)
result = np.array([20180405,20180406,20180407], dtype=dtype)
result = np.array([20180405,20180406,20180407], dtype=dtype)
result = np.append(result, np.array([20180409], dtype=dtype))
print(result)
print(result['date'])
2 多维数组添加
import numpy as np
dtype = np.dtype([('date', 'uint32'), ('close', 'uint32')])
result = np.empty(shape=(3,), dtype=dtype)
result['date'] = np.array([20180405,20180406,20180407], dtype=np.uint32)
result['close'] = np.array([32, 50, 45], dtype=np.uint32)
print(result)
result = np.append(result, np.array([(20180409, 50)], dtype=dtype))
print(result)
3 字符串相关
import numpy as np
dtype = np.dtype([('date', 'uint32'), ('close', np.uint32), ('name', np.object)])
result = np.empty(shape=(0,), dtype=dtype)
result = np.append(result, np.array([(20180409, 50, "abcdef")], dtype=dtype))
print(result)
4 转成pandas
import pandas as pd
arr = pd.DataFrame(result)
print(arr)
5 多维数组添加
2 的添加方式对于数据量很大的情况下明显速度会很慢,可以采用先预分配空间,再修改数据的方式:
import numpy as np
dtype = np.dtype([('date', 'uint32'), ('close', 'uint32')])
result = np.empty(shape=(2,), dtype=dtype)
result[0] = np.array([(20200401, 33)], dtype=dtype)
print(result)
result[1] = np.array([(20200501, 99)], dtype=dtype)
print(result)