def handle(self, **options): #TODO replace settings dict with market table exchange = [] if options.get('exchange'): # force download of the specified exchange for x in settings.TSB['downloads']: if x['exchange'] == options.get('exchange'): exchange.append(x) break if not exchange: raise SystemExit('Nothing to do: exchange {} not found'.format( options.get('exchange'))) else: # for each exchange, check if it needs to be downloaded for x in settings.TSB['downloads']: exchange_time = datetime.now(timezone(x.get('timezone'))) if not is_weekday(exchange_time): continue if exchange_time.hour == x.get('time_h'): exchange.append(x) for x in exchange: #FIXME: retries will cause delay *before* running next exchange! # implementing delays in hours could be easy-ish (but how to # remember which symbols need retrying?? - NoRedis??) #TODO: implement datasource (now yahoo is always used) #TODO: use -verbosity to see output when using cmd line #TODO: with -verbosity=0 send *certain* messages to admin Price.download_prices_today(currency=x.get('currency'), num_retries=x.get('num_retries'))
def test_Price(self): stock = Stock.objects.get(name='AAPL') date1 = date(2010,1,5) date2 = date(2010,1,10) date3 = date(2010,1,15) date4 = date(2010,1,20) price_record = {'stock': stock, 'date': date2, 'open': 1., 'high': 1., 'low': 1., 'close': 1., 'volume': 1 } # _update_or_create Price._update_or_create(**price_record) self.assertEqual(Price.objects.all().count(), 1) self.assertEqual(Price.objects.get(stock=stock).volume, 1) price_record['volume'] = 100 Price._update_or_create(**price_record) self.assertEqual(Price.objects.all().count(), 1) self.assertEqual(Price.objects.get(stock=stock).volume, 100) # get_latest_date price_record['date'] = date1 Price._update_or_create(**price_record) self.assertEqual(Price.get_latest_date(), date2) price_record['date'] = date4 Price._update_or_create(**price_record) self.assertEqual(Price.get_latest_date(), date4) price_record['date'] = date3 Price._update_or_create(**price_record) self.assertEqual(Price.get_latest_date(), date4) self.assertEqual(Price.objects.all().count(), 4) # insert_prices data = [] for x in range(11,15): data.append({'stock': stock, 'date': date(2010,1,x), 'open': 1., 'high': 1., 'low': 1., 'close': 1., 'volume': x }) Price.insert_prices(data) self.assertEqual(Price.objects.all().count(), 8) Price.insert_prices([]) self.assertEqual(Price.objects.all().count(), 8)