示例#1
0
    def setUp(self):
        self.client = Client()

        today = datetime.today()
        timestamp = int(
            time.mktime(
                (today.year, today.month, today.day, 10, 30, 0, 0, 0, 0)))
        dt = datetime.fromtimestamp(timestamp)

        d = {
            'SH000001': {
                'amount': 84596203520.0,
                'close': 2856.9899999999998,
                'high': 2880.5599999999999,
                'low': 2851.9499999999998,
                'name': u'\u4e0a\u8bc1\u6307\u6570',
                'open': 2868.73,
                'preclose': 2875.8600000000001,
                'price': 2856.9899999999998,
                'symbol': u'SH000001',
                'time': str(dt),
                'timestamp': timestamp,
                'volume': 75147848.0
            }
        }
        self.client.put_reports(d)
示例#2
0
    def setUp(self):
        self.client = Client()

        today = datetime.today()
        timestamp = int(time.mktime((today.year, today.month, today.day,
                                      10, 30, 0, 0, 0, 0)))
        dt = datetime.fromtimestamp(timestamp)
        
        d = {
            'SH000001' : {
                'amount': 84596203520.0,
                'close': 2856.9899999999998,
                'high': 2880.5599999999999,
                'low': 2851.9499999999998,
                'name': u'\u4e0a\u8bc1\u6307\u6570',
                'open': 2868.73,
                'preclose': 2875.8600000000001,
                'price': 2856.9899999999998,
                'symbol': u'SH000001',
                'time': str(dt),
                'timestamp': timestamp,
                'volume': 75147848.0
                }
            }
        self.client.put_reports(d)
示例#3
0
    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
示例#4
0
import sys

import numpy as np

ROOT_PATH = os.path.join(os.path.realpath(os.path.dirname(__file__)), '..')
sys.path[0:0] = [ROOT_PATH]

from cStringIO import StringIO
from datafeed.client import Client
from datafeed.datastore import Manager
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)
示例#5
0
class ClientTest(unittest.TestCase):

    def setUp(self):
        self.client = Client()

        today = datetime.today()
        timestamp = int(time.mktime((today.year, today.month, today.day,
                                      10, 30, 0, 0, 0, 0)))
        dt = datetime.fromtimestamp(timestamp)
        
        d = {
            'SH000001' : {
                'amount': 84596203520.0,
                'close': 2856.9899999999998,
                'high': 2880.5599999999999,
                'low': 2851.9499999999998,
                'name': u'\u4e0a\u8bc1\u6307\u6570',
                'open': 2868.73,
                'preclose': 2875.8600000000001,
                'price': 2856.9899999999998,
                'symbol': u'SH000001',
                'time': str(dt),
                'timestamp': timestamp,
                'volume': 75147848.0
                }
            }
        self.client.put_reports(d)

    def test_connect(self):
        self.client.connect()
        self.assertTrue(isinstance(self.client._sock, socket._socketobject))

    def test_put_reports(self):
        path = os.path.dirname(os.path.realpath(__file__))
        r = self.client.get_report('SH000001')
        f = open(os.path.join(path, 'reports.dump'), 'r')
        data = marshal.load(f)
        for v in data.itervalues():
            if 'amount' not in v:
                continue
            v['time'] = r['time']
            v['timestamp'] = r['timestamp']

        ret = self.client.put_reports(data)
        self.assertEqual(ret, 'OK')

    def test_put_empty_reports(self):
        ret = self.client.put_reports({})
        self.assertEqual(ret, 'OK')

    def test_get_list(self):
        stocks = self.client.get_list()
        self.assertTrue(isinstance(stocks, dict))
        self.assertTrue('SH000001' in stocks)

    def test_get_report(self):
        quote = self.client.get_report('SH000001')
        self.assertTrue(isinstance(quote, dict))
        self.assertTrue(isinstance(quote['price'], float))

    def test_get_reports(self):
        stocks = self.client.get_reports('SH000001', 'KeyError')
        self.assertTrue(isinstance(stocks, dict))
        self.assertTrue('SH000001' in stocks)
        self.assertFalse('KeyError' in stocks)

    def test_put_then_get_minute(self):
        path = os.path.dirname(os.path.realpath(__file__))
        data = numpy.load(os.path.join(path, 'minute.npy'))

        symbol = 'SH999999'

        today = datetime.today()
        for row in data:
            day = datetime.fromtimestamp(int(row['time']))
            t = time.mktime((today.year, today.month, today.day,
                             day.hour, day.minute, 0, 0, 0, 0))
            
            row['time'] = int(t)

        self.client.put_minute(symbol, data)

        ret = self.client.get_minute(symbol, int(time.time()))
        self.assertEqual(data['price'].tolist(), ret['price'].tolist())
示例#6
0
        for sector in _SECTORS:
            options = OrderedDict()
            for name, value in config.items(sector):
                options[name] = value.split(' ')
            yield sector, options

        self.f.close()
        file.close


if __name__ == '__main__':
    from cStringIO import StringIO
    from datafeed.client import Client

    client = Client()

    # path = os.path.join(os.path.realpath(os.path.dirname(__file__)),
    #                     '../../var')

    # filename = os.path.join(path, "/dzh/sh/DAY.DAT")
    # io = DzhDay()
    # for symbol, ohlcs in io.read(filename, 'SH') :
    #     memfile = StringIO()
    #     np.save(memfile, ohlcs)
    #     client.put('DayHistory', symbol, memfile.getvalue())

    io = DzhDividend()
    for data in io.read():
        memfile = StringIO()
        np.save(memfile, data[1])
示例#7
0
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)
示例#8
0
import datetime
import os
import sys

import numpy as np

ROOT_PATH = os.path.join(os.path.realpath(os.path.dirname(__file__)), '..')
sys.path[0:0] = [ROOT_PATH]

from cStringIO import StringIO
from pandas import DataFrame

from datafeed.client import Client
from datafeed.dividend import Dividend

client = Client()
symbol = 'SH600036'

y = client.get_day(symbol, 1000)
dividends = client.get_dividend(symbol)

index = np.array([datetime.date.fromtimestamp(v) for v in y['time']],
                 dtype=object)
y = DataFrame.from_records(y, index=index, exclude=['time'])

print dividends

for div in dividends:
    d = Dividend(div)
    d.adjust(y)
示例#9
0
class ClientTest(unittest.TestCase):
    def setUp(self):
        self.client = Client()

        today = datetime.today()
        timestamp = int(
            time.mktime(
                (today.year, today.month, today.day, 10, 30, 0, 0, 0, 0)))
        dt = datetime.fromtimestamp(timestamp)

        d = {
            'SH000001': {
                'amount': 84596203520.0,
                'close': 2856.9899999999998,
                'high': 2880.5599999999999,
                'low': 2851.9499999999998,
                'name': u'\u4e0a\u8bc1\u6307\u6570',
                'open': 2868.73,
                'preclose': 2875.8600000000001,
                'price': 2856.9899999999998,
                'symbol': u'SH000001',
                'time': str(dt),
                'timestamp': timestamp,
                'volume': 75147848.0
            }
        }
        self.client.put_reports(d)

    def test_connect(self):
        self.client.connect()
        self.assertTrue(isinstance(self.client._sock, socket._socketobject))

    def test_put_reports(self):
        path = os.path.dirname(os.path.realpath(__file__))
        r = self.client.get_report('SH000001')
        f = open(os.path.join(path, 'reports.dump'), 'r')
        data = marshal.load(f)
        for v in data.itervalues():
            if 'amount' not in v:
                continue
            v['time'] = r['time']
            v['timestamp'] = r['timestamp']

        ret = self.client.put_reports(data)
        self.assertEqual(ret, 'OK')

    def test_put_empty_reports(self):
        ret = self.client.put_reports({})
        self.assertEqual(ret, 'OK')

    def test_get_list(self):
        stocks = self.client.get_list()
        self.assertTrue(isinstance(stocks, dict))
        self.assertTrue('SH000001' in stocks)

    def test_get_report(self):
        quote = self.client.get_report('SH000001')
        self.assertTrue(isinstance(quote, dict))
        self.assertTrue(isinstance(quote['price'], float))

    def test_get_reports(self):
        stocks = self.client.get_reports('SH000001', 'KeyError')
        self.assertTrue(isinstance(stocks, dict))
        self.assertTrue('SH000001' in stocks)
        self.assertFalse('KeyError' in stocks)

    def test_put_then_get_minute(self):
        path = os.path.dirname(os.path.realpath(__file__))
        data = numpy.load(os.path.join(path, 'minute.npy'))

        symbol = 'SH999999'

        today = datetime.today()
        for row in data:
            day = datetime.fromtimestamp(int(row['time']))
            t = time.mktime((today.year, today.month, today.day, day.hour,
                             day.minute, 0, 0, 0, 0))

            row['time'] = int(t)

        self.client.put_minute(symbol, data)

        ret = self.client.get_minute(symbol, int(time.time()))
        self.assertEqual(data['price'].tolist(), ret['price'].tolist())
示例#10
0
import sys

import numpy as np

ROOT_PATH = os.path.join(os.path.realpath(os.path.dirname(__file__)), '..')
sys.path[0:0] = [ROOT_PATH]

from cStringIO import StringIO
from datafeed.client import Client
from datafeed.datastore import Manager
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)
示例#11
0
文件: dzh.py 项目: lebowsk1s/datafeed
        for sector in _SECTORS:
            options = OrderedDict()
            for name, value in config.items(sector):
                options[name] = value.split(" ")
            yield sector, options

        self.f.close()
        file.close


if __name__ == "__main__":
    from cStringIO import StringIO
    from datafeed.client import Client

    client = Client()

    # path = os.path.join(os.path.realpath(os.path.dirname(__file__)),
    #                     '../../var')

    # filename = os.path.join(path, "/dzh/sh/DAY.DAT")
    # io = DzhDay()
    # for symbol, ohlcs in io.read(filename, 'SH') :
    #     memfile = StringIO()
    #     np.save(memfile, ohlcs)
    #     client.put('DayHistory', symbol, memfile.getvalue())

    io = DzhDividend()
    for data in io.read():
        memfile = StringIO()
        np.save(memfile, data[1])
示例#12
0
import os
import sys

import numpy as np


ROOT_PATH = os.path.join(os.path.realpath(os.path.dirname(__file__)), '..')
sys.path[0:0] = [ROOT_PATH]

from cStringIO import StringIO
from pandas import DataFrame

from datafeed.client import Client
from  datafeed.dividend import Dividend

client = Client()
symbol = 'SH600036'

y = client.get_day(symbol, 1000)
dividends = client.get_dividend(symbol)

index = np.array([datetime.date.fromtimestamp(v) for v in y['time']],
                 dtype=object)
y = DataFrame.from_records(y, index=index, exclude=['time'])

print dividends

for div in dividends:
    d = Dividend(div)
    d.adjust(y)