class MainWindow(object): _WM_USER_STOCK_DATA = win32con.WM_USER + 10 def __init__(self, host='localhost', password=None): self.client = Client(host=host, password=password, socket_timeout=10) msg_task_bar_restart = win32gui.RegisterWindowMessage("TaskbarCreated") message_map = { msg_task_bar_restart: self._on_restart, win32con.WM_DESTROY: self._on_destroy, win32con.WM_COMMAND: self._on_command, self._WM_USER_STOCK_DATA: self._on_data_receive, win32con.WM_USER + 20: self._on_taskbar_notify } # Register the Window class. wc = win32gui.WNDCLASS() hinst = wc.hInstance = win32api.GetModuleHandle(None) wc.lpszClassName = "StockTaskBar" wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hCursor = win32api.LoadCursor(0, win32con.IDC_ARROW) wc.hbrBackground = win32con.COLOR_WINDOW wc.lpfnWndProc = message_map # could also specify a wndproc. # Don't blow up if class already registered to make testing easier try: classAtom = win32gui.RegisterClass(wc) except (win32gui.error, err_info): if err_info.winerror != winerror.ERROR_CLASS_ALREADY_EXISTS: raise # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow(wc.lpszClassName, "WJF Data Processer", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None) win32gui.UpdateWindow(self.hwnd) self._do_create_icons() self._do_stock_quote() self.periodic_wrapper(30) def Timer(self, timeout): time.sleep(timeout) self._on_time() self.periodic_wrapper(timeout) def periodic_wrapper(self, timeout): # Thread needed because win32gui does not expose SetTimer API thread.start_new_thread(self.Timer, (timeout, )) def _on_time(self): d = datetime.today() if d.hour == 15 and d.minute == 3: # make sure we are not receiving reporting data after market closed. log.info("Market closed, exit on %d:%d." % (d.hour, d.minute)) win32gui.PostMessage(self.hwnd, win32con.WM_COMMAND, 1025, 0) def _do_stock_quote(self): self.stockdll = windll.LoadLibrary('C:\Windows\System32\Stock.dll') ret = self.stockdll.Stock_Init(self.hwnd, self._WM_USER_STOCK_DATA, RCV_WORK_SENDMSG) if ret != 1: raise Exception("Stock Init failed.") def _do_create_icons(self): # Try and find a custom icon hinst = win32api.GetModuleHandle(None) iconPathName = os.path.abspath( os.path.join(os.path.split(sys.executable)[0], "pyc.ico")) if not os.path.isfile(iconPathName): # Look in DLLs dir, a-la py 2.5 iconPathName = os.path.abspath( os.path.join( os.path.split(sys.executable)[0], "DLLs", "pyc.ico")) if not os.path.isfile(iconPathName): # Look in the source tree. iconPathName = os.path.abspath( os.path.join( os.path.split(sys.executable)[0], "..\\PC\\pyc.ico")) if os.path.isfile(iconPathName): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) else: print("Can't find a Python icon file - using default") hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "Python Demo") try: win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) except win32gui.error: # This is common when windows is starting, and this code is hit # before the taskbar has been created. print("Failed to add the taskbar icon - is explorer running?") # but keep running anyway - when explorer starts, we get the # TaskbarCreated message. def _on_restart(self, hwnd, msg, wparam, lparam): self._do_create_icons() def _on_destroy(self, hwnd, msg, wparam, lparam): nid = (self.hwnd, 0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) win32gui.PostQuitMessage(0) # Terminate the app. def _on_data_receive(self, hwnd, msg, wparam, lparam): header = ReceiveData.from_address(lparam) if wparam == RCV_REPORT: # Report records = {} for i in xrange(header.m_nPacketNum): r = Report.from_address(header.ptr + sizeof(Report) * i) if r.is_valid(): records[r.symbol] = r.to_dict() self.client.put_reports(records) print("%d report data sended", header.m_nPacketNum) elif wparam == RCV_FILEDATA: if header.m_wDataType in (FILE_HISTORY_EX, FILE_5MINUTE_EX, FILE_1MINUTE_EX): # Daily history history_head = HistoryUnion.from_address(header.ptr) records = [] key = history_head.market() + history_head.symbol() for i in xrange(header.m_nPacketNum - 1): # start from ptr + sizeof(History), first one was the header q = History.from_address(header.ptr + sizeof(History) * (i + 1)) records.append(q.to_tuple()) rec = np.array(records, dtype=HistoryUnion.DTYPE) if header.m_wDataType == FILE_HISTORY_EX: self.client.put_day(key, rec) elif header.m_wDataType == FILE_5MINUTE_EX: self.client.put_5minute(key, rec) elif header.m_wDataType == FILE_1MINUTE_EX: self.client.put_1minute(key, rec) # no implementation elif header.m_wDataType == FILE_MINUTE_EX: # Minute minute_head = MinuteUnion.from_address(header.ptr) records = [] key = minute_head.market() + minute_head.symbol() for i in xrange(header.m_nPacketNum - 1): # start from ptr + sizeof(Minute), first one was the header q = Minute.from_address(header.ptr + sizeof(Minute) * (i + 1)) records.append(q.to_tuple()) rec = np.array(records, dtype=MinuteUnion.DTYPE) self.client.put_minute(key, rec) elif header.m_wDataType == FILE_POWER_EX: log.error("power ex") elif header.m_wDataType == FILE_BASE_EX: log.error("base ex") elif header.m_wDataType == FILE_NEWS_EX: log.error("news ex") elif header.m_wDataType == FILE_HTML_EX: log.error("html ex") elif header.m_wDataType == FILE_SOFTWARE_EX: log.error("software ex") else: log.error("Unknown file data.") else: log.error("Unknown data type.") return 1 def _on_taskbar_notify(self, hwnd, msg, wparam, lparam): if lparam == win32con.WM_LBUTTONUP or lparam == win32con.WM_RBUTTONUP: log.error("You right clicked me.") menu = win32gui.CreatePopupMenu() win32gui.AppendMenu(menu, win32con.MF_STRING, 1023, "Display Dialog") win32gui.AppendMenu(menu, win32con.MF_STRING, 1025, "Exit program") pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) return 1 def _on_command(self, hwnd, msg, wparam, lparam): id = win32api.LOWORD(wparam) if id == 1023: log.info("Goodbye") elif id == 1025: log.info("Goodbye") win32gui.DestroyWindow(self.hwnd) else: log.error("Unknown command -", id)
from datafeed.providers.dzh import * var_path = os.path.join(ROOT_PATH, 'var') client = Client() store = Manager('/tmp/df', SH()) filename = os.path.join(var_path, "dzh/sh/MIN1.DAT") io = DzhMinute() for symbol, ohlcs in io.read(filename, 'SH'): client.put_minute(symbol, ohlcs) filename = os.path.join(var_path, "dzh/sh/MIN1.DAT") io = DzhMinute() for symbol, ohlcs in io.read(filename, 'SH'): for ohlc in ohlcs: ohlc['time'] = ohlc['time'] - 8 * 3600 print symbol #client.put_1minute(symbol, ohlcs) store.oneminstore.update(symbol, ohlcs) filename = os.path.join(var_path, "dzh/sh/MIN.DAT") io = DzhFiveMinute() for symbol, ohlcs in io.read(filename, 'SH'): for ohlc in ohlcs: ohlc['time'] = ohlc['time'] - 8 * 3600 print symbol client.put_5minute(symbol, ohlcs) # store.fiveminstore.update(symbol, ohlcs)
from datafeed.exchange import * from datafeed.providers.dzh import * var_path = os.path.join(ROOT_PATH, 'var') client = Client() store = Manager('/tmp/df', SH()) filename = os.path.join(var_path, "dzh/sh/MIN1.DAT") io = DzhMinute() for symbol, ohlcs in io.read(filename, 'SH'): client.put_minute(symbol, ohlcs) filename = os.path.join(var_path, "dzh/sh/MIN1.DAT") io = DzhMinute() for symbol, ohlcs in io.read(filename, 'SH'): for ohlc in ohlcs: ohlc['time'] = ohlc['time'] - 8 * 3600 print symbol #client.put_1minute(symbol, ohlcs) store.oneminstore.update(symbol, ohlcs) filename = os.path.join(var_path, "dzh/sh/MIN.DAT") io = DzhFiveMinute() for symbol, ohlcs in io.read(filename, 'SH'): for ohlc in ohlcs: ohlc['time'] = ohlc['time'] - 8 * 3600 print symbol client.put_5minute(symbol, ohlcs) # store.fiveminstore.update(symbol, ohlcs)