Python笔记17-Dma指标算法

项目中要求求出Dma

def dma(X,A):
	pass

X是一个数组,A是一个数组,或者是数字
dma的输出值也要是数组
目前是通过reduce算出最后一个值,然后加入到历史数组里面去,最后返回历史数组
可是因为一些原因不能使用历史数组存放前值。。。。
只用map函数的话,map函数转np又很费时,头疼。。。先记录下来,以后有机会再改了

#encoding=utf-8
from functools import reduce
import numpy as np
import time
import pandas as pd
X = np.random.randint(3500,3600,200000)
A = np.full(200000,0.5)

d = np.vstack((X,A))
# print(d)

t1 = time.time()
res = reduce(lambda x, y: (1 - y[1]) * x + y[1] * y[0], zip(X, A), X[0])
t2 = time.time()
print(res)
print(t2-t2)
pre = X[0]
"""
若Y = dma(X,A)
Y = A*X + (1-A)*Y' 其中Y'表示上一周期Y值,A必须小于1
"""
def func(x,a):
    global pre
    res = a*x + (1-a)*pre
    pre = res
    return res

def func2(d):
    x = d[0]
    a = d[1]
    global pre
    res = a * x + (1 - a) * pre
    pre = res
    return res
t3 = time.time()
res2 = map(func,X,A)
res2 = np.fromiter(res2,np.float)
# res2 = np.apply_along_axis(func2,0,d)

t4 = time.time()
print(res2[-5:])
print(t4-t3)


df = pd.DataFrame({'X':X,'A':A})
def func_df(x):
    print(x)


猜你喜欢

转载自blog.csdn.net/q275343119/article/details/89515398