def get_queryset(self): queryset = [] queryset = HSGTCG.getlist() self.code = self.request.GET.get('code', '') if self.code: queryset = HSGTCG.getlist(self.code) return queryset.order_by('-tradedate')
def test_everydayCount(self): Stocktradedate.importList() for d in [ d[0] for d in set( list(HSGTCGHold.getlist().values_list('tradedate'))) ]: dcount = HSGTCGHold.getlist(d).count() print(d, dcount) from stocks.models import HSGTCG tdate = HSGTCGHold.getNearestTradedate() while tdate > convertToDate('2018-5-1'): dcount = HSGTCGHold.getlist(tdate).count() dcount1 = HSGTCG.getlist().filter(tradedate=tdate, hamount__gte=8000).count() print(tdate, dcount, dcount1, dcount - dcount1) tdate = HSGTCGHold.getNearestTradedate(tdate - datetime.timedelta(1)) from stocks.models import HSGTCG tdate = HSGTCGHold.getNearestTradedate() while tdate > convertToDate('2018-5-1'): df1 = pd.DataFrame(list(HSGTCGHold.getlist(tdate).values('code'))) dcount = len(df1) df2 = pd.DataFrame( list(HSGTCG.getlist().filter( tradedate=tdate, hamount__gte=8000).values('code'))) dcount1 = len(df2) print(tdate, dcount, dcount1, dcount - dcount1) if dcount - dcount1 != 0 and tdate > convertToDate('2018-6-1'): print('数据不一致:', end='') print(list(HSGTCGHold.dfNotInAnotherdf(df1, df2)['code'])) tdate = HSGTCGHold.getNearestTradedate(tdate - datetime.timedelta(1))
def test_newcomingin(self): """ 新加入市值八千万的个股 :return: """ Stocktradedate.importList() from stocks.models import HSGTCG # 2018 - 06 - 04 新增北向持股金额大于八千万 list1 = [ '603658', '600460', '002812', '002557', '600188', '000690', '600329' ] tdate = HSGTCGHold.getNearestTradedate() # tdate = HSGTCGHold.getNearestTradedate('2018-6-4') tdate1 = HSGTCGHold.getNearestTradedate(tdate - datetime.timedelta(1)) hsg = HSGTCGHold.getlist(tdate) hsg1 = HSGTCGHold.getlist(tdate1) list2 = [] for c in hsg.exclude(code__in=hsg1.values_list('code')): list2.append(c.code) # 验证是否前一天市值小于八千万 for code in list2: df = pd.DataFrame( list( HSGTCG.getlist(code).filter(tradedate__gte=tdate1).values( ).order_by('-tradedate'))) data1 = float(df.iloc[-2].hamount) data2 = float(df.iloc[-1].hamount) if not (data1 >= 8000 and data2 < 8000): print('不是新增持股金额大于八千万:{} 持股金额:{} {}'.format(code, data1, data2)) tdate = HSGTCGHold.getNearestTradedate() while tdate > convertToDate('2018-5-2'): tdate1 = HSGTCGHold.getNearestTradedate(tdate - datetime.timedelta(1)) hsg = HSGTCGHold.getlist(tdate) hsg1 = HSGTCGHold.getlist(tdate1) list2 = [] for c in hsg.exclude(code__in=hsg1.values_list('code')): list2.append(c.code) # 验证是否前一天市值小于八千万 for code in list2: df = pd.DataFrame( list( HSGTCG.getlist(code).filter(tradedate__gte=tdate1). values().order_by('-tradedate'))) if len(df) > 1 and HSGTCG.getlist().filter( tradedate=tdate1).count() > 0: # if len(df)> 1 : data1 = float(df.iloc[-2].hamount) data2 = float(df.iloc[-1].hamount) if not (data1 >= 8000 and data2 < 8000): print('{} 不是新增持股金额大于八千万:{} 持股金额:{} {}'.format( tdate, code, data1, data2)) tdate = HSGTCGHold.getNearestTradedate(tdate - datetime.timedelta(1))
def test_importList(self): """ 测试保存沪深港通持股 :return: """ HSGTCG.importList() hsgtcg = HSGTCG.getlist() print(hsgtcg) self.assertTrue(hsgtcg.count() > 10, '保存的数量: {}'.format(hsgtcg.count())) print('数据库中保存的记录数量:{}'.format(hsgtcg.count()))
def test_hamountMA(self): # n日持仓增加 n = 5 malist1 = MAUpper(n) n = 10 malist2 = MAUpper(n) HSGTCG.dfNotInAnotherdf(pd.DataFrame(malist1), pd.DataFrame(malist2)) up = list( HSGTCG.dfNotInAnotherdf(pd.DataFrame(malist1), pd.DataFrame(malist2))[0])
def test_importListdup(self): """ 测试保存沪深港通持股 :return: """ # HSGTCG.importList() HSGTCG.importList(firefoxHeadless=False) hsgtcg = HSGTCG.getlist() print(hsgtcg) self.assertTrue(hsgtcg.count() > 1000, '保存的数量: {}'.format(hsgtcg.count())) print('数据库中保存的记录数量:{}'.format(hsgtcg.count()))
def MAUpper(n): import QUANTAXIS as qa tdate = HSGTCG.getNearestTradedate() tdate = HSGTCG.getNearestTradedate(tdate, -(n + 100)) # hsg = HSGTCG.getlist().filter(tradedate__gte=(tdate, tdate), hamount__gte=8000) hsg = HSGTCG.getlist().filter(tradedate__gte=tdate).order_by( 'code', 'tradedate') df = pd.DataFrame(list(hsg.values('hamount', 'code', 'tradedate'))) results = [] for c in df.code.unique(): v = df[df['code'] == c] ma1 = qa.MA(v.hamount, n) if (ma1.iloc[-1] - ma1.iloc[-2]) >= 0: results.append(v.code.iloc[0]) continue return results
def test_newcomingin(self): """ 最近n个交易日,新进市值大于8000万的个股 :return: """ n = 5 tdate = HSGTCG.getNearestTradedate() tdate1 = HSGTCG.getNearestTradedate(tdate, -n) tdate2 = HSGTCG.getNearestTradedate(tdate1, -n - 1) yesterdayhsg = HSGTCG.getlist().filter( tradedate=tdate2, hamount__gte=8000, ) hsg = HSGTCG.getlist().filter(tradedate__range=(tdate1, tdate), hamount__gte=8000) newcomming = hsg.exclude( code__in=yesterdayhsg.values_list('code')).values('code') print(newcomming)
def test_scrapt(self): """ 个股北向持股 :return: """ code = '600066' code = '000425' code = '000792' code = '002493' url = 'http://data.eastmoney.com/hsgtcg/StockHdStatistics.aspx?stock={}'.format( code) browser = webdriver.Firefox() browser.maximize_window() try: browser.get(url) for x in ['lxml', 'xml', 'html5lib']: # 可能会出现lxml版本大于4.1.1时,获取不到table try: time.sleep(random.random() / 4) soup = BeautifulSoup(browser.page_source, x) table = soup.find_all(id='tb_cgtj')[0] if table: break except: time.sleep(0.1) print('using BeautifulSoup {}'.format(x)) # soup = BeautifulSoup(browser.page_source, 'lxml') # table = soup.find_all(id='tb_cgtj')[0] df = pd.read_html(str(table), header=1)[0] df.columns = [ 'date', 'related', 'close', 'zd', 'hvol', 'hamount', 'hpercent', 'oneday', 'fiveday', 'tenday' ] for i in df.index: v = df.iloc[i] print('{} {} {} {}'.format(v.close, v.hvol, v.hamount, v.hpercent)) HSGTCG.objects.get_or_create(code=code, close=v.close, hvol=str2Float(v.hvol), hamount=str2Float(v.hamount), hpercent=v.hpercent, tradedate=convertToDate(v.date)) finally: if browser: browser.close() hsgtcg = HSGTCG.getlist(code) # hsgtcg = HSGTCG.getlist() print(hsgtcg) self.assertTrue(hsgtcg.count() > 10, '保存的数量: {}'.format(hsgtcg.count())) self.assertTrue(isinstance(hsgtcg[0].tradedate, datetime.date)) self.assertTrue(hsgtcg.filter(tradedate=None).count() == 0)
def test_scrapt(self): """ 个股北向持股 :return: """ code = '600066' url = 'http://data.eastmoney.com/hsgtcg/StockHdStatistics.aspx?stock={}'.format( code) opts = Options() opts.set_headless() assert opts.headless # operating in headless mode browser = webdriver.Firefox(options=opts) browser.maximize_window() try: browser.get(url) soup = BeautifulSoup(browser.page_source, 'lxml') table = soup.find_all(id='tb_cgtj')[0] df = pd.read_html(str(table), header=1)[0] df.columns = [ 'date', 'related', 'close', 'zd', 'hvol', 'hamount', 'hpercent', 'oneday', 'fiveday', 'tenday' ] for i in df.index: v = df.iloc[i] print('{} {} {} {}'.format(v.close, v.hvol, v.hamount, v.hpercent)) HSGTCG.objects.get_or_create(code=code, close=v.close, hvol=str2Float(v.hvol), hamount=str2Float(v.hamount), hpercent=v.hpercent, tradedate=v.date) finally: if browser: browser.close() hsgtcg = HSGTCG.getlist(code) # hsgtcg = HSGTCG.getlist() print(hsgtcg) self.assertTrue(hsgtcg.count() > 10, '保存的数量: {}'.format(hsgtcg.count())) self.assertTrue(isinstance(hsgtcg[0].tradedate, datetime.date)) self.assertTrue(hsgtcg.filter(tradedate=None).count() == 0)
def importHSGTCG(): HSGTCGHold.importList() HSGTCG.importList()