-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer2.py
executable file
·107 lines (94 loc) · 3.83 KB
/
analyzer2.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import sys
import datetime as dt
import math
import numpy as np
import pandas as pd
# QSTK Imports
import QSTK.qstkutil.DataAccess as da
import QSTK.qstkutil.tsutil as tsu
def get_values_list(s_file_path):
df_values_list = pd.read_csv(s_file_path, sep=',', header=None)
df_values_list.columns = ["year", "month", "day", "total"]
return df_values_list
def get_values(df_values_list):
np_values_list = df_values_list.values
l_values = []
for value in np_values_list:
dt_date = dt.datetime(value[0], value[1], value[2], hour=16)
total = float(value[3])
l_values.append([dt_date, total])
np_values = np.array(l_values)
df_values = pd.DataFrame(np_values[:, 1], index=np_values[:, 0], columns=["val"])
return df_values
def get_data(ldt_timestamps, ls_symbols):
ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
dataobj = da.DataAccess('Yahoo')
ldf_data = dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
d_data = dict(zip(ls_keys, ldf_data))
for s_key in ls_keys:
d_data[s_key] = d_data[s_key].fillna(method='ffill')
d_data[s_key] = d_data[s_key].fillna(method='bfill')
d_data[s_key] = d_data[s_key].fillna(1.0)
return d_data
def get_prices(ldt_timestamps, ls_symbols, s_key="close"):
# close = adjusted close
# actual_close = actual close
d_data = get_data(ldt_timestamps, ls_symbols)
return d_data[s_key]
def get_performance_indicators(df_data):
na_data = df_data.values
df_result = pd.DataFrame(index=["avg_daily_ret", "std_daily_ret", "sharpe_ratio", "total_ret"], \
columns=df_data.columns)
# Calculating the daily return
# It gets a copy of na_data becouse tsu.returnize0 does not return
# anything, the function changes the argument.
na_daily_ret = na_data.copy()
tsu.returnize0(na_daily_ret)
na_cum_ret = na_data / na_data[0, :]
for col in range(na_data.shape[1]):
df_result.ix["avg_daily_ret", col] = np.mean(na_daily_ret[:, col])
df_result.ix["std_daily_ret", col] = np.std(na_daily_ret[:, col])
df_result.ix["sharpe_ratio", col] = math.sqrt(252) * df_result.ix["avg_daily_ret", col] / df_result.ix["std_daily_ret", col]
df_result.ix["total_ret", col] = na_cum_ret[-1 , col]
return df_result
# print df_result
if __name__ == '__main__':
#print "start analyze.py"
# print
lookback=sys.argv[1]
holding=sys.argv[2]
trigger=sys.argv[3]
market=sys.argv[4]
switch = sys.argv[5]
switch2=sys.argv[6]
s_file_path = "data\\q2_values.csv"
ls_symbols =["IXIC"]
df_values_list = get_values_list(s_file_path)
df_values = get_values(df_values_list)
df_prices = get_prices(list(df_values.index), ls_symbols)
df_data = df_values.join(df_prices)
get_performance_indicators(df_data)
# print
# df_result=get_performance_indicators(df_data)
# print df_result
#score= df_result.ix["sharpe_ratio","val"]-df_result.ix["sharpe_ratio","FTSE.AT"]
#if (score >0):
# print "we have a winner:"
#print lookback, holding, trigger, market, switch,switch2,score
#else:
#print "this is a looser"
# print df_result.ix["sharpe_ratio","val"]-df_result.ix["sharpe_ratio","FTSE.AT"]
# print "end analize.py"
# print
df_result=get_performance_indicators(df_data)
# print df_result
score= df_result.ix["sharpe_ratio","val"]-df_result.ix["sharpe_ratio","IXIC"]
#
delta= df_result.ix["total_ret","val"]-df_result.ix["total_ret","IXIC"]
# if (score >0):
# print "we have a winner:"
print lookback, holding, trigger, market, switch,switch2,score,delta
# else:
# print "this is a looser"
# print df_result.ix["sharpe_ratio","val"]-df_result.ix["sharpe_ratio","FTSE.AT"]
# print "end analize.py"