forked from Constantineople/Assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
research_momentum.py
49 lines (39 loc) · 1.64 KB
/
research_momentum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from main import *
# momentum
instruments = ['hc', 'rb', 'i', 'j', 'jm',
'au', 'ag',
'v', 'ru', 'l', 'pp', 'bu', 'TA', 'FG', 'MA',
'y', 'p', 'm', 'a', 'c', 'cs', 'jd', 'RM', 'CF', 'SR', 'OI']
dict_parameter = {}
dict_parameter['window'] = 15 # lookback period for momentum
dict_parameter['trade_percent'] = 0.2 # long short percent
dict_parameter['hold_period'] = 2 # days to hold the position
# load data from sqlite
dict_data = load_data_df_from_sql(instruments, start_date=20180101)
data = {}
data['price'] = dict_data['adjclose']
data['volume'] = dict_data['TotalVolume']
# signal: use price/volume data to calculate signal
signal = logreturns(data, dict_parameter)
signal.to_csv('logreturns.csv')
# strategy: calculate position based on price and signal
position = deltaneutral(data=data,
signal=signal,
dict_parameter=dict_parameter,
)
position.to_csv('position(1).csv')
# calculate pnl, turnovers, volumes
bkt_result = cal_bkt(dict_data['adjclose'], position)
# calculate backtest performance of the whole portfolio: sharpe ratio, pot
# sharpe ratio = 16 * mean(portfolio daily pnl) / std(portfolio daily pnl)
# pot = sum(portfolio daily pnl) / sum(portfolio daily turnovers) * 10000
performance = cal_perf(bkt_result)
print(bkt_result)
print(performance)
# plot the cumulative pnl
plt.plot(bkt_result['pnl_ptf'].values.cumsum())
plt.show()