Skip to main content
File added late

1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.

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

Thus to update v_t, we would start with calculating s^f_t, ss, sv^f_t, then cost^f_t, available_value^f_t, then sv_t, cost_t and c_t, and finally v_t

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 processor

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

Multi processors

Value,Turnover=run_back_test(data_box=db,back_end='loky',n=5,n_jobs=-1,out_path=None,verbose=50)

or

with __name__=='__main__':
    Value,Turnover=run_back_test(data_box=db,back_end='multiprocessing',n=5,n_jobs=-1,out_path=None)

To check detailed positions of each portfolio every day, just assign out_path.

Back test for specific industries

from single_factor_model import  run_back_test_by_industry
Value_list,Turnover_list=run_back_test_by_industry(db,industry_list=None,back_end='loky',n_jobs=-1,double_side_cost=0.003,verbose=50)

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=True,double_side_cost=0.003)

Summary

from single_factor_model import summary
S=summary(Return)

Plot

from single_factor_model import run_plot
run_plot(Return,show=True)

Metadata

Release files for single-factor-model 0.3.2

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

File added late

1 file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release files before installing.

Source distribution (sdist)

Source distribution for single-factor-model 0.3.2
File Size Uploaded
single_factor_model-0.3.2.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for single-factor-model 0.3.2
File Interpreter ABI Platform
single_factor_model-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 22.1 kB

Release files / single_factor_model-0.3.2.tar.gz

File added late

This file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release file before installing.

Download URL single_factor_model-0.3.2.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2fa46a605861d6d4d93808355c9a95c2d1459b6360e34523187e6c19d3ce94c0
BLAKE2b-256 checksum
How to use checksums
d375c71269a2ca5648e9d800de4839db34d603507fbec51d42f7fc738bfe6c3f
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

Release files / single_factor_model-0.3.2-py3-none-any.whl

Download URL single_factor_model-0.3.2-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd7d50fec54ccb2ca664f230f5ff9e2123460943883c5bcd9a2b6f895e506c4b
BLAKE2b-256 checksum
How to use checksums
779a6ae258ca68b533e90d81f58c4d82e5cfecaeae64f4b1b9fb00064eaf079d
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

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.0

1 release file

0.2.1

1 release file

0.2.0

2 release files

0.1.8

2 release files

0.1.7

1 release file

0.1.6

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

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