Ejemplo n.º 1
0
class GetBarThread(PollingThread):
    # Events
    ON_BARS = 1

    def __init__(self, queue, identifiers, frequency):
        super(GetBarThread, self).__init__()

        self.__queue = queue
        self.__identifiers = identifiers
        self.__frequency = frequency
        self.__nextBarStart = None
        self.__nextBarClose = None

        self._ticksDf = {}
        self.__lock = Lock()

        for identifier in self.__identifiers:
            self._ticksDf[identifier] = DataFrame(
                columns=['time', 'price', 'volume', 'amount'])

        self._ctpMdApi = CTPMdApi(self.__identifiers, self._ticksDf,
                                  self.__lock, logger)
        #self._ctpMdApi.login("tcp://222.66.97.241:41213", '9017811', '123456', '7070')
        self._ctpMdApi.login("tcp://180.168.212.228:41213", "simnow申请",
                             "simnow申请", "9999")
        #self._ctpMdApi.login("tcp://211.144.195.163:34505", "", "", "")

        self.__updateNextBarClose()

    def __updateNextBarClose(self):
        self.__nextBarStart = resamplebase.build_range(
            utcnow(), self.__frequency).getBeginning()
        self.__nextBarClose = resamplebase.build_range(
            utcnow(), self.__frequency).getEnding()

    def getNextCallDateTime(self):
        return self.__nextBarClose

    def doCall(self):
        startDateTime = to_market_datetime(self.__nextBarStart)
        endDateTime = to_market_datetime(self.__nextBarClose)
        self.__updateNextBarClose()
        barDict = dict()

        for identifier in self.__identifiers:
            try:
                period_bar = self._build_bar(identifier, startDateTime,
                                             endDateTime)
                if period_bar:
                    barDict[identifier] = period_bar

            except Exception, e:
                logger.error(e)

        if len(barDict):
            bars = bar.Bars(barDict)
            self.__queue.put((GetBarThread.ON_BARS, bars))
    def setUp(self):
        self._instrument = 'au1606'
        ticks_df = dict()
        lock = Lock()
        self._logger = mock.MagicMock()

        self._df = DataFrame(columns=['time', 'price', 'volume', 'amount'])
        ticks_df[self._instrument] = self._df

        self._api = CTPMdApi([self._instrument], ticks_df, lock, self._logger)
Ejemplo n.º 3
0
    def setUp(self):
        self._instrument = "au1606"
        ticks_df = dict()
        lock = Lock()
        self._logger = mock.MagicMock()

        self._df = DataFrame(columns=["time", "price", "volume", "amount"])
        ticks_df[self._instrument] = self._df

        self._api = CTPMdApi([self._instrument], ticks_df, lock, self._logger)
Ejemplo n.º 4
0
    def __init__(self, queue, identifiers, frequency):
        super(GetBarThread, self).__init__()

        self.__queue = queue
        self.__identifiers = identifiers
        self.__frequency = frequency
        self.__nextBarStart = None
        self.__nextBarClose = None

        self._ticksDf = {}
        self.__lock = Lock()

        for identifier in self.__identifiers:
            self._ticksDf[identifier] = DataFrame(
                columns=['time', 'price', 'volume', 'amount'])

        self._ctpMdApi = CTPMdApi(self.__identifiers, self._ticksDf,
                                  self.__lock, logger)
        #self._ctpMdApi.login("tcp://222.66.97.241:41213", '9017811', '123456', '7070')
        self._ctpMdApi.login("tcp://180.168.212.228:41213", "simnow申请",
                             "simnow申请", "9999")
        #self._ctpMdApi.login("tcp://211.144.195.163:34505", "", "", "")

        self.__updateNextBarClose()
Ejemplo n.º 5
0
class TestCTPMdApi(TestCase):
    def setUp(self):
        self._instrument = "au1606"
        ticks_df = dict()
        lock = Lock()
        self._logger = mock.MagicMock()

        self._df = DataFrame(columns=["time", "price", "volume", "amount"])
        ticks_df[self._instrument] = self._df

        self._api = CTPMdApi([self._instrument], ticks_df, lock, self._logger)

    def test_onRtnDepthMarketData(self):
        data = dict()
        data["InstrumentID"] = self._instrument
        data["UpdateTime"] = "09:31:00"
        data["LastPrice"] = 10.0
        data["Volume"] = 200
        data["Turnover"] = 412.3
        data["PreClosePrice"] = 10.0
        self._api.onRtnDepthMarketData(data)

        self.assertEqual(1, len(self._df))
        self.assertEqual(10.0, self._df.ix[0].price)
        self.assertEqual(412.3, self._df.ix[0].amount)

        # new tick data
        data["UpdateTime"] = "09:31:02"
        data["LastPrice"] = 10.1
        data["Volume"] = 300
        data["Turnover"] = 555.3

        self._api.onRtnDepthMarketData(data)

        self.assertEqual(2, len(self._df))
        self.assertEqual(10.1, self._df.ix[1].price)
        self.assertEqual(555.3, self._df.ix[1].amount)

    def test_onRtnDepthMarketData_With_Invalid_TickData(self):
        data = dict()
        data["InstrumentID"] = self._instrument
        data["UpdateTime"] = "09:31:00"
        data["LastPrice"] = 10.0
        data["Volume"] = 200
        data["Turnover"] = 412.3

        data["PreClosePrice"] = 10.0

        self._api.onRtnDepthMarketData(data)
        self.assertEqual(1, len(self._df))

        # new tick data with older time
        data["UpdateTime"] = "09:30:50"
        data["LastPrice"] = 10.1
        data["Volume"] = 300
        data["Turnover"] = 555.3
        self._api.onRtnDepthMarketData(data)
        self.assertEqual(1, len(self._df))
        self.assertTrue(self._logger.warn.called)

        # price is higher than pre_close * 1.1
        data["UpdateTime"] = "09:31:50"
        data["LastPrice"] = 12.0
        data["Volume"] = 300
        data["Turnover"] = 555.3
        self._api.onRtnDepthMarketData(data)
        self.assertEqual(1, len(self._df))
        self.assertTrue(self._logger.warn.called)

        # price is lower than pre_close * 0.9
        data["UpdateTime"] = "09:31:50"
        data["LastPrice"] = 8.0
        data["Volume"] = 300
        data["Turnover"] = 555.3
        self._api.onRtnDepthMarketData(data)
        self.assertEqual(1, len(self._df))
        self.assertTrue(self._logger.warn.called)

    def test_onRtnDepthMarketData_With_Invalid_DataContent(self):
        data = dict()
        data["InstrumentID"] = self._instrument
        data["UpdateTime"] = "09:31:00"
        data["LastPriceXXXXX"] = 10.0  # invalid dict data
        data["Volume"] = 200
        data["Turnover"] = 412.3

        data["PreClosePrice"] = 10.0

        self._api.onRtnDepthMarketData(data)

        self.assertEqual(0, len(self._df))

        # logger called check
        self.assertTrue(self._logger.error.called)

        data["InstrumentID"] = "NonExist"
        self._api.onRtnDepthMarketData(data)

        self.assertEqual(0, len(self._df))

        # logger called check
        self.assertTrue(self._logger.error.called)
class TestCTPMdApi(TestCase):
    def setUp(self):
        self._instrument = 'au1606'
        ticks_df = dict()
        lock = Lock()
        self._logger = mock.MagicMock()

        self._df = DataFrame(columns=['time', 'price', 'volume', 'amount'])
        ticks_df[self._instrument] = self._df

        self._api = CTPMdApi([self._instrument], ticks_df, lock, self._logger)

    def test_onRtnDepthMarketData(self):
        data = dict()
        data['InstrumentID'] = self._instrument
        data['UpdateTime'] = '09:31:00'
        data['LastPrice'] = 10.0
        data['Volume'] = 200
        data['Turnover'] = 412.3
        data['PreClosePrice'] = 10.0
        self._api.onRtnDepthMarketData(data)

        self.assertEqual(1, len(self._df))
        self.assertEqual(10.0, self._df.ix[0].price)
        self.assertEqual(412.3, self._df.ix[0].amount)

        # new tick data
        data['UpdateTime'] = '09:31:02'
        data['LastPrice'] = 10.1
        data['Volume'] = 300
        data['Turnover'] = 555.3

        self._api.onRtnDepthMarketData(data)

        self.assertEqual(2, len(self._df))
        self.assertEqual(10.1, self._df.ix[1].price)
        self.assertEqual(555.3, self._df.ix[1].amount)

    def test_onRtnDepthMarketData_With_Invalid_TickData(self):
        data = dict()
        data['InstrumentID'] = self._instrument
        data['UpdateTime'] = '09:31:00'
        data['LastPrice'] = 10.0
        data['Volume'] = 200
        data['Turnover'] = 412.3

        data['PreClosePrice'] = 10.0

        self._api.onRtnDepthMarketData(data)
        self.assertEqual(1, len(self._df))

        # new tick data with older time
        data['UpdateTime'] = '09:30:50'
        data['LastPrice'] = 10.1
        data['Volume'] = 300
        data['Turnover'] = 555.3
        self._api.onRtnDepthMarketData(data)
        self.assertEqual(1, len(self._df))
        self.assertTrue(self._logger.warn.called)

        # price is higher than pre_close * 1.1
        data['UpdateTime'] = '09:31:50'
        data['LastPrice'] = 12.0
        data['Volume'] = 300
        data['Turnover'] = 555.3
        self._api.onRtnDepthMarketData(data)
        self.assertEqual(1, len(self._df))
        self.assertTrue(self._logger.warn.called)

        # price is lower than pre_close * 0.9
        data['UpdateTime'] = '09:31:50'
        data['LastPrice'] = 8.0
        data['Volume'] = 300
        data['Turnover'] = 555.3
        self._api.onRtnDepthMarketData(data)
        self.assertEqual(1, len(self._df))
        self.assertTrue(self._logger.warn.called)

    def test_onRtnDepthMarketData_With_Invalid_DataContent(self):
        data = dict()
        data['InstrumentID'] = self._instrument
        data['UpdateTime'] = '09:31:00'
        data['LastPriceXXXXX'] = 10.0 # invalid dict data
        data['Volume'] = 200
        data['Turnover'] = 412.3

        data['PreClosePrice'] = 10.0

        self._api.onRtnDepthMarketData(data)

        self.assertEqual(0, len(self._df))

        # logger called check
        self.assertTrue(self._logger.error.called)

        data['InstrumentID'] = 'NonExist'
        self._api.onRtnDepthMarketData(data)

        self.assertEqual(0, len(self._df))

        # logger called check
        self.assertTrue(self._logger.error.called)