def check2(code, wechate_account, cache_rate=None): time.sleep(1) df = ts.get_realtime_quotes(code) # print(df) e = df[['code', 'name', 'price', 'pre_close', 'time']] p = df[u'price'] pre_close = float(df[u'pre_close']) name = df[u'name'][0] open_price = float(df['open'][0]) # print(open_price) current_price = float(p[0]) # todo 1:涨跌超过指定百分比报警,当前价格/昨日收盘价*100,取整,>1的整数倍报警;2:涨跌超过基准百分比报警 current_rate, cache_rate, diff, is_alert = alert(current_price, pre_close, cache_rate) if diff > Decimal('0'): print('%s 当前价格:%.2f, 涨跌幅:%.2f, 上涨 %.2f' % (name, current_price, current_rate, diff)) elif diff < Decimal('0'): print('%s 当前价格:%.2f, 涨跌幅:%.2f, 下跌 %.2f' % (name, current_price, current_rate, diff)) if is_alert: if diff > Decimal('0'): sendmsg( '%s 当前价格:%.2f, 涨跌幅:%.2f, 上涨 %.2f' % (name, current_price, current_rate, diff), wechate_account) else: sendmsg( '%s 当前价格:%.2f, 涨跌幅:%.2f, 下跌 %.2f' % (name, current_price, current_rate, diff), wechate_account) return cache_rate
def price2(price_max, price_min, current_price, name, wechate_account): if current_price >= price_max: sendmsg('上涨到价提醒:%s, 当前价格:%.2f, 设定价格:%.2f' % (name, current_price, price_max)) elif current_price <= price_min: sendmsg('下跌到价提醒:%s, 当前价格:%.2f, 设定价格:%.2f' % (name, current_price, price_max))
def profit_and_loss(cost, current_price, target_profit, stop_loss, name, wechate_account): ratio = (current_price - cost) * 100.0 / cost if ratio >= target_profit: sendmsg('上涨提醒:%s, 当前价格:%.2f, 涨幅超止盈线:%s%%' % (name, current_price, target_profit)) elif ratio <= percent_min: sendmsg('下跌提醒:%s, 当前价格:%.2f, 跌幅超止损线:%s%%' % (name, current_price, stop_loss))
def up_and_down(pre_close, current_price, percent_max, percent_min, name, wechate_account): ratio = (current_price - pre_close) * 100.0 / pre_close if ratio >= percent_max: sendmsg('上涨提醒:%s, 当前价格:%.2f, 涨幅超:%s%%' % (name, current_price, percent_max)) elif ratio <= percent_min: sendmsg('下跌提醒:%s, 当前价格:%.2f, 跌幅超:%s%%' % (name, current_price, percent_min))
def sendsocket(control, identifier, sock): payload = struct.pack('i', sock.fileno()) control.wait_for_write() sendmsg.sendmsg( control.fileno(), identifier, 0, (socket.SOL_SOCKET, sendmsg.SCM_RIGHTS, payload))
def check(code, base, price_max, price_min, percent_max, percent_min, percent_max_10, percent_min_10, cost, stop_loss, target_profit, wechate_account, rate, cache, is_first, cache_rate=None): time.sleep(1) df = ts.get_realtime_quotes(code) print(df) e = df[['code', 'name', 'price', 'pre_close', 'time']] p = df[u'price'] open = float(df[[u'open']]) pre_close = float(df[u'pre_close']) name = df[u'name'][0] print(e) current_price = float(p[0]) if not base: base = current_price # todo 1:涨跌超过指定百分比报警,当前价格/昨日收盘价*100,取整,>1的整数倍报警;2:涨跌超过基准百分比报警 rate = 1 cache_rate, diff = alert(current_price, pre_close, cache_rate) if diff > 0 and diff >= rate: sendmsg('%s current:%.2f, more %.2f' % (name, p[0], diff), wechate_account) elif diff < 0 and diff <= rate: sendmsg('%s current:%.2f, less %.2f' % (name, p[0], diff), wechate_account) price2_key = 'price2_%s' % code if price2_key not in cache: price2(price_max, price_min, current_price, name, wechate_account) cache[price2_key] = 'alerted' up_and_down_key = 'up_and_down_%s' % code if up_and_down_key not in cache: up_and_down(pre_close, current_price, percent_max, percent_min, name, wechate_account) cache[up_and_down_key] = 'alerted' profit_and_loss_key = 'profit_and_loss_%s' % code if profit_and_loss_key not in cache: profit_and_loss(cost, current_price, target_profit, stop_loss, name, wechate_account) cache[profit_and_loss_key] = 'alerted' return base
def run(self): us = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) us.bind(('mig.sock')) us.listen(1) print 'waiting for standby process to start and connect to unix socket' uconn, addr = us.accept() print 'standby process connected, hand it over our server-socket' payload = struct.pack('i', self.server.fileno()) sendmsg.sendmsg( uconn.fileno(), '', 0, (socket.SOL_SOCKET, sendmsg.SCM_RIGHTS, payload)) print 'Sent server-socket...awaiting for handover complete signal', self.server.fileno() result = uconn.recv(1024) if result == 'KILL': #1. TODO: wait for already clients connected to complete #2. Terminate return
def redirect_fds(fds, sock, unit): """Redirect each fd to a pipe and pass the read end of the pipe to logductd. """ pipes_to_send = [] lognames = [] for fd, logname in fds: read_end, write_end = os.pipe() os.dup2(write_end, fd) os.close(write_end) pipes_to_send.append(read_end) lognames.append(logname) header = {"unit": unit, "lognames": lognames} sendmsg(sock, [(json.dumps(header) + '\n').encode()], [(socket.SOL_SOCKET, SCM_RIGHTS, array.array("i", pipes_to_send))]) for read_end in pipes_to_send: os.close(read_end)
def redirect_fds(fds, sock, unit): """Redirect each fd to a pipe and pass the read end of the pipe to logductd. """ pipes_to_send = [] lognames = [] for fd, logname in fds: read_end, write_end = os.pipe() os.dup2(write_end, fd) os.close(write_end) pipes_to_send.append(read_end) lognames.append(logname) header = {"unit": unit, "lognames": lognames} sendmsg(sock, json.dumps(header) + '\n', [(socket.SOL_SOCKET, SCM_RIGHTS, array.array("i", pipes_to_send))]) for read_end in pipes_to_send: os.close(read_end)
return PROC_ERROR except MissingInputException, mie: ciel.log("Task died due to missing input", 'PROC', logging.WARN) raise except TaskFailedError: raise except: ciel.log('Error during method handling in JSON event loop', 'PROC', logging.ERROR, True) return PROC_ERROR try: if response is not None: with self.transmit_lock: write_framed_json((method, response), writer) if response_fd is not None: socket_name = args["fd_socket_name"] sock = socket.socket(socket.AF_UNIX) sock.connect(socket_name) sendmsg.sendmsg(fd=sock.fileno(), data="FD", ancillary=(socket.SOL_SOCKET, sendmsg.SCM_RIGHTS, struct.pack("i", response_fd))) os.close(response_fd) sock.close() except: ciel.log('Error writing response in JSON event loop', 'PROC', logging.WARN, True) return PROC_ERROR return True
except TaskFailedError: raise except: ciel.log('Error during method handling in JSON event loop', 'PROC', logging.ERROR, True) return PROC_ERROR try: if response is not None: with self.transmit_lock: write_framed_json((method, response), writer) if response_fd is not None: socket_name = args["fd_socket_name"] sock = socket.socket(socket.AF_UNIX) sock.connect(socket_name) sendmsg.sendmsg(fd=sock.fileno(), data="FD", ancillary=(socket.SOL_SOCKET, sendmsg.SCM_RIGHTS, struct.pack("i", response_fd))) os.close(response_fd) sock.close() except: ciel.log('Error writing response in JSON event loop', 'PROC', logging.WARN, True) return PROC_ERROR return True
def sendsocket(control, identifier, sock): payload = struct.pack('i', sock.fileno()) control.wait_for_write() sendmsg.sendmsg(control.fileno(), identifier, 0, (socket.SOL_SOCKET, sendmsg.SCM_RIGHTS, payload))