GithubHelp home page GithubHelp logo

crluna / fractal-market-analysis Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hyperstripe50/fractal-market-analysis

0.0 0.0 0.0 7.05 MB

We are hard pressed to find a concrete implementation of both Benoit Mandelbrot's "A Multifractal Model of Asset Returns" and Edgar E. Peters' "Fractal Market Analysis" so, with apologies to the Authors, we attempt to fill this vacancy.

License: MIT License

Python 100.00%

fractal-market-analysis's Introduction

fractalmarkets

Motivation

We are hard pressed to find a concrete implementation of both Benoit Mandelbrot's "A Multifractal Model of Asset Returns" and Edgar E. Peters' "Fractal Market Analysis" so, with apologies to the Authors, we attempt to fill this vacancy.

Installation

pip install fractalmarkets

Features

This package offers time series simulation processes as well as rescaled range analysis.

The time series simulations are implemented as per Benoit Mandelbrot's description in "A Multifractal Model of Asset Returns", and further elaborated on within "Scaling in financial prices: III. Cartoon Brownian motions in multifractal time".

The rescaled range analysis is implemented as per Edgar E. Peters "Fractal Market Analysis".

Simulation Processes

  • fractalmarkets
    • mmar
      • BrownianMotion
      • BrownianMotionMultifractalTime
    • rs
      • RS

Usage

To simulate timeseries with fractalmarkets, instantiate the simulation process that you want with the required parameters and run simulate.

Brownian Motion in Multifractal Time

from fractalmarkets.mmar.brownian_motion_multifractal_time import BrownianMotionMultifractalTime
import matplotlib
import matplotlib.pyplot as plt; plt.style.use('ggplot')
from scipy import interpolate
import numpy as np

bmmt = BrownianMotionMultifractalTime(9, x=0.457, y=0.603, randomize_segments=True, randomize_time=True, M=[0.6, 0.4])
data = bmmt.simulate() # [ [x, y], ..., [x_n, y_n]]

f = interpolate.interp1d(data[:,0], data[:,1])

y = f(np.arange(0, 1, .001))
x = np.linspace(0, 1, len(y), endpoint=True)

y_diff = [b - a for a, b in zip(y[:-1], y[1:])]

fig, axs = plt.subplots(2)
fig.suptitle('Brownian Motion in Multifractal Time')

axs[0].plot(x, y, 'b-')
axs[1].bar(x[:-1],y_diff,align='edge',width=0.001,alpha=0.5)
bar_list=filter(lambda x: isinstance(x,matplotlib.patches.Rectangle),axs[1].get_children())
for bar,ret in zip(bar_list,y_diff):
    if ret >= 0:
        bar.set_facecolor('green')
    else:
        bar.set_facecolor('red')

z1 = np.array(y)
z2 = np.array([0] * len(y))

axs[0].fill_between(x, y, 0,
                where=(z1 >= z2),
                alpha=0.30, color='green', interpolate=True)

axs[0].fill_between(x, y, 0,
                where=(z1 < z2),
                alpha=0.30, color='red', interpolate=True)

plt.show()

R/S Annalysis

Brownian Motion

from fractalmarkets.mmar.brownian_motion import BrownianMotion
import matplotlib
import matplotlib.pyplot as plt; plt.style.use('ggplot')
from scipy import interpolate
import numpy as np

bm =  BrownianMotion(9, .457, .603, randomize_segments=True)
data = bm.simulate() # [ [x, y], ..., [x_n, y_n]]

f = interpolate.interp1d(data[:,0], data[:,1])

y = f(np.arange(0, 1, .001))
x = np.linspace(0, 1, len(y), endpoint=True)

y_diff = [b - a for a, b in zip(y[:-1], y[1:])]

fig, axs = plt.subplots(2)
fig.suptitle('Brownian Motion')

axs[0].plot(x, y, 'b-')
axs[1].bar(x[:-1],y_diff,align='edge',width=0.001,alpha=0.5)
bar_list=filter(lambda x: isinstance(x,matplotlib.patches.Rectangle),axs[1].get_children())
for bar,ret in zip(bar_list,y_diff):
    if ret >= 0:
        bar.set_facecolor('green')
    else:
        bar.set_facecolor('red')

z1 = np.array(y)
z2 = np.array([0] * len(y))

axs[0].fill_between(x, y, 0,
                where=(z1 >= z2),
                alpha=0.30, color='green', interpolate=True)

axs[0].fill_between(x, y, 0,
                where=(z1 < z2),
                alpha=0.30, color='red', interpolate=True)

plt.show()

R/S Annalysis

RS Analysis

from fractalmarkets.rs.rs import RS
from fractalmarkets.mmar.brownian_motion_multifractal_time import BrownianMotionMultifractalTime

bmmt = BrownianMotionMultifractalTime(9, x=4/9, y=0.603, randomize_segments=False, randomize_time=False, M=[0.6, 0.4])
data = bmmt.simulate()

rs = RS(data[1:,1]) # timeseries starts at zero which must be omitted to avoid division error
(H, c) = rs.get_Hc()
print("Estimated H from RS Analysis: {}".format(H))

rs.plot_vstat() # plot vstat and RS
>> Estimated H from RS Analysis: 0.6237751068336207

R/S Annalysis

Developer Guide

Install virtualenv

pip install virtualenv

Create a virtual environment

// from fractal_market_analysis directory
python -m virtualenv venv
// sometimes that does not work so try below
// virtualenv venv

Start the virtual environment

// from fractal_market_analysis directory
source venv/Scripts/activate
// it is possible that your venv has a bin directory rather than a Scripts directory. If so run the following
// source venv/bin/activate

Install proper packages

// from fractal_market_analysis directory
pip install -r requirements.txt

Install our application and watch for edits

// from fractal_market_analysis directory. This will allow us to use fma imports in our modules.
pip install -e .

Bonus for the anaconda users...

// from fractal_market_analysis directory
conda env create -f environment.yml
conda activate fractalmarkets

Run an example

// from fractal_market_analysis directory
python examples/rs_analysis.py // or any other file

Run the Tests

// from fractal_market_analysis directory
pytest

fractal-market-analysis's People

Contributors

hyperstripe50 avatar xavierdcruz0 avatar dluftspring avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.