コード例 #1
0
    def setUp(self):
        Base.metadata.create_all()

        instrument = Instrument()
        instrument.ticker = 'MyTicker'
        instrument.asset_class = 'MyAssetClass'
        instrument.name = 'MyName'
        instrument.currency = 'USD'
        instrument.type = 'MyType'
        instrument.transactions_fees = 0.
        instrument.point_value = 1
        instrument.exchange = 'MyExchange'
        instrument.rolling_month = 'H'
        
        market_data = MarketData()
        market_data.instrument = instrument
        market_data.type = DataType.CLOSE
        market_data.date = datetime(2000,1,3)
        market_data.value = 100.
        
        session = Session()
        
        instrument_dao = InstrumentDAO()
        instrument_dao.save(session, instrument)
        
        market_data_dao = MarketDataDAO()
        market_data_dao.save(session, market_data)
        
        
        self.instrument_service = InstrumentService() 
コード例 #2
0
 def testDAO(self):
     data = MarketData()
     data.type = self.type
     data.value = self.value
     data.date = self.date
     data.instrument = self.instrument
     
     market_data_dao = MarketDataDAO()
     
     # test save
     market_data_dao.save(self.session, data)
     self.assertNotEqual(data.id, None, 'market data id')
     self.assertEqual(data.type, self.type, 'market data type')
     self.assertEqual(data.value, self.value, 'market data value')
     self.assertEqual(data.date, self.date, 'market data date')
     self.assertEqual(data.instrument_id, self.instrument.id, 'market data type')
     
     # test get by instrument
     new_data = market_data_dao.get_by_instrument(self.session, self.instrument)
     self.assertEqual(new_data[0].id, data.id, 'both ids equals')
     
     last_date = market_data_dao.get_last_date(self.session, self.instrument)
     self.assertEqual(last_date, data.date)
     # test delete
     market_data_dao.delete(self.session, data)
コード例 #3
0
 def __init__(self):
     
     self.__instrument_dao = InstrumentDAO()
 
     self.__market_data_dao = MarketDataDAO()
コード例 #4
0
class InstrumentService(object):
    
    logger = logging.getLogger('InstrumentService')
    
    def __init__(self):
        
        self.__instrument_dao = InstrumentDAO()
    
        self.__market_data_dao = MarketDataDAO()
    
    def market_data_model_to_serialized(self, market_data_model):
        market_data_serialized = {}
        market_data_serialized['id'] = market_data_model.id
        market_data_serialized['value'] = market_data_model.value
        market_data_serialized['data_type'] = market_data_model.type
        
        data_date = market_data_model.date
        market_data_serialized['date'] = datetime(data_date.year, data_date.month, data_date.day)
        
        return market_data_serialized
    
    def instrument_model_to_serialized(self, instrument_model):
        
        # build the serialized object
        instrument_serialized = {}
        instrument_serialized['id'] = instrument_model.id
        instrument_serialized['ticker'] = instrument_model.ticker
        instrument_serialized['name'] = instrument_model.name
        instrument_serialized['exchange'] = instrument_model.exchange
        instrument_serialized['rolling_month'] = instrument_model.rolling_month
        instrument_serialized['currency'] = instrument_model.currency
        instrument_serialized['point_value'] = instrument_model.point_value
        instrument_serialized['transactions_fees'] = instrument_model.transactions_fees
        instrument_serialized['asset_class'] = instrument_model.asset_class
        instrument_serialized['instrument_type'] = instrument_model.type
        instrument_serialized['datas'] = []
            
        return instrument_serialized

    def get_date(self, a_date):
        # return a date from a datetime if a_date is instance of datetime
        if isinstance(a_date, datetime):
            return a_date.date()
        elif isinstance(a_date, date):
            return a_date
        # return a date from a XML RPC DateTime else
        else:
            return datetime.strptime(a_date.value, '%Y%m%dT%H:%M:%S').date()
    
    def get_by_ticker(self, ticker, first_date, last_date):
        '''
        return an instrument serialized for a given ticker
        @param ticker: the ticker
        @param first_date: the first time series date
        @param last_date: the last time series date 
        @return: the instrument serialized 
        '''
        
        try:
            self.logger.info('Asking data for %s between %s and %s'%(ticker, first_date, last_date))
            # get the model
            session = Session()
            
            model = self.__instrument_dao.get_by_ticker(session, ticker)
            
            # convert into serialized object
            if model:
                instrument = self.instrument_model_to_serialized(model)
        
            # fill data
            first_date_converted = self.get_date(first_date)
            
            last_date_converted = self.get_date(last_date)
        
            datas = self.__market_data_dao.get_data_range(session, model, first_date_converted, last_date_converted)
        
            for market_data in datas:
                self.logger.debug('Appending data %s at %s for %s'%(market_data.type,market_data.date,ticker))
                serialized = self.market_data_model_to_serialized(market_data)
                instrument['datas'].append(serialized)
                
        except DataError, e:
            self.logger.error(e.message)
            raise ServerError(e.message)
        finally: