def stocks_corr_analyzation(days, stock, *compare_stocks): ''' analyze the pct_change corr between stocks ''' compare_stock_data = {} for s in compare_stocks: try: compare_stock_data[s.name] = get_recent_data(s.code, s.market_code, days, update=False) except Exception as e: print_err(e) df_compare_stocks_pct = DataFrame( {stock: df['Adj Close'] for stock, df in compare_stock_data.items()}).pct_change() if stock is not None: if stock in compare_stocks: ser_stock_pct = df_compare_stocks_pct[stock.name] df_compare_stocks_pct.drop(columns=stock.name, inplace=True) else: df = get_recent_data(stock.code, stock.market_code, days, update=False) ser_stock_pct = df['Adj Close'].pct_change() ser = df_compare_stocks_pct.corrwith(ser_stock_pct) # 筛选出0.5以上的,并格式化 ser = ser[ser >= 0.5].map(lambda x: '{:.2f}'.format(x)) ser.name = '涨跌相关性' return ser.to_frame() else: df = df_compare_stocks_pct.corr() return df.applymap(lambda x: '{:.2f}'.format(x))
def all_industry_stocks_updown(request): try: updowns = Industry.all_industry_stocks_updown() return Response(updowns) except Exception as e: print_err(e) raise APIException
def insert_dataframe(self, df, exclude_dates=None): try: print('[+] inserting dataframe to mangodb: {} items'.format( df.shape[0])) ds = [] for i in range(df.shape[0]): s = df.iloc[i] if (exclude_dates is not None) and (s.name.date() in exclude_dates): continue d = s.to_dict() d['date'] = s.name ds.append(d) if len(ds) > 0: self.col.insert_many(ds) except DuplicateKeyError as e: print('[-] DuplicateKeyError: {!r}'.format(e)) except BulkWriteError as e: print('[-] BulkWriteError: {!r}'.format(e)) self.insert_dataframe_onebyone(df) except Exception as e: print('[-] Unknow Exception: {!r}'.format(e)) print_err(e)
def account_activate(request, key): try: profile = UserProfile.objects.get(activate_key=key) if (not profile.user.is_active) and (timezone.now() < profile.activate_key_expires): profile.user.is_active = True profile.activate_key = None profile.activate_key_expires = None profile.user.save() success = True info = '激活成功,正在跳转' elif profile.user.is_active: success = False info = '该帐号已经激活过' elif timezone.now() >= profile.activate_key_expires: success = False info = '激活失败,该链接已过期' else: success = False info = '服务器出错,暂时无法激活' return Response(template_name='account/info.html', data={ 'title': '帐号激活', 'success': success, 'info': info, 'redirect_url': reverse('account:page_login'), }) except ObjectDoesNotExist: raise NotFound except Exception as e: print_err(e) raise APIException
def industry_stocks_updown(request, industry_id): industry = get_object_or_404(Industry, id=industry_id) try: updowns = industry.cur_industry_stocks_updown() return Response(updowns) except Exception as e: print_err(e) raise APIException
def _request_and_cache_db(stock_code, market_code, start, end, cache_exclude_dates=None): try: df = _get_stock_data(stock_code, market_code, start, end) if df.shape[0] > 0: coll = mh.get_stock_collection(stock_code, market_code) coll.insert_dataframe(df, cache_exclude_dates) except KeyError as e: print_err(e)
def industry_stocks_corr_analyzation(request, industry_id): try: industry = Industry.get(id=industry_id) df_corr = industry.industry_stocks_corr() table_html = df2html(df_corr) return Response(table_html) except ObjectDoesNotExist: raise NotFound except Exception as e: print_err(e) raise APIException
def stocks_search(request): try: kw = request.GET['kw'] stocks = Stock.objects.filter(Q(name__contains=kw) | Q(code__contains=kw))[:100] serializer = StockSerializer4List(stocks, many=True) return Response(serializer.data) except KeyError: raise ParseError except Exception as e: print_err(e) raise APIException
def stock_pv_analyzation(request, stock_id): try: stock = Stock.objects.get(id=stock_id) df = stock.pv_analyzation() table_html = df2html(df) return Response(table_html) except ObjectDoesNotExist: raise NotFound except Exception as e: print_err(e) raise APIException
def stock_beta(request, id): try: stock = Stock.objects.get(id=id) beta = stock.beta() return Response(dict(result=beta)) except ObjectDoesNotExist: raise Http404 except DataMissingError: return Response(dict(result='数据缺失')) except Exception as e: print_err(e) raise APIException
def stocks_corr_analyzation(request, stock_id): try: stock = Stock.get(id=stock_id) industry = stock.get_belong_industries()[0] df_corr = stock.corr_with_industry_stocks(industry) table_html = df2html(df_corr) return Response(table_html) except ObjectDoesNotExist: raise NotFound except Exception as e: print_err(e) raise APIException
def page_reset_pw_user(request, key): try: profile = UserProfile.objects.get(reset_pw_key=key) if (timezone.now() < profile.reset_pw_key_expires): return Response(template_name='account/reset_pw.html', data={ 'key': key, }) except ObjectDoesNotExist: raise NotFound except Exception as e: print_err(e) raise APIException
def stock_last_deal_data(request, id): try: row = request.GET['row'] page = request.GET['page'] stock = Stock.objects.get(id=id) return Response(stock.last_deal_data(row, page)) except ObjectDoesNotExist: raise ValidationError except KeyError: raise ValidationError except Exception as e: print_err(e) raise APIException
def stock_cur_price_realtime(request, stock_code, market_code): try: pc, pc_change, pc_change_pct, time = sd.cur_price_info( stock_code, market_code, enable_cache=False) return Response(data={ 'pc': pc, 'pc_change': pc_change, 'pc_change_pct': pc_change_pct, 'update_time': time }) except Exception as e: print_err(e) raise APIException
def insert_dataframe_onebyone(self, df): try: print('[+] inserting dataframe one by one') for i in range(df.shape[0]): s = df.iloc[i] d = s.to_dict() d['date'] = s.name self.col.insert_one(d) except DuplicateKeyError as e: print('[-] DuplicateKeyError: {!r}'.format(e)) except Exception as e: print('[-] Unknow Exception: {!r}'.format(e)) print_err(e)
def api_remove_user_site(request): if not request.user.is_authenticated: raise NotAuthenticated(detail='请先登录') try: site = Site.objects.get(id=request.data['id'], owner=request.user) site.delete() return Response(dict(success=True, detail='操作成功,已删除')) except KeyError: raise OperateError(detail='参数错误,请检查') except ObjectDoesNotExist: raise OperateError(detail='操作失败,id或用户信息错误') except Exception as e: print_err(e) raise APIException(detail='服务器出错')
def cur_price_info(stock_code, market_code, enable_cache=True): '''get current price, price change, price change percent''' try: url = 'http://nuff.eastmoney.com/EM_Finance2015TradeInterface/JS.ashx?cb=&id={}{}' \ .format(stock_code, market_code) r = _request(url, enable_cache).text v = json.loads(r[r.index('(') + 1: r.rindex(')')])['Value'] pc, pc_change, pc_change_pct, time = v[25], v[27], v[29], v[-4] except Exception as e: print_err(e) # 停牌之类的股票获取时数据是空的 print('error happend when get cur price, url: %s' % url) return 0, 0, 0, False else: return pc, pc_change, pc_change_pct, time
def stock_recent_data(request, stock_id, days): if days > QUERY_DATA_MAX_DAYS: # 查询不超过一年 raise ValidationError try: stock = Stock.get(id=stock_id) df = stock.recent_data(days) df.columns = translate_column_name(df.columns) table_html = df2html(df) return Response(table_html) except Stock.DoesNotExist: raise NotFound except Exception as e: print_err(e) raise APIException
def start_scrapy(): count = 0 stocks = Stock.objects.filter()[count:] print('[+] Total {} stocks '.format(stocks.count())) for s in stocks: try: count += 1 print('[+] Scraping the {} item'.format(count)) print('[+] stock_code: {}, market_code: {}'.format( s.code, s.market_code)) get_recent_data(s.code, s.market_code, 400) print('[+] scrapy success.') print('[+] sleep a random time...' + '\n') time.sleep(random.random()) except Exception as e: print('[-] Error happened.') print_err(e)
def download_stock_data(request, stock_id, days): # need login if not request.user.is_authenticated: return redirect('account:page_login') # response StreamingHttpResponse to download csv file if days > QUERY_DATA_MAX_DAYS: raise ValidationError try: s = Stock.objects.get(id=stock_id) date = str(datetime.now().date()) response = StreamingHttpResponse( sd.get_recent_data_generator(s.code, s.market_code, days), content_type="text/csv") response['Content-Disposition'] = \ 'attachment; filename="{}-{}-{}.csv"'.format(s.code, date, days) return response except ObjectDoesNotExist: raise NotFound except Exception as e: print_err(e) raise APIException
def api_add_user_site(request): if not request.user.is_authenticated: raise NotAuthenticated(detail='请先登录') try: if Site.objects.filter(owner=request.user).count() > 1000: raise OperateError('用户自定义网址数量已达上限') ser = SiteSerializer(data=dict(name=request.data['name'], url=request.data['url'], category=Site.PERSONAL, owner=request.user.id)) if ser.is_valid(): ser.save() return Response(data=ser.data) raise OperateError(detail='参数错误,请检查。注意网址的格式是否正确。') except KeyError: raise OperateError(detail='参数错误,请检查') except APIException as e: raise e except Exception as e: print_err(e) raise APIException(detail='服务器出错')
def api_register(request): try: if request.user.is_authenticated: raise OperateError(detail='您当前处于登录状态,无法注册') username = request.POST['username'] nickname = request.POST['nickname'] email = request.POST['email'] password = request.POST['password'] confirm_password = request.POST['confirm_password'] # check if not (username and nickname and email and password and confirm_password): raise ValidationError if password != confirm_password: raise OperateError(detail='两次输入的密码不一致') if User.objects.filter(username=username).exists(): raise OperateError(detail='该用户名已存在') if UserProfile.objects.filter(nickname=nickname).exists(): raise OperateError(detail='该昵称已被占用') if not validate_mail(email): raise OperateError(detail='邮箱格式错误') if len(username) < 2 or len(username) > 10: raise OperateError(detail='用户名长度应在2至10个字符') if len(nickname) < 1: raise OperateError(detail='请填写昵称') if not validate_password(password): raise OperateError(detail='密码不符合要求,请重新填写') if User.objects.filter(email=email).exists(): user = User.objects.get(email=email) if user.is_active: raise OperateError(detail='该邮箱已注册过') else: # 注册过但是未激活的可重新注册 user_profile = UserProfile.objects.get(user=user) user_profile.delete() user.delete() activate_key = gen_user_key(username) activate_key_expires = gen_user_key_expires(days=2) ser = UserProfileSerializer( data={ 'nickname': nickname, 'activate_key': activate_key, 'activate_key_expires': activate_key_expires, 'user': { 'username': username, 'password': password, 'email': email, } }) if ser.is_valid(): ser.save() # send verify mail subject = '{}网站帐号激活'.format(settings.MY_APP_NAME) link = 'http://{}'.format(settings.MY_SERVER_DOMAIN) + reverse( 'account:page_activate', args=[activate_key]) html_content = render_to_string('account/activation_mail.html', { 'link': link, 'app_name': settings.MY_APP_NAME }) from_mail = settings.EMAIL_HOST_USER to_mail = email send_html_mail(from_mail, [to_mail], subject, html_content) result = AccountOperate('register', True, info='已发送激活链接至所填邮箱,请前往邮箱激活帐号', redirect_url=reverse('account:page_login')) return Response(data=AccountOperateSerializer(result).data) else: raise ValidationError except KeyError: raise OperateError(detail='注册信息不完整') except ValidationError: raise OperateError(detail='注册信息有误,请检查') except OperateError as err: raise err except Exception as e: print_err(e) raise OperateError(detail='服务器出错,暂时无法注册')