<jupyter_start><jupyter_text>Finance-Python--------------------------------- > 原始项目地址：**Finance-Python**（https://github.com/wegamekinglc/Finance-Python）；> * ``python setup.py install`` 或者,> * ``pip install finance-python``> 相关依赖请见主目录下 ``requirements`` 文件夹。  Operator in Declarative Style----------------------------------------------  声明式计算表达式被抽象为一些**算子**，用户无需给出计算的流程，只需要使用这些**算子**表达自己的想法。例如，我们需要计算向量 $\bar x$ 的均值，**命令式**的做法可能如下：```pythonsum_value = 0for v in x:   sum_value += v average_value = sum_value / len(x)```而**声明式**的做法：```pythonaverage_value = mean(x)```上面的 ``mean`` 就是我们所谓的算子概念。  延迟计算表达式无需在定义的位置获得结果，只需在需要的时候进行计算即可：```pythonexpression = operator(dependency)```中间会有一些其他计算的代码：```......```这里的 ``expression`` 是一个表达式对象，而不是计算的结果，在需要值的时候：```pythonexpression_value = expression.value```  支持算术运算表达式对象支持基本的 ``+-*/`` 运算。例如，计算收益率的sharp值，我们可以这样去定义表达式：```pythonsharp_expression = mean(x) / std(x)```这里的 ``sharp_expression`` 是一个新的表达式，由两个基础的表达式构造而成。  支持复合运算运算可以复合，例如实现以下的逻辑，计算过去20日每日50日均线的标准差：```pythoncompounded_expression = std(mean(x, 50), 20)```  Implementation in Finance-Python在 **Finance-Python** 中，以 **accumulator** 的形式实现了上面的 **Declarative Style Operator**。**accumulator** 是具有自身状态的算符，  Accumulator  Hello World下面的这个例子，使用 ``Latest`` 算符，保留输入值的最近状态。<jupyter_code>from PyFin.Math.Accumulators import Latest

exp1 = Latest('x')
exp1<jupyter_output><empty_output><jupyter_text>上面可以看到 ```exp1``` 是一个 ``accumulator`` 的实例。<jupyter_code># 1st round
exp1.push({'x': 1})
print("Value after 1st round: {0}".format(exp1.value))

# 2nd round
exp1.push({'x': 2})
print("Value after 2nd round: {0}".format(exp1.value))

# repeate
print("Do nothing: {0}".format(exp1.value))

# 3rd and 4th round
exp1.push({'x': 3})
exp1.push({'x': 4})
print("Value after 3rd/4th round: {0}".format(exp1.value))<jupyter_output>Value after 1st round: 1.0
Value after 2nd round: 2.0
Do nothing: 2.0
Value after 3rd/4th round: 4.0<jupyter_text>One Step Further下面的例子，计算过去两个输入值的均值：<jupyter_code>from PyFin.Math.Accumulators import MovingAverage

ma = MovingAverage(x='x', window=2)

values = [1, 2, 3, 4, 5]

for i, x in enumerate(values):
    ma.push({'x': x})
    print("{0}: {1}".format(i, ma.value))<jupyter_output>0: 1.0
1: 1.5
2: 2.5
3: 3.5
4: 4.5<jupyter_text>More complicated examples  算术运算/复合运算计算一组收益率序列的滚动sharp，时间窗口为250日。构造算子：<jupyter_code>import numpy as np
from PyFin.Math.Accumulators import MovingVariance
from PyFin.Math.Accumulators import Sqrt

np.random.seed(47)
ret_simulated = 0.0005 + np.random.randn(2000) / 100.

ret_mean = MovingAverage(x='x', window=250)
ret_std = Sqrt(MovingVariance(x='x', window=250)) # Compounded accumlator is used here

sharp = ret_mean / ret_std # dividing can be used for accumulators
sharp<jupyter_output><empty_output><jupyter_text>输入数据：<jupyter_code>sharp_series = []
for ret in ret_simulated:
    sharp.push({'x': ret})
    sharp_series.append(sharp.value)<jupyter_output><empty_output><jupyter_text>把数据画出来出来：<jupyter_code>%matplotlib inline
import pandas as pd
df = pd.DataFrame({'returns': ret_simulated.cumsum(), 'sharp': sharp_series})
df[250:].plot(secondary_y='sharp', figsize=(12, 6))<jupyter_output><empty_output><jupyter_text>复合运算下面的例子中，我们计算一组序列，20日均线和50日均线的250日相关系数：<jupyter_code>from PyFin.Math.Accumulators import MovingCorrelation

ma20 = MovingAverage(x='x', window=20)
ma50 = MovingAverage(x='x', window=50)

corr = MovingCorrelation(window=250, x=ma20, y=ma50)
corr
ma20_series = []
ma50_series = []
corr_series = []
for ret in ret_simulated:
    ma20.push({'x': ret})
    ma50.push({'x': ret})
    corr.push({'x': ret})
    
    ma20_series.append(ma20.value)
    ma50_series.append(ma50.value)
    corr_series.append(corr.value)
df = pd.DataFrame({'ma20': ma20_series, 'ma50': ma50_series, 'corr': corr_series})
df[300:].plot(secondary_y='corr', figsize=(12, 6))<jupyter_output><empty_output><jupyter_text>Working with Pandas<jupyter_code>from PyFin.api import MA
from PyFin.examples.datas import sample_data
sample_data
ma2 = MA(2, 'close')<jupyter_output><empty_output><jupyter_text>根据 ``category_field`` 计算<jupyter_code>ma2.transform(sample_data, category_field='code')<jupyter_output><empty_output><jupyter_text>直接计算<jupyter_code>ma2 = MA(2, 'close')
ma2.transform(sample_data)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17734338200659444, 'fraction_numerical': 0.037211493170042395, 'mean_word_length': 6.597495527728086, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 43, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '602577', 'license': 'MIT', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1967, 'n_words': 277, 'path': 'ipynb/Presentation for Analysis.ipynb', 'repo_name': 'wangjiehui11235/Finance-Python', 'size': '4246'}