-
Notifications
You must be signed in to change notification settings - Fork 54
python递归函数
ChannelCMT edited this page Jun 3, 2019
·
1 revision
在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
可视化过程链接: http://www.pythontutor.com/visualize.html#mode=edit
def fact(n):
if n==1:
return 1
return n * fact(n - 1)
fact(1) =
fact(2) =
fact(3) =
fact(4) = $432 = 4*6 = 24$
fact(5) = $5432 = 524 = 120$
print(fact(5))
120
- 自定义方法
- Pandas递归函数
- talib的EMA
import pandas as pd
a = 2/7
Prices = list(range(10)) #prices
def ema ( N , Price) :
if N < 1:
return Price[0]
return (1-a)*ema(N-1,Price) + a*Price[N]
print (pd.Series([ema(i,Prices) for i in range(10)]))
0 0.000000
1 0.285714
2 0.775510
3 1.411079
4 2.150771
5 2.964836
6 3.832026
7 4.737161
8 5.669401
9 6.621001
dtype: float64
recursion_df = pd.DataFrame({'test': Prices})
recursion_result = recursion_df.ewm(alpha=2/7, adjust=False).mean()
print(recursion_result)
test
0 0.000000
1 0.285714
2 0.775510
3 1.411079
4 2.150771
5 2.964836
6 3.832026
7 4.737161
8 5.669401
9 6.621001
import talib as ta
import numpy as np
EMA = pd.Series(ta.EMA(np.array(Prices, dtype=np.float64),6))
print(EMA)
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 2.5
6 3.5
7 4.5
8 5.5
9 6.5
dtype: float64
-
python基础
-
python进阶
-
数据格式处理
-
数据计算与展示
-
因子横截面排序分析
-
信号时间序列分析
-
CTA策略类型
-
附录:因子算法