Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

This programme is built for back-testing factors.

Dependencies

  • python 3.5

  • pandas 0.23.0

  • numba 0.38.0

  • empyrical 0.5.0

  • data_box

  • pickle

  • multiprocessing

  • joblib

Logic

Basic definitions

  1. v_t,s_t,c_t: total value, stock value and cash value at time t after trading

  2. v^f_t,s^f_t,c^f_t: total value, stock value and cash value at time t before trading

  3. ss,sv: suspended stock value and valid stock value

  4. r_t: return at time t

  5. cost_t: cost to trade at time t

Note: s,ss,sv are all vectors while others are numbers

Equations

  1. v_t = |s_t| + c_t

  2. s^f_t = s_{t-1} * (1 + r_t) = ss^f_t + sv^f_t = ss_t + sv^f_t

  3. ss_t <- suspend, s^f_t

  4. c_{t-1} + |sv^f_t| = |sv_t| + c_t + cost_t ( where c_t, cost_t >= 0 )

  5. cost_t =|sv_t - sv^f_t| * costRate

  6. weight_t <- factor_{t-1},industry_t,suspend_t ( |weight_t| = 1 or 0 if there is no valid stocks or factors or industries)

  7. define cost^f_t = (2|sv^f_t| + c_{t-1}) * costRate s.t. cost^f_t >= cost_t, which is greater than the maximum cost we may have during the trade

  8. define available_value^f_t = c_{t-1} + |sv^f_t| - cost^f_t, which means the value ( = |sv_t| if weight_t != 0) we have in stocks after trading

  9. let sv_t = weight_t * available_value^f_t s.t. c_t = c_{t-1} + |sv^f_t| - |sv_t| - cost_t >=0

Example

Data Box: pre-process

from data_box import data_box
db=data_box()\
    .load_indestry(ind)\
    .load_indexWeight(ind_weight)\
    .calc_indweight()\
    .load_suspend(sus)\
    .load_adjPrice(price)\
    .add_factor('factor0',factor0)\
    .add_factor('factor1',factor1)\
    .set_lag(freq='d',day_lag=1)\
    .align_data()
# freq can be 'd' or 'm', for detail please refer to db.set_lag doc.

Where price,ind,ind_weight,sus,factor0,factor1 are all dataframes with index as date (yyyymmdd,int) and column as tickers. You can save and load this data box object by db.save('path') and db.load('path'). You can find more in data_box project.

Back Test

from single_factor_model import run_back_test

single process

Value,Turnover=run_back_test(data_box=db,back_end=None,n=5,weight_path=None,double_side_cost=0.003)

multi process

Value,Turnover=run_back_test(data_box=db,back_end='loky',n=5,weight_path=None,verbose=50)

or

with __name__=='__main__':
    Value,Turnover=run_back_test(data_box=db,back_end='multiprocessing',n=5,weight_path=None)

To check detailed position of each portfolio each day, just assign weight_path.

Summary and Plot

calculate return including long short portfolio(and reverse)

from single_factor_model import calc_return
Return = calc_return(Value,Turnover,long_short,double_side_cost=0.003)

summary

from single_factor_model import summary
S=summary(Return)

plot

run_plot(Return,show=True)

Metadata

Release files for single-factor-model 0.3.0a0

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

Source distribution (sdist)

Source distribution for single-factor-model 0.3.0a0
File Size Uploaded Provenance Actions
single_factor_model-0.3.0a0.tar.gz 8.1 kB Without provenance Details for single_factor_model-0.3.0a0.tar.gz

Release files / single_factor_model-0.3.0a0.tar.gz

Download URL single_factor_model-0.3.0a0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ec0aa190ead20c26b9c5482fd6009f45b40eb2dadecc57996bf645f0b7181be5
BLAKE2b-256 checksum
How to use checksums
ca00cc9d89157d01401b07493675cd8da13d4e97a5fa4cabdf255e5d8a5d4f00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.14.2 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.5.5

Security for single-factor-model 0.3.0a0

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