Skip to main content

A small example package for CRI PMT test.

Project description

hdk_pkg_critest

This is a demo package for CRI test. This package is for processing kline data and running strategy backtest.

1 Installation

Use the package manager pip to install hdk_pkg_critest.

pip install hdk_pkg_critest

2 Quick Start

The package can be run on shell or CMD, but better to use an IDE (Try Spyder that check the outputs easily.

This chapter gives a simple demo of doing a momentum strategy backtest using the package. All the codes can also be found in demo.py.

Please refer to next chapter for detailed documentation on the package.

STEP 1: Import the Package and Initialization

First, let's import the package, and initialize four core classes. Once you initialize an instance of a class, you can easily call methods (functions) from it by doing this: return = instance.function(Args..)

from hdk_pkg_critest import critest

sampledata = critest.SampleData()    # a class for fetching the sample data set
process = critest.Process()    # a class for processing data
backtest = critest.BackTest()    # a class for running backtesting
plotting = critest.Plotting()    # a class for Plotting the backtest results

STEP 2: Get a Sample Data Set for Test

Then, Let's use the get_sampledata() from the sampledata to get a sample data set. Use data.head() to check the data.

data = sampledata.get_sampledata()    # a time-series data set of a future's kline 

data.head()    # check what the data set looks like

STEP 3: Process the Data to Generate Factors

There are some kline process funcions in the class process. You can call and apply them on the data. For a momentum strategy, we need to process the kline as below:

#1 transfer the 1min kline to 60mins kline:
data_60 = process.chg_Granularity(data_1min=data, granularity=60)
data_60.head() 

#2 generate HA kline:
data_HA = process.get_BarHA(Open=data_60.open, High=data_60.high, Low=data_60.low, Close=data_60.close)
data_HA.head() 

#3 generate PS factor:
data_PS = process.get_BarPS(HA_Open=data_HA.HA_Open, HA_Close=data_HA.HA_Close, HA_PS_Lookback=20, PS_pct_level=[0.35, 0.5, 0.95, 0.97], combine=False)
data_PS.head() 

#4 generate CT factor:
data_CT = process.get_BarCT(HA_Open=data_HA.HA_Open, HA_Close=data_HA.HA_Close, HA_PS=data_PS, bar_pass_cut=2, ps_pass_cut=2)
data_CT.head() 

STEP 4: Generate Buy Sell Indicators and Do Backtest

Now we should use it to generate buy sell indicators. This should follow some specific rules based on different strategies. (I have only built in one strategy rule called "momentum". In fact, other strategies can be add inside the package easily.)

Call backtest_Momentum() from class backtest, then you will get a return as matrix (two-dimensional array). The buy sell indicator is in 4th column. We can use it to do backtest by calling get_PnL().

backtest_Momentum = backtest.backtest_Momentum(data_PS=data_PS, data_CT=data_CT.bar_idx_live)
print (backtest_Momentum)    # see the return matrix

result = backtest.get_PnL(close=data_60.close, indicator=backtest_Momentum[:,2], cost=0.15/100, sharpe_frequency=365*2, beta=1)
print (result)    # see the backtest result

STEP 5: Plot the Backtest Result

Finally, you can plot a custom image based on the backtest result.

plot1 = plotting.plot_type1(Open=data_60.open, High=data_60.high, Low=data_60.low, Close=data_60.close, indicator=backtest_Momentum[:,2], result=result)

3 Documentation for Functions

sampledata: get_sampledata()

re = sampledata.get_sampledata()
"""
This function is for fetching the sample data set.

Args: None

Returns: Returns a Dataframe with a time index and 5 coumns, inclding:
        (1) open: Float. Open price
        (2) high: Float. High price
        (3) low: Float. Low price
        (4) close: Float. Close price
        (5) volume: Float. Volume

4 Author

He Dekun

5 License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for hdk-pkg-critest, version 0.5.1
Filename, size File type Python version Upload date Hashes
Filename, size hdk_pkg_critest-0.5.1-py3-none-any.whl (4.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size hdk_pkg_critest-0.5.1.tar.gz (3.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page