-
Notifications
You must be signed in to change notification settings - Fork 10
/
stock-alarms.py
executable file
·171 lines (137 loc) · 5.38 KB
/
stock-alarms.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
import sys
import argparse
import datetime
import json
import os
from pandas_datareader import data
from enum import Enum
class AlarmState(Enum):
Inactive = 0
Active = 1
alarmsConfigFile = 'config/alarms.json'
alarmsIsChanged = False
alarms = []
hysteresis = 10 # percent value
def alarmsRead():
global alarmsConfigFile
global alarms
if os.path.isfile(alarmsConfigFile):
with open(alarmsConfigFile, 'r') as f:
alarms = json.load(f)
def alarmsWrite():
global alarmsConfigFile
global alarms
with open(alarmsConfigFile, 'w') as f:
json.dump(alarms, f, indent=4, sort_keys=True)
def alarmsShow():
global alarmsConfigFile
global alarms
for alarm in alarms:
print(alarm)
def alarmsAdd(name, reference, alarmType, value, state):
global alarms
alarms.append({'name': name, 'reference': float(reference),
'type': str(alarmType), 'value': float(value), 'state': int(state.value)})
def alarmsRemove(name, reference, alarmType, value):
global alarms
try:
alarms.remove({'name': name, 'reference': float(reference),
'type': str(alarmType), 'value': float(value), 'state': AlarmState.Active})
except ValueError:
pass
try:
alarms.remove({'name': name, 'reference': float(reference),
'type': str(alarmType), 'value': float(value), 'state': AlarmState.Inactive})
except ValueError:
pass
def alarmPrint(alarm):
print(alarm)
def alarmCheck(value, alarm):
diffrence = abs(close[-1] - alarm['reference'])
if alarm['type'] == 'percent':
valueChange = float(alarm['reference'] * alarm['value']) / 100
else:
valueChange = alarm['value']
valueChangeReset = valueChange - float(valueChange * hysteresis) / 100
# Check if alarm happend!
if ((alarm['state'] == AlarmState.Active) and (diffrence > valueChange)):
print('!Alarm! ' + str(alarm['name']) + ' price ' + str(price) + ' '
'(ref. ' + str(alarm['reference']) + ' +/-' + str(valueChange) + ')!')
alarms[i]['state'] = AlarmState.Inactive
return True
# Check if alarm should be reseted!
if ((alarm['state'] == AlarmState.Inactive) and (diffrence <= valueChangeReset)):
print('Alarm reseted!')
alarms[i]['state'] = AlarmState.Active
return True
return False
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--addAlarm', action='store_true',
required=False, help='Adds given alarm')
parser.add_argument('-d', '--deleteAlarm', action='store_true',
required=False, help='Removes alarm')
parser.add_argument('-c', '--checkAlarms', action='store_true',
required=False, help='Check all alarms')
parser.add_argument('-p', '--printAlarms', action='store_true',
required=False, help='Print all alarms')
parser.add_argument('-n', '--stockCode', type=str, required=False, help='')
parser.add_argument('-r', '--referencePrice',
type=float, required=False, help='')
parser.add_argument('-t', '--type', type=str, required=False, help='')
parser.add_argument('-v', '--value', type=float, required=False, help='')
parser.add_argument('-W', '--lastWeek', action='store_true',
required=False, help='Last Week')
args = parser.parse_args()
# Assert
if (not args.addAlarm and not args.checkAlarms and not args.deleteAlarm and not args.printAlarms):
print('Missing event')
sys.exit(1)
if (args.addAlarm):
if (not args.stockCode or not args.referencePrice or not args.type or not args.value):
print('Missing arguments for adding.')
sys.exit(1)
if (args.deleteAlarm):
if (not args.stockCode or not args.referencePrice or not args.type or not args.value):
print('Missing arguments for removal.')
sys.exit(1)
# End date
currentDateTime = datetime.datetime.now()
end_date = currentDateTime.strftime('%Y-%m-%d')
# Check last month
tmpDate = datetime.datetime.now() - datetime.timedelta(days=30)
start_date = tmpDate.strftime('%Y-%m-%d')
alarmsRead()
# 0. Adding alarms
# #####################################################33
if (args.addAlarm):
alarmsRemove(args.stockCode, args.referencePrice, args.type, args.value)
alarmsAdd(args.stockCode, args.referencePrice,
args.type, args.value, AlarmState.Active)
alarmsIsChanged = True
# 1. Removing alarms
# #####################################################33
if (args.deleteAlarm):
alarmsRemove(args.stockCode, args.referencePrice, args.type, args.value)
alarmsIsChanged = True
# 2. Checking alarms
# #####################################################33
for i in range(len(alarms)):
alarm = alarms[i]
if (args.printAlarms):
alarmPrint(alarm)
if (args.checkAlarms):
# User pandas_reader.data.DataReader to load the desired data. As simple as that.
panel_data = data.DataReader(
alarm['name'], 'stooq', start_date, end_date)
if len(panel_data) != 0:
close = panel_data['Close']
price = close[-1]
if (alarmCheck(price, alarm) is True):
alarmsIsChanged = True
else:
print('No Stooq data for entry!')
# 4. Write alarms if were changed
# #####################################################33
if (alarmsIsChanged is True):
alarmsWrite()