Skip to main content

Simple Backtest Module (Personal Usage)

Chang Sun | 孙畅

Email

Author Package License README

Install and Update

pip install --upgrade sc-backtest

or (if slow)

pip install --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple sc-backtest

Simple Test

  • Check for factor validity
    • Statistical:
      • CDF
      • Markout
      • Hist
      • ...
    • Time-Series:
      • Sign-Trade
      • Value-Trade
      • ...
# x: factors
# y: asset's future ret

import pandas as pd
import numpy as np
from sc_backtest import simpletest, dataset

data = dataset.get_data('adj_close_price', frequency=5)
x = data.pct_change(240).iloc[:, 0]
y = data.pct_change().shift(-1).iloc[:, 0]
st = simpletest()
st.plot_cdf(x, y)
st.plot_composite(x, y)

Backtest (bt)

  • Backtest
    • get_report
    • get_pnl_plot
    • round_test
    • ...
# x: factors
# y: asset's future ret

import pandas as pd
import numpy as np
from sc_backtest import simpletest, bt, dataset

data = dataset.get_data('adj_close_price', frequency=5)
x = data.pct_change(240).iloc[:, 0]
y = data.pct_change().shift(-1).iloc[:, 0]
st = simpletest()
data = st.simple_pnl(x, y, data_return=True)
report = bt.get_report(data['delta_med'], y)
bt.get_pnl_plot(data['delta_med'], y)

Technical Analysis (ta)

Reference: ta

import pandas as pd
import numpy as np
from sc_backtest import ta, dataset

data = dataset.get_data('adj_close_price', frequency=5)
macd_diff = ta.trend.macd(data.iloc[:, 0]).macd_diff()

Technical Analysis2 (ta2)

Variou moving average function and stat model

  • sma, ema, wma, ...
  • rsi, atr, ...
  • z_score, div_std, de_mean, ...
import pandas as pd
import numpy as np
from sc_backtest import ta2, dataset

data = dataset.get_data('adj_close_price', frequency=5)
wma = ta2.wma(data.iloc[:, 0], window=5)

DataFrame Function (df_func)

# example

def df_sim_yoy(window):
    def _sim_yoy(x):
        temp = pd.DataFrame(x)
        return (temp - temp.shift(window)) / ((temp.abs() + temp.shift(window).abs()) / 2)

    _sim_yoy.__name__ = f'df_sim_yoy_{int(window)}'
    return _sim_yoy

Example

Input your factor and underlying asset's future return with index type as DatetimeIndex and get the composite factor analysis stat and simple-pnl time-series plots.

# x: factors
# y: asset's future ret

import pandas as pd
import numpy as np
from sc_backtest import simpletest, bt, dataset, ta2

data = dataset.get_data('adj_close_price', frequency=5)
x = data.pct_change().apply(lambda x: ta2.ema(x, window=240))
y = data.pct_change().shift(-1)
st = simpletest()
st.plot_composite_cs(x, y, ic=True, horizon=5)
bt.get_pnl_plot(x, y, alpha=True)

Metadata

Release files for sc-backtest 0.1.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sc-backtest 0.1.14
File Size Uploaded Provenance Actions
sc-backtest-0.1.14.tar.gz 45.6 MB Without provenance Details for sc-backtest-0.1.14.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sc-backtest 0.1.14
File Interpreter ABI Platform Actions
sc_backtest-0.1.14-py3-none-any.whl Python 3 none any Details for sc_backtest-0.1.14-py3-none-any.whl

Total release size: 92.3 MB

Release files / sc-backtest-0.1.14.tar.gz

Download URL sc-backtest-0.1.14.tar.gz
Size 45.6 MB
Tags Source
SHA-256 checksum
How to use checksums
32964f5361f0217b6fe95650d2b723ab5be1c64170abf55fa4161b92ca51c4e4
BLAKE2b-256 checksum
How to use checksums
6de6ce384a4f6cc1c6c7ae79d8aced5f93855ecf4848115ca4ea6a062dec6b7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.12.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

Release files / sc_backtest-0.1.14-py3-none-any.whl

Download URL sc_backtest-0.1.14-py3-none-any.whl
Size 46.7 MB
Tags Python 3
SHA-256 checksum
How to use checksums
950478e86926364659ee36aafbe33710b9bcc03c86f286ee4e33fe190d8d81f5
BLAKE2b-256 checksum
How to use checksums
935ba0f41fbd24c7df317281ac7021639d0e5f638bde37452e2737f049640a46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.12.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

Security for sc-backtest 0.1.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page