def populatedb(request): myTeams = ['Real Madrid', 'Barcelona'] myCompetitions = ['La liga', 'English Premier League'] myMarkets = ['Match Odds', 'Half Time'] for market in myMarkets: try: Market.objects.get(market_name=market) print("Encontrado o market " + market) except ObjectDoesNotExist: print("A criar o market " + market) x = Market(market_name=market) x.save() for competition in myCompetitions: try: Competition.objects.get(competition_name=competition) print("Encontrada a competition " + competition) except ObjectDoesNotExist: print("A criar a competition " + competition) x = Competition(competition_name=competition) x.save() for team in myTeams: try: Team.objects.get(team_name=team) print("Encontrada a Team " + team) except ObjectDoesNotExist: print("A criar a team " + team) x = Team(team_name=team) x.save() return HttpResponseRedirect('/Trade/')
def cix_one(item, weight_range): value = 0 cix_data = {} try: # 1 PE index = Index.objects.get(name='沪深300', date=item.date) print(index) avg_pe = 13.58 # pe = interp(index.pe, [8.0159, 50], [0, 50]) pe = interp(index.pe, [avg_pe*0.6, avg_pe*2], [0, 50]) # print('min_pe:{} max_pe:{} latest_pe:{} pe:{}'.format(min_pe, max_pe, latest_pe, pe)) value += pe cix_data.update({'pe': pe}) # 2 破净率 min_low_pb = 0.02 max_low_pb = 0.15 broken_net_ratio = item.broken_net_ratio pb = interp(-broken_net_ratio, [-max_low_pb, min_low_pb], weight_range) value += pb cix_data.update({'broken_net': pb}) # 3 AH premium index try: avg_ah = 120 equity = Equity.objects.get(code='HKHSAHP', date=item.date) ah = interp(equity.close, [avg_ah*0.7, avg_ah*1.3], weight_range) value += ah cix_data.update({'ah': ah}) except: print('error') # 4 GDP rate print(item.gdp) if item.gdp: gdp = interp(item.gdp, [0.4, 1], weight_range) value += gdp cix_data.update({'gdp': gdp}) # 5 百元股 [0,3.6%] g100_ratio = item.over_100_ratio if g100_ratio: high = interp(g100_ratio, [0, 0.036], weight_range) value += high cix_data.update({'over_100': high}) # 8 NHNL n = interp(item.nhnl, [-1000, 1000], weight_range) value += n cix_data.update({'nhnl': n}) print(cix_data) Market.objects(date=item.date).update_one(cix=value, cix_data=cix_data, upsert=True) except DoesNotExist as e: import sys, traceback traceback.print_exc(file=sys.stdout)
def cix(): weight_range = [0, 10] items = Market.objects().order_by('date') for item in items: value = 0 cix_data = {} try: index = Index.objects.get(name='沪深A股', date=item.date) print(index) pe = interp(index.pe, [10, 20], [0, 50]) # print('min_pe:{} max_pe:{} latest_pe:{} pe:{}'.format(min_pe, max_pe, latest_pe, pe)) value += pe cix_data.update({'pe': pe}) min_low_pb = 0.02 max_low_pb = 0.15 broken_net_ratio = item.broken_net_ratio pb = interp(-broken_net_ratio, [-max_low_pb, min_low_pb], weight_range) value += pb cix_data.update({'broken_net': pb}) # 3 AH premium index equity = Equity.objects.get(code='HKHSAHP', date=item.date) ah = interp(equity.close, [100, 130], weight_range) value += ah cix_data.update({'ah': ah}) # 4 GDP rate print(item.gdp) if item.gdp: gdp = interp(item.gdp, [0.4, 1], weight_range) value += gdp cix_data.update({'gdp': gdp}) # 5 百元股 [0,3.6%] g100_ratio = item.over_100_ratio if g100_ratio: high = interp(g100_ratio, [0, 0.036], weight_range) value += high cix_data.update({'over_100': high}) # 8 NHNL n = interp(item.nhnl, [-1000, 1000], weight_range) value += n cix_data.update({'nhnl': n}) print(cix_data) Market.objects(date=item.date).update_one(cix=value, cix_data=cix_data, upsert=True) except DoesNotExist as e: continue
def scrape_market(self, limit=None): """ scrapes all coins from coinmarketcap.com """ coins = [] for row in self._coins(): if self._limit_reached(limit, len(coins)): break tds = row.find_all('td') props = [td.get_text().strip() for td in tds] icon = tds[1].find('img') props.append(icon['src'] if icon else '') coins.append( Coin( **{ 'rank': props[0], 'name': props[1], 'symbol': props[2], 'cap': props[3], 'price': props[4], 'supply': props[5].split(' ')[0], 'volume': props[6], 'percent1h': props[7], 'percent24h': props[8], 'percent7d': props[9] })) return Market(coins)
def parse_cyb2( url='http://www.szse.cn/szseWeb/FrontController.szse?randnum=0.5328349224291742' ): payload = { 'ACTIONID': 7, 'AJAX': 'AJAX-TRUE', 'CATALOGID': '1898_nm', 'TABKEY': 'tab1', 'txtQueryDate': '2016-01-15', 'REPORT_ACTION': 'reach' } res = requests.post(url, data=payload) # print res.text # read html <table> to list of DataFrame dfs = pd.read_html(res.text, flavor='lxml') # dfs = pd.read_html(etree.tostring(r), flavor='bs4') if len(dfs) >= 1: df = dfs[0] # print df tradable_shares = df.iloc[4][1] total_market = df.iloc[5][1] volume_money = df.iloc[7][1] volume = df.iloc[8][1] pe = df.iloc[10][1] high_pe = df.iloc[10][3] value = df.iloc[13][1] if isinstance(tradable_shares, type(pd.NaT)): tradable_shares = 0 if type(total_market) == type(pd.NaT): total_market = 0 if isinstance(volume_money, type(pd.NaT)): volume_money = 0 if isinstance(volume, type(pd.NaT)): volume = 0 if isinstance(pe, type(pd.NaT)): pe = 0 if type(value) != float: value = 0.0 # 换手率=成交量÷当日实际流通量 if tradable_shares == 0: turnover = 0 else: turnover = float(volume) / float(tradable_shares) # print 'name:{} total_market:{} volume:{} turnover:{} pe:{} value:{}'.format(name, # total_market, volume_money, # turnover, pe, value) market = Market('CYB', float(total_market) / 100000000, float(volume_money) / 100000000, turnover, pe, value) # print market return market
def parse_sz_market_common(name, url): page = parse(url).getroot() result = etree.tostring(page) # print '*'*20 # print result # print '*'*20 r = page.get_element_by_id('REPORTID_tab1') # print '*'*20 # print etree.tostring(r) # print '*'*20 # read html <table> to list of DataFrame dfs = pd.read_html(etree.tostring(r), flavor='lxml') # dfs = pd.read_html(etree.tostring(r), flavor='bs4') if len(dfs) >= 1: df = dfs[0] # print df tradable_shares = df.iloc[4][1] total_market = df.iloc[5][1] volume_money = df.iloc[7][1] volume = df.iloc[8][1] pe = df.iloc[10][1] high_pe = df.iloc[10][3] value = df.iloc[13][1] if isinstance(tradable_shares, type(pd.NaT)): tradable_shares = 0 if type(total_market) == type(pd.NaT): total_market = 0 if isinstance(volume_money, type(pd.NaT)): volume_money = 0 if isinstance(volume, type(pd.NaT)): volume = 0 if isinstance(pe, type(pd.NaT)): pe = 0 if type(value) != float: value = 0.0 # 换手率=成交量÷当日实际流通量 if tradable_shares == 0: turnover = 0 else: turnover = float(volume) / float(tradable_shares) # print 'name:{} total_market:{} volume:{} turnover:{} pe:{} value:{}'.format(name, # total_market, volume_money, # turnover, pe, value) market = Market(name, float(total_market) / 100000000, float(volume_money) / 100000000, turnover, pe, value) # print market return market
def parse_sh_market(): page = parse( 'http://www.sse.com.cn/market/stockdata/overview/day/').getroot() result = etree.tostring(page) # print result r = page.get_element_by_id('dateList') statistics = r.text_content().split() # for word in statistics: # print word market = Market(name='sh', total_market_cap=statistics[1], volume=float(statistics[8]) / 10000, turnover=statistics[12], pe=statistics[14], date=statistics[2]) # print market return market
def get(self, request, *args, **kw): # Process any get params that you may need # If you don't need to process get params, # you can skip this part data = Market.objects().order_by('date') print(data) # df = DataFrame(list(data)) # print df # max_ah = df['value'].max() # min_ah = df['value'].min() # avg_ah = df['value'].mean() # print('PE max:{} min:{} average:{} median:{}'.format(max_ah, min_ah, avg_ah)) serializer = MarketListSerializer({'items': data}) # content = JSONRenderer().render(serializer.data) # print '**********content:{}'.format(content) # json_output = json.loads(content) # print '****json:{}'.format(json_output) result = get_market_result(serializer) response = Response(result, status=status.HTTP_200_OK) return get_response_cors(response)
def parse_sz_market(): page = parse('http://www.szse.cn/main/marketdata/tjsj/jbzb/').getroot() r = page.get_element_by_id('REPORTID_tab1') # print etree.tostring(r) # read html <table> to list of DataFrame dfs = pd.read_html(etree.tostring(r), flavor='lxml') # print dfs # print len(dfs) if len(dfs) >= 1: df = dfs[0] # print df total_market = df.iloc[10][1] volume = df.iloc[12][1] avg_price = df.iloc[13][1] pe = df.iloc[14][1] turnover_rate = df.iloc[15][1] if type(total_market) == type(pd.NaT): total_market = 0 if type(volume) == type(pd.NaT): volume = 0 if type(turnover_rate) == type(pd.NaT): turnover_rate = 0 if type(pe) == type(pd.NaT): pe = 0 # print 'total_market:{} volume:{} turnover_rate:{} pe:{}'.format(total_market, volume, turnover_rate, pe) market = Market('sz', total_market_cap=float(total_market) / 100000000, volume=float(volume) / 100000000, turnover=float(turnover_rate), pe=float(pe)) # print market # print df.index # print df.columns # print df.values # print df.describe() return market
def read_market(nh, nl, date): # 破净率 low_pb = low_pb_ratio() print(low_pb) broken_net_ratio = low_pb[0] broken_net = low_pb[1] stock_count = low_pb[3] nh_ratio = float(nh)/stock_count nl_ratio = float(nl)/stock_count nhnl = nh - nl # 跌停板 dt = screen_by_pencentage(-10.11, -9.9) dt_ratio = dt/stock_count # 涨停板 zt = screen_by_pencentage(9.9, 10.11) zt_ratio = zt/stock_count zdr = zt-dt print('dtb:{} ztb:{} zdr'.format(dt, zt, zdr)) # 仙股 penny_stocks = screen_by_price(0.1, 1)['count'] penny_stocks_ratio = penny_stocks/stock_count # 破发率 broken_ipo_count, total_ipo, broken_ipo_rate, broken_list = broken_ipo() # CIX范围从0到100,由10个指标组成 cix = 0 cix_data = {} weight_range = [0, 10] # 1 SH PE # pe_df = avg_sh_pe('2000-1-31') # max_pe = pe_df['PE'].max() # min_pe = pe_df['PE'].min() # # get latest PE DF by tail() # # latest_pe_df = pe_df.tail(1) # # latest_pe = latest_pe_df.iloc[0][1] # # print 'latest PE:{}'.format(latest_pe) # latest_sh = Index.objects(name='上海A股').order_by('-date').first() # print('items***{}'.format(latest_sh)) # pe = interp(latest_sh.pe, [min_pe, max_pe], weight_range) # # print('min_pe:{} max_pe:{} latest_pe:{} pe:{}'.format(min_pe, max_pe, latest_pe, pe)) # cix += pe # 1 替换为沪深A股PE TODO max_pe = 30 min_pe = 12 # get latest PE DF by tail() # latest_pe_df = pe_df.tail(1) # latest_pe = latest_pe_df.iloc[0][1] # print 'latest PE:{}'.format(latest_pe) latest_sh = Index.objects(name='沪深A股').order_by('-date').first() print('items***{}'.format(latest_sh)) pe = interp(latest_sh.pe, [min_pe, max_pe], [0, 50]) # print('min_pe:{} max_pe:{} latest_pe:{} pe:{}'.format(min_pe, max_pe, latest_pe, pe)) cix += pe cix_data.update({'pe': pe}) # 2 破净率 min_low_pb = 0.02 max_low_pb = 0.15 pb = interp(-broken_net_ratio, [-max_low_pb, min_low_pb], weight_range) cix += pb cix_data.update({'broken_net': pb}) # 3 AH premium index ah_now = xueqiu('HKHSAHP') ah_current = ah_now.current ah = interp(ah_current, [100, 130], weight_range) cix += ah cix_data.update({'ah': ah}) # 4 GDP rate rate = gdp_rate() gdp = interp(rate, [0.4, 1], weight_range) cix += gdp cix_data.update({'gdp': gdp}) # 5 百元股 [0,3.6%] high_price = high_price_ratio() g100 = high_price[0] g100_ratio = high_price[1] high = interp(g100_ratio, [0, 0.036], weight_range) cix += high cix_data.update({'over_100': high}) # 8 NHNL n = interp(nhnl, [-1000, 1000], weight_range) cix += n cix_data.update({'nhnl': n}) # 9 融资规模及占比 TODO tushare # 10 社交媒体挖掘 TODO xueqiu # 5 SH换手率 [1%,3%] sh = read_index_market('SH000001') turnover_rate = sh['turnover_rate'] turnover = interp(turnover_rate, [1, 3], weight_range) # cix += turnover # 6 涨跌停差额 # 7 TODO 最近一年IPO、可转债涨幅或破发率 # low price low_price = low_price_ratio() print('low_price***{}'.format(low_price)) print(cix_data) # TODO cix 映射到0.5-1.5区间,代表持仓比例 Market.objects(date=get_date(date)).update_one(nh=nh, nl=nl, nhnl=nhnl, nh_ratio=nh_ratio, nl_ratio=nl_ratio, stock_count=stock_count, over_100=g100, over_100_ratio=g100_ratio, penny_stocks=penny_stocks, penny_stocks_ratio=penny_stocks_ratio, low_price_ratio=low_price, pe=latest_sh.pe, turnover=turnover_rate, ah=ah_current, gdp=rate, cix=cix,cix_data=cix_data, broken_net=broken_net, broken_net_ratio=broken_net_ratio, broken_net_stocks=low_pb[2], dt=dt, dt_ratio=dt_ratio, zt=zt, zt_ratio=zt_ratio, zdr=zdr, ipo=total_ipo, broken_ipo=broken_ipo_count, broken_ipo_ratio=broken_ipo_rate,broken_ipo_list=broken_list, upsert=True)
def read_market(nh, nl, date): # 破净率 low_pb = low_pb_ratio() print(low_pb) broken_net_ratio = low_pb[0] broken_net = low_pb[1] stock_count = low_pb[3] nh_ratio = float(nh) / stock_count nl_ratio = float(nl) / stock_count # 跌停板 dt = screen_by_pencentage(-10.11, -9.9) dt_ratio = dt / stock_count # 涨停板 zt = screen_by_pencentage(9.9, 10.11) zt_ratio = zt / stock_count zdr = zt / dt print('dtb:{} ztb:{} zdr'.format(dt, zt, zdr)) # 仙股 penny_stocks = screen_by_price(0.1, 1)['count'] penny_stocks_ratio = penny_stocks / stock_count # 破发率 broken_ipo_count, total_ipo, broken_ipo_rate, broken_list = broken_ipo() cix = 0 weight_range = [0, 10] # 1 latest SH PE pe_df = avg_sh_pe('2000-1-31') max_pe = pe_df['PE'].max() min_pe = pe_df['PE'].min() # get latest PE DF by tail() # latest_pe_df = pe_df.tail(1) # latest_pe = latest_pe_df.iloc[0][1] # print 'latest PE:{}'.format(latest_pe) latest_sh = Index.objects(name='上海A股').order_by('-date').first() print('items***{}'.format(latest_sh)) pe = interp(latest_sh.pe, [min_pe, max_pe], weight_range) # print('min_pe:{} max_pe:{} latest_pe:{} pe:{}'.format(min_pe, max_pe, latest_pe, pe)) cix += pe # 2 破净率 min_low_pb = 0 max_low_pb = 0.1 pb = interp(-broken_net_ratio, [-max_low_pb, min_low_pb], weight_range) cix += pb # 3 AH premium index ah_now = xueqiu('HKHSAHP') ah_current = ah_now.current ah = interp(ah_current, [100, 150], weight_range) cix += ah # 4 GDP rate rate = gdp_rate() gdp = interp(rate, [0.4, 1], weight_range) cix += gdp # 5 百元股 [0,3.6%] high_price = high_price_ratio() g100 = high_price[0] g100_ratio = high_price[1] high = interp(g100_ratio, [0, 0.036], weight_range) cix += high # 5 low price low_price = low_price_ratio() print('low_price***{}'.format(low_price)) # 6 SH换手率 [1%,3%] sh = read_index_market('SH000001') turnover_rate = sh['turnover_rate'] turnover = interp(turnover_rate, [1, 3], weight_range) cix += turnover Market.objects(date=get_date(date)).update_one( nh=nh, nl=nl, nhnl=nh - nl, nh_ratio=nh_ratio, nl_ratio=nl_ratio, stock_count=stock_count, over_100=g100, over_100_ratio=g100_ratio, penny_stocks=penny_stocks, penny_stocks_ratio=penny_stocks_ratio, low_price_ratio=low_price, pe=latest_sh.pe, turnover=turnover_rate, ah=ah_current, gdp=rate, cix=cix, broken_net=broken_net, broken_net_ratio=broken_net_ratio, broken_net_stocks=low_pb[2], dt=dt, dt_ratio=dt_ratio, zt=zt, zt_ratio=zt_ratio, zdr=zdr, ipo=total_ipo, broken_ipo=broken_ipo_count, broken_ipo_ratio=broken_ipo_rate, broken_ipo_list=broken_list, upsert=True)
def insert(request): market = Market(market_name = request.POST.get('marketName')) market.save() return render(request, 'market/new.html', )
def cix(): weight_range = [0, 10] items = Market.objects().order_by('date') for item in items: cix_one(item, weight_range)
def write(request, page=1): # 인증 authuser = request.session.get('authUser') if authuser is None: return HttpResponseRedirect('/market/list') market = Market() market.title = request.POST['title'] market.price = request.POST['price'] market.content = request.POST['content'] market.category = request.POST.get('category') market.user = User.objects.get(id=request.session['authUser']['id']) # return redirect('/detail/' + str(post.id)) # 새글 작성 if request.POST['no'] == '-1': value = Market.objects.aggregate(max_groupno=Max('groupno')) market.groupno = value["max_groupno"] + 1 market.save() # 답글 작성 else: market2 = Market.objects.get(id=request.POST['no']) Market.objects.filter(orderno__gte=market2.orderno + 1).update(orderno=F('orderno') + 1) market.groupno = market2.groupno market.orderno = market2.orderno + 1 market.depth = market2.depth + 1 market.save() for img in request.FILES.getlist('imgs'): photo = Photo() photo.post = market photo.image = img photo.save() data = {'page': 1} # 검색어 kwd = request.GET.get('kwd') print(kwd, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") if kwd is None: data['kwd'] = json.dumps(kwd) else: data['kwd'] = kwd return HttpResponseRedirect(f'/market/list/{page}?kwd={kwd}')