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() 
 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)
def _fill_market_data(file_name, instrument):
    reader = csv.reader(open(file_name, 'r'))

    data_list = []
    
    for row in reader:
        # set the current_date from string
        t = time.strptime(row[0], '%Y%m%d')
        current_date = date(t.tm_year, t.tm_mon, t.tm_mday)
        
        # open
        open_price = MarketData()
        open_price.value = float(row[1])
        open_price.instrument = instrument
        open_price.type = DataType.OPEN
        open_price.date = current_date
        data_list.append(open_price)
        
        # high
        high = MarketData()
        high.value = float(row[2])
        high.instrument = instrument
        high.type = DataType.HIGH
        high.date = current_date
        data_list.append(high)
        
        # low
        low = MarketData()
        low.value = float(row[3])
        low.instrument = instrument
        low.type = DataType.LOW
        low.date = current_date
        data_list.append(low)
        
        # adj close
        adj_close = MarketData()
        adj_close.value = float(row[4])
        adj_close.instrument = instrument
        adj_close.type = DataType.ADJUSTED_CLOSE
        adj_close.date = current_date
        data_list.append(adj_close)
        
        # volume
        volume = MarketData()
        volume.value = float(row[5])
        volume.instrument = instrument
        volume.type = DataType.VOLUME
        volume.date = current_date
        data_list.append(volume)
        
        # open interest
        open_interest = MarketData()
        open_interest.value = float(row[6])
        open_interest.instrument = instrument
        open_interest.type = DataType.OPEN_INTEREST
        open_interest.date = current_date
        data_list.append(open_interest)
        
        # reference month
        reference_month = MarketData()
        reference_month.value = int(row[7])
        reference_month.instrument = instrument
        reference_month.type = DataType.REFERENCE_MONTH
        reference_month.date = current_date
        data_list.append(reference_month)
        
        # close
        close = MarketData()
        close.value = float(row[8])
        close.instrument = instrument
        close.type = DataType.CLOSE
        close.date = current_date
        data_list.append(close)
        
    market_data_dao.save_all(session, data_list)