10
10
from time import sleep
11
11
12
12
import matplotlib
13
- matplotlib .use ('Agg' )
14
- import matplotlib .pyplot as plt
15
13
import matplotlib .dates as mdates
14
+ import matplotlib .pyplot as plt
16
15
import matplotlib .ticker as mticker
17
16
import numpy as np
18
17
import pandas as pd
36
35
from notifiers .twilio_client import TwilioNotifier
37
36
from notifiers .webhook_client import WebhookNotifier
38
37
38
+ matplotlib .use ('Agg' )
39
+
39
40
40
41
class Notifier (IndicatorUtils ):
41
42
"""Handles sending notifications via the configured notifiers
42
43
"""
43
44
44
- def __init__ (self , notifier_config , indicator_config , market_data ):
45
+ def __init__ (self , notifier_config , indicator_config , conditional_config , market_data ):
45
46
"""Initializes Notifier class
46
47
47
48
Args:
@@ -51,6 +52,7 @@ def __init__(self, notifier_config, indicator_config, market_data):
51
52
self .logger = structlog .get_logger ()
52
53
self .notifier_config = notifier_config
53
54
self .indicator_config = indicator_config
55
+ self .conditional_config = conditional_config
54
56
self .market_data = market_data
55
57
self .last_analysis = dict ()
56
58
self .enable_charts = False
@@ -147,13 +149,58 @@ def notify_all(self, new_analysis):
147
149
for market_pair in messages [exchange ]:
148
150
_messages = messages [exchange ][market_pair ]
149
151
150
- for candle_period in _messages :
151
- if not isinstance (_messages [candle_period ], list ) or len (_messages [candle_period ]) == 0 :
152
- continue
152
+ if self .conditional_config :
153
+ self .notify_conditional (exchange , market_pair , _messages )
154
+ else :
155
+ for candle_period in _messages :
156
+ if not isinstance (_messages [candle_period ], list ) or len (_messages [candle_period ]) == 0 :
157
+ continue
158
+
159
+ self .notify_all_messages (
160
+ exchange , market_pair , candle_period , _messages [candle_period ])
161
+ sleep (4 )
162
+
163
+ def notify_conditional (self , exchange , market_pair , messages ):
164
+ status = ['hot' , 'cold' ]
165
+
166
+ for condition in self .conditional_config :
167
+ x = 0
168
+ nb_conditions = 0
169
+ new_message = {}
170
+ new_message ['values' ] = []
171
+ new_message ['indicator' ] = []
172
+ for candle_period in messages :
173
+ if messages [candle_period ]:
174
+ new_message ['exchange' ] = messages [candle_period ][0 ]['exchange' ]
175
+ new_message ['market' ] = messages [candle_period ][0 ]['market' ]
176
+ new_message ['base_currency' ] = messages [candle_period ][0 ]['base_currency' ]
177
+ new_message ['quote_currency' ] = messages [candle_period ][0 ]['quote_currency' ]
178
+ for msg in messages [candle_period ]:
179
+ for stat in status :
180
+ if msg ['status' ] == stat :
181
+ try :
182
+ for indicator in condition [stat ]:
183
+ if msg ['indicator' ] in indicator .keys ():
184
+ if indicator [msg ['indicator' ]] == msg ['indicator_number' ]:
185
+ new_message ['values' ].append (
186
+ msg ['values' ])
187
+ new_message ['indicator' ].append (
188
+ msg ['indicator' ])
189
+ x += 1
190
+ except :
191
+ pass
192
+ for stat in status :
193
+ try :
194
+ nb_conditions += len (condition [stat ])
195
+ except :
196
+ pass
153
197
154
- self .notify_all_messages (
155
- exchange , market_pair , candle_period , _messages [candle_period ])
156
- sleep (4 )
198
+ if x == nb_conditions and x != 0 :
199
+ new_message ['status' ] = condition ['label' ]
200
+ self .notify_discord ([new_message ])
201
+ self .notify_webhook ([new_message ], None )
202
+ self .notify_telegram ([new_message ], None )
203
+ self .notify_stdout ([new_message ])
157
204
158
205
def notify_all_messages (self , exchange , market_pair , candle_period , messages ):
159
206
chart_file = None
@@ -682,7 +729,7 @@ def create_charts(self, messages):
682
729
except Exception as e :
683
730
self .logger .info (
684
731
'Error creating chart for %s %s' , market_pair , candle_period )
685
- self .logger .exception (e )
732
+ self .logger .exception (e )
686
733
687
734
def create_chart (self , exchange , market_pair , candle_period , candles_data ):
688
735
0 commit comments