如何在Python中生成市场的分形图

3 投票
3 回答
5498 浏览
提问于 2025-04-18 15:32

我想用Python生成这个:

http://classes.yale.edu/fractals/RandFrac/Market/TradingTime/Example1/Example1.html

但是我现在完全卡住了,对这个概念还很陌生。有没有人知道有什么库或者代码可以参考一下?

enter image description here

编辑:
根据我理解的,似乎每次都需要把分形分成两部分。所以你需要从两个中间点之间的线来计算y轴的点。然后这两个部分需要根据分形的规则来形成,对吗?

3 个回答

0

使用 @tobias_k 的解决方案和 pandas,我们可以把标准化的分形转换成基于时间的形式,并进行缩放。

import arrow
import pandas as pd
import time

depth = 5
# the "geometry" of fractal
turns = [
        (1 / 9, 0.60),
        (5 / 9, 0.30),
        (8 / 9, 0.70),
    ]

# select start / end time 
t0 = arrow.now().floor("hours")
t1 = t0.shift(days=5)
start = (pd.to_datetime(t0._datetime), 1000)
end = (pd.to_datetime(t1._datetime), 2000)

# create a non-dimensionalized [0,0]x[1,1] Fractal
_start, _end = (0, 0), (1, 1)  
graph = set()
make_graph(depth, graph, _start, _end, turns)
# just check graph length
assert len(graph) == (len(turns) + 1) ** depth + 1

# create a pandas dataframe from the normalized Fractal
df = pd.DataFrame(graph)
df.sort_values(0, inplace=True)
df.reset_index(drop=True, inplace=True)

# translate to real coordinates
X = pd.DataFrame(
        data=[(start[0].timestamp(), start[1]), (end[0].timestamp(), end[1])]
        ).T
delta = X[1] - X[0]
Y = df.mul(delta) + X[0]
Y[0] = [*map(lambda x: pd.to_datetime(x, unit="s"), Y[0])]

# now resample and interpolate data according to *grid* size
grid ="min"
Z = Y.set_index(0)
A = Z.resample(grid).mean().interpolate()

# plot both graph to check errors
import matplotlib.pyplot as plt
ax = Z.plot()
A.plot(ax=ax)
plt.show()

这里展示了两个图表:

两个图表

并且放大查看插值和对齐网格的差异:

在这里输入图像描述

0

我也对这个很感兴趣,所以开发了一个Python3的库,正好可以满足你的需求。

pip install fractalmarkets

你可以查看这个链接:https://github.com/hyperstripe50/fractal-market-analysis/blob/master/README.md

9

我不太确定你具体在问什么,但根据你的评论,我理解你想用链接中描述的递归方法生成一个看起来真实的股市曲线。

根据我对链接页面和一些相关页面的理解,它的工作原理大致是这样的:

  1. 你会有一个起点和一个终点,还有一些转折点,格式是 (t1, v1), (t2, v2) 等等,比如 start=(0,0), end=(1,1), turns = [(1/4, 1/2), (3/4, 1/4)],其中 tivi 是0到1之间的分数。
  2. 你需要确定这些转折点在起点和终点之间的实际位置,并计算这些点之间的距离,也就是从 pipi+1 需要走多远。
  3. 你把这些段落打乱,增加一些随机性;组合在一起后,它们仍然覆盖相同的距离,也就是连接了原来的 startend 点。
  4. 对新点之间的不同段落递归调用这个函数,重复这个过程。

这是我刚写的一些Python代码:

from __future__ import division
from random import shuffle

def make_graph(depth, graph, start, end, turns):
    # add points to graph
    graph.add(start)
    graph.add(end)

    if depth > 0:   
        # unpack input values
        fromtime, fromvalue = start
        totime, tovalue = end

        # calcualte differences between points
        diffs = []
        last_time, last_val = fromtime, fromvalue
        for t, v in turns:
            new_time = fromtime + (totime - fromtime) * t
            new_val = fromvalue + (tovalue - fromvalue) * v
            diffs.append((new_time - last_time, new_val - last_val))
            last_time, last_val = new_time, new_val

        # add 'brownian motion' by reordering the segments
        shuffle(diffs)

        # calculate actual intermediate points and recurse
        last = start
        for segment in diffs:
            p = last[0] + segment[0], last[1] + segment[1]
            make_graph(depth - 1, graph, last, p, turns)
            last = p
        make_graph(depth - 1, graph, last, end, turns)

from matplotlib import pyplot
depth = 8
graph = set()
make_graph(depth, graph, (0, 0), (1, 1), [(1/9, 2/3), (5/9, 1/3)])
pyplot.plot(*zip(*sorted(graph)))
pyplot.show()

下面是一些示例输出:

enter image description here

撰写回答