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 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>")