-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstock_common.py
130 lines (97 loc) · 2.89 KB
/
stock_common.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#-*- coding: utf-8 -*-
from datetime import date,datetime,timedelta
import urllib
import os,sys,math,pickle
import csv
import time
import glob
import re
import pandas as pd
import numpy
#reload(sys)
#sys.setdefaultencoding("utf-8")
def getQuote(str):
return "'%s'" % (str)
def getToday():
return time.strftime("%Y%m%d")
def getTime():
return time.strftime("%H%M")
def clearString(value):
new_value = value.replace("'",'`')
new_value = new_value.replace('"',"'")
return new_value
def last_day(d, day_name):
days_of_week = ['sunday','monday','tuesday','wednesday', 'thursday','friday','saturday']
target_day = days_of_week.index(day_name.lower())
delta_day = target_day - d.isoweekday()
if delta_day >= 0: delta_day -= 7 # go back 7 days
return d + timedelta(days=delta_day)
def trim(value):
words = ['\n','\r',' ',' ']
new_str = str(value)
for word in words:
new_str = new_str.replace(word, '')
return new_str
def findBetween( s, first, last ):
return (s.split(first))[1].split(last)[0]
def findBetweenExt( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
def getValue(str):
new_str = trim(str)
return clearString(new_str)
def getNumericValue(str):
new_str = trim(str)
new_str = new_str.replace('"',"'")
if new_str == "":
return "0"
return new_str
def getSafeNumericValue(value):
try:
if value is None:
return "0"
if value=="":
return "0"
if math.isnan(float(value)):
return "0"
return str(value)
except:
print("Fatal Error!!! : getSafeNumericValue = %s" % (value))
return "0"
def convertMarketType(str):
if str=='kospiVal':
return 1
elif str=='kosdaqVal':
return 2
return -1
def convertStringToDate(value):
value_year = value[0:4]
value_month = value[4:6]
value_day = value[6:8]
#print "convertStringToDate : %s, %s, %s" % (value_year,value_month,value_day)
return datetime(int(value_year), int(value_month), int(value_day)).strftime('%Y-%m-%d %H:%M:%S')
def getDateByPerent(start_date,end_date,percent):
days = (end_date - start_date).days
target_days = numpy.trunc(days * percent)
target_date = start_date + timedelta(days=target_days)
#print days, target_days,target_date
return target_date
def getPercentileIndex(percentile_arr,value):
for index in range(len(percentile_arr)):
if value<=percentile_arr[index]:
return index
return len(percentile_arr)
def get_data_file_path(file_name):
full_file_name = "%s/data/%s" % (os.path.dirname(os.path.abspath(__file__)),file_name)
#print full_file_name
return full_file_name
def saveToFile(filename):
new_file_name = get_data_file_path(file_name)
pickle.dump(self,open( new_file_name, "wb" ))
def loadFromFile(filename):
new_file_name = get_data_file_path(file_name)
return pickle.load(open( new_file_name, "rb" ))