def save_individual_trades(element_list, individual_code): for element in element_list: insider_trade = IndividualInsiderTrades() link = element[0] stock_code = link.replace('https://www.nasdaq.com/symbol/', '') stock_code = stock_code.replace('/insider-trades', '').strip(' \t\n\r').upper() print(stock_code) try: Stock().get_by_id(stock_code) print('success') insider_trade.company = stock_code except Exception: stock = Stock() stock_url = make_url('main', stock_code) stock_page = load_page(stock_url) stock_name = scrape_stock_name(stock_page) stock.name = stock_name stock.code = stock_code save_stock(stock) insider_trade.company = Stock().get_by_id(stock_code) insider_trade.insider = individual_code insider_trade.relation = element[2] trade_last_date = datetime.date( datetime.strptime(element[3], '%m/%d/%Y')) insider_trade.last_date = trade_last_date insider_trade.tran = element[5] insider_trade.owner_type = element[6] shares_traded = element[7].replace(',', '') if shares_traded == '': shares_traded = 0 insider_trade.shares_traded = shares_traded last_price = element[8] if last_price == '': last_price = 0.0 insider_trade.last_price = last_price shares_held = element[9].replace(',', '') if shares_held == '': shares_held = 0 insider_trade.shares_held = shares_held try: insider_trade.save() logger.info( f'Trade object of {individual_code} on last date {element[3]} saved in db' ) except Exception as exception: print(exception) logger.error(f'{individual_code} is allready exist: {exception}') db.rollback()
def stock_add(request): from app.remote.stocks import StockHistoryClient from app.forms import StockWizardForm from app.models import Company, Stock if request.method == "POST": form = StockWizardForm(request.POST) pprint(form.is_valid()) if form.is_valid(): # save company = Company() company.name = form.cleaned_data['company_name'] company.country = form.cleaned_data['company_country'] company.save() stock = Stock() stock.company = company stock.name = form.cleaned_data['name'] stock.wkn = form.cleaned_data['wkn'] stock.isin = form.cleaned_data['isin'] stock.symbol = form.cleaned_data['symbol'] stock.type = form.cleaned_data['type'] stock.default_stock_exchange = form.cleaned_data['default_stock_exchange'] stock.save() return HttpResponseRedirect(reverse('stock', args=(stock.symbol,))) else: pprint(form.errors) else: data = None if 'wkn_or_isin' in request.GET: shc = StockHistoryClient() data = shc.get_basics_by_wkn_or_isin(wkn_or_isin=request.GET['wkn_or_isin']) data['company_country'] = data['country'] data['company_name'] = data['name'] form = StockWizardForm(initial=data) return render_to_response( 'stock_wizard_add.html', { 'form': form }, context_instance=RequestContext(request) )
def stock_add(request): from app.remote.stocks import StockHistoryClient from app.forms import StockWizardForm from app.models import Company, Stock if request.method == "POST": form = StockWizardForm(request.POST) pprint(form.is_valid()) if form.is_valid(): # save company = Company() company.name = form.cleaned_data['company_name'] company.country = form.cleaned_data['company_country'] company.save() stock = Stock() stock.company = company stock.name = form.cleaned_data['name'] stock.wkn = form.cleaned_data['wkn'] stock.isin = form.cleaned_data['isin'] stock.symbol = form.cleaned_data['symbol'] stock.type = form.cleaned_data['type'] stock.default_stock_exchange = form.cleaned_data[ 'default_stock_exchange'] stock.save() return HttpResponseRedirect(reverse('stock', args=(stock.symbol, ))) else: pprint(form.errors) else: data = None if 'wkn_or_isin' in request.GET: shc = StockHistoryClient() data = shc.get_basics_by_wkn_or_isin( wkn_or_isin=request.GET['wkn_or_isin']) data['company_country'] = data['country'] data['company_name'] = data['name'] form = StockWizardForm(initial=data) return render_to_response('stock_wizard_add.html', {'form': form}, context_instance=RequestContext(request))
def fetch_stock_profile(stock_code): market = 'sh' if stock_code.startswith('6') else 'sz' url = stock_page_url.format(market=market, stock_code=stock_code) resp = requests.get(url) soup = BeautifulSoup(resp.content.decode('gbk')) name_elem = soup.find('h2', id='name') stock_name = name_elem.text if name_elem else None # last_price = soup.find('strong', id='price9').text status = 'L' ''' 上市状态 上市: L 退市: DE dead_status = [u'已退市', u'终止上市'] stop_status = [u'暂停上市'] if stock_name in dead_status: status = 'DE' elif stock_name in stop_status: status = 'S' ''' texts = [] trs = soup.find('table', id='rtp2').find('tbody').find_all('tr') for tr in trs: tds = tr.find_all('td') for td in tds: texts.append(td.text.split(u':')) datas = {_[0]: _[1] for _ in texts} eps = value_or_zero(datas.get(u'收益(一)'), Decimal) if eps == 0: eps = value_or_zero(datas.get(u'收益(二)'), Decimal) if eps == 0: eps = value_or_zero(datas.get(u'收益(三)'), Decimal) pe = value_or_zero(datas.get(u'PE(动)'), Decimal) asset_value_per_share = value_or_zero(datas.get(u'净资产'), Decimal) pb = value_or_zero(datas.get(u'净利率'), cn_text_to_number) revenue = value_or_zero(datas.get(u'总收入'), cn_text_to_int) revenue_growth = value_or_zero(texts[5][1], cn_text_to_number) net_profit = value_or_zero(datas.get(u'净利润'), cn_text_to_number) net_profit_growth = value_or_zero(texts[7][1], cn_text_to_number) roe = value_or_zero(datas.get(u'ROE'), cn_text_to_number) outstanding_shares = value_or_zero(datas.get(u'总股本'), cn_text_to_int) tradable_shares = value_or_zero(datas.get(u'流通股'), cn_text_to_int) market_value = value_or_zero(datas.get(u'总值'), cn_text_to_number) circulating_value = value_or_zero(datas.get(u'流值'), cn_text_to_number) retained_earnings_per_share = value_or_zero(datas.get(u'每股未分配利润'), cn_text_to_number) debt_ratio = value_or_zero(datas.get(u'负债率'), cn_text_to_number) listing_date_str = value_or_zero(datas.get(u'上市时间'), str, zero='-') gross_profit_margin = value_or_zero(datas.get(u'毛利率'), cn_text_to_number) if listing_date_str != '-': listing_date = datetime.strptime(listing_date_str, '%Y-%m-%d').date() else: listing_date = '-' stock = Stock(market=market, code=stock_code, status=status, listing_date=listing_date, outstanding_shares=outstanding_shares, tradable_shares=tradable_shares, market_value=market_value, circulating_value=circulating_value, revenue=revenue, revenue_growth=revenue_growth, net_profit=net_profit, net_profit_growth=net_profit_growth, asset_value_per_share=asset_value_per_share, retained_earnings_per_share=retained_earnings_per_share, debt_ratio=debt_ratio, gross_profit_margin=gross_profit_margin, pe=pe, pb=pb, eps=eps, roe=roe, update_time=datetime.now()) if stock_name: stock.name = stock_name return stock
def info_submit(request): rt = '1' act = request.POST.get('act') if (act == 'del'): code = request.POST.get('code').strip() jy_id = request.POST.get('jy_id') try: print 'code=%s' % code del_stock = Stock.objects.filter(code=code).filter(jy_id=jy_id) for dstock in del_stock: del_issueprice = dstock.issueprice break #print 'del_issueprice=%s'%del_issueprice del_stock.delete() #同步到mysql sync_mysql_by_code(code=code, old_code=code, jy_id=int(jy_id)) #for notify issueprice change result = oracle_connect.execute( select([zjjy_hk_ipo.c.issueprice ]).select_from(zjjy_hk_ipo).where( zjjy_hk_ipo.c.code == code).where( zjjy_hk_ipo.c.jy_id == int(jy_id))).fetchall() if (len(result) > 0): #print 'issueprice=%s,in jy'%result[0].issueprice newissueprice = result[0].issueprice else: #print 'code:%s,jyid:%s is not record in jy'%(code,jy_id) newissueprice = '' if (newissueprice != '' and newissueprice != del_issueprice): notify_issueprice(exchange='HKEX', code=code, lclose=float(newissueprice)) except Exception as e: print e rt = str(e).replace('\'', '') return HttpResponse("<script>parent.del_result('" + rt + "')</script>") elif act == 'update' or act == 'add': code = request.POST.get('code').strip() old_code = request.POST.get('old_code').strip() id = request.POST.get('stockid') #check whether this is a new code if (code != old_code and False == is_zj_new_code(code)): return HttpResponse("<script>parent.submit_result('2','" + act + "')</script>") #return HttpResponse("<script>parent.submit_result('can submit','"+act+"')</script>") if (act == 'update'): if (code != old_code and False == is_jy_new_code(code)): return HttpResponse("<script>parent.submit_result('3','" + act + "')</script>") try: print 'oldcode=%s' % old_code stock = Stock.objects.get(code=old_code) except Exception as e: print e else: #act==add stock = Stock() stockname = request.POST.get('stockname').strip() issuevolplanned = request.POST.get('issuevolplanned').strip() issuevolplanned = issuevolplanned.replace(',', '') publicnewshareplanned = request.POST.get( 'publicnewshareplanned').strip() publicnewshareplanned = publicnewshareplanned.replace(',', '').strip() issuepriceceiling = request.POST.get('issuepriceceiling').strip() issuepricefloor = request.POST.get('issuepricefloor').strip() tradeunitpriceatceiling = request.POST.get( 'tradeunitpriceatceiling').strip() exchange = request.POST.get('exchange').strip() issuetype = request.POST.get('issuetype').strip() applystartdate = request.POST.get('applystartdate').strip() issueenddate = request.POST.get('issueenddate').strip() proposedlistdate = request.POST.get('proposedlistdate').strip() datetoaccount = request.POST.get('datetoaccount').strip() tradeunit = request.POST.get('tradeunit').strip() issueprice = request.POST.get('issueprice').strip() marketcode = request.POST.get('marketcode').strip() pin_yin = request.POST.get('pin_yin').strip().upper() old_issueprice = request.POST.get('old_issueprice').strip() jy_id = request.POST.get('jy_id').strip() try: stock.code = code stock.name = stockname if (issuevolplanned != ''): stock.issuevolplanned = issuevolplanned else: stock.issuevolplanned = None if (publicnewshareplanned != ''): stock.publicnewshareplanned = publicnewshareplanned else: stock.publicnewshareplanned = None if (issuepriceceiling != ''): stock.issuepriceceiling = issuepriceceiling else: stock.issuepriceceiling = None if (issuepricefloor != ''): stock.issuepricefloor = issuepricefloor else: stock.issuepricefloor = None if (tradeunitpriceatceiling != ''): stock.tradeunitpriceatceiling = tradeunitpriceatceiling else: stock.tradeunitpriceatceiling = None stock.exchange = exchange stock.issuetype = issuetype stock.applystartdate = applystartdate stock.issueenddate = issueenddate if (proposedlistdate != ''): stock.proposedlistdate = proposedlistdate else: stock.proposedlistdate = None if (datetoaccount != ''): stock.datetoaccount = datetoaccount else: stock.datetoaccount = None if (tradeunit != ''): stock.tradeunit = tradeunit else: stock.tradeunit = None if (issueprice != ''): stock.issueprice = issueprice else: stock.issueprice = None stock.marketcode = marketcode stock.pin_yin = pin_yin if (jy_id != ''): stock.jy_id = jy_id ijy_id = int(jy_id) else: ijy_id = 0 stock.save() id = stock.id print 'id=%d' % id #se='select * from nls_session_parameters' #print oracle_connect.execute(se).fetchall() if (old_issueprice != issueprice): if (issueprice == ''): #修改了发行价为空 issueprice = '0' print("use redis interface") notify_issueprice(exchange='HKEX', code=code, lclose=float(issueprice)) #同步到 mysql print 'code=%s' % code sync_mysql_by_code(code=code, old_code=old_code, jy_id=ijy_id) except Exception as e: print e rt = str(e).replace("'", "") return HttpResponse("<script>parent.submit_result('" + rt + "','" + act + "')</script>") elif (act == 'sync_data'): try: add_count = 0 rt = sync_database(calltype='django') except Exception as e: rt = str(e).replace("'", "") #print rt print "rt=%s" % (rt) return HttpResponse("<script>parent.sync_result('" + str(rt) + "','" + str(add_count) + "')</script>") else: #处理get的情况 act = request.GET.get('act') if (act == 'set_issueprice'): #在列表页填issueprice code = request.GET.get('code') jy_id = request.GET.get('jy_id') issueprice = request.GET.get('issueprice_' + code + '_' + jy_id).strip() old_issueprice = request.GET.get('old_issueprice_' + code + '_' + jy_id).strip() source = request.GET.get('source') try: if (source == 'ZJ'): stock = Stock.objects.get(code=code) stock.issueprice = issueprice stock.save() elif (source == 'JY'): stock = Stock() ipo = oracle_connect.execute(zjjy_hk_ipo.select().where( zjjy_hk_ipo.c.code == code).where( zjjy_hk_ipo.c.jy_id == jy_id)).first() stock.name = ipo.name stock.code = ipo.code if (ipo.issuevolplanned): stock.issuevolplanned = ipo.issuevolplanned if (ipo.publicnewshareplanned): stock.publicnewshareplanned = ipo.publicnewshareplanned if (ipo.issuepriceceiling): stock.issuepriceceiling = ipo.issuepriceceiling if (ipo.issuepricefloor): stock.issuepricefloor = ipo.issuepricefloor if (ipo.tradeunitpriceatceiling): stock.tradeunitpriceatceiling = ipo.tradeunitpriceatceiling stock.exchange = ipo.exchange stock.issuetype = ipo.issuetype stock.applystartdate = ipo.applystartdate.strftime( '%Y-%m-%d') stock.issueenddate = ipo.issueenddate.strftime('%Y-%m-%d') if (ipo.proposedlistdate): stock.proposedlistdate = ipo.proposedlistdate.strftime( '%Y-%m-%d') if (ipo.datetoaccount): stock.datetoaccount = ipo.datetoaccount.strftime( '%Y-%m-%d') if (ipo.tradeunit): stock.tradeunit = ipo.tradeunit stock.marketcode = ipo.marketcode if (ipo.pin_yin): stock.pin_yin = ipo.pin_yin stock.issueprice = issueprice stock.jy_id = jy_id stock.save() #use redis interface if (issueprice != '' and old_issueprice != issueprice): notify_issueprice(exchange='HKEX', code=code, lclose=float(issueprice)) #save to mysql sync_mysql_by_code(code=code, old_code=code, jy_id=int(jy_id)) except Exception as e: str(e).replace("'", "") return HttpResponse("<script>parent.set_issuprice_result('" + rt + "','" + code + "','" + jy_id + "','" + issueprice + "')</script>")
def fetch_stock_profile(stock_code): market = 'sh' if stock_code.startswith('6') else 'sz' url = stock_page_url.format(market=market, stock_code=stock_code) resp = requests.get(url) soup = BeautifulSoup(resp.content.decode('gbk')) name_elem = soup.find('h2', id='name') stock_name = name_elem.text if name_elem else None # last_price = soup.find('strong', id='price9').text status = 'L' ''' 上市状态 上市: L 退市: DE dead_status = [u'已退市', u'终止上市'] stop_status = [u'暂停上市'] if stock_name in dead_status: status = 'DE' elif stock_name in stop_status: status = 'S' ''' texts = [] trs = soup.find('table', id='rtp2').find('tbody').find_all('tr') for tr in trs: tds = tr.find_all('td') for td in tds: texts.append(td.text.split(u':')) datas = {_[0]: _[1] for _ in texts} eps = value_or_zero(datas.get(u'收益(一)'), Decimal) if eps == 0: eps = value_or_zero(datas.get(u'收益(二)'), Decimal) if eps == 0: eps = value_or_zero(datas.get(u'收益(三)'), Decimal) pe = value_or_zero(datas.get(u'PE(动)'), Decimal) asset_value_per_share = value_or_zero(datas.get(u'净资产'), Decimal) pb = value_or_zero(datas.get(u'净利率'), cn_text_to_number) revenue = value_or_zero(datas.get(u'总收入'), cn_text_to_int) revenue_growth = value_or_zero(texts[5][1], cn_text_to_number) net_profit = value_or_zero(datas.get(u'净利润'), cn_text_to_number) net_profit_growth = value_or_zero(texts[7][1], cn_text_to_number) roe = value_or_zero(datas.get(u'ROE'), cn_text_to_number) outstanding_shares = value_or_zero(datas.get(u'总股本'), cn_text_to_int) tradable_shares = value_or_zero(datas.get(u'流通股'), cn_text_to_int) market_value = value_or_zero(datas.get(u'总值'), cn_text_to_number) circulating_value = value_or_zero(datas.get(u'流值'), cn_text_to_number) retained_earnings_per_share = value_or_zero(datas.get(u'每股未分配利润'), cn_text_to_number) debt_ratio = value_or_zero(datas.get(u'负债率'), cn_text_to_number) listing_date_str = value_or_zero(datas.get(u'上市时间'), str, zero='-') gross_profit_margin = value_or_zero(datas.get(u'毛利率'), cn_text_to_number) if listing_date_str != '-': listing_date = datetime.strptime(listing_date_str, '%Y-%m-%d').date() else: listing_date = '-' stock = Stock( market=market, code=stock_code, status=status, listing_date=listing_date, outstanding_shares=outstanding_shares, tradable_shares=tradable_shares, market_value=market_value, circulating_value=circulating_value, revenue=revenue, revenue_growth=revenue_growth, net_profit=net_profit, net_profit_growth=net_profit_growth, asset_value_per_share=asset_value_per_share, retained_earnings_per_share=retained_earnings_per_share, debt_ratio=debt_ratio, gross_profit_margin=gross_profit_margin, pe=pe, pb=pb, eps=eps, roe=roe, update_time=datetime.now()) if stock_name: stock.name = stock_name return stock